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