Whamcloud - gitweb
98827d7d4019955205cf54f637bfec7327a828b1
[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 Whamcloud, Inc.
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 static inline struct osd_device *osd_scrub2dev(struct osd_scrub *scrub)
54 {
55         return container_of0(scrub, struct osd_device, od_scrub);
56 }
57
58 static inline struct super_block *osd_scrub2sb(struct osd_scrub *scrub)
59 {
60         return osd_sb(osd_scrub2dev(scrub));
61 }
62
63 static void osd_scrub_file_to_cpu(struct scrub_file *des,
64                                   struct scrub_file *src)
65 {
66         memcpy(des->sf_uuid, src->sf_uuid, 16);
67         des->sf_flags   = le64_to_cpu(src->sf_flags);
68         des->sf_magic   = le32_to_cpu(src->sf_magic);
69         des->sf_status  = le16_to_cpu(src->sf_status);
70         des->sf_param   = le16_to_cpu(src->sf_param);
71         des->sf_time_last_complete      =
72                                 le64_to_cpu(src->sf_time_last_complete);
73         des->sf_time_latest_start       =
74                                 le64_to_cpu(src->sf_time_latest_start);
75         des->sf_time_last_checkpoint    =
76                                 le64_to_cpu(src->sf_time_last_checkpoint);
77         des->sf_pos_latest_start        =
78                                 le64_to_cpu(src->sf_pos_latest_start);
79         des->sf_pos_last_checkpoint     =
80                                 le64_to_cpu(src->sf_pos_last_checkpoint);
81         des->sf_pos_first_inconsistent  =
82                                 le64_to_cpu(src->sf_pos_first_inconsistent);
83         des->sf_items_checked           =
84                                 le64_to_cpu(src->sf_items_checked);
85         des->sf_items_updated           =
86                                 le64_to_cpu(src->sf_items_updated);
87         des->sf_items_failed            =
88                                 le64_to_cpu(src->sf_items_failed);
89         des->sf_items_updated_prior     =
90                                 le64_to_cpu(src->sf_items_updated_prior);
91         des->sf_run_time        = le32_to_cpu(src->sf_run_time);
92         des->sf_success_count   = le32_to_cpu(src->sf_success_count);
93         des->sf_oi_count        = le16_to_cpu(src->sf_oi_count);
94         memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
95 }
96
97 static void osd_scrub_file_to_le(struct scrub_file *des,
98                                  struct scrub_file *src)
99 {
100         memcpy(des->sf_uuid, src->sf_uuid, 16);
101         des->sf_flags   = cpu_to_le64(src->sf_flags);
102         des->sf_magic   = cpu_to_le32(src->sf_magic);
103         des->sf_status  = cpu_to_le16(src->sf_status);
104         des->sf_param   = cpu_to_le16(src->sf_param);
105         des->sf_time_last_complete      =
106                                 cpu_to_le64(src->sf_time_last_complete);
107         des->sf_time_latest_start       =
108                                 cpu_to_le64(src->sf_time_latest_start);
109         des->sf_time_last_checkpoint    =
110                                 cpu_to_le64(src->sf_time_last_checkpoint);
111         des->sf_pos_latest_start        =
112                                 cpu_to_le64(src->sf_pos_latest_start);
113         des->sf_pos_last_checkpoint     =
114                                 cpu_to_le64(src->sf_pos_last_checkpoint);
115         des->sf_pos_first_inconsistent  =
116                                 cpu_to_le64(src->sf_pos_first_inconsistent);
117         des->sf_items_checked           =
118                                 cpu_to_le64(src->sf_items_checked);
119         des->sf_items_updated           =
120                                 cpu_to_le64(src->sf_items_updated);
121         des->sf_items_failed            =
122                                 cpu_to_le64(src->sf_items_failed);
123         des->sf_items_updated_prior     =
124                                 cpu_to_le64(src->sf_items_updated_prior);
125         des->sf_run_time        = cpu_to_le32(src->sf_run_time);
126         des->sf_success_count   = cpu_to_le32(src->sf_success_count);
127         des->sf_oi_count        = cpu_to_le16(src->sf_oi_count);
128         memcpy(des->sf_oi_bitmap, src->sf_oi_bitmap, SCRUB_OI_BITMAP_SIZE);
129 }
130
131 static void osd_scrub_file_init(struct osd_scrub *scrub, __u8 *uuid)
132 {
133         struct scrub_file *sf = &scrub->os_file;
134
135         memset(sf, 0, sizeof(*sf));
136         memcpy(sf->sf_uuid, uuid, 16);
137         sf->sf_magic = SCRUB_MAGIC_V1;
138         sf->sf_status = SS_INIT;
139 }
140
141 void osd_scrub_file_reset(struct osd_scrub *scrub, __u8 *uuid, __u64 flags)
142 {
143         struct scrub_file *sf = &scrub->os_file;
144
145         CDEBUG(D_LFSCK, "Reset OI scrub file, flags = "LPX64"\n", flags);
146         memcpy(sf->sf_uuid, uuid, 16);
147         sf->sf_status = SS_INIT;
148         sf->sf_flags |= flags;
149         sf->sf_param = 0;
150         sf->sf_run_time = 0;
151         sf->sf_time_latest_start = 0;
152         sf->sf_time_last_checkpoint = 0;
153         sf->sf_pos_latest_start = 0;
154         sf->sf_pos_last_checkpoint = 0;
155         sf->sf_pos_first_inconsistent = 0;
156         sf->sf_items_checked = 0;
157         sf->sf_items_updated = 0;
158         sf->sf_items_failed = 0;
159         sf->sf_items_updated_prior = 0;
160         sf->sf_items_noscrub = 0;
161         sf->sf_items_igif = 0;
162 }
163
164 static int osd_scrub_file_load(struct osd_scrub *scrub)
165 {
166         loff_t  pos  = 0;
167         char   *name = LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name;
168         int     len  = sizeof(scrub->os_file_disk);
169         int     rc;
170
171         rc = osd_ldiskfs_read(scrub->os_inode, &scrub->os_file_disk, len, &pos);
172         if (rc == len) {
173                 struct scrub_file *sf = &scrub->os_file;
174
175                 osd_scrub_file_to_cpu(sf, &scrub->os_file_disk);
176                 if (sf->sf_magic != SCRUB_MAGIC_V1) {
177                         CWARN("%.16s: invalid scrub magic 0x%x != 0x%x\n,",
178                               name, sf->sf_magic, SCRUB_MAGIC_V1);
179                         /* Process it as new scrub file. */
180                         rc = -ENOENT;
181                 } else {
182                         rc = 0;
183                 }
184         } else if (rc != 0) {
185                 CERROR("%.16s: fail to load scrub file, expected = %d, "
186                        "rc = %d\n", name, len, rc);
187                 if (rc > 0)
188                         rc = -EFAULT;
189         } else {
190                 /* return -ENOENT for empty scrub file case. */
191                 rc = -ENOENT;
192         }
193
194         return rc;
195 }
196
197 int osd_scrub_file_store(struct osd_scrub *scrub)
198 {
199         struct osd_device *dev;
200         handle_t          *jh;
201         loff_t             pos     = 0;
202         int                len     = sizeof(scrub->os_file_disk);
203         int                credits;
204         int                rc;
205
206         dev = container_of0(scrub, struct osd_device, od_scrub);
207         credits = osd_dto_credits_noquota[DTO_WRITE_BASE] +
208                   osd_dto_credits_noquota[DTO_WRITE_BLOCK];
209         jh = ldiskfs_journal_start_sb(osd_sb(dev), credits);
210         if (IS_ERR(jh)) {
211                 rc = PTR_ERR(jh);
212                 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
213                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,rc);
214                 return rc;
215         }
216
217         osd_scrub_file_to_le(&scrub->os_file_disk, &scrub->os_file);
218         rc = osd_ldiskfs_write_record(scrub->os_inode, &scrub->os_file_disk,
219                                       len, 0, &pos, jh);
220         ldiskfs_journal_stop(jh);
221         if (rc != 0)
222                 CERROR("%.16s: fail to store scrub file, expected = %d, "
223                        "rc = %d\n",
224                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
225                        len, rc);
226         scrub->os_time_last_checkpoint = cfs_time_current();
227         scrub->os_time_next_checkpoint = scrub->os_time_last_checkpoint +
228                                 cfs_time_seconds(SCRUB_CHECKPOINT_INTERVAL);
229         return rc;
230 }
231
232 static int osd_scrub_prep(struct osd_device *dev)
233 {
234         struct osd_scrub     *scrub  = &dev->od_scrub;
235         struct ptlrpc_thread *thread = &scrub->os_thread;
236         struct scrub_file    *sf     = &scrub->os_file;
237         __u32                 flags  = scrub->os_start_flags;
238         int                   rc;
239         ENTRY;
240
241         cfs_down_write(&scrub->os_rwsem);
242         if (flags & SS_SET_FAILOUT)
243                 sf->sf_param |= SP_FAILOUT;
244
245         if (flags & SS_CLEAR_FAILOUT)
246                 sf->sf_param &= ~SP_FAILOUT;
247
248         if (flags & SS_RESET)
249                 osd_scrub_file_reset(scrub,
250                         LDISKFS_SB(osd_sb(dev))->s_es->s_uuid, sf->sf_flags);
251
252         if (flags & SS_AUTO) {
253                 scrub->os_full_speed = 1;
254                 sf->sf_flags |= SF_AUTO;
255         } else {
256                 scrub->os_full_speed = 0;
257         }
258
259         if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT))
260                 scrub->os_full_speed = 1;
261
262         scrub->os_in_prior = 0;
263         scrub->os_waiting = 0;
264         scrub->os_new_checked = 0;
265         if (sf->sf_pos_last_checkpoint != 0)
266                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
267         else
268                 sf->sf_pos_latest_start = LDISKFS_FIRST_INO(osd_sb(dev));
269
270         scrub->os_pos_current = sf->sf_pos_latest_start;
271         sf->sf_status = SS_SCANNING;
272         sf->sf_time_latest_start = cfs_time_current_sec();
273         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
274         rc = osd_scrub_file_store(scrub);
275         if (rc == 0) {
276                 cfs_spin_lock(&scrub->os_lock);
277                 thread_set_flags(thread, SVC_RUNNING);
278                 cfs_spin_unlock(&scrub->os_lock);
279                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
280         }
281         cfs_up_write(&scrub->os_rwsem);
282
283         RETURN(rc);
284 }
285
286 static int
287 osd_scrub_error(struct osd_device *dev, struct osd_inode_id *lid, int rc)
288 {
289         struct osd_scrub  *scrub = &dev->od_scrub;
290         struct scrub_file *sf    = &scrub->os_file;
291
292         cfs_down_write(&scrub->os_rwsem);
293         scrub->os_new_checked++;
294         sf->sf_items_failed++;
295         if (sf->sf_pos_first_inconsistent == 0 ||
296             sf->sf_pos_first_inconsistent > lid->oii_ino)
297                 sf->sf_pos_first_inconsistent = lid->oii_ino;
298         cfs_up_write(&scrub->os_rwsem);
299         return sf->sf_param & SP_FAILOUT ? rc : 0;
300 }
301
302 static int
303 osd_scrub_check_update(struct osd_thread_info *info, struct osd_device *dev,
304                        struct osd_idmap_cache *oic)
305 {
306         struct osd_scrub             *scrub  = &dev->od_scrub;
307         struct scrub_file            *sf     = &scrub->os_file;
308         struct osd_inode_id          *lid2   = &info->oti_id;
309         struct lu_fid                *oi_fid = &info->oti_fid;
310         struct osd_inode_id          *oi_id  = &info->oti_id2;
311         handle_t                     *jh     = NULL;
312         struct osd_inconsistent_item *oii    = NULL;
313         struct inode                 *inode  = NULL;
314         struct lu_fid                *fid    = &oic->oic_fid;
315         struct osd_inode_id          *lid    = &oic->oic_lid;
316         struct iam_container         *bag;
317         struct iam_path_descr        *ipd;
318         int                           ops    = DTO_INDEX_UPDATE;
319         int                           idx;
320         int                           rc;
321         ENTRY;
322
323         if (scrub->os_in_prior)
324                 oii = cfs_list_entry(oic, struct osd_inconsistent_item,
325                                      oii_cache);
326
327         cfs_down_write(&scrub->os_rwsem);
328         scrub->os_new_checked++;
329         if (lid->oii_ino < sf->sf_pos_latest_start && oii == NULL)
330                 GOTO(out, rc = 0);
331
332         if (oii != NULL && oii->oii_insert)
333                 goto iget;
334
335         rc = osd_oi_lookup(info, dev, fid, lid2);
336         if (rc != 0) {
337                 if (rc != -ENOENT)
338                         GOTO(out, rc);
339
340 iget:
341                 inode = osd_iget(info, dev, lid);
342                 if (IS_ERR(inode)) {
343                         rc = PTR_ERR(inode);
344                         /* Someone removed the inode. */
345                         if (rc == -ENOENT || rc == -ESTALE)
346                                 rc = 0;
347                         GOTO(out, rc);
348                 }
349
350                 /* Prevent the inode to be unlinked during OI scrub. */
351                 cfs_mutex_lock(&inode->i_mutex);
352                 if (unlikely(inode->i_nlink == 0)) {
353                         cfs_mutex_unlock(&inode->i_mutex);
354                         iput(inode);
355                         GOTO(out, rc = 0);
356                 }
357
358                 ops = DTO_INDEX_INSERT;
359                 idx = osd_oi_fid2idx(dev, fid);
360                 if (unlikely(!ldiskfs_test_bit(idx, sf->sf_oi_bitmap)))
361                         ldiskfs_set_bit(idx, sf->sf_oi_bitmap);
362                 sf->sf_flags |= SF_RECREATED;
363         } else if (osd_id_eq(lid, lid2)) {
364                         GOTO(out, rc = 0);
365         }
366
367         sf->sf_flags |= SF_INCONSISTENT;
368         fid_cpu_to_be(oi_fid, fid);
369         osd_id_pack(oi_id, &oic->oic_lid);
370         jh = ldiskfs_journal_start_sb(osd_sb(dev),
371                                 osd_dto_credits_noquota[ops]);
372         if (IS_ERR(jh)) {
373                 rc = PTR_ERR(jh);
374                 CERROR("%.16s: fail to start trans for scrub store, rc = %d\n",
375                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
376                 GOTO(out, rc);
377         }
378
379         bag = &osd_fid2oi(dev, fid)->oi_dir.od_container;
380         ipd = osd_idx_ipd_get(info->oti_env, bag);
381         if (unlikely(ipd == NULL)) {
382                 ldiskfs_journal_stop(jh);
383                 CERROR("%.16s: fail to get ipd for scrub store\n",
384                         LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name);
385                 GOTO(out, rc = -ENOMEM);
386         }
387
388         if (ops == DTO_INDEX_UPDATE)
389                 rc = iam_update(jh, bag, (const struct iam_key *)oi_fid,
390                                 (struct iam_rec *)oi_id, ipd);
391         else
392                 rc = iam_insert(jh, bag, (const struct iam_key *)oi_fid,
393                                 (struct iam_rec *)oi_id, ipd);
394         osd_ipd_put(info->oti_env, bag, ipd);
395         ldiskfs_journal_stop(jh);
396         if (rc == 0) {
397                 if (scrub->os_in_prior)
398                         sf->sf_items_updated_prior++;
399                 else
400                         sf->sf_items_updated++;
401         }
402
403         GOTO(out, rc);
404
405 out:
406         if (rc != 0) {
407                 sf->sf_items_failed++;
408                 if (sf->sf_pos_first_inconsistent == 0 ||
409                     sf->sf_pos_first_inconsistent > lid->oii_ino)
410                         sf->sf_pos_first_inconsistent = lid->oii_ino;
411         }
412
413         if (ops == DTO_INDEX_INSERT) {
414                 cfs_mutex_unlock(&inode->i_mutex);
415                 iput(inode);
416         }
417         cfs_up_write(&scrub->os_rwsem);
418
419         if (oii != NULL) {
420                 LASSERT(!cfs_list_empty(&oii->oii_list));
421
422                 cfs_spin_lock(&scrub->os_lock);
423                 cfs_list_del_init(&oii->oii_list);
424                 cfs_spin_unlock(&scrub->os_lock);
425                 OBD_FREE_PTR(oii);
426         }
427         RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
428 }
429
430 static int do_osd_scrub_checkpoint(struct osd_scrub *scrub)
431 {
432         struct scrub_file *sf = &scrub->os_file;
433         int                rc;
434         ENTRY;
435
436         cfs_down_write(&scrub->os_rwsem);
437         sf->sf_items_checked += scrub->os_new_checked;
438         scrub->os_new_checked = 0;
439         sf->sf_pos_last_checkpoint = scrub->os_pos_current;
440         sf->sf_time_last_checkpoint = cfs_time_current_sec();
441         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
442                                             scrub->os_time_last_checkpoint);
443         rc = osd_scrub_file_store(scrub);
444         cfs_up_write(&scrub->os_rwsem);
445
446         RETURN(rc);
447 }
448
449 static inline int osd_scrub_checkpoint(struct osd_scrub *scrub)
450 {
451         if (unlikely(cfs_time_beforeq(scrub->os_time_next_checkpoint,
452                                       cfs_time_current()) &&
453                      scrub->os_new_checked > 0))
454                 return do_osd_scrub_checkpoint(scrub);
455         return 0;
456 }
457
458 static void osd_scrub_post(struct osd_scrub *scrub, int result)
459 {
460         struct scrub_file *sf = &scrub->os_file;
461         ENTRY;
462
463         cfs_down_write(&scrub->os_rwsem);
464         cfs_spin_lock(&scrub->os_lock);
465         thread_set_flags(&scrub->os_thread, SVC_STOPPING);
466         cfs_spin_unlock(&scrub->os_lock);
467         if (scrub->os_new_checked > 0) {
468                 sf->sf_items_checked += scrub->os_new_checked;
469                 scrub->os_new_checked = 0;
470                 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
471         }
472         sf->sf_time_last_checkpoint = cfs_time_current_sec();
473         if (result > 0) {
474                 sf->sf_status = SS_COMPLETED;
475                 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
476                 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT | SF_AUTO);
477                 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
478                 sf->sf_success_count++;
479         } else if (result == 0) {
480                 if (scrub->os_paused)
481                         sf->sf_status = SS_PAUSED;
482                 else
483                         sf->sf_status = SS_STOPPED;
484         } else {
485                 sf->sf_status = SS_FAILED;
486         }
487         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
488                                             scrub->os_time_last_checkpoint);
489         result = osd_scrub_file_store(scrub);
490         if (result < 0)
491                 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
492                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
493                        result);
494         cfs_up_write(&scrub->os_rwsem);
495
496         EXIT;
497 }
498
499 #define SCRUB_NEXT_BREAK        1 /* exit current loop and process next group */
500 #define SCRUB_NEXT_CONTINUE     2 /* skip current object and process next bit */
501 #define SCRUB_NEXT_EXIT         3 /* exit all the loops */
502 #define SCRUB_NEXT_WAIT         4 /* wait for free cache slot */
503 #define SCRUB_NEXT_CRASH        5 /* simulate system crash during OI scrub */
504 #define SCRUB_NEXT_FATAL        6 /* simulate failure during OI scrub */
505 #define SCRUB_NEXT_NOSCRUB      7 /* new created object, no scrub on it */
506 #define SCRUB_NEXT_IGIF         8 /* IGIF object */
507
508 struct osd_iit_param {
509         struct super_block *sb;
510         struct buffer_head *bitmap;
511         ldiskfs_group_t bg;
512         __u32 gbase;
513         __u32 offset;
514 };
515
516 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
517                                    struct osd_device *dev,
518                                    struct osd_iit_param *param,
519                                    struct osd_idmap_cache **oic,
520                                    int noslot);
521
522 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
523                                    struct osd_device *dev,
524                                    struct osd_iit_param *param,
525                                    struct osd_idmap_cache *oic,
526                                    int *noslot, int rc);
527
528 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
529                                        struct osd_otable_cache *ooc)
530 {
531         return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
532 }
533
534 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
535 {
536         param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
537                         LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
538         if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
539                 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
540                 return SCRUB_NEXT_BREAK;
541         } else {
542                 *pos = param->gbase + param->offset;
543                 return 0;
544         }
545 }
546
547 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
548                         struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
549                         struct super_block *sb, struct inode **pinode)
550 {
551         struct inode *inode;
552         int           rc;
553
554         osd_id_gen(lid, pos, OSD_OII_NOGEN);
555         inode = osd_iget_fid(info, dev, lid, fid);
556         if (IS_ERR(inode)) {
557                 rc = PTR_ERR(inode);
558                 /* The inode may be removed after bitmap searching, or the
559                  * file is new created without inode initialized yet. */
560                 if (rc == -ENOENT || rc == -ESTALE)
561                         return SCRUB_NEXT_CONTINUE;
562
563                 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
564                        LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
565                 return rc;
566         }
567
568         *pinode = inode;
569         return 0;
570 }
571
572 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
573                           struct osd_iit_param *param,
574                           struct osd_idmap_cache **oic, int noslot)
575 {
576         struct osd_scrub     *scrub  = &dev->od_scrub;
577         struct ptlrpc_thread *thread = &scrub->os_thread;
578         struct lu_fid        *fid;
579         struct osd_inode_id  *lid;
580         struct inode         *inode;
581         int                   rc;
582
583         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
584                 struct l_wait_info lwi;
585
586                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
587                 l_wait_event(thread->t_ctl_waitq,
588                              !cfs_list_empty(&scrub->os_inconsistent_items) ||
589                              !thread_is_running(thread),
590                              &lwi);
591         }
592
593         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
594                 cfs_spin_lock(&scrub->os_lock);
595                 thread_set_flags(thread, SVC_STOPPING);
596                 cfs_spin_unlock(&scrub->os_lock);
597                 return SCRUB_NEXT_CRASH;
598         }
599
600         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
601                 return SCRUB_NEXT_FATAL;
602
603         if (unlikely(!thread_is_running(thread)))
604                 return SCRUB_NEXT_EXIT;
605
606         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
607                 struct osd_inconsistent_item *oii;
608
609                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
610                                      struct osd_inconsistent_item, oii_list);
611                 *oic = &oii->oii_cache;
612                 scrub->os_in_prior = 1;
613                 return 0;
614         }
615
616         if (noslot != 0)
617                 return SCRUB_NEXT_WAIT;
618
619         rc = osd_iit_next(param, &scrub->os_pos_current);
620         if (rc != 0)
621                 return rc;
622
623         *oic = &scrub->os_oic;
624         fid = &(*oic)->oic_fid;
625         lid = &(*oic)->oic_lid;
626         rc = osd_iit_iget(info, dev, fid, lid,
627                           scrub->os_pos_current, param->sb, &inode);
628         if (rc != 0)
629                 return rc;
630
631         if (inode->i_state & I_LUSTRE_NOSCRUB) {
632                 /* Only skip it for the first OI scrub accessing. */
633                 inode->i_state &= ~I_LUSTRE_NOSCRUB;
634                 rc = SCRUB_NEXT_NOSCRUB;
635         } else if (!fid_is_norm(fid)) {
636                 rc = SCRUB_NEXT_IGIF;
637         }
638
639         iput(inode);
640         return rc;
641 }
642
643 static int osd_preload_next(struct osd_thread_info *info,
644                             struct osd_device *dev, struct osd_iit_param *param,
645                             struct osd_idmap_cache **oic, int noslot)
646 {
647         struct osd_otable_cache *ooc    = &dev->od_otable_it->ooi_cache;
648         struct osd_scrub        *scrub;
649         struct ptlrpc_thread    *thread;
650         struct inode            *inode;
651         int                      rc;
652
653         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
654         if (rc != 0)
655                 return rc;
656
657         scrub = &dev->od_scrub;
658         thread = &scrub->os_thread;
659         if (thread_is_running(thread) &&
660             ooc->ooc_pos_preload >= scrub->os_pos_current)
661                 return SCRUB_NEXT_EXIT;
662
663         rc = osd_iit_iget(info, dev,
664                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
665                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
666                           ooc->ooc_pos_preload, param->sb, &inode);
667         /* If succeed, it needs to move forward; otherwise up layer LFSCK may
668          * ignore the failure, so it still need to skip the inode next time. */
669         ooc->ooc_pos_preload = param->gbase + ++(param->offset);
670         if (rc == 0)
671                 iput(inode);
672         return rc;
673 }
674
675 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
676                           struct osd_iit_param *param,
677                           struct osd_idmap_cache *oic, int *noslot, int rc)
678 {
679         struct l_wait_info       lwi    = { 0 };
680         struct osd_scrub        *scrub  = &dev->od_scrub;
681         struct scrub_file       *sf     = &scrub->os_file;
682         __u64                   *items  = NULL;
683         struct ptlrpc_thread    *thread = &scrub->os_thread;
684         struct osd_otable_it    *it     = dev->od_otable_it;
685         struct osd_otable_cache *ooc    = it ? &it->ooi_cache : NULL;
686
687         switch (rc) {
688         case SCRUB_NEXT_CONTINUE:
689                 goto next;
690         case SCRUB_NEXT_WAIT:
691                 goto wait;
692         case SCRUB_NEXT_NOSCRUB:
693                 items = &sf->sf_items_noscrub;
694                 break;
695         case SCRUB_NEXT_IGIF:
696                 items = &sf->sf_items_igif;
697                 break;
698         }
699
700         if (items != NULL) {
701                 cfs_down_write(&scrub->os_rwsem);
702                 scrub->os_new_checked++;
703                 (*items)++;
704                 cfs_up_write(&scrub->os_rwsem);
705                 goto next;
706         }
707
708         LASSERTF(rc <= 0, "unexpected rc = %d\n", rc);
709
710         if (rc != 0)
711                 rc = osd_scrub_error(dev, &oic->oic_lid, rc);
712         else
713                 rc = osd_scrub_check_update(info, dev, oic);
714         if (rc != 0)
715                 return rc;
716
717         rc = osd_scrub_checkpoint(scrub);
718         if (rc != 0) {
719                 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
720                        LDISKFS_SB(param->sb)->s_es->s_volume_name,
721                        scrub->os_pos_current, rc);
722                 /* Continue, as long as the scrub itself can go ahead. */
723         }
724
725         if (scrub->os_in_prior) {
726                 scrub->os_in_prior = 0;
727                 return 0;
728         }
729
730 next:
731         scrub->os_pos_current = param->gbase + ++(param->offset);
732         if (it != NULL && it->ooi_waiting &&
733             ooc->ooc_pos_preload < scrub->os_pos_current) {
734                 it->ooi_waiting = 0;
735                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
736         }
737
738         if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
739                 return 0;
740
741 wait:
742         if (osd_scrub_has_window(scrub, ooc)) {
743                 *noslot = 0;
744                 return 0;
745         }
746
747         scrub->os_waiting = 1;
748         l_wait_event(thread->t_ctl_waitq,
749                      osd_scrub_has_window(scrub, ooc) ||
750                      !cfs_list_empty(&scrub->os_inconsistent_items) ||
751                      !thread_is_running(thread),
752                      &lwi);
753         scrub->os_waiting = 0;
754
755         if (osd_scrub_has_window(scrub, ooc))
756                 *noslot = 0;
757         else
758                 *noslot = 1;
759         return 0;
760 }
761
762 static int osd_preload_exec(struct osd_thread_info *info,
763                             struct osd_device *dev, struct osd_iit_param *param,
764                             struct osd_idmap_cache *oic, int *noslot, int rc)
765 {
766         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
767
768         if (rc == 0) {
769                 ooc->ooc_cached_items++;
770                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
771                                         ~OSD_OTABLE_IT_CACHE_MASK;
772         }
773         return rc > 0 ? 0 : rc;
774 }
775
776 #define SCRUB_IT_ALL    1
777 #define SCRUB_IT_CRASH  2
778
779 static int osd_inode_iteration(struct osd_thread_info *info,
780                                struct osd_device *dev, __u32 max, int preload)
781 {
782         osd_iit_next_policy   next;
783         osd_iit_exec_policy   exec;
784         __u32                *pos;
785         __u32                *count;
786         struct osd_iit_param  param;
787         __u32                 limit;
788         int                   noslot = 0;
789         int                   rc;
790         ENTRY;
791
792         if (preload == 0) {
793                 struct osd_scrub *scrub = &dev->od_scrub;
794
795                 next = osd_scrub_next;
796                 exec = osd_scrub_exec;
797                 pos = &scrub->os_pos_current;
798                 count = &scrub->os_new_checked;
799         } else {
800                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
801
802                 next = osd_preload_next;
803                 exec = osd_preload_exec;
804                 pos = &ooc->ooc_pos_preload;
805                 count = &ooc->ooc_cached_items;
806         }
807         param.sb = osd_sb(dev);
808         limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
809
810         while (*pos <= limit && *count < max) {
811                 struct osd_idmap_cache *oic = NULL;
812
813                 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
814                 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
815                 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
816                 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
817                 if (param.bitmap == NULL) {
818                         CERROR("%.16s: fail to read bitmap for %u, "
819                                "scrub will stop, urgent mode\n",
820                                LDISKFS_SB(param.sb)->s_es->s_volume_name,
821                                (__u32)param.bg);
822                         RETURN(-EIO);
823                 }
824
825                 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
826                        *count < max) {
827                         rc = next(info, dev, &param, &oic, noslot);
828                         switch (rc) {
829                         case SCRUB_NEXT_BREAK:
830                                 goto next_group;
831                         case SCRUB_NEXT_EXIT:
832                                 brelse(param.bitmap);
833                                 RETURN(0);
834                         case SCRUB_NEXT_CRASH:
835                                 brelse(param.bitmap);
836                                 RETURN(SCRUB_IT_CRASH);
837                         case SCRUB_NEXT_FATAL:
838                                 brelse(param.bitmap);
839                                 RETURN(-EINVAL);
840                         }
841
842                         rc = exec(info, dev, &param, oic, &noslot, rc);
843                         if (rc != 0) {
844                                 brelse(param.bitmap);
845                                 RETURN(rc);
846                         }
847                 }
848
849 next_group:
850                 brelse(param.bitmap);
851         }
852
853         if (*pos > limit)
854                 RETURN(SCRUB_IT_ALL);
855         RETURN(0);
856 }
857
858 static int osd_scrub_main(void *args)
859 {
860         struct lu_env         env;
861         struct osd_device    *dev    = (struct osd_device *)args;
862         struct osd_scrub     *scrub  = &dev->od_scrub;
863         struct ptlrpc_thread *thread = &scrub->os_thread;
864         struct super_block   *sb     = osd_sb(dev);
865         int                   rc;
866         ENTRY;
867
868         cfs_daemonize("OI_scrub");
869         rc = lu_env_init(&env, LCT_DT_THREAD);
870         if (rc != 0) {
871                 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
872                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
873                 GOTO(noenv, rc);
874         }
875
876         rc = osd_scrub_prep(dev);
877         if (rc != 0) {
878                 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
879                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
880                 GOTO(out, rc);
881         }
882
883         if (!scrub->os_full_speed) {
884                 struct l_wait_info lwi = { 0 };
885                 struct osd_otable_it *it = dev->od_otable_it;
886                 struct osd_otable_cache *ooc = &it->ooi_cache;
887
888                 l_wait_event(thread->t_ctl_waitq,
889                              it->ooi_user_ready || !thread_is_running(thread),
890                              &lwi);
891                 if (unlikely(!thread_is_running(thread)))
892                         GOTO(post, rc = 0);
893
894                 LASSERT(scrub->os_pos_current >= ooc->ooc_pos_preload);
895                 scrub->os_pos_current = ooc->ooc_pos_preload;
896         }
897
898         CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
899                scrub->os_start_flags, scrub->os_pos_current);
900
901         rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, 0);
902         if (unlikely(rc == SCRUB_IT_CRASH))
903                 GOTO(out, rc = -EINVAL);
904         GOTO(post, rc);
905
906 post:
907         osd_scrub_post(scrub, rc);
908         CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
909                rc, scrub->os_pos_current);
910
911 out:
912         while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
913                 struct osd_inconsistent_item *oii;
914
915                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
916                                      struct osd_inconsistent_item, oii_list);
917                 cfs_list_del_init(&oii->oii_list);
918                 OBD_FREE_PTR(oii);
919         }
920         lu_env_fini(&env);
921
922 noenv:
923         cfs_spin_lock(&scrub->os_lock);
924         thread_set_flags(thread, SVC_STOPPED);
925         cfs_waitq_broadcast(&thread->t_ctl_waitq);
926         cfs_spin_unlock(&scrub->os_lock);
927         return rc;
928 }
929
930 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
931 {
932         struct osd_scrub     *scrub  = &dev->od_scrub;
933         struct ptlrpc_thread *thread = &scrub->os_thread;
934         struct l_wait_info    lwi    = { 0 };
935         int                   rc;
936         ENTRY;
937
938 again:
939         /* os_lock: sync status between stop and scrub thread */
940         cfs_spin_lock(&scrub->os_lock);
941         if (thread_is_running(thread)) {
942                 cfs_spin_unlock(&scrub->os_lock);
943                 RETURN(-EALREADY);
944         } else if (unlikely(thread_is_stopping(thread))) {
945                 cfs_spin_unlock(&scrub->os_lock);
946                 l_wait_event(thread->t_ctl_waitq,
947                              thread_is_stopped(thread),
948                              &lwi);
949                 goto again;
950         }
951         cfs_spin_unlock(&scrub->os_lock);
952
953         scrub->os_start_flags = flags;
954         thread_set_flags(thread, 0);
955         rc = cfs_create_thread(osd_scrub_main, dev, 0);
956         if (rc < 0) {
957                 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
958                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
959                 RETURN(rc);
960         }
961
962         l_wait_event(thread->t_ctl_waitq,
963                      thread_is_running(thread) || thread_is_stopped(thread),
964                      &lwi);
965
966         RETURN(0);
967 }
968
969 int osd_scrub_start(struct osd_device *dev)
970 {
971         __u32 flags = SS_AUTO;
972         int   rc;
973         ENTRY;
974
975         if (dev->od_scrub.os_file.sf_status == SS_COMPLETED)
976                 flags |= SS_RESET;
977
978         /* od_otable_mutex: prevent curcurrent start/stop */
979         cfs_mutex_lock(&dev->od_otable_mutex);
980         rc = do_osd_scrub_start(dev, flags);
981         cfs_mutex_unlock(&dev->od_otable_mutex);
982
983         RETURN(rc == -EALREADY ? 0 : rc);
984 }
985
986 static void do_osd_scrub_stop(struct osd_scrub *scrub)
987 {
988         struct ptlrpc_thread *thread = &scrub->os_thread;
989         struct l_wait_info    lwi    = { 0 };
990
991         /* os_lock: sync status between stop and scrub thread */
992         cfs_spin_lock(&scrub->os_lock);
993         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
994                 thread_set_flags(thread, SVC_STOPPING);
995                 cfs_spin_unlock(&scrub->os_lock);
996                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
997                 l_wait_event(thread->t_ctl_waitq,
998                              thread_is_stopped(thread),
999                              &lwi);
1000                 /* Do not skip the last lock/unlock, which can guarantee that
1001                  * the caller cannot return until the OI scrub thread exit. */
1002                 cfs_spin_lock(&scrub->os_lock);
1003         }
1004         cfs_spin_unlock(&scrub->os_lock);
1005 }
1006
1007 static void osd_scrub_stop(struct osd_device *dev)
1008 {
1009         /* od_otable_mutex: prevent curcurrent start/stop */
1010         cfs_mutex_lock(&dev->od_otable_mutex);
1011         dev->od_scrub.os_paused = 1;
1012         do_osd_scrub_stop(&dev->od_scrub);
1013         cfs_mutex_unlock(&dev->od_otable_mutex);
1014 }
1015
1016 static const char osd_scrub_name[] = "OI_scrub";
1017
1018 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1019 {
1020         struct osd_thread_info     *info   = osd_oti_get(env);
1021         struct osd_scrub           *scrub  = &dev->od_scrub;
1022         struct lvfs_run_ctxt       *ctxt   = &scrub->os_ctxt;
1023         struct scrub_file          *sf     = &scrub->os_file;
1024         struct osd_inode_id        *id     = &scrub->os_oic.oic_lid;
1025         struct super_block         *sb     = osd_sb(dev);
1026         struct ldiskfs_super_block *es     = LDISKFS_SB(sb)->s_es;
1027         struct inode               *inode;
1028         struct lvfs_run_ctxt        saved;
1029         struct file                *filp;
1030         int                         dirty  = 0;
1031         int                         init   = 0;
1032         int                         rc     = 0;
1033         ENTRY;
1034
1035         memset(scrub, 0, sizeof(*scrub));
1036         OBD_SET_CTXT_MAGIC(ctxt);
1037         ctxt->pwdmnt = dev->od_mnt;
1038         ctxt->pwd = dev->od_mnt->mnt_root;
1039         ctxt->fs = get_ds();
1040
1041         cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1042         cfs_init_rwsem(&scrub->os_rwsem);
1043         cfs_spin_lock_init(&scrub->os_lock);
1044         CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1045
1046         push_ctxt(&saved, ctxt, NULL);
1047         filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1048         if (IS_ERR(filp))
1049                 RETURN(PTR_ERR(filp));
1050
1051         scrub->os_inode = igrab(filp->f_dentry->d_inode);
1052         filp_close(filp, 0);
1053         pop_ctxt(&saved, ctxt, NULL);
1054
1055         rc = osd_scrub_file_load(scrub);
1056         if (rc == -ENOENT) {
1057                 osd_scrub_file_init(scrub, es->s_uuid);
1058                 dirty = 1;
1059                 init = 1;
1060         } else if (rc != 0) {
1061                 RETURN(rc);
1062         } else {
1063                 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1064                         osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1065                         dirty = 1;
1066                 } else if (sf->sf_status == SS_SCANNING) {
1067                         sf->sf_status = SS_CRASHED;
1068                         dirty = 1;
1069                 }
1070         }
1071
1072         if (sf->sf_pos_last_checkpoint != 0)
1073                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1074         else
1075                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb);
1076
1077         if (dirty != 0) {
1078                 rc = osd_scrub_file_store(scrub);
1079                 if (rc != 0)
1080                         RETURN(rc);
1081         }
1082
1083         /* Initialize OI files. */
1084         rc = osd_oi_init(info, dev);
1085         if (rc < 0)
1086                 RETURN(rc);
1087
1088         if (init != 0) {
1089                 rc = __osd_oi_lookup(info, dev, &LU_DOT_LUSTRE_FID, id);
1090                 if (rc == 0) {
1091                         inode = osd_iget(info, dev, id);
1092                         if (IS_ERR(inode)) {
1093                                 rc = PTR_ERR(inode);
1094                                 /* It is restored from old 2.x backup. */
1095                                 if (rc == -ENOENT || rc == -ESTALE) {
1096                                         osd_scrub_file_reset(scrub, es->s_uuid,
1097                                                              SF_INCONSISTENT);
1098                                         rc = osd_scrub_file_store(scrub);
1099                                 }
1100                         } else {
1101                                 iput(inode);
1102                         }
1103                 } else if (rc == -ENOENT) {
1104                         rc = 0;
1105                 }
1106         }
1107
1108         if (rc == 0 && !scrub->os_no_scrub &&
1109             ((sf->sf_status == SS_PAUSED) ||
1110              (sf->sf_status == SS_CRASHED &&
1111               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_AUTO)) ||
1112              (sf->sf_status == SS_INIT &&
1113               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT))))
1114                 rc = osd_scrub_start(dev);
1115
1116         RETURN(rc);
1117 }
1118
1119 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1120 {
1121         struct osd_scrub *scrub = &dev->od_scrub;
1122
1123         LASSERT(dev->od_otable_it == NULL);
1124
1125         if (scrub->os_inode != NULL) {
1126                 osd_scrub_stop(dev);
1127                 iput(scrub->os_inode);
1128                 scrub->os_inode = NULL;
1129         }
1130         if (dev->od_oi_table != NULL)
1131                 osd_oi_fini(osd_oti_get(env), dev);
1132 }
1133
1134 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1135                                        struct dt_object *dt, __u32 attr,
1136                                        struct lustre_capa *capa)
1137 {
1138         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1139         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1140         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
1141         struct osd_scrub       *scrub = &dev->od_scrub;
1142         struct osd_otable_it   *it;
1143         __u32                   start = 0;
1144         int                     rc;
1145         ENTRY;
1146
1147         /* od_otable_mutex: prevent curcurrent init/fini */
1148         cfs_mutex_lock(&dev->od_otable_mutex);
1149         if (dev->od_otable_it != NULL)
1150                 GOTO(out, it = ERR_PTR(-EALREADY));
1151
1152         OBD_ALLOC_PTR(it);
1153         if (it == NULL)
1154                 GOTO(out, it = ERR_PTR(-ENOMEM));
1155
1156         dev->od_otable_it = it;
1157         it->ooi_dev = dev;
1158         it->ooi_cache.ooc_consumer_idx = -1;
1159         if (flags & DOIF_OUTUSED)
1160                 it->ooi_used_outside = 1;
1161
1162         if (flags & DOIF_RESET)
1163                 start |= SS_RESET;
1164
1165         if (valid & DOIV_ERROR_HANDLE) {
1166                 if (flags & DOIF_FAILOUT)
1167                         start |= SS_SET_FAILOUT;
1168                 else
1169                         start |= SS_CLEAR_FAILOUT;
1170         }
1171
1172         rc = do_osd_scrub_start(dev, start);
1173         if (rc == -EALREADY) {
1174                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current - 1;
1175         } else if (rc < 0) {
1176                 dev->od_otable_it = NULL;
1177                 OBD_FREE_PTR(it);
1178                 GOTO(out, it = ERR_PTR(-EALREADY));
1179         } else {
1180                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
1181         }
1182
1183         GOTO(out, it);
1184
1185 out:
1186         cfs_mutex_unlock(&dev->od_otable_mutex);
1187         return (struct dt_it *)it;
1188 }
1189
1190 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1191 {
1192         struct osd_otable_it *it  = (struct osd_otable_it *)di;
1193         struct osd_device    *dev = it->ooi_dev;
1194
1195         /* od_otable_mutex: prevent curcurrent init/fini */
1196         cfs_mutex_lock(&dev->od_otable_mutex);
1197         do_osd_scrub_stop(&dev->od_scrub);
1198         LASSERT(dev->od_otable_it == it);
1199
1200         dev->od_otable_it = NULL;
1201         cfs_mutex_unlock(&dev->od_otable_mutex);
1202         OBD_FREE_PTR(it);
1203 }
1204
1205 /**
1206  * XXX: Temporary used to notify otable iteration to be paused.
1207  */
1208 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1209 {
1210         struct osd_device *dev = ((struct osd_otable_it *)di)->ooi_dev;
1211
1212         /* od_otable_mutex: prevent curcurrent init/fini */
1213         cfs_mutex_lock(&dev->od_otable_mutex);
1214         dev->od_scrub.os_paused = 1;
1215         cfs_mutex_unlock(&dev->od_otable_mutex);
1216 }
1217
1218 /**
1219  * Set the OSD layer iteration start position as the specified key.
1220  *
1221  * The LFSCK out of OSD layer does not know the detail of the key, so if there
1222  * are several keys, they cannot be compared out of OSD, so call "::get()" for
1223  * each key, and OSD will select the smallest one by itself.
1224  */
1225 static int osd_otable_it_get(const struct lu_env *env,
1226                              struct dt_it *di, const struct dt_key *key)
1227 {
1228         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1229         struct osd_otable_cache *ooc = &it->ooi_cache;
1230         const char              *str = (const char *)key;
1231         __u32                    ino;
1232         ENTRY;
1233
1234         /* Forbid to set iteration position after iteration started. */
1235         if (it->ooi_user_ready)
1236                 RETURN(-EPERM);
1237
1238         if (str[0] == '\0')
1239                 RETURN(-EINVAL);
1240
1241         if (sscanf(str, "%u", &ino) <= 0)
1242                 RETURN(-EINVAL);
1243
1244         /* Skip the one that has been processed last time. */
1245         if (ooc->ooc_pos_preload > ++ino)
1246                 ooc->ooc_pos_preload = ino;
1247
1248         RETURN(0);
1249 }
1250
1251 static int osd_otable_it_preload(const struct lu_env *env,
1252                                  struct osd_otable_it *it)
1253 {
1254         struct osd_device       *dev   = it->ooi_dev;
1255         struct osd_scrub        *scrub = &dev->od_scrub;
1256         struct osd_otable_cache *ooc   = &it->ooi_cache;
1257         int                      rc;
1258         ENTRY;
1259
1260         rc = osd_inode_iteration(osd_oti_get(env), dev,
1261                                  OSD_OTABLE_IT_CACHE_SIZE, 1);
1262         if (rc == SCRUB_IT_ALL)
1263                 it->ooi_all_cached = 1;
1264
1265         CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1266                le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1267                ooc->ooc_pos_preload, rc);
1268
1269         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1270                 scrub->os_waiting = 0;
1271                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1272         }
1273
1274         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1275 }
1276
1277 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1278 {
1279         struct osd_otable_it    *it     = (struct osd_otable_it *)di;
1280         struct osd_device       *dev    = it->ooi_dev;
1281         struct osd_scrub        *scrub  = &dev->od_scrub;
1282         struct osd_otable_cache *ooc    = &it->ooi_cache;
1283         struct ptlrpc_thread    *thread = &scrub->os_thread;
1284         struct l_wait_info       lwi    = { 0 };
1285         int                      rc;
1286         ENTRY;
1287
1288         LASSERT(it->ooi_user_ready);
1289
1290 again:
1291         if (!thread_is_running(thread) && !it->ooi_used_outside)
1292                 RETURN(1);
1293
1294         if (ooc->ooc_cached_items > 0) {
1295                 ooc->ooc_cached_items--;
1296                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
1297                                         ~OSD_OTABLE_IT_CACHE_MASK;
1298                 RETURN(0);
1299         }
1300
1301         if (it->ooi_all_cached) {
1302                 l_wait_event(thread->t_ctl_waitq,
1303                              !thread_is_running(thread),
1304                              &lwi);
1305                 RETURN(1);
1306         }
1307
1308         it->ooi_waiting = 1;
1309         l_wait_event(thread->t_ctl_waitq,
1310                      ooc->ooc_pos_preload < scrub->os_pos_current ||
1311                      !thread_is_running(thread),
1312                      &lwi);
1313         it->ooi_waiting = 0;
1314
1315         if (!thread_is_running(thread) && !it->ooi_used_outside)
1316                 RETURN(1);
1317
1318         rc = osd_otable_it_preload(env, it);
1319         if (rc >= 0)
1320                 goto again;
1321
1322         RETURN(rc);
1323 }
1324
1325 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1326                                         const struct dt_it *di)
1327 {
1328         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1329         struct osd_otable_cache *ooc = &it->ooi_cache;
1330
1331         sprintf(it->ooi_key, "%u",
1332                 ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino);
1333         return (struct dt_key *)it->ooi_key;
1334 }
1335
1336 static int osd_otable_it_key_size(const struct lu_env *env,
1337                                   const struct dt_it *di)
1338 {
1339         return sizeof(((struct osd_otable_it *)di)->ooi_key);
1340 }
1341
1342 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1343                              struct dt_rec *rec, __u32 attr)
1344 {
1345         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1346         struct osd_otable_cache *ooc = &it->ooi_cache;
1347
1348         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
1349         return 0;
1350 }
1351
1352 static int osd_otable_it_load(const struct lu_env *env,
1353                               const struct dt_it *di, __u64 hash)
1354 {
1355         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
1356         struct osd_device       *dev   = it->ooi_dev;
1357         struct osd_otable_cache *ooc   = &it->ooi_cache;
1358         struct osd_scrub        *scrub = &dev->od_scrub;
1359
1360         if (it->ooi_user_ready)
1361                 return 0;
1362
1363         if (ooc->ooc_pos_preload < LDISKFS_FIRST_INO(osd_sb(dev)))
1364                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev));
1365         it->ooi_user_ready = 1;
1366         if (!scrub->os_full_speed)
1367                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1368
1369         /* Unplug OSD layer iteration by the first next() call. */
1370         return osd_otable_it_next(env, (struct dt_it *)it);
1371 }
1372
1373 const struct dt_index_operations osd_otable_ops = {
1374         .dio_it = {
1375                 .init     = osd_otable_it_init,
1376                 .fini     = osd_otable_it_fini,
1377                 .put      = osd_otable_it_put,
1378                 .get      = osd_otable_it_get,
1379                 .next     = osd_otable_it_next,
1380                 .key      = osd_otable_it_key,
1381                 .key_size = osd_otable_it_key_size,
1382                 .rec      = osd_otable_it_rec,
1383                 .load     = osd_otable_it_load,
1384         }
1385 };
1386
1387 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
1388                    int insert)
1389 {
1390         struct osd_inconsistent_item *oii;
1391         struct osd_scrub             *scrub  = &dev->od_scrub;
1392         struct ptlrpc_thread         *thread = &scrub->os_thread;
1393         int                           wakeup = 0;
1394         ENTRY;
1395
1396         OBD_ALLOC_PTR(oii);
1397         if (unlikely(oii == NULL))
1398                 RETURN(-ENOMEM);
1399
1400         CFS_INIT_LIST_HEAD(&oii->oii_list);
1401         oii->oii_cache = *oic;
1402         oii->oii_insert = insert;
1403
1404         cfs_spin_lock(&scrub->os_lock);
1405         if (unlikely(!thread_is_running(thread))) {
1406                 cfs_spin_unlock(&scrub->os_lock);
1407                 OBD_FREE_PTR(oii);
1408                 RETURN(-EAGAIN);
1409         }
1410
1411         if (cfs_list_empty(&scrub->os_inconsistent_items))
1412                 wakeup = 1;
1413         cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1414         cfs_spin_unlock(&scrub->os_lock);
1415
1416         if (wakeup != 0)
1417                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1418
1419         RETURN(0);
1420 }
1421
1422 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1423                    struct osd_inode_id *id)
1424 {
1425         struct osd_scrub             *scrub = &dev->od_scrub;
1426         struct osd_inconsistent_item *oii;
1427         ENTRY;
1428
1429         cfs_spin_lock(&scrub->os_lock);
1430         cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1431                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1432                         *id = oii->oii_cache.oic_lid;
1433                         cfs_spin_unlock(&scrub->os_lock);
1434                         RETURN(0);
1435                 }
1436         }
1437         cfs_spin_unlock(&scrub->os_lock);
1438
1439         RETURN(-ENOENT);
1440 }
1441
1442 static const char *scrub_status_names[] = {
1443         "init",
1444         "scanning",
1445         "completed",
1446         "failed",
1447         "stopped",
1448         "paused",
1449         "crashed",
1450         NULL
1451 };
1452
1453 static const char *scrub_flags_names[] = {
1454         "recreated",
1455         "inconsistent",
1456         "auto",
1457         NULL
1458 };
1459
1460 static const char *scrub_param_names[] = {
1461         "failout",
1462         NULL
1463 };
1464
1465 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
1466                            const char *prefix)
1467 {
1468         int save = *len;
1469         int flag;
1470         int rc;
1471         int i;
1472
1473         rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1474         if (rc <= 0)
1475                 return -ENOSPC;
1476
1477         *buf += rc;
1478         *len -= rc;
1479         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1480                 if (flag & bits) {
1481                         bits &= ~flag;
1482                         rc = snprintf(*buf, *len, "%s%c", names[i],
1483                                       bits != 0 ? ',' : '\n');
1484                         if (rc <= 0)
1485                                 return -ENOSPC;
1486
1487                         *buf += rc;
1488                         *len -= rc;
1489                 }
1490         }
1491         return save - *len;
1492 }
1493
1494 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
1495 {
1496         int rc;
1497
1498         if (time != 0)
1499                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
1500                               cfs_time_current_sec() - time);
1501         else
1502                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
1503         if (rc <= 0)
1504                 return -ENOSPC;
1505
1506         *buf += rc;
1507         *len -= rc;
1508         return rc;
1509 }
1510
1511 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
1512 {
1513         int rc;
1514
1515         if (pos != 0)
1516                 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
1517         else
1518                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
1519         if (rc <= 0)
1520                 return -ENOSPC;
1521
1522         *buf += rc;
1523         *len -= rc;
1524         return rc;
1525 }
1526
1527 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
1528 {
1529         struct osd_scrub  *scrub   = &dev->od_scrub;
1530         struct scrub_file *sf      = &scrub->os_file;
1531         __u64              checked;
1532         __u64              speed;
1533         int                save    = len;
1534         int                ret     = -ENOSPC;
1535         int                rc;
1536
1537         cfs_down_read(&scrub->os_rwsem);
1538         rc = snprintf(buf, len,
1539                       "name: OI scrub\n"
1540                       "magic: 0x%x\n"
1541                       "oi_files: %d\n"
1542                       "status: %s\n",
1543                       sf->sf_magic, (int)sf->sf_oi_count,
1544                       scrub_status_names[sf->sf_status]);
1545         if (rc <= 0)
1546                 goto out;
1547
1548         buf += rc;
1549         len -= rc;
1550         rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
1551                              "flags");
1552         if (rc < 0)
1553                 goto out;
1554
1555         rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
1556                              "param");
1557         if (rc < 0)
1558                 goto out;
1559
1560         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
1561                              "time_since_last_completed");
1562         if (rc < 0)
1563                 goto out;
1564
1565         rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
1566                              "time_since_latest_start");
1567         if (rc < 0)
1568                 goto out;
1569
1570         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
1571                              "time_since_last_checkpoint");
1572         if (rc < 0)
1573                 goto out;
1574
1575         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
1576                             "latest_start_position");
1577         if (rc < 0)
1578                 goto out;
1579
1580         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
1581                             "last_checkpoint_position");
1582         if (rc < 0)
1583                 goto out;
1584
1585         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
1586                             "first_failure_position");
1587         if (rc < 0)
1588                 goto out;
1589
1590         checked = sf->sf_items_checked + scrub->os_new_checked;
1591         rc = snprintf(buf, len,
1592                       "checked: "LPU64"\n"
1593                       "updated: "LPU64"\n"
1594                       "failed: "LPU64"\n"
1595                       "prior_updated: "LPU64"\n"
1596                       "noscrub: "LPU64"\n"
1597                       "igif: "LPU64"\n"
1598                       "success_count: %u\n",
1599                       checked, sf->sf_items_updated, sf->sf_items_failed,
1600                       sf->sf_items_updated_prior, sf->sf_items_noscrub,
1601                       sf->sf_items_igif, sf->sf_success_count);
1602         if (rc <= 0)
1603                 goto out;
1604
1605         buf += rc;
1606         len -= rc;
1607         speed = checked;
1608         if (thread_is_running(&scrub->os_thread)) {
1609                 cfs_duration_t duration = cfs_time_current() -
1610                                           scrub->os_time_last_checkpoint;
1611                 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
1612                 __u32 rtime = sf->sf_run_time +
1613                               cfs_duration_sec(duration + HALF_SEC);
1614
1615                 if (duration != 0)
1616                         do_div(new_checked, duration);
1617                 if (rtime != 0)
1618                         do_div(speed, rtime);
1619                 rc = snprintf(buf, len,
1620                               "run_time: %u seconds\n"
1621                               "average_speed: "LPU64" objects/sec\n"
1622                               "real-time_speed: "LPU64" objects/sec\n"
1623                               "current_position: %u\n",
1624                               rtime, speed, new_checked, scrub->os_pos_current);
1625         } else {
1626                 if (sf->sf_run_time != 0)
1627                         do_div(speed, sf->sf_run_time);
1628                 rc = snprintf(buf, len,
1629                               "run_time: %u seconds\n"
1630                               "average_speed: "LPU64" objects/sec\n"
1631                               "real-time_speed: N/A\n"
1632                               "current_position: N/A\n",
1633                               sf->sf_run_time, speed);
1634         }
1635         if (rc <= 0)
1636                 goto out;
1637
1638         buf += rc;
1639         len -= rc;
1640         ret = save - len;
1641
1642 out:
1643         cfs_up_read(&scrub->os_rwsem);
1644         return ret;
1645 }