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