Whamcloud - gitweb
6be982cdb39638ac647645c172c66dbc785e1efa
[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_scrub_get_fid(struct osd_thread_info *info,
707                              struct osd_device *dev, struct inode *inode,
708                              struct lu_fid *fid, bool scrub)
709 {
710         struct lustre_mdt_attrs *lma     = &info->oti_mdt_attrs;
711         int                      rc;
712         bool                     has_lma = false;
713
714         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
715         if (rc == 0) {
716                 has_lma = true;
717                 if (lma->lma_compat & LMAC_NOT_IN_OI) {
718                         ldiskfs_set_inode_state(inode,
719                                                 LDISKFS_STATE_LUSTRE_NO_OI);
720                         return SCRUB_NEXT_CONTINUE;
721                 }
722
723                 *fid = lma->lma_self_fid;
724                 if (fid_is_internal(&lma->lma_self_fid)) {
725                         if (!scrub)
726                                 rc = SCRUB_NEXT_CONTINUE;
727                         return rc;
728                 }
729
730                 if (!scrub)
731                         return 0;
732
733                 if (fid_is_namespace_visible(fid) && !fid_is_norm(fid))
734                         return 0;
735
736                 if (lma->lma_compat & LMAC_FID_ON_OST)
737                         return SCRUB_NEXT_OSTOBJ;
738
739                 if (fid_is_idif(fid) || fid_is_last_id(fid))
740                         return SCRUB_NEXT_OSTOBJ_OLD;
741
742                 if (lma->lma_incompat & LMAI_AGENT)
743                         return SCRUB_NEXT_CONTINUE;
744
745                 /* Here, it may be MDT-object, or may be 2.4 OST-object.
746                  * Fall through. */
747         }
748
749         if (rc == -ENODATA || rc == 0) {
750                 rc = osd_get_idif(info, inode, &info->oti_obj_dentry, fid);
751                 if (rc == 0) {
752                         if (scrub)
753                                 /* It is old 2.x (x <= 3) or 1.8 OST-object. */
754                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
755                         return rc;
756                 }
757
758                 if (rc > 0) {
759                         if (!has_lma)
760                                 /* It is FID-on-OST, but we do not know how
761                                  * to generate its FID, ignore it directly. */
762                                 rc = SCRUB_NEXT_CONTINUE;
763                         else
764                                 /* It is 2.4 OST-object. */
765                                 rc = SCRUB_NEXT_OSTOBJ_OLD;
766                         return rc;
767                 }
768
769                 if (rc != -ENODATA)
770                         return rc;
771
772                 if (!has_lma) {
773                         if (dev->od_handle_nolma) {
774                                 lu_igif_build(fid, inode->i_ino,
775                                               inode->i_generation);
776                                 if (scrub)
777                                         rc = SCRUB_NEXT_NOLMA;
778                                 else
779                                         rc = 0;
780                         } else {
781                                 /* It may be FID-on-OST, or may be FID for
782                                  * non-MDT0, anyway, we do not know how to
783                                  * generate its FID, ignore it directly. */
784                                 rc = SCRUB_NEXT_CONTINUE;
785                         }
786                         return rc;
787                 }
788
789                 /* For OI scrub case only: the object has LMA but has no ff
790                  * (or ff crashed). It may be MDT-object, may be OST-object
791                  * with crashed ff. The last check is local FLDB. */
792                 rc = osd_scrub_check_local_fldb(info, dev, fid);
793         }
794
795         return rc;
796 }
797
798 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
799                         struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
800                         struct super_block *sb, bool scrub)
801 {
802         struct inode *inode;
803         int           rc;
804         ENTRY;
805
806         osd_id_gen(lid, pos, OSD_OII_NOGEN);
807         inode = osd_iget(info, dev, lid);
808         if (IS_ERR(inode)) {
809                 rc = PTR_ERR(inode);
810                 /* The inode may be removed after bitmap searching, or the
811                  * file is new created without inode initialized yet. */
812                 if (rc == -ENOENT || rc == -ESTALE)
813                         RETURN(SCRUB_NEXT_CONTINUE);
814
815                 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
816                        LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
817                 RETURN(rc);
818         }
819
820         /* If the inode has no OI mapping, then it is special locally used,
821          * should be invisible to OI scrub or up layer LFSCK. */
822         if (ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI))
823                 GOTO(put, rc = SCRUB_NEXT_CONTINUE);
824
825         if (scrub &&
826             ldiskfs_test_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB)) {
827                 /* Only skip it for the first OI scrub accessing. */
828                 ldiskfs_clear_inode_state(inode, LDISKFS_STATE_LUSTRE_NOSCRUB);
829                 GOTO(put, rc = SCRUB_NEXT_NOSCRUB);
830         }
831
832         rc = osd_scrub_get_fid(info, dev, inode, fid, scrub);
833
834         GOTO(put, rc);
835
836 put:
837         iput(inode);
838         return rc;
839 }
840
841 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
842                           struct osd_iit_param *param,
843                           struct osd_idmap_cache **oic, int noslot)
844 {
845         struct osd_scrub     *scrub  = &dev->od_scrub;
846         struct ptlrpc_thread *thread = &scrub->os_thread;
847         struct lu_fid        *fid;
848         struct osd_inode_id  *lid;
849         int                   rc;
850
851         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
852                 struct l_wait_info lwi;
853
854                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
855                 l_wait_event(thread->t_ctl_waitq,
856                              !cfs_list_empty(&scrub->os_inconsistent_items) ||
857                              !thread_is_running(thread),
858                              &lwi);
859         }
860
861         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
862                 spin_lock(&scrub->os_lock);
863                 thread_set_flags(thread, SVC_STOPPING);
864                 spin_unlock(&scrub->os_lock);
865                 return SCRUB_NEXT_CRASH;
866         }
867
868         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
869                 return SCRUB_NEXT_FATAL;
870
871         if (unlikely(!thread_is_running(thread)))
872                 return SCRUB_NEXT_EXIT;
873
874         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
875                 struct osd_inconsistent_item *oii;
876
877                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
878                                      struct osd_inconsistent_item, oii_list);
879                 *oic = &oii->oii_cache;
880                 scrub->os_in_prior = 1;
881                 return 0;
882         }
883
884         if (noslot != 0)
885                 return SCRUB_NEXT_WAIT;
886
887         rc = osd_iit_next(param, &scrub->os_pos_current);
888         if (rc != 0)
889                 return rc;
890
891         *oic = &scrub->os_oic;
892         fid = &(*oic)->oic_fid;
893         lid = &(*oic)->oic_lid;
894         rc = osd_iit_iget(info, dev, fid, lid,
895                           scrub->os_pos_current, param->sb, true);
896         return rc;
897 }
898
899 static int osd_preload_next(struct osd_thread_info *info,
900                             struct osd_device *dev, struct osd_iit_param *param,
901                             struct osd_idmap_cache **oic, int noslot)
902 {
903         struct osd_otable_cache *ooc    = &dev->od_otable_it->ooi_cache;
904         struct osd_scrub        *scrub;
905         struct ptlrpc_thread    *thread;
906         int                      rc;
907
908         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
909         if (rc != 0)
910                 return rc;
911
912         scrub = &dev->od_scrub;
913         thread = &scrub->os_thread;
914         if (thread_is_running(thread) &&
915             ooc->ooc_pos_preload >= scrub->os_pos_current)
916                 return SCRUB_NEXT_EXIT;
917
918         rc = osd_iit_iget(info, dev,
919                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
920                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
921                           ooc->ooc_pos_preload, param->sb, false);
922         /* If succeed, it needs to move forward; otherwise up layer LFSCK may
923          * ignore the failure, so it still need to skip the inode next time. */
924         ooc->ooc_pos_preload = param->gbase + ++(param->offset);
925         return rc;
926 }
927
928 static inline int
929 osd_scrub_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
930 {
931         spin_lock(&scrub->os_lock);
932         if (osd_scrub_has_window(scrub, &it->ooi_cache) ||
933             !cfs_list_empty(&scrub->os_inconsistent_items) ||
934             it->ooi_waiting || !thread_is_running(&scrub->os_thread))
935                 scrub->os_waiting = 0;
936         else
937                 scrub->os_waiting = 1;
938         spin_unlock(&scrub->os_lock);
939
940         return !scrub->os_waiting;
941 }
942
943 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
944                           struct osd_iit_param *param,
945                           struct osd_idmap_cache *oic, int *noslot, int rc)
946 {
947         struct l_wait_info       lwi    = { 0 };
948         struct osd_scrub        *scrub  = &dev->od_scrub;
949         struct scrub_file       *sf     = &scrub->os_file;
950         struct ptlrpc_thread    *thread = &scrub->os_thread;
951         struct osd_otable_it    *it     = dev->od_otable_it;
952         struct osd_otable_cache *ooc    = it ? &it->ooi_cache : NULL;
953
954         switch (rc) {
955         case SCRUB_NEXT_CONTINUE:
956                 goto next;
957         case SCRUB_NEXT_WAIT:
958                 goto wait;
959         case SCRUB_NEXT_NOSCRUB:
960                 down_write(&scrub->os_rwsem);
961                 scrub->os_new_checked++;
962                 sf->sf_items_noscrub++;
963                 up_write(&scrub->os_rwsem);
964                 goto next;
965         }
966
967         rc = osd_scrub_check_update(info, dev, oic, rc);
968         if (rc != 0)
969                 return rc;
970
971         rc = osd_scrub_checkpoint(scrub);
972         if (rc != 0) {
973                 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
974                        LDISKFS_SB(param->sb)->s_es->s_volume_name,
975                        scrub->os_pos_current, rc);
976                 /* Continue, as long as the scrub itself can go ahead. */
977         }
978
979         if (scrub->os_in_prior) {
980                 scrub->os_in_prior = 0;
981                 return 0;
982         }
983
984 next:
985         scrub->os_pos_current = param->gbase + ++(param->offset);
986
987 wait:
988         if (it != NULL && it->ooi_waiting &&
989             ooc->ooc_pos_preload < scrub->os_pos_current) {
990                 spin_lock(&scrub->os_lock);
991                 it->ooi_waiting = 0;
992                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
993                 spin_unlock(&scrub->os_lock);
994         }
995
996         if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
997                 return 0;
998
999         if (osd_scrub_has_window(scrub, ooc)) {
1000                 *noslot = 0;
1001                 return 0;
1002         }
1003
1004         l_wait_event(thread->t_ctl_waitq,
1005                      osd_scrub_wakeup(scrub, it),
1006                      &lwi);
1007
1008         if (osd_scrub_has_window(scrub, ooc))
1009                 *noslot = 0;
1010         else
1011                 *noslot = 1;
1012         return 0;
1013 }
1014
1015 static int osd_preload_exec(struct osd_thread_info *info,
1016                             struct osd_device *dev, struct osd_iit_param *param,
1017                             struct osd_idmap_cache *oic, int *noslot, int rc)
1018 {
1019         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1020
1021         if (rc == 0) {
1022                 ooc->ooc_cached_items++;
1023                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
1024                                         ~OSD_OTABLE_IT_CACHE_MASK;
1025         }
1026         return rc > 0 ? 0 : rc;
1027 }
1028
1029 #define SCRUB_IT_ALL    1
1030 #define SCRUB_IT_CRASH  2
1031
1032 static int osd_inode_iteration(struct osd_thread_info *info,
1033                                struct osd_device *dev, __u32 max, bool preload)
1034 {
1035         osd_iit_next_policy   next;
1036         osd_iit_exec_policy   exec;
1037         __u32                *pos;
1038         __u32                *count;
1039         struct osd_iit_param  param;
1040         __u32                 limit;
1041         int                   noslot = 0;
1042         int                   rc;
1043         ENTRY;
1044
1045         if (!preload) {
1046                 struct osd_scrub *scrub = &dev->od_scrub;
1047
1048                 next = osd_scrub_next;
1049                 exec = osd_scrub_exec;
1050                 pos = &scrub->os_pos_current;
1051                 count = &scrub->os_new_checked;
1052         } else {
1053                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
1054
1055                 next = osd_preload_next;
1056                 exec = osd_preload_exec;
1057                 pos = &ooc->ooc_pos_preload;
1058                 count = &ooc->ooc_cached_items;
1059         }
1060         param.sb = osd_sb(dev);
1061         limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
1062
1063         while (*pos <= limit && *count < max) {
1064                 struct osd_idmap_cache *oic = NULL;
1065
1066                 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
1067                 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
1068                 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
1069                 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
1070                 if (param.bitmap == NULL) {
1071                         CERROR("%.16s: fail to read bitmap for %u, "
1072                                "scrub will stop, urgent mode\n",
1073                                LDISKFS_SB(param.sb)->s_es->s_volume_name,
1074                                (__u32)param.bg);
1075                         RETURN(-EIO);
1076                 }
1077
1078                 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
1079                        *count < max) {
1080                         rc = next(info, dev, &param, &oic, noslot);
1081                         switch (rc) {
1082                         case SCRUB_NEXT_BREAK:
1083                                 goto next_group;
1084                         case SCRUB_NEXT_EXIT:
1085                                 brelse(param.bitmap);
1086                                 RETURN(0);
1087                         case SCRUB_NEXT_CRASH:
1088                                 brelse(param.bitmap);
1089                                 RETURN(SCRUB_IT_CRASH);
1090                         case SCRUB_NEXT_FATAL:
1091                                 brelse(param.bitmap);
1092                                 RETURN(-EINVAL);
1093                         }
1094
1095                         rc = exec(info, dev, &param, oic, &noslot, rc);
1096                         if (rc != 0) {
1097                                 brelse(param.bitmap);
1098                                 RETURN(rc);
1099                         }
1100                 }
1101
1102 next_group:
1103                 brelse(param.bitmap);
1104         }
1105
1106         if (*pos > limit)
1107                 RETURN(SCRUB_IT_ALL);
1108         RETURN(0);
1109 }
1110
1111 static int osd_otable_it_preload(const struct lu_env *env,
1112                                  struct osd_otable_it *it)
1113 {
1114         struct osd_device       *dev   = it->ooi_dev;
1115         struct osd_scrub        *scrub = &dev->od_scrub;
1116         struct osd_otable_cache *ooc   = &it->ooi_cache;
1117         int                      rc;
1118         ENTRY;
1119
1120         rc = osd_inode_iteration(osd_oti_get(env), dev,
1121                                  OSD_OTABLE_IT_CACHE_SIZE, true);
1122         if (rc == SCRUB_IT_ALL)
1123                 it->ooi_all_cached = 1;
1124
1125         CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1126                le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1127                ooc->ooc_pos_preload, rc);
1128
1129         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1130                 scrub->os_waiting = 0;
1131                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1132         }
1133
1134         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1135 }
1136
1137 static int osd_scrub_main(void *args)
1138 {
1139         struct lu_env         env;
1140         struct osd_device    *dev    = (struct osd_device *)args;
1141         struct osd_scrub     *scrub  = &dev->od_scrub;
1142         struct ptlrpc_thread *thread = &scrub->os_thread;
1143         struct super_block   *sb     = osd_sb(dev);
1144         int                   rc;
1145         ENTRY;
1146
1147         rc = lu_env_init(&env, LCT_LOCAL);
1148         if (rc != 0) {
1149                 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
1150                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1151                 GOTO(noenv, rc);
1152         }
1153
1154         rc = osd_scrub_prep(dev);
1155         if (rc != 0) {
1156                 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
1157                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
1158                 GOTO(out, rc);
1159         }
1160
1161         if (!scrub->os_full_speed) {
1162                 struct l_wait_info lwi = { 0 };
1163                 struct osd_otable_it *it = dev->od_otable_it;
1164                 struct osd_otable_cache *ooc = &it->ooi_cache;
1165
1166                 l_wait_event(thread->t_ctl_waitq,
1167                              it->ooi_user_ready || !thread_is_running(thread),
1168                              &lwi);
1169                 if (unlikely(!thread_is_running(thread)))
1170                         GOTO(post, rc = 0);
1171
1172                 scrub->os_pos_current = ooc->ooc_pos_preload;
1173         }
1174
1175         CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
1176                scrub->os_start_flags, scrub->os_pos_current);
1177
1178         rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, false);
1179         if (unlikely(rc == SCRUB_IT_CRASH))
1180                 GOTO(out, rc = -EINVAL);
1181         GOTO(post, rc);
1182
1183 post:
1184         osd_scrub_post(scrub, rc);
1185         CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
1186                rc, scrub->os_pos_current);
1187
1188 out:
1189         while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
1190                 struct osd_inconsistent_item *oii;
1191
1192                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
1193                                      struct osd_inconsistent_item, oii_list);
1194                 cfs_list_del_init(&oii->oii_list);
1195                 OBD_FREE_PTR(oii);
1196         }
1197         lu_env_fini(&env);
1198
1199 noenv:
1200         spin_lock(&scrub->os_lock);
1201         thread_set_flags(thread, SVC_STOPPED);
1202         cfs_waitq_broadcast(&thread->t_ctl_waitq);
1203         spin_unlock(&scrub->os_lock);
1204         return rc;
1205 }
1206
1207 /* initial OI scrub */
1208
1209 typedef int (*scandir_t)(struct osd_thread_info *, struct osd_device *,
1210                          struct dentry *, filldir_t filldir);
1211
1212 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1213                                loff_t offset, __u64 ino, unsigned d_type);
1214 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1215                            loff_t offset, __u64 ino, unsigned d_type);
1216
1217 static int
1218 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1219                      struct dentry *dentry, filldir_t filldir);
1220 static int
1221 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1222                   struct dentry *dentry, filldir_t filldir);
1223
1224 static int
1225 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1226                      struct dentry *dentry, filldir_t filldir);
1227
1228 enum osd_lf_flags {
1229         OLF_SCAN_SUBITEMS       = 0x0001,
1230         OLF_HIDE_FID            = 0x0002,
1231         OLF_SHOW_NAME           = 0x0004,
1232         OLF_NO_OI               = 0x0008,
1233 };
1234
1235 struct osd_lf_map {
1236         char            *olm_name;
1237         struct lu_fid    olm_fid;
1238         __u16            olm_flags;
1239         scandir_t        olm_scandir;
1240         filldir_t        olm_filldir;
1241 };
1242
1243 /* Add the new introduced local files in the list in the future. */
1244 static const struct osd_lf_map osd_lf_maps[] = {
1245         /* CATALOGS */
1246         { CATLIST, { FID_SEQ_LOCAL_FILE, LLOG_CATALOGS_OID, 0 }, OLF_SHOW_NAME,
1247                 NULL, NULL },
1248
1249         /* CONFIGS */
1250         { MOUNT_CONFIGS_DIR, { FID_SEQ_LOCAL_FILE, MGS_CONFIGS_OID, 0 },
1251                 OLF_SCAN_SUBITEMS, osd_ios_general_scan,
1252                 osd_ios_varfid_fill },
1253
1254         /* NIDTBL_VERSIONS */
1255         { MGS_NIDTBL_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1256                 osd_ios_general_scan, osd_ios_varfid_fill },
1257
1258         /* PENDING */
1259         { "PENDING", { 0, 0, 0 }, 0, NULL, NULL },
1260
1261         /* ROOT */
1262         { "ROOT", { FID_SEQ_ROOT, 1, 0 },
1263                 OLF_SCAN_SUBITEMS | OLF_HIDE_FID, osd_ios_ROOT_scan, NULL },
1264
1265         /* changelog_catalog */
1266         { CHANGELOG_CATALOG, { 0, 0, 0 }, 0, NULL, NULL },
1267
1268         /* changelog_users */
1269         { CHANGELOG_USERS, { 0, 0, 0 }, 0, NULL, NULL },
1270
1271         /* fld */
1272         { "fld", { FID_SEQ_LOCAL_FILE, FLD_INDEX_OID, 0 }, OLF_SHOW_NAME,
1273                 NULL, NULL },
1274
1275         /* last_rcvd */
1276         { LAST_RCVD, { FID_SEQ_LOCAL_FILE, LAST_RECV_OID, 0 }, OLF_SHOW_NAME,
1277                 NULL, NULL },
1278
1279         /* lfsck_bookmark */
1280         { "lfsck_bookmark", { 0, 0, 0 }, 0, NULL, NULL },
1281
1282         /* lov_objid */
1283         { LOV_OBJID, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OID, 0 }, OLF_SHOW_NAME,
1284                 NULL, NULL },
1285
1286         /* lov_objseq */
1287         { LOV_OBJSEQ, { FID_SEQ_LOCAL_FILE, MDD_LOV_OBJ_OSEQ, 0 },
1288                 OLF_SHOW_NAME, NULL, NULL },
1289
1290         /* quota_master */
1291         { QMT_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1292                 osd_ios_general_scan, osd_ios_varfid_fill },
1293
1294         /* quota_slave */
1295         { QSD_DIR, { 0, 0, 0 }, OLF_SCAN_SUBITEMS,
1296                 osd_ios_general_scan, osd_ios_varfid_fill },
1297
1298         /* seq_ctl */
1299         { "seq_ctl", { FID_SEQ_LOCAL_FILE, FID_SEQ_CTL_OID, 0 },
1300                 OLF_SHOW_NAME, NULL, NULL },
1301
1302         /* seq_srv */
1303         { "seq_srv", { FID_SEQ_LOCAL_FILE, FID_SEQ_SRV_OID, 0 },
1304                 OLF_SHOW_NAME, NULL, NULL },
1305
1306         /* health_check */
1307         { HEALTH_CHECK, { FID_SEQ_LOCAL_FILE, OFD_HEALTH_CHECK_OID, 0 },
1308                 OLF_SHOW_NAME, NULL, NULL },
1309
1310         /* lfsck_namespace */
1311         { "lfsck_namespace", { 0, 0, 0 }, 0, NULL, NULL },
1312
1313         /* OBJECTS, upgrade from old device */
1314         { OBJECTS, { 0, 0, 0 }, OLF_SCAN_SUBITEMS, osd_ios_OBJECTS_scan, NULL },
1315
1316         /* lquota_v2.user, upgrade from old device */
1317         { "lquota_v2.user", { 0, 0, 0 }, 0, NULL, NULL },
1318
1319         /* lquota_v2.group, upgrade from old device */
1320         { "lquota_v2.group", { 0, 0, 0 }, 0, NULL, NULL },
1321
1322         /* LAST_GROUP, upgrade from old device */
1323         { "LAST_GROUP", { FID_SEQ_LOCAL_FILE, OFD_LAST_GROUP_OID, 0 },
1324                 OLF_SHOW_NAME, NULL, NULL },
1325
1326         /* lost+found */
1327         { "lost+found", { 0, 0, 0 }, OLF_SCAN_SUBITEMS | OLF_NO_OI,
1328                 osd_ios_general_scan, osd_ios_lf_fill },
1329
1330         { NULL, { 0, 0, 0 }, 0, NULL, NULL }
1331 };
1332
1333 struct osd_ios_item {
1334         cfs_list_t       oii_list;
1335         struct dentry   *oii_dentry;
1336         scandir_t        oii_scandir;
1337         filldir_t        oii_filldir;
1338 };
1339
1340 struct osd_ios_filldir_buf {
1341         struct osd_thread_info  *oifb_info;
1342         struct osd_device       *oifb_dev;
1343         struct dentry           *oifb_dentry;
1344 };
1345
1346 static inline struct dentry *
1347 osd_ios_lookup_one_len(const char *name, struct dentry *parent, int namelen)
1348 {
1349         struct dentry *dentry;
1350
1351         dentry = ll_lookup_one_len(name, parent, namelen);
1352         if (!IS_ERR(dentry) && dentry->d_inode == NULL) {
1353                 dput(dentry);
1354                 return ERR_PTR(-ENOENT);
1355         }
1356
1357         return dentry;
1358 }
1359
1360 static inline void
1361 osd_ios_llogname2fid(struct lu_fid *fid, const char *name, int namelen)
1362 {
1363         obd_id id = 0;
1364         int    i  = 0;
1365
1366         fid->f_seq = FID_SEQ_LLOG;
1367         while (i < namelen)
1368                 id = id * 10 + name[i++] - '0';
1369
1370         fid->f_oid = id & 0x00000000ffffffffULL;
1371         fid->f_ver = id >> 32;
1372 }
1373
1374 static inline void
1375 osd_ios_Oname2fid(struct lu_fid *fid, const char *name, int namelen)
1376 {
1377         __u64 seq = 0;
1378         int   i   = 0;
1379
1380         while (i < namelen)
1381                 seq = seq * 10 + name[i++] - '0';
1382
1383         lu_last_id_fid(fid, seq);
1384 }
1385
1386 static int
1387 osd_ios_new_item(struct osd_device *dev, struct dentry *dentry,
1388                  scandir_t scandir, filldir_t filldir)
1389 {
1390         struct osd_ios_item *item;
1391
1392         OBD_ALLOC_PTR(item);
1393         if (item == NULL)
1394                 return -ENOMEM;
1395
1396         CFS_INIT_LIST_HEAD(&item->oii_list);
1397         item->oii_dentry = dget(dentry);
1398         item->oii_scandir = scandir;
1399         item->oii_filldir = filldir;
1400         cfs_list_add_tail(&item->oii_list, &dev->od_ios_list);
1401         return 0;
1402 }
1403
1404 /**
1405  * osd_ios_scan_one() - check/fix LMA FID and OI entry for one inode
1406  *
1407  * The passed \a inode's \a fid is verified against the LMA FID. If the \a fid
1408  * is NULL or is empty the IGIF FID is used. The FID is verified in the OI to
1409  * reference the inode, or fixed if it is missing or references another inode.
1410  */
1411 static int
1412 osd_ios_scan_one(struct osd_thread_info *info, struct osd_device *dev,
1413                  struct inode *inode, const struct lu_fid *fid, int flags)
1414 {
1415         struct lustre_mdt_attrs *lma    = &info->oti_mdt_attrs;
1416         struct osd_inode_id     *id     = &info->oti_id;
1417         struct osd_inode_id     *id2    = &info->oti_id2;
1418         struct osd_scrub        *scrub  = &dev->od_scrub;
1419         struct scrub_file       *sf     = &scrub->os_file;
1420         struct lu_fid            tfid;
1421         int                      rc;
1422         ENTRY;
1423
1424         rc = osd_get_lma(info, inode, &info->oti_obj_dentry, lma);
1425         if (rc != 0 && rc != -ENODATA)
1426                 RETURN(rc);
1427
1428         osd_id_gen(id, inode->i_ino, inode->i_generation);
1429         if (rc == -ENODATA) {
1430                 if (fid == NULL || fid_is_zero(fid) || flags & OLF_HIDE_FID)
1431                         lu_igif_build(&tfid, inode->i_ino, inode->i_generation);
1432                 else
1433                         tfid = *fid;
1434                 rc = osd_ea_fid_set(info, inode, &tfid, 0, 0);
1435                 if (rc != 0)
1436                         RETURN(rc);
1437         } else {
1438                 if (lma->lma_compat & LMAC_NOT_IN_OI)
1439                         RETURN(0);
1440
1441                 tfid = lma->lma_self_fid;
1442         }
1443
1444         rc = osd_oi_lookup(info, dev, &tfid, id2, 0);
1445         if (rc != 0) {
1446                 if (rc != -ENOENT)
1447                         RETURN(rc);
1448
1449                 rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1450                                                DTO_INDEX_INSERT, 0);
1451                 if (rc > 0)
1452                         rc = 0;
1453
1454                 RETURN(rc);
1455         }
1456
1457         if (osd_id_eq_strict(id, id2))
1458                 RETURN(0);
1459
1460         if (!(sf->sf_flags & SF_INCONSISTENT)) {
1461                 osd_scrub_file_reset(scrub,
1462                                      LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1463                                      SF_INCONSISTENT);
1464                 rc = osd_scrub_file_store(scrub);
1465                 if (rc != 0)
1466                         RETURN(rc);
1467         }
1468
1469         rc = osd_scrub_refresh_mapping(info, dev, &tfid, id,
1470                                        DTO_INDEX_UPDATE, 0);
1471         if (rc > 0)
1472                 rc = 0;
1473
1474         RETURN(rc);
1475 }
1476
1477 /**
1478  * It scans the /lost+found, and for the OST-object (with filter_fid
1479  * or filter_fid_old), move them back to its proper /O/<seq>/d<x>.
1480  */
1481 static int osd_ios_lf_fill(void *buf, const char *name, int namelen,
1482                            loff_t offset, __u64 ino, unsigned d_type)
1483 {
1484         struct osd_ios_filldir_buf *fill_buf = buf;
1485         struct osd_thread_info     *info     = fill_buf->oifb_info;
1486         struct osd_device          *dev      = fill_buf->oifb_dev;
1487         struct lu_fid              *fid      = &info->oti_fid;
1488         struct osd_scrub           *scrub    = &dev->od_scrub;
1489         struct dentry              *parent   = fill_buf->oifb_dentry;
1490         struct dentry              *child;
1491         struct inode               *dir      = parent->d_inode;
1492         struct inode               *inode;
1493         int                         rc;
1494         ENTRY;
1495
1496         /* skip any '.' started names */
1497         if (name[0] == '.')
1498                 RETURN(0);
1499
1500         scrub->os_lf_scanned++;
1501         child = osd_ios_lookup_one_len(name, parent, namelen);
1502         if (IS_ERR(child)) {
1503                 CWARN("%s: cannot lookup child '%.*s': rc = %d\n",
1504                       osd_name(dev), namelen, name, (int)PTR_ERR(child));
1505                 RETURN(0);
1506         }
1507
1508         inode = child->d_inode;
1509         if (S_ISDIR(inode->i_mode)) {
1510                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1511                                       osd_ios_lf_fill);
1512                 if (rc != 0)
1513                         CWARN("%s: cannot add child '%.*s': rc = %d\n",
1514                               osd_name(dev), namelen, name, rc);
1515                 GOTO(put, rc);
1516         }
1517
1518         if (!S_ISREG(inode->i_mode))
1519                 GOTO(put, rc = 0);
1520
1521         rc = osd_scrub_get_fid(info, dev, inode, fid, true);
1522         if (rc == SCRUB_NEXT_OSTOBJ || rc == SCRUB_NEXT_OSTOBJ_OLD) {
1523                 rc = osd_obj_map_recover(info, dev, dir, child, fid);
1524                 if (rc == 0) {
1525                         CDEBUG(D_LFSCK, "recovered '%.*s' ["DFID"] from "
1526                                "/lost+found.\n", namelen, name, PFID(fid));
1527                         scrub->os_lf_repaired++;
1528                 } else {
1529                         CWARN("%s: cannot rename for '%.*s' "DFID": rc = %d\n",
1530                               osd_name(dev), namelen, name, PFID(fid), rc);
1531                 }
1532         }
1533
1534         /* XXX: For MDT-objects, we can move them from /lost+found to namespace
1535          *      visible place, such as the /ROOT/.lustre/lost+found, then LFSCK
1536          *      can process them in furtuer. */
1537
1538         GOTO(put, rc);
1539
1540 put:
1541         if (rc < 0)
1542                 scrub->os_lf_failed++;
1543         dput(child);
1544         /* skip the failure to make the scanning to continue. */
1545         return 0;
1546 }
1547
1548 static int osd_ios_varfid_fill(void *buf, const char *name, int namelen,
1549                                loff_t offset, __u64 ino, unsigned d_type)
1550 {
1551         struct osd_ios_filldir_buf *fill_buf = buf;
1552         struct osd_device          *dev      = fill_buf->oifb_dev;
1553         struct dentry              *child;
1554         int                         rc;
1555         ENTRY;
1556
1557         /* skip any '.' started names */
1558         if (name[0] == '.')
1559                 RETURN(0);
1560
1561         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1562         if (IS_ERR(child))
1563                 RETURN(PTR_ERR(child));
1564
1565         rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1566                               NULL, 0);
1567         if (rc == 0 && S_ISDIR(child->d_inode->i_mode))
1568                 rc = osd_ios_new_item(dev, child, osd_ios_general_scan,
1569                                       osd_ios_varfid_fill);
1570         dput(child);
1571
1572         RETURN(rc);
1573 }
1574
1575 static int osd_ios_root_fill(void *buf, const char *name, int namelen,
1576                              loff_t offset, __u64 ino, unsigned d_type)
1577 {
1578         struct osd_ios_filldir_buf *fill_buf = buf;
1579         struct osd_device          *dev      = fill_buf->oifb_dev;
1580         const struct osd_lf_map    *map;
1581         struct dentry              *child;
1582         int                         rc       = 0;
1583         ENTRY;
1584
1585         /* skip any '.' started names */
1586         if (name[0] == '.')
1587                 RETURN(0);
1588
1589         for (map = osd_lf_maps; map->olm_name != NULL; map++) {
1590                 if (strlen(map->olm_name) != namelen)
1591                         continue;
1592
1593                 if (strncmp(map->olm_name, name, namelen) == 0)
1594                         break;
1595         }
1596
1597         if (map->olm_name == NULL)
1598                 RETURN(0);
1599
1600         child = osd_ios_lookup_one_len(name, fill_buf->oifb_dentry, namelen);
1601         if (IS_ERR(child))
1602                 RETURN(PTR_ERR(child));
1603
1604         if (!(map->olm_flags & OLF_NO_OI))
1605                 rc = osd_ios_scan_one(fill_buf->oifb_info, dev, child->d_inode,
1606                                       &map->olm_fid, map->olm_flags);
1607         if (rc == 0 && map->olm_flags & OLF_SCAN_SUBITEMS)
1608                 rc = osd_ios_new_item(dev, child, map->olm_scandir,
1609                                       map->olm_filldir);
1610         dput(child);
1611
1612         RETURN(rc);
1613 }
1614
1615 static int
1616 osd_ios_general_scan(struct osd_thread_info *info, struct osd_device *dev,
1617                      struct dentry *dentry, filldir_t filldir)
1618 {
1619         struct osd_ios_filldir_buf    buf   = { info, dev, dentry };
1620         struct file                  *filp  = &info->oti_it_ea.oie_file;
1621         struct inode                 *inode = dentry->d_inode;
1622         const struct file_operations *fops  = inode->i_fop;
1623         int                           rc;
1624         ENTRY;
1625
1626         LASSERT(filldir != NULL);
1627
1628         filp->f_pos = 0;
1629         filp->f_dentry = dentry;
1630         filp->f_mode = FMODE_64BITHASH;
1631         filp->f_mapping = inode->i_mapping;
1632         filp->f_op = fops;
1633         filp->private_data = NULL;
1634
1635         rc = fops->readdir(filp, &buf, filldir);
1636         fops->release(inode, filp);
1637
1638         RETURN(rc);
1639 }
1640
1641 static int
1642 osd_ios_ROOT_scan(struct osd_thread_info *info, struct osd_device *dev,
1643                   struct dentry *dentry, filldir_t filldir)
1644 {
1645         struct osd_scrub  *scrub  = &dev->od_scrub;
1646         struct scrub_file *sf     = &scrub->os_file;
1647         struct dentry     *child;
1648         int                rc;
1649         ENTRY;
1650
1651         /* It is existing MDT0 device. We only allow the case of object without
1652          * LMA to happen on the MDT0, which is usually for old 1.8 MDT. Then we
1653          * can generate IGIF mode FID for the object and related OI mapping. If
1654          * it is on other MDTs, then becuase file-level backup/restore, related
1655          * OI mapping may be invalid already, we do not know which is the right
1656          * FID for the object. We only allow IGIF objects to reside on the MDT0.
1657          *
1658          * XXX: For the case of object on non-MDT0 device with neither LMA nor
1659          *      "fid" xattr, then something crashed. We cannot re-generate the
1660          *      FID directly, instead, the OI scrub will scan the OI structure
1661          *      and try to re-generate the LMA from the OI mapping. But if the
1662          *      OI mapping crashed or lost also, then we have to give up under
1663          *      double failure cases. */
1664         dev->od_handle_nolma = 1;
1665         child = osd_ios_lookup_one_len(dot_lustre_name, dentry,
1666                                        strlen(dot_lustre_name));
1667         if (IS_ERR(child)) {
1668                 rc = PTR_ERR(child);
1669                 if (rc == -ENOENT) {
1670                         /* It is 1.8 MDT device. */
1671                         if (!(sf->sf_flags & SF_UPGRADE)) {
1672                                 osd_scrub_file_reset(scrub,
1673                                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid,
1674                                         SF_UPGRADE);
1675                                 rc = osd_scrub_file_store(scrub);
1676                         } else {
1677                                 rc = 0;
1678                         }
1679                 }
1680         } else {
1681                 /* For lustre-2.x (x <= 3), the ".lustre" has NO FID-in-LMA,
1682                  * so the client will get IGIF for the ".lustre" object when
1683                  * the MDT restart.
1684                  *
1685                  * From the OI scrub view, when the MDT upgrade to Lustre-2.4,
1686                  * it does not know whether there are some old clients cached
1687                  * the ".lustre" IGIF during the upgrading. Two choices:
1688                  *
1689                  * 1) Generate IGIF-in-LMA and IGIF-in-OI for the ".lustre".
1690                  *    It will allow the old connected clients to access the
1691                  *    ".lustre" with cached IGIF. But it will cause others
1692                  *    on the MDT failed to check "fid_is_dot_lustre()".
1693                  *
1694                  * 2) Use fixed FID {FID_SEQ_DOT_LUSTRE, FID_OID_DOT_LUSTRE, 0}
1695                  *    for ".lustre" in spite of whether there are some clients
1696                  *    cached the ".lustre" IGIF or not. It enables the check
1697                  *    "fid_is_dot_lustre()" on the MDT, although it will cause
1698                  *    that the old connected clients cannot access the ".lustre"
1699                  *    with the cached IGIF.
1700                  *
1701                  * Usually, it is rare case for the old connected clients
1702                  * to access the ".lustre" with cached IGIF. So we prefer
1703                  * to the solution 2). */
1704                 rc = osd_ios_scan_one(info, dev, child->d_inode,
1705                                       &LU_DOT_LUSTRE_FID, 0);
1706                 dput(child);
1707         }
1708
1709         RETURN(rc);
1710 }
1711
1712 static int
1713 osd_ios_OBJECTS_scan(struct osd_thread_info *info, struct osd_device *dev,
1714                      struct dentry *dentry, filldir_t filldir)
1715 {
1716         struct dentry *child;
1717         int            rc;
1718         ENTRY;
1719
1720         child = osd_ios_lookup_one_len(ADMIN_USR, dentry, strlen(ADMIN_USR));
1721         if (!IS_ERR(child)) {
1722                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1723                 dput(child);
1724         } else {
1725                 rc = PTR_ERR(child);
1726         }
1727
1728         if (rc != 0 && rc != -ENOENT)
1729                 RETURN(rc);
1730
1731         child = osd_ios_lookup_one_len(ADMIN_GRP, dentry, strlen(ADMIN_GRP));
1732         if (!IS_ERR(child)) {
1733                 rc = osd_ios_scan_one(info, dev, child->d_inode, NULL, 0);
1734                 dput(child);
1735         } else {
1736                 rc = PTR_ERR(child);
1737         }
1738
1739         if (rc == -ENOENT)
1740                 rc = 0;
1741
1742         RETURN(rc);
1743 }
1744
1745 static int osd_initial_OI_scrub(struct osd_thread_info *info,
1746                                 struct osd_device *dev)
1747 {
1748         struct osd_ios_item     *item    = NULL;
1749         scandir_t                scandir = osd_ios_general_scan;
1750         filldir_t                filldir = osd_ios_root_fill;
1751         struct dentry           *dentry  = osd_sb(dev)->s_root;
1752         const struct osd_lf_map *map     = osd_lf_maps;
1753         int                      rc;
1754         ENTRY;
1755
1756         while (1) {
1757                 rc = scandir(info, dev, dentry, filldir);
1758                 if (item != NULL) {
1759                         dput(item->oii_dentry);
1760                         OBD_FREE_PTR(item);
1761                 }
1762
1763                 if (rc != 0)
1764                         break;
1765
1766                 if (cfs_list_empty(&dev->od_ios_list))
1767                         break;
1768
1769                 item = cfs_list_entry(dev->od_ios_list.next,
1770                                       struct osd_ios_item, oii_list);
1771                 cfs_list_del_init(&item->oii_list);
1772
1773                 LASSERT(item->oii_scandir != NULL);
1774                 scandir = item->oii_scandir;
1775                 filldir = item->oii_filldir;
1776                 dentry = item->oii_dentry;
1777         }
1778
1779         while (!cfs_list_empty(&dev->od_ios_list)) {
1780                 item = cfs_list_entry(dev->od_ios_list.next,
1781                                       struct osd_ios_item, oii_list);
1782                 cfs_list_del_init(&item->oii_list);
1783                 dput(item->oii_dentry);
1784                 OBD_FREE_PTR(item);
1785         }
1786
1787         if (rc != 0)
1788                 RETURN(rc);
1789
1790         /* There maybe the case that the object has been removed, but its OI
1791          * mapping is still in the OI file, such as the "CATALOGS" after MDT
1792          * file-level backup/restore. So here cleanup the stale OI mappings. */
1793         while (map->olm_name != NULL) {
1794                 struct dentry *child;
1795
1796                 if (fid_is_zero(&map->olm_fid)) {
1797                         map++;
1798                         continue;
1799                 }
1800
1801                 child = osd_ios_lookup_one_len(map->olm_name,
1802                                                osd_sb(dev)->s_root,
1803                                                strlen(map->olm_name));
1804                 if (!IS_ERR(child))
1805                         dput(child);
1806                 else if (PTR_ERR(child) == -ENOENT)
1807                         osd_scrub_refresh_mapping(info, dev, &map->olm_fid,
1808                                                   NULL, DTO_INDEX_DELETE, 0);
1809                 map++;
1810         }
1811
1812         RETURN(0);
1813 }
1814
1815 char *osd_lf_fid2name(const struct lu_fid *fid)
1816 {
1817         const struct osd_lf_map *map = osd_lf_maps;
1818
1819         while (map->olm_name != NULL) {
1820                 if (!lu_fid_eq(fid, &map->olm_fid)) {
1821                         map++;
1822                         continue;
1823                 }
1824
1825                 if (map->olm_flags & OLF_SHOW_NAME)
1826                         return map->olm_name;
1827                 else
1828                         return "";
1829         }
1830
1831         return NULL;
1832 }
1833
1834 /* OI scrub start/stop */
1835
1836 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
1837 {
1838         struct osd_scrub     *scrub  = &dev->od_scrub;
1839         struct ptlrpc_thread *thread = &scrub->os_thread;
1840         struct l_wait_info    lwi    = { 0 };
1841         int                   rc;
1842         ENTRY;
1843
1844 again:
1845         /* os_lock: sync status between stop and scrub thread */
1846         spin_lock(&scrub->os_lock);
1847         if (thread_is_running(thread)) {
1848                 spin_unlock(&scrub->os_lock);
1849                 RETURN(-EALREADY);
1850         } else if (unlikely(thread_is_stopping(thread))) {
1851                 spin_unlock(&scrub->os_lock);
1852                 l_wait_event(thread->t_ctl_waitq,
1853                              thread_is_stopped(thread),
1854                              &lwi);
1855                 goto again;
1856         }
1857         spin_unlock(&scrub->os_lock);
1858
1859         if (scrub->os_file.sf_status == SS_COMPLETED)
1860                 flags |= SS_RESET;
1861
1862         scrub->os_start_flags = flags;
1863         thread_set_flags(thread, 0);
1864         rc = PTR_ERR(kthread_run(osd_scrub_main, dev, "OI_scrub"));
1865         if (IS_ERR_VALUE(rc)) {
1866                 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
1867                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
1868                 RETURN(rc);
1869         }
1870
1871         l_wait_event(thread->t_ctl_waitq,
1872                      thread_is_running(thread) || thread_is_stopped(thread),
1873                      &lwi);
1874
1875         RETURN(0);
1876 }
1877
1878 int osd_scrub_start(struct osd_device *dev)
1879 {
1880         int rc;
1881         ENTRY;
1882
1883         /* od_otable_mutex: prevent curcurrent start/stop */
1884         mutex_lock(&dev->od_otable_mutex);
1885         rc = do_osd_scrub_start(dev, SS_AUTO);
1886         mutex_unlock(&dev->od_otable_mutex);
1887
1888         RETURN(rc == -EALREADY ? 0 : rc);
1889 }
1890
1891 static void do_osd_scrub_stop(struct osd_scrub *scrub)
1892 {
1893         struct ptlrpc_thread *thread = &scrub->os_thread;
1894         struct l_wait_info    lwi    = { 0 };
1895
1896         /* os_lock: sync status between stop and scrub thread */
1897         spin_lock(&scrub->os_lock);
1898         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
1899                 thread_set_flags(thread, SVC_STOPPING);
1900                 spin_unlock(&scrub->os_lock);
1901                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1902                 l_wait_event(thread->t_ctl_waitq,
1903                              thread_is_stopped(thread),
1904                              &lwi);
1905                 /* Do not skip the last lock/unlock, which can guarantee that
1906                  * the caller cannot return until the OI scrub thread exit. */
1907                 spin_lock(&scrub->os_lock);
1908         }
1909         spin_unlock(&scrub->os_lock);
1910 }
1911
1912 static void osd_scrub_stop(struct osd_device *dev)
1913 {
1914         /* od_otable_mutex: prevent curcurrent start/stop */
1915         mutex_lock(&dev->od_otable_mutex);
1916         dev->od_scrub.os_paused = 1;
1917         do_osd_scrub_stop(&dev->od_scrub);
1918         mutex_unlock(&dev->od_otable_mutex);
1919 }
1920
1921 /* OI scrub setup/cleanup */
1922
1923 static const char osd_scrub_name[] = "OI_scrub";
1924
1925 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1926 {
1927         struct osd_thread_info     *info   = osd_oti_get(env);
1928         struct osd_scrub           *scrub  = &dev->od_scrub;
1929         struct lvfs_run_ctxt       *ctxt   = &scrub->os_ctxt;
1930         struct scrub_file          *sf     = &scrub->os_file;
1931         struct super_block         *sb     = osd_sb(dev);
1932         struct ldiskfs_super_block *es     = LDISKFS_SB(sb)->s_es;
1933         struct lvfs_run_ctxt        saved;
1934         struct file                *filp;
1935         struct inode               *inode;
1936         struct lu_fid              *fid    = &info->oti_fid;
1937         int                         dirty  = 0;
1938         int                         rc     = 0;
1939         ENTRY;
1940
1941         memset(scrub, 0, sizeof(*scrub));
1942         OBD_SET_CTXT_MAGIC(ctxt);
1943         ctxt->pwdmnt = dev->od_mnt;
1944         ctxt->pwd = dev->od_mnt->mnt_root;
1945         ctxt->fs = get_ds();
1946
1947         cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1948         init_rwsem(&scrub->os_rwsem);
1949         spin_lock_init(&scrub->os_lock);
1950         CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1951
1952         push_ctxt(&saved, ctxt, NULL);
1953         filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1954         if (IS_ERR(filp)) {
1955                 pop_ctxt(&saved, ctxt, NULL);
1956                 RETURN(PTR_ERR(filp));
1957         }
1958
1959         inode = filp->f_dentry->d_inode;
1960         ldiskfs_set_inode_state(inode, LDISKFS_STATE_LUSTRE_NO_OI);
1961         /* 'What the @fid is' is not imporatant, because the object
1962          * has no OI mapping, and only is visible inside the OSD.*/
1963         lu_igif_build(fid, inode->i_ino, inode->i_generation);
1964         rc = osd_ea_fid_set(info, inode, fid, LMAC_NOT_IN_OI, 0);
1965         if (rc != 0) {
1966                 filp_close(filp, 0);
1967                 pop_ctxt(&saved, ctxt, NULL);
1968                 RETURN(rc);
1969         }
1970
1971         scrub->os_inode = igrab(inode);
1972         filp_close(filp, 0);
1973         pop_ctxt(&saved, ctxt, NULL);
1974
1975         rc = osd_scrub_file_load(scrub);
1976         if (rc == -ENOENT) {
1977                 osd_scrub_file_init(scrub, es->s_uuid);
1978                 dirty = 1;
1979         } else if (rc != 0) {
1980                 RETURN(rc);
1981         } else {
1982                 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1983                         osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1984                         dirty = 1;
1985                 } else if (sf->sf_status == SS_SCANNING) {
1986                         sf->sf_status = SS_CRASHED;
1987                         dirty = 1;
1988                 }
1989         }
1990
1991         if (sf->sf_pos_last_checkpoint != 0)
1992                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1993         else
1994                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb) + 1;
1995
1996         if (dirty != 0) {
1997                 rc = osd_scrub_file_store(scrub);
1998                 if (rc != 0)
1999                         RETURN(rc);
2000         }
2001
2002         /* Initialize OI files. */
2003         rc = osd_oi_init(info, dev);
2004         if (rc < 0)
2005                 RETURN(rc);
2006
2007         rc = osd_initial_OI_scrub(info, dev);
2008         if (rc == 0) {
2009                 if ((sf->sf_flags & SF_UPGRADE) &&
2010                    !(sf->sf_flags & SF_INCONSISTENT))
2011                         /* The 'od_igif_inoi' will be set after the
2012                          * upgrading completed, needs NOT remount. */
2013                         dev->od_igif_inoi = 0;
2014                 else
2015                         /* The 'od_igif_inoi' will be set under the
2016                          * following cases:
2017                          * 1) new created system, or
2018                          * 2) restored from file-level backup, or
2019                          * 3) the upgrading completed.
2020                          *
2021                          * The 'od_igif_inoi' may be cleared by OI scrub
2022                          * later if found that the system is upgrading. */
2023                         dev->od_igif_inoi = 1;
2024
2025                 if (!dev->od_noscrub &&
2026                     ((sf->sf_status == SS_PAUSED) ||
2027                      (sf->sf_status == SS_CRASHED &&
2028                       sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2029                                       SF_UPGRADE | SF_AUTO)) ||
2030                      (sf->sf_status == SS_INIT &&
2031                       sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
2032                                       SF_UPGRADE))))
2033                         rc = osd_scrub_start(dev);
2034         }
2035
2036         /* it is possible that dcache entries may keep objects after they are
2037          * deleted by OSD. While it looks safe this can cause object data to
2038          * stay until umount causing failures in tests calculating free space,
2039          * e.g. replay-ost-single. Since those dcache entries are not used
2040          * anymore let's just free them after use here */
2041         shrink_dcache_sb(sb);
2042
2043         RETURN(rc);
2044 }
2045
2046 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
2047 {
2048         struct osd_scrub *scrub = &dev->od_scrub;
2049
2050         LASSERT(dev->od_otable_it == NULL);
2051
2052         if (scrub->os_inode != NULL) {
2053                 osd_scrub_stop(dev);
2054                 iput(scrub->os_inode);
2055                 scrub->os_inode = NULL;
2056         }
2057         if (dev->od_oi_table != NULL)
2058                 osd_oi_fini(osd_oti_get(env), dev);
2059 }
2060
2061 /* object table based iteration APIs */
2062
2063 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
2064                                        struct dt_object *dt, __u32 attr,
2065                                        struct lustre_capa *capa)
2066 {
2067         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
2068         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
2069         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
2070         struct osd_scrub       *scrub = &dev->od_scrub;
2071         struct osd_otable_it   *it;
2072         __u32                   start = 0;
2073         int                     rc;
2074         ENTRY;
2075
2076         /* od_otable_mutex: prevent curcurrent init/fini */
2077         mutex_lock(&dev->od_otable_mutex);
2078         if (dev->od_otable_it != NULL)
2079                 GOTO(out, it = ERR_PTR(-EALREADY));
2080
2081         OBD_ALLOC_PTR(it);
2082         if (it == NULL)
2083                 GOTO(out, it = ERR_PTR(-ENOMEM));
2084
2085         dev->od_otable_it = it;
2086         it->ooi_dev = dev;
2087         it->ooi_cache.ooc_consumer_idx = -1;
2088         if (flags & DOIF_OUTUSED)
2089                 it->ooi_used_outside = 1;
2090
2091         if (flags & DOIF_RESET)
2092                 start |= SS_RESET;
2093
2094         if (valid & DOIV_ERROR_HANDLE) {
2095                 if (flags & DOIF_FAILOUT)
2096                         start |= SS_SET_FAILOUT;
2097                 else
2098                         start |= SS_CLEAR_FAILOUT;
2099         }
2100
2101         rc = do_osd_scrub_start(dev, start);
2102         if (rc < 0 && rc != -EALREADY) {
2103                 dev->od_otable_it = NULL;
2104                 OBD_FREE_PTR(it);
2105                 GOTO(out, it = ERR_PTR(rc));
2106         }
2107
2108         it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
2109
2110         GOTO(out, it);
2111
2112 out:
2113         mutex_unlock(&dev->od_otable_mutex);
2114         return (struct dt_it *)it;
2115 }
2116
2117 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
2118 {
2119         struct osd_otable_it *it  = (struct osd_otable_it *)di;
2120         struct osd_device    *dev = it->ooi_dev;
2121
2122         /* od_otable_mutex: prevent curcurrent init/fini */
2123         mutex_lock(&dev->od_otable_mutex);
2124         do_osd_scrub_stop(&dev->od_scrub);
2125         LASSERT(dev->od_otable_it == it);
2126
2127         dev->od_otable_it = NULL;
2128         mutex_unlock(&dev->od_otable_mutex);
2129         OBD_FREE_PTR(it);
2130 }
2131
2132 static int osd_otable_it_get(const struct lu_env *env,
2133                              struct dt_it *di, const struct dt_key *key)
2134 {
2135         return 0;
2136 }
2137
2138 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
2139 {
2140 }
2141
2142 static inline int
2143 osd_otable_it_wakeup(struct osd_scrub *scrub, struct osd_otable_it *it)
2144 {
2145         spin_lock(&scrub->os_lock);
2146         if (it->ooi_cache.ooc_pos_preload < scrub->os_pos_current ||
2147             scrub->os_waiting ||
2148             !thread_is_running(&scrub->os_thread))
2149                 it->ooi_waiting = 0;
2150         else
2151                 it->ooi_waiting = 1;
2152         spin_unlock(&scrub->os_lock);
2153
2154         return !it->ooi_waiting;
2155 }
2156
2157 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
2158 {
2159         struct osd_otable_it    *it     = (struct osd_otable_it *)di;
2160         struct osd_device       *dev    = it->ooi_dev;
2161         struct osd_scrub        *scrub  = &dev->od_scrub;
2162         struct osd_otable_cache *ooc    = &it->ooi_cache;
2163         struct ptlrpc_thread    *thread = &scrub->os_thread;
2164         struct l_wait_info       lwi    = { 0 };
2165         int                      rc;
2166         ENTRY;
2167
2168         LASSERT(it->ooi_user_ready);
2169
2170 again:
2171         if (!thread_is_running(thread) && !it->ooi_used_outside)
2172                 RETURN(1);
2173
2174         if (ooc->ooc_cached_items > 0) {
2175                 ooc->ooc_cached_items--;
2176                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
2177                                         ~OSD_OTABLE_IT_CACHE_MASK;
2178                 RETURN(0);
2179         }
2180
2181         if (it->ooi_all_cached) {
2182                 l_wait_event(thread->t_ctl_waitq,
2183                              !thread_is_running(thread),
2184                              &lwi);
2185                 RETURN(1);
2186         }
2187
2188         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
2189                 spin_lock(&scrub->os_lock);
2190                 scrub->os_waiting = 0;
2191                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2192                 spin_unlock(&scrub->os_lock);
2193         }
2194
2195         if (it->ooi_cache.ooc_pos_preload >= scrub->os_pos_current)
2196                 l_wait_event(thread->t_ctl_waitq,
2197                              osd_otable_it_wakeup(scrub, it),
2198                              &lwi);
2199
2200         if (!thread_is_running(thread) && !it->ooi_used_outside)
2201                 RETURN(1);
2202
2203         rc = osd_otable_it_preload(env, it);
2204         if (rc >= 0)
2205                 goto again;
2206
2207         RETURN(rc);
2208 }
2209
2210 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
2211                                         const struct dt_it *di)
2212 {
2213         return NULL;
2214 }
2215
2216 static int osd_otable_it_key_size(const struct lu_env *env,
2217                                   const struct dt_it *di)
2218 {
2219         return sizeof(__u64);
2220 }
2221
2222 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
2223                              struct dt_rec *rec, __u32 attr)
2224 {
2225         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2226         struct osd_otable_cache *ooc = &it->ooi_cache;
2227
2228         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
2229
2230         /* Filter out Invald FID already. */
2231         LASSERTF(fid_is_sane((struct lu_fid *)rec),
2232                  "Invalid FID "DFID", p_idx = %d, c_idx = %d\n",
2233                  PFID((struct lu_fid *)rec),
2234                  ooc->ooc_producer_idx, ooc->ooc_consumer_idx);
2235
2236         return 0;
2237 }
2238
2239 static __u64 osd_otable_it_store(const struct lu_env *env,
2240                                  const struct dt_it *di)
2241 {
2242         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
2243         struct osd_otable_cache *ooc = &it->ooi_cache;
2244         __u64                    hash;
2245
2246         if (it->ooi_user_ready && ooc->ooc_consumer_idx != -1)
2247                 hash = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino;
2248         else
2249                 hash = ooc->ooc_pos_preload;
2250         return hash;
2251 }
2252
2253 /**
2254  * Set the OSD layer iteration start position as the specified hash.
2255  */
2256 static int osd_otable_it_load(const struct lu_env *env,
2257                               const struct dt_it *di, __u64 hash)
2258 {
2259         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
2260         struct osd_device       *dev   = it->ooi_dev;
2261         struct osd_otable_cache *ooc   = &it->ooi_cache;
2262         struct osd_scrub        *scrub = &dev->od_scrub;
2263         int                      rc;
2264         ENTRY;
2265
2266         /* Forbid to set iteration position after iteration started. */
2267         if (it->ooi_user_ready)
2268                 RETURN(-EPERM);
2269
2270         if (hash > OSD_OTABLE_MAX_HASH)
2271                 hash = OSD_OTABLE_MAX_HASH;
2272
2273         ooc->ooc_pos_preload = hash;
2274         if (ooc->ooc_pos_preload <= LDISKFS_FIRST_INO(osd_sb(dev)))
2275                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev)) + 1;
2276
2277         it->ooi_user_ready = 1;
2278         if (!scrub->os_full_speed)
2279                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
2280
2281         /* Unplug OSD layer iteration by the first next() call. */
2282         rc = osd_otable_it_next(env, (struct dt_it *)it);
2283
2284         RETURN(rc);
2285 }
2286
2287 static int osd_otable_it_key_rec(const struct lu_env *env,
2288                                  const struct dt_it *di, void *key_rec)
2289 {
2290         return 0;
2291 }
2292
2293 const struct dt_index_operations osd_otable_ops = {
2294         .dio_it = {
2295                 .init     = osd_otable_it_init,
2296                 .fini     = osd_otable_it_fini,
2297                 .get      = osd_otable_it_get,
2298                 .put      = osd_otable_it_put,
2299                 .next     = osd_otable_it_next,
2300                 .key      = osd_otable_it_key,
2301                 .key_size = osd_otable_it_key_size,
2302                 .rec      = osd_otable_it_rec,
2303                 .store    = osd_otable_it_store,
2304                 .load     = osd_otable_it_load,
2305                 .key_rec  = osd_otable_it_key_rec,
2306         }
2307 };
2308
2309 /* high priority inconsistent items list APIs */
2310
2311 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
2312                    int insert)
2313 {
2314         struct osd_inconsistent_item *oii;
2315         struct osd_scrub             *scrub  = &dev->od_scrub;
2316         struct ptlrpc_thread         *thread = &scrub->os_thread;
2317         int                           wakeup = 0;
2318         ENTRY;
2319
2320         OBD_ALLOC_PTR(oii);
2321         if (unlikely(oii == NULL))
2322                 RETURN(-ENOMEM);
2323
2324         CFS_INIT_LIST_HEAD(&oii->oii_list);
2325         oii->oii_cache = *oic;
2326         oii->oii_insert = insert;
2327
2328         spin_lock(&scrub->os_lock);
2329         if (unlikely(!thread_is_running(thread))) {
2330                 spin_unlock(&scrub->os_lock);
2331                 OBD_FREE_PTR(oii);
2332                 RETURN(-EAGAIN);
2333         }
2334
2335         if (cfs_list_empty(&scrub->os_inconsistent_items))
2336                 wakeup = 1;
2337         cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
2338         spin_unlock(&scrub->os_lock);
2339
2340         if (wakeup != 0)
2341                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
2342
2343         RETURN(0);
2344 }
2345
2346 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
2347                    struct osd_inode_id *id)
2348 {
2349         struct osd_scrub             *scrub = &dev->od_scrub;
2350         struct osd_inconsistent_item *oii;
2351         ENTRY;
2352
2353         spin_lock(&scrub->os_lock);
2354         cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
2355                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
2356                         *id = oii->oii_cache.oic_lid;
2357                         spin_unlock(&scrub->os_lock);
2358                         RETURN(0);
2359                 }
2360         }
2361         spin_unlock(&scrub->os_lock);
2362
2363         RETURN(-ENOENT);
2364 }
2365
2366 /* OI scrub dump */
2367
2368 static const char *scrub_status_names[] = {
2369         "init",
2370         "scanning",
2371         "completed",
2372         "failed",
2373         "stopped",
2374         "paused",
2375         "crashed",
2376         NULL
2377 };
2378
2379 static const char *scrub_flags_names[] = {
2380         "recreated",
2381         "inconsistent",
2382         "auto",
2383         "upgrade",
2384         NULL
2385 };
2386
2387 static const char *scrub_param_names[] = {
2388         "failout",
2389         NULL
2390 };
2391
2392 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
2393                            const char *prefix)
2394 {
2395         int save = *len;
2396         int flag;
2397         int rc;
2398         int i;
2399
2400         rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
2401         if (rc <= 0)
2402                 return -ENOSPC;
2403
2404         *buf += rc;
2405         *len -= rc;
2406         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
2407                 if (flag & bits) {
2408                         bits &= ~flag;
2409                         rc = snprintf(*buf, *len, "%s%c", names[i],
2410                                       bits != 0 ? ',' : '\n');
2411                         if (rc <= 0)
2412                                 return -ENOSPC;
2413
2414                         *buf += rc;
2415                         *len -= rc;
2416                 }
2417         }
2418         return save - *len;
2419 }
2420
2421 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
2422 {
2423         int rc;
2424
2425         if (time != 0)
2426                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
2427                               cfs_time_current_sec() - time);
2428         else
2429                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2430         if (rc <= 0)
2431                 return -ENOSPC;
2432
2433         *buf += rc;
2434         *len -= rc;
2435         return rc;
2436 }
2437
2438 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
2439 {
2440         int rc;
2441
2442         if (pos != 0)
2443                 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
2444         else
2445                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
2446         if (rc <= 0)
2447                 return -ENOSPC;
2448
2449         *buf += rc;
2450         *len -= rc;
2451         return rc;
2452 }
2453
2454 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
2455 {
2456         struct osd_scrub  *scrub   = &dev->od_scrub;
2457         struct scrub_file *sf      = &scrub->os_file;
2458         __u64              checked;
2459         __u64              speed;
2460         int                save    = len;
2461         int                ret     = -ENOSPC;
2462         int                rc;
2463
2464         down_read(&scrub->os_rwsem);
2465         rc = snprintf(buf, len,
2466                       "name: OI_scrub\n"
2467                       "magic: 0x%x\n"
2468                       "oi_files: %d\n"
2469                       "status: %s\n",
2470                       sf->sf_magic, (int)sf->sf_oi_count,
2471                       scrub_status_names[sf->sf_status]);
2472         if (rc <= 0)
2473                 goto out;
2474
2475         buf += rc;
2476         len -= rc;
2477         rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
2478                              "flags");
2479         if (rc < 0)
2480                 goto out;
2481
2482         rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
2483                              "param");
2484         if (rc < 0)
2485                 goto out;
2486
2487         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
2488                              "time_since_last_completed");
2489         if (rc < 0)
2490                 goto out;
2491
2492         rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
2493                              "time_since_latest_start");
2494         if (rc < 0)
2495                 goto out;
2496
2497         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
2498                              "time_since_last_checkpoint");
2499         if (rc < 0)
2500                 goto out;
2501
2502         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
2503                             "latest_start_position");
2504         if (rc < 0)
2505                 goto out;
2506
2507         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
2508                             "last_checkpoint_position");
2509         if (rc < 0)
2510                 goto out;
2511
2512         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
2513                             "first_failure_position");
2514         if (rc < 0)
2515                 goto out;
2516
2517         checked = sf->sf_items_checked + scrub->os_new_checked;
2518         rc = snprintf(buf, len,
2519                       "checked: "LPU64"\n"
2520                       "updated: "LPU64"\n"
2521                       "failed: "LPU64"\n"
2522                       "prior_updated: "LPU64"\n"
2523                       "noscrub: "LPU64"\n"
2524                       "igif: "LPU64"\n"
2525                       "success_count: %u\n",
2526                       checked, sf->sf_items_updated, sf->sf_items_failed,
2527                       sf->sf_items_updated_prior, sf->sf_items_noscrub,
2528                       sf->sf_items_igif, sf->sf_success_count);
2529         if (rc <= 0)
2530                 goto out;
2531
2532         buf += rc;
2533         len -= rc;
2534         speed = checked;
2535         if (thread_is_running(&scrub->os_thread)) {
2536                 cfs_duration_t duration = cfs_time_current() -
2537                                           scrub->os_time_last_checkpoint;
2538                 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
2539                 __u32 rtime = sf->sf_run_time +
2540                               cfs_duration_sec(duration + HALF_SEC);
2541
2542                 if (duration != 0)
2543                         do_div(new_checked, duration);
2544                 if (rtime != 0)
2545                         do_div(speed, rtime);
2546                 rc = snprintf(buf, len,
2547                               "run_time: %u seconds\n"
2548                               "average_speed: "LPU64" objects/sec\n"
2549                               "real-time_speed: "LPU64" objects/sec\n"
2550                               "current_position: %u\n"
2551                               "lf_scanned: "LPU64"\n"
2552                               "lf_reparied: "LPU64"\n"
2553                               "lf_failed: "LPU64"\n",
2554                               rtime, speed, new_checked, scrub->os_pos_current,
2555                               scrub->os_lf_scanned, scrub->os_lf_repaired,
2556                               scrub->os_lf_failed);
2557         } else {
2558                 if (sf->sf_run_time != 0)
2559                         do_div(speed, sf->sf_run_time);
2560                 rc = snprintf(buf, len,
2561                               "run_time: %u seconds\n"
2562                               "average_speed: "LPU64" objects/sec\n"
2563                               "real-time_speed: N/A\n"
2564                               "current_position: N/A\n"
2565                               "lf_scanned: "LPU64"\n"
2566                               "lf_reparied: "LPU64"\n"
2567                               "lf_failed: "LPU64"\n",
2568                               sf->sf_run_time, speed, scrub->os_lf_scanned,
2569                               scrub->os_lf_repaired, scrub->os_lf_failed);
2570         }
2571         if (rc <= 0)
2572                 goto out;
2573
2574         buf += rc;
2575         len -= rc;
2576         ret = save - len;
2577
2578 out:
2579         up_read(&scrub->os_rwsem);
2580         return ret;
2581 }