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