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