Whamcloud - gitweb
LU-1540 osd: add NUL terminator for long symlink
[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, 0, &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                 if (scrub->os_paused)
479                         sf->sf_status = SS_PAUSED;
480                 else
481                         sf->sf_status = SS_STOPPED;
482         } else {
483                 sf->sf_status = SS_FAILED;
484         }
485         sf->sf_run_time += cfs_duration_sec(cfs_time_current() + HALF_SEC -
486                                             scrub->os_time_last_checkpoint);
487         result = osd_scrub_file_store(scrub);
488         if (result < 0)
489                 CERROR("%.16s: fail to osd_scrub_post, rc = %d\n",
490                        LDISKFS_SB(osd_scrub2sb(scrub))->s_es->s_volume_name,
491                        result);
492         cfs_up_write(&scrub->os_rwsem);
493
494         EXIT;
495 }
496
497 #define SCRUB_NEXT_BREAK        1 /* exit current loop and process next group */
498 #define SCRUB_NEXT_CONTINUE     2 /* skip current object and process next bit */
499 #define SCRUB_NEXT_EXIT         3 /* exit all the loops */
500 #define SCRUB_NEXT_WAIT         4 /* wait for free cache slot */
501 #define SCRUB_NEXT_CRASH        5 /* simulate system crash during OI scrub */
502 #define SCRUB_NEXT_FATAL        6 /* simulate failure during OI scrub */
503
504 struct osd_iit_param {
505         struct super_block *sb;
506         struct buffer_head *bitmap;
507         ldiskfs_group_t bg;
508         __u32 gbase;
509         __u32 offset;
510 };
511
512 typedef int (*osd_iit_next_policy)(struct osd_thread_info *info,
513                                    struct osd_device *dev,
514                                    struct osd_iit_param *param,
515                                    struct osd_idmap_cache **oic,
516                                    int noslot);
517
518 typedef int (*osd_iit_exec_policy)(struct osd_thread_info *info,
519                                    struct osd_device *dev,
520                                    struct osd_iit_param *param,
521                                    struct osd_idmap_cache *oic,
522                                    int *noslot, int rc);
523
524 static inline int osd_scrub_has_window(struct osd_scrub *scrub,
525                                        struct osd_otable_cache *ooc)
526 {
527         return scrub->os_pos_current < ooc->ooc_pos_preload + SCRUB_WINDOW_SIZE;
528 }
529
530 static int osd_iit_next(struct osd_iit_param *param, __u32 *pos)
531 {
532         param->offset = ldiskfs_find_next_bit(param->bitmap->b_data,
533                         LDISKFS_INODES_PER_GROUP(param->sb), param->offset);
534         if (param->offset >= LDISKFS_INODES_PER_GROUP(param->sb)) {
535                 *pos = 1 + (param->bg+1) * LDISKFS_INODES_PER_GROUP(param->sb);
536                 return SCRUB_NEXT_BREAK;
537         } else {
538                 *pos = param->gbase + param->offset;
539                 return 0;
540         }
541 }
542
543 static int osd_iit_iget(struct osd_thread_info *info, struct osd_device *dev,
544                         struct lu_fid *fid, struct osd_inode_id *lid, __u32 pos,
545                         struct super_block *sb, struct inode **pinode)
546 {
547         struct inode *inode;
548         int           rc;
549
550         osd_id_gen(lid, pos, OSD_OII_NOGEN);
551         inode = osd_iget_fid(info, dev, lid, fid);
552         if (IS_ERR(inode)) {
553                 rc = PTR_ERR(inode);
554                 /* The inode may be removed after bitmap searching, or the
555                  * file is new created without inode initialized yet. */
556                 if (rc == -ENOENT || rc == -ESTALE)
557                         return SCRUB_NEXT_CONTINUE;
558
559                 CERROR("%.16s: fail to read inode, ino# = %u, rc = %d\n",
560                        LDISKFS_SB(sb)->s_es->s_volume_name, pos, rc);
561                 return rc;
562         }
563
564         *pinode = inode;
565         return 0;
566 }
567
568 static int osd_scrub_next(struct osd_thread_info *info, struct osd_device *dev,
569                           struct osd_iit_param *param,
570                           struct osd_idmap_cache **oic, int noslot)
571 {
572         struct osd_scrub     *scrub  = &dev->od_scrub;
573         struct ptlrpc_thread *thread = &scrub->os_thread;
574         struct lu_fid        *fid;
575         struct osd_inode_id  *lid;
576         struct inode         *inode;
577         int                   rc;
578
579         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
580                 struct l_wait_info lwi;
581
582                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
583                 l_wait_event(thread->t_ctl_waitq,
584                              !cfs_list_empty(&scrub->os_inconsistent_items) ||
585                              !thread_is_running(thread),
586                              &lwi);
587         }
588
589         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
590                 cfs_spin_lock(&scrub->os_lock);
591                 thread_set_flags(thread, SVC_STOPPING);
592                 cfs_spin_unlock(&scrub->os_lock);
593                 return SCRUB_NEXT_CRASH;
594         }
595
596         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
597                 return SCRUB_NEXT_FATAL;
598
599         if (unlikely(!thread_is_running(thread)))
600                 return SCRUB_NEXT_EXIT;
601
602         if (!cfs_list_empty(&scrub->os_inconsistent_items)) {
603                 struct osd_inconsistent_item *oii;
604
605                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
606                                      struct osd_inconsistent_item, oii_list);
607                 *oic = &oii->oii_cache;
608                 scrub->os_in_prior = 1;
609                 return 0;
610         }
611
612         if (noslot != 0)
613                 return SCRUB_NEXT_WAIT;
614
615         rc = osd_iit_next(param, &scrub->os_pos_current);
616         if (rc != 0)
617                 return rc;
618
619         *oic = &scrub->os_oic;
620         fid = &(*oic)->oic_fid;
621         lid = &(*oic)->oic_lid;
622         rc = osd_iit_iget(info, dev, fid, lid,
623                           scrub->os_pos_current, param->sb, &inode);
624         if (rc != 0)
625                 return rc;
626
627         if (!fid_is_norm(fid) || inode->i_state & I_LUSTRE_NOSCRUB)
628                 rc = SCRUB_NEXT_CONTINUE;
629         iput(inode);
630         return rc;
631 }
632
633 static int osd_preload_next(struct osd_thread_info *info,
634                             struct osd_device *dev, struct osd_iit_param *param,
635                             struct osd_idmap_cache **oic, int noslot)
636 {
637         struct osd_otable_cache *ooc    = &dev->od_otable_it->ooi_cache;
638         struct osd_scrub        *scrub;
639         struct ptlrpc_thread    *thread;
640         struct inode            *inode;
641         int                      rc;
642
643         rc = osd_iit_next(param, &ooc->ooc_pos_preload);
644         if (rc != 0)
645                 return rc;
646
647         scrub = &dev->od_scrub;
648         thread = &scrub->os_thread;
649         if (thread_is_running(thread) &&
650             ooc->ooc_pos_preload >= scrub->os_pos_current)
651                 return SCRUB_NEXT_EXIT;
652
653         rc = osd_iit_iget(info, dev,
654                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_fid,
655                           &ooc->ooc_cache[ooc->ooc_producer_idx].oic_lid,
656                           ooc->ooc_pos_preload, param->sb, &inode);
657         /* If succeed, it needs to move forward; otherwise up layer LFSCK may
658          * ignore the failure, so it still need to skip the inode next time. */
659         ooc->ooc_pos_preload = param->gbase + ++(param->offset);
660         if (rc == 0)
661                 iput(inode);
662         return rc;
663 }
664
665 static int osd_scrub_exec(struct osd_thread_info *info, struct osd_device *dev,
666                           struct osd_iit_param *param,
667                           struct osd_idmap_cache *oic, int *noslot, int rc)
668 {
669         struct l_wait_info       lwi    = { 0 };
670         struct osd_scrub        *scrub  = &dev->od_scrub;
671         struct ptlrpc_thread    *thread = &scrub->os_thread;
672         struct osd_otable_it    *it     = dev->od_otable_it;
673         struct osd_otable_cache *ooc    = it ? &it->ooi_cache : NULL;
674
675         switch (rc) {
676         case SCRUB_NEXT_CONTINUE:
677                 goto next;
678         case SCRUB_NEXT_WAIT:
679                 goto wait;
680         }
681
682         LASSERTF(rc <= 0, "unexpected rc = %d\n", rc);
683
684         if (rc != 0)
685                 rc = osd_scrub_error(dev, &oic->oic_lid, rc);
686         else
687                 rc = osd_scrub_check_update(info, dev, oic);
688         if (rc != 0)
689                 return rc;
690
691         rc = osd_scrub_checkpoint(scrub);
692         if (rc != 0) {
693                 CERROR("%.16s: fail to checkpoint, pos = %u, rc = %d\n",
694                        LDISKFS_SB(param->sb)->s_es->s_volume_name,
695                        scrub->os_pos_current, rc);
696                 /* Continue, as long as the scrub itself can go ahead. */
697         }
698
699         if (scrub->os_in_prior) {
700                 scrub->os_in_prior = 0;
701                 return 0;
702         }
703
704 next:
705         scrub->os_pos_current = param->gbase + ++(param->offset);
706         if (it != NULL && it->ooi_waiting &&
707             ooc->ooc_pos_preload < scrub->os_pos_current) {
708                 it->ooi_waiting = 0;
709                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
710         }
711
712         if (scrub->os_full_speed || rc == SCRUB_NEXT_CONTINUE)
713                 return 0;
714
715 wait:
716         if (osd_scrub_has_window(scrub, ooc)) {
717                 *noslot = 0;
718                 return 0;
719         }
720
721         scrub->os_waiting = 1;
722         l_wait_event(thread->t_ctl_waitq,
723                      osd_scrub_has_window(scrub, ooc) ||
724                      !cfs_list_empty(&scrub->os_inconsistent_items) ||
725                      !thread_is_running(thread),
726                      &lwi);
727         scrub->os_waiting = 0;
728
729         if (osd_scrub_has_window(scrub, ooc))
730                 *noslot = 0;
731         else
732                 *noslot = 1;
733         return 0;
734 }
735
736 static int osd_preload_exec(struct osd_thread_info *info,
737                             struct osd_device *dev, struct osd_iit_param *param,
738                             struct osd_idmap_cache *oic, int *noslot, int rc)
739 {
740         struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
741
742         if (rc == 0) {
743                 ooc->ooc_cached_items++;
744                 ooc->ooc_producer_idx = (ooc->ooc_producer_idx + 1) &
745                                         ~OSD_OTABLE_IT_CACHE_MASK;
746         }
747         return rc > 0 ? 0 : rc;
748 }
749
750 #define SCRUB_IT_ALL    1
751 #define SCRUB_IT_CRASH  2
752
753 static int osd_inode_iteration(struct osd_thread_info *info,
754                                struct osd_device *dev, __u32 max, int preload)
755 {
756         osd_iit_next_policy   next;
757         osd_iit_exec_policy   exec;
758         __u32                *pos;
759         __u32                *count;
760         struct osd_iit_param  param;
761         __u32                 limit;
762         int                   noslot = 0;
763         int                   rc;
764         ENTRY;
765
766         if (preload == 0) {
767                 struct osd_scrub *scrub = &dev->od_scrub;
768
769                 next = osd_scrub_next;
770                 exec = osd_scrub_exec;
771                 pos = &scrub->os_pos_current;
772                 count = &scrub->os_new_checked;
773         } else {
774                 struct osd_otable_cache *ooc = &dev->od_otable_it->ooi_cache;
775
776                 next = osd_preload_next;
777                 exec = osd_preload_exec;
778                 pos = &ooc->ooc_pos_preload;
779                 count = &ooc->ooc_cached_items;
780         }
781         param.sb = osd_sb(dev);
782         limit = le32_to_cpu(LDISKFS_SB(param.sb)->s_es->s_inodes_count);
783
784         while (*pos <= limit && *count < max) {
785                 struct osd_idmap_cache *oic = NULL;
786
787                 param.bg = (*pos - 1) / LDISKFS_INODES_PER_GROUP(param.sb);
788                 param.offset = (*pos - 1) % LDISKFS_INODES_PER_GROUP(param.sb);
789                 param.gbase = 1 + param.bg * LDISKFS_INODES_PER_GROUP(param.sb);
790                 param.bitmap = ldiskfs_read_inode_bitmap(param.sb, param.bg);
791                 if (param.bitmap == NULL) {
792                         CERROR("%.16s: fail to read bitmap for %u, "
793                                "scrub will stop, urgent mode\n",
794                                LDISKFS_SB(param.sb)->s_es->s_volume_name,
795                                (__u32)param.bg);
796                         RETURN(-EIO);
797                 }
798
799                 while (param.offset < LDISKFS_INODES_PER_GROUP(param.sb) &&
800                        *count < max) {
801                         rc = next(info, dev, &param, &oic, noslot);
802                         switch (rc) {
803                         case SCRUB_NEXT_BREAK:
804                                 goto next_group;
805                         case SCRUB_NEXT_EXIT:
806                                 brelse(param.bitmap);
807                                 RETURN(0);
808                         case SCRUB_NEXT_CRASH:
809                                 brelse(param.bitmap);
810                                 RETURN(SCRUB_IT_CRASH);
811                         case SCRUB_NEXT_FATAL:
812                                 brelse(param.bitmap);
813                                 RETURN(-EINVAL);
814                         }
815
816                         rc = exec(info, dev, &param, oic, &noslot, rc);
817                         if (rc != 0) {
818                                 brelse(param.bitmap);
819                                 RETURN(rc);
820                         }
821                 }
822
823 next_group:
824                 brelse(param.bitmap);
825         }
826
827         if (*pos > limit)
828                 RETURN(SCRUB_IT_ALL);
829         RETURN(0);
830 }
831
832 static int osd_scrub_main(void *args)
833 {
834         struct lu_env         env;
835         struct osd_device    *dev    = (struct osd_device *)args;
836         struct osd_scrub     *scrub  = &dev->od_scrub;
837         struct ptlrpc_thread *thread = &scrub->os_thread;
838         struct super_block   *sb     = osd_sb(dev);
839         int                   rc;
840         ENTRY;
841
842         cfs_daemonize("OI_scrub");
843         rc = lu_env_init(&env, LCT_DT_THREAD);
844         if (rc != 0) {
845                 CERROR("%.16s: OI scrub, fail to init env, rc = %d\n",
846                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
847                 GOTO(noenv, rc);
848         }
849
850         rc = osd_scrub_prep(dev);
851         if (rc != 0) {
852                 CERROR("%.16s: OI scrub, fail to scrub prep, rc = %d\n",
853                        LDISKFS_SB(sb)->s_es->s_volume_name, rc);
854                 GOTO(out, rc);
855         }
856
857         if (!scrub->os_full_speed) {
858                 struct l_wait_info lwi = { 0 };
859                 struct osd_otable_it *it = dev->od_otable_it;
860                 struct osd_otable_cache *ooc = &it->ooi_cache;
861
862                 l_wait_event(thread->t_ctl_waitq,
863                              it->ooi_user_ready || !thread_is_running(thread),
864                              &lwi);
865                 if (unlikely(!thread_is_running(thread)))
866                         GOTO(post, rc = 0);
867
868                 LASSERT(scrub->os_pos_current >= ooc->ooc_pos_preload);
869                 scrub->os_pos_current = ooc->ooc_pos_preload;
870         }
871
872         CDEBUG(D_LFSCK, "OI scrub: flags = 0x%x, pos = %u\n",
873                scrub->os_start_flags, scrub->os_pos_current);
874
875         rc = osd_inode_iteration(osd_oti_get(&env), dev, ~0U, 0);
876         if (unlikely(rc == SCRUB_IT_CRASH))
877                 GOTO(out, rc = -EINVAL);
878         GOTO(post, rc);
879
880 post:
881         osd_scrub_post(scrub, rc);
882         CDEBUG(D_LFSCK, "OI scrub: stop, rc = %d, pos = %u\n",
883                rc, scrub->os_pos_current);
884
885 out:
886         while (!cfs_list_empty(&scrub->os_inconsistent_items)) {
887                 struct osd_inconsistent_item *oii;
888
889                 oii = cfs_list_entry(scrub->os_inconsistent_items.next,
890                                      struct osd_inconsistent_item, oii_list);
891                 cfs_list_del_init(&oii->oii_list);
892                 OBD_FREE_PTR(oii);
893         }
894         lu_env_fini(&env);
895
896 noenv:
897         cfs_spin_lock(&scrub->os_lock);
898         thread_set_flags(thread, SVC_STOPPED);
899         cfs_waitq_broadcast(&thread->t_ctl_waitq);
900         cfs_spin_unlock(&scrub->os_lock);
901         return rc;
902 }
903
904 static int do_osd_scrub_start(struct osd_device *dev, __u32 flags)
905 {
906         struct osd_scrub     *scrub  = &dev->od_scrub;
907         struct ptlrpc_thread *thread = &scrub->os_thread;
908         struct l_wait_info    lwi    = { 0 };
909         int                   rc;
910         ENTRY;
911
912 again:
913         /* os_lock: sync status between stop and scrub thread */
914         cfs_spin_lock(&scrub->os_lock);
915         if (thread_is_running(thread)) {
916                 cfs_spin_unlock(&scrub->os_lock);
917                 RETURN(-EALREADY);
918         } else if (unlikely(thread_is_stopping(thread))) {
919                 cfs_spin_unlock(&scrub->os_lock);
920                 l_wait_event(thread->t_ctl_waitq,
921                              thread_is_stopped(thread),
922                              &lwi);
923                 goto again;
924         }
925         cfs_spin_unlock(&scrub->os_lock);
926
927         scrub->os_start_flags = flags;
928         thread_set_flags(thread, 0);
929         rc = cfs_create_thread(osd_scrub_main, dev, 0);
930         if (rc < 0) {
931                 CERROR("%.16s: cannot start iteration thread, rc = %d\n",
932                        LDISKFS_SB(osd_sb(dev))->s_es->s_volume_name, rc);
933                 RETURN(rc);
934         }
935
936         l_wait_event(thread->t_ctl_waitq,
937                      thread_is_running(thread) || thread_is_stopped(thread),
938                      &lwi);
939
940         RETURN(0);
941 }
942
943 int osd_scrub_start(struct osd_device *dev)
944 {
945         __u32 flags = SS_AUTO;
946         int   rc;
947         ENTRY;
948
949         if (dev->od_scrub.os_file.sf_status == SS_COMPLETED)
950                 flags |= SS_RESET;
951
952         /* od_otable_mutex: prevent curcurrent start/stop */
953         cfs_mutex_lock(&dev->od_otable_mutex);
954         rc = do_osd_scrub_start(dev, flags);
955         cfs_mutex_unlock(&dev->od_otable_mutex);
956
957         RETURN(rc == -EALREADY ? 0 : rc);
958 }
959
960 static void do_osd_scrub_stop(struct osd_scrub *scrub)
961 {
962         struct ptlrpc_thread *thread = &scrub->os_thread;
963         struct l_wait_info    lwi    = { 0 };
964
965         /* os_lock: sync status between stop and scrub thread */
966         cfs_spin_lock(&scrub->os_lock);
967         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
968                 thread_set_flags(thread, SVC_STOPPING);
969                 cfs_spin_unlock(&scrub->os_lock);
970                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
971                 l_wait_event(thread->t_ctl_waitq,
972                              thread_is_stopped(thread),
973                              &lwi);
974                 /* Do not skip the last lock/unlock, which can guarantee that
975                  * the caller cannot return until the OI scrub thread exit. */
976                 cfs_spin_lock(&scrub->os_lock);
977         }
978         cfs_spin_unlock(&scrub->os_lock);
979 }
980
981 static void osd_scrub_stop(struct osd_device *dev)
982 {
983         /* od_otable_mutex: prevent curcurrent start/stop */
984         cfs_mutex_lock(&dev->od_otable_mutex);
985         dev->od_scrub.os_paused = 1;
986         do_osd_scrub_stop(&dev->od_scrub);
987         cfs_mutex_unlock(&dev->od_otable_mutex);
988 }
989
990 static const char osd_scrub_name[] = "OI_scrub";
991
992 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
993 {
994         struct osd_thread_info     *info   = osd_oti_get(env);
995         struct osd_scrub           *scrub  = &dev->od_scrub;
996         struct lvfs_run_ctxt       *ctxt   = &scrub->os_ctxt;
997         struct scrub_file          *sf     = &scrub->os_file;
998         struct osd_inode_id        *id     = &scrub->os_oic.oic_lid;
999         struct super_block         *sb     = osd_sb(dev);
1000         struct ldiskfs_super_block *es     = LDISKFS_SB(sb)->s_es;
1001         struct inode               *inode;
1002         struct lvfs_run_ctxt        saved;
1003         struct file                *filp;
1004         int                         dirty  = 0;
1005         int                         init   = 0;
1006         int                         rc     = 0;
1007         ENTRY;
1008
1009         memset(scrub, 0, sizeof(*scrub));
1010         OBD_SET_CTXT_MAGIC(ctxt);
1011         ctxt->pwdmnt = dev->od_mnt;
1012         ctxt->pwd = dev->od_mnt->mnt_root;
1013         ctxt->fs = get_ds();
1014
1015         cfs_waitq_init(&scrub->os_thread.t_ctl_waitq);
1016         cfs_init_rwsem(&scrub->os_rwsem);
1017         cfs_spin_lock_init(&scrub->os_lock);
1018         CFS_INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1019         if (get_mount_flags(dev->od_mount->lmi_sb) & LMD_FLG_NOSCRUB)
1020                 scrub->os_no_scrub = 1;
1021
1022         push_ctxt(&saved, ctxt, NULL);
1023         filp = filp_open(osd_scrub_name, O_RDWR | O_CREAT, 0644);
1024         if (IS_ERR(filp))
1025                 RETURN(PTR_ERR(filp));
1026
1027         scrub->os_inode = igrab(filp->f_dentry->d_inode);
1028         filp_close(filp, 0);
1029         pop_ctxt(&saved, ctxt, NULL);
1030
1031         rc = osd_scrub_file_load(scrub);
1032         if (rc == -ENOENT) {
1033                 osd_scrub_file_init(scrub, es->s_uuid);
1034                 dirty = 1;
1035                 init = 1;
1036         } else if (rc != 0) {
1037                 RETURN(rc);
1038         } else {
1039                 if (memcmp(sf->sf_uuid, es->s_uuid, 16) != 0) {
1040                         osd_scrub_file_reset(scrub, es->s_uuid,SF_INCONSISTENT);
1041                         dirty = 1;
1042                 } else if (sf->sf_status == SS_SCANNING) {
1043                         sf->sf_status = SS_CRASHED;
1044                         dirty = 1;
1045                 }
1046         }
1047
1048         if (sf->sf_pos_last_checkpoint != 0)
1049                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1050         else
1051                 scrub->os_pos_current = LDISKFS_FIRST_INO(sb);
1052
1053         if (dirty != 0) {
1054                 rc = osd_scrub_file_store(scrub);
1055                 if (rc != 0)
1056                         RETURN(rc);
1057         }
1058
1059         /* Initialize OI files. */
1060         rc = osd_oi_init(info, dev);
1061         if (rc < 0)
1062                 RETURN(rc);
1063
1064         if (init != 0) {
1065                 rc = __osd_oi_lookup(info, dev, &LU_DOT_LUSTRE_FID, id);
1066                 if (rc == 0) {
1067                         inode = osd_iget(info, dev, id);
1068                         if (IS_ERR(inode)) {
1069                                 rc = PTR_ERR(inode);
1070                                 /* It is restored from old 2.x backup. */
1071                                 if (rc == -ENOENT || rc == -ESTALE) {
1072                                         osd_scrub_file_reset(scrub, es->s_uuid,
1073                                                              SF_INCONSISTENT);
1074                                         rc = osd_scrub_file_store(scrub);
1075                                 }
1076                         } else {
1077                                 iput(inode);
1078                         }
1079                 } else if (rc == -ENOENT) {
1080                         rc = 0;
1081                 }
1082         }
1083
1084         if (rc == 0 && !scrub->os_no_scrub &&
1085             ((sf->sf_status == SS_PAUSED) ||
1086              (sf->sf_status == SS_CRASHED &&
1087               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT | SF_AUTO)) ||
1088              (sf->sf_status == SS_INIT &&
1089               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT))))
1090                 rc = osd_scrub_start(dev);
1091
1092         RETURN(rc);
1093 }
1094
1095 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1096 {
1097         struct osd_scrub *scrub = &dev->od_scrub;
1098
1099         LASSERT(dev->od_otable_it == NULL);
1100
1101         if (scrub->os_inode != NULL) {
1102                 osd_scrub_stop(dev);
1103                 iput(scrub->os_inode);
1104                 scrub->os_inode = NULL;
1105         }
1106         if (dev->od_oi_table != NULL)
1107                 osd_oi_fini(osd_oti_get(env), dev);
1108 }
1109
1110 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1111                                        struct dt_object *dt, __u32 attr,
1112                                        struct lustre_capa *capa)
1113 {
1114         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1115         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1116         struct osd_device      *dev   = osd_dev(dt->do_lu.lo_dev);
1117         struct osd_scrub       *scrub = &dev->od_scrub;
1118         struct osd_otable_it   *it;
1119         __u32                   start = 0;
1120         int                     rc;
1121         ENTRY;
1122
1123         /* od_otable_mutex: prevent curcurrent init/fini */
1124         cfs_mutex_lock(&dev->od_otable_mutex);
1125         if (dev->od_otable_it != NULL)
1126                 GOTO(out, it = ERR_PTR(-EALREADY));
1127
1128         OBD_ALLOC_PTR(it);
1129         if (it == NULL)
1130                 GOTO(out, it = ERR_PTR(-ENOMEM));
1131
1132         dev->od_otable_it = it;
1133         it->ooi_dev = dev;
1134         it->ooi_cache.ooc_consumer_idx = -1;
1135         if (flags & DOIF_OUTUSED)
1136                 it->ooi_used_outside = 1;
1137
1138         if (flags & DOIF_RESET)
1139                 start |= SS_RESET;
1140
1141         if (valid & DOIV_ERROR_HANDLE) {
1142                 if (flags & DOIF_FAILOUT)
1143                         start |= SS_SET_FAILOUT;
1144                 else
1145                         start |= SS_CLEAR_FAILOUT;
1146         }
1147
1148         rc = do_osd_scrub_start(dev, start);
1149         if (rc == -EALREADY) {
1150                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current - 1;
1151         } else if (rc < 0) {
1152                 dev->od_otable_it = NULL;
1153                 OBD_FREE_PTR(it);
1154                 GOTO(out, it = ERR_PTR(-EALREADY));
1155         } else {
1156                 it->ooi_cache.ooc_pos_preload = scrub->os_pos_current;
1157         }
1158
1159         GOTO(out, it);
1160
1161 out:
1162         cfs_mutex_unlock(&dev->od_otable_mutex);
1163         return (struct dt_it *)it;
1164 }
1165
1166 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1167 {
1168         struct osd_otable_it *it  = (struct osd_otable_it *)di;
1169         struct osd_device    *dev = it->ooi_dev;
1170
1171         /* od_otable_mutex: prevent curcurrent init/fini */
1172         cfs_mutex_lock(&dev->od_otable_mutex);
1173         do_osd_scrub_stop(&dev->od_scrub);
1174         LASSERT(dev->od_otable_it == it);
1175
1176         dev->od_otable_it = NULL;
1177         cfs_mutex_unlock(&dev->od_otable_mutex);
1178         OBD_FREE_PTR(it);
1179 }
1180
1181 /**
1182  * XXX: Temporary used to notify otable iteration to be paused.
1183  */
1184 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1185 {
1186         struct osd_device *dev = ((struct osd_otable_it *)di)->ooi_dev;
1187
1188         /* od_otable_mutex: prevent curcurrent init/fini */
1189         cfs_mutex_lock(&dev->od_otable_mutex);
1190         dev->od_scrub.os_paused = 1;
1191         cfs_mutex_unlock(&dev->od_otable_mutex);
1192 }
1193
1194 /**
1195  * Set the OSD layer iteration start position as the specified key.
1196  *
1197  * The LFSCK out of OSD layer does not know the detail of the key, so if there
1198  * are several keys, they cannot be compared out of OSD, so call "::get()" for
1199  * each key, and OSD will select the smallest one by itself.
1200  */
1201 static int osd_otable_it_get(const struct lu_env *env,
1202                              struct dt_it *di, const struct dt_key *key)
1203 {
1204         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1205         struct osd_otable_cache *ooc = &it->ooi_cache;
1206         const char              *str = (const char *)key;
1207         __u32                    ino;
1208         ENTRY;
1209
1210         /* Forbid to set iteration position after iteration started. */
1211         if (it->ooi_user_ready)
1212                 RETURN(-EPERM);
1213
1214         if (str[0] == '\0')
1215                 RETURN(-EINVAL);
1216
1217         if (sscanf(str, "%u", &ino) <= 0)
1218                 RETURN(-EINVAL);
1219
1220         /* Skip the one that has been processed last time. */
1221         if (ooc->ooc_pos_preload > ++ino)
1222                 ooc->ooc_pos_preload = ino;
1223
1224         RETURN(0);
1225 }
1226
1227 static int osd_otable_it_preload(const struct lu_env *env,
1228                                  struct osd_otable_it *it)
1229 {
1230         struct osd_device       *dev   = it->ooi_dev;
1231         struct osd_scrub        *scrub = &dev->od_scrub;
1232         struct osd_otable_cache *ooc   = &it->ooi_cache;
1233         int                      rc;
1234         ENTRY;
1235
1236         rc = osd_inode_iteration(osd_oti_get(env), dev,
1237                                  OSD_OTABLE_IT_CACHE_SIZE, 1);
1238         if (rc == SCRUB_IT_ALL)
1239                 it->ooi_all_cached = 1;
1240
1241         CDEBUG(D_LFSCK, "OSD pre-loaded: max = %u, preload = %u, rc = %d\n",
1242                le32_to_cpu(LDISKFS_SB(osd_sb(dev))->s_es->s_inodes_count),
1243                ooc->ooc_pos_preload, rc);
1244
1245         if (scrub->os_waiting && osd_scrub_has_window(scrub, ooc)) {
1246                 scrub->os_waiting = 0;
1247                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1248         }
1249
1250         RETURN(rc < 0 ? rc : ooc->ooc_cached_items);
1251 }
1252
1253 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1254 {
1255         struct osd_otable_it    *it     = (struct osd_otable_it *)di;
1256         struct osd_device       *dev    = it->ooi_dev;
1257         struct osd_scrub        *scrub  = &dev->od_scrub;
1258         struct osd_otable_cache *ooc    = &it->ooi_cache;
1259         struct ptlrpc_thread    *thread = &scrub->os_thread;
1260         struct l_wait_info       lwi    = { 0 };
1261         int                      rc;
1262         ENTRY;
1263
1264         LASSERT(it->ooi_user_ready);
1265
1266 again:
1267         if (!thread_is_running(thread) && !it->ooi_used_outside)
1268                 RETURN(1);
1269
1270         if (ooc->ooc_cached_items > 0) {
1271                 ooc->ooc_cached_items--;
1272                 ooc->ooc_consumer_idx = (ooc->ooc_consumer_idx + 1) &
1273                                         ~OSD_OTABLE_IT_CACHE_MASK;
1274                 RETURN(0);
1275         }
1276
1277         if (it->ooi_all_cached) {
1278                 l_wait_event(thread->t_ctl_waitq,
1279                              !thread_is_running(thread),
1280                              &lwi);
1281                 RETURN(1);
1282         }
1283
1284         it->ooi_waiting = 1;
1285         l_wait_event(thread->t_ctl_waitq,
1286                      ooc->ooc_pos_preload < scrub->os_pos_current ||
1287                      !thread_is_running(thread),
1288                      &lwi);
1289         it->ooi_waiting = 0;
1290
1291         if (!thread_is_running(thread) && !it->ooi_used_outside)
1292                 RETURN(1);
1293
1294         rc = osd_otable_it_preload(env, it);
1295         if (rc >= 0)
1296                 goto again;
1297
1298         RETURN(rc);
1299 }
1300
1301 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1302                                         const struct dt_it *di)
1303 {
1304         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1305         struct osd_otable_cache *ooc = &it->ooi_cache;
1306
1307         sprintf(it->ooi_key, "%u",
1308                 ooc->ooc_cache[ooc->ooc_consumer_idx].oic_lid.oii_ino);
1309         return (struct dt_key *)it->ooi_key;
1310 }
1311
1312 static int osd_otable_it_key_size(const struct lu_env *env,
1313                                   const struct dt_it *di)
1314 {
1315         return sizeof(((struct osd_otable_it *)di)->ooi_key);
1316 }
1317
1318 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1319                              struct dt_rec *rec, __u32 attr)
1320 {
1321         struct osd_otable_it    *it  = (struct osd_otable_it *)di;
1322         struct osd_otable_cache *ooc = &it->ooi_cache;
1323
1324         *(struct lu_fid *)rec = ooc->ooc_cache[ooc->ooc_consumer_idx].oic_fid;
1325         return 0;
1326 }
1327
1328 static int osd_otable_it_load(const struct lu_env *env,
1329                               const struct dt_it *di, __u64 hash)
1330 {
1331         struct osd_otable_it    *it    = (struct osd_otable_it *)di;
1332         struct osd_device       *dev   = it->ooi_dev;
1333         struct osd_otable_cache *ooc   = &it->ooi_cache;
1334         struct osd_scrub        *scrub = &dev->od_scrub;
1335
1336         if (it->ooi_user_ready)
1337                 return 0;
1338
1339         if (ooc->ooc_pos_preload < LDISKFS_FIRST_INO(osd_sb(dev)))
1340                 ooc->ooc_pos_preload = LDISKFS_FIRST_INO(osd_sb(dev));
1341         it->ooi_user_ready = 1;
1342         if (!scrub->os_full_speed)
1343                 cfs_waitq_broadcast(&scrub->os_thread.t_ctl_waitq);
1344
1345         /* Unplug OSD layer iteration by the first next() call. */
1346         return osd_otable_it_next(env, (struct dt_it *)it);
1347 }
1348
1349 const struct dt_index_operations osd_otable_ops = {
1350         .dio_it = {
1351                 .init     = osd_otable_it_init,
1352                 .fini     = osd_otable_it_fini,
1353                 .put      = osd_otable_it_put,
1354                 .get      = osd_otable_it_get,
1355                 .next     = osd_otable_it_next,
1356                 .key      = osd_otable_it_key,
1357                 .key_size = osd_otable_it_key_size,
1358                 .rec      = osd_otable_it_rec,
1359                 .load     = osd_otable_it_load,
1360         }
1361 };
1362
1363 int osd_oii_insert(struct osd_device *dev, struct osd_idmap_cache *oic,
1364                    int insert)
1365 {
1366         struct osd_inconsistent_item *oii;
1367         struct osd_scrub             *scrub  = &dev->od_scrub;
1368         struct ptlrpc_thread         *thread = &scrub->os_thread;
1369         int                           wakeup = 0;
1370         ENTRY;
1371
1372         OBD_ALLOC_PTR(oii);
1373         if (unlikely(oii == NULL))
1374                 RETURN(-ENOMEM);
1375
1376         CFS_INIT_LIST_HEAD(&oii->oii_list);
1377         oii->oii_cache = *oic;
1378         oii->oii_insert = insert;
1379
1380         cfs_spin_lock(&scrub->os_lock);
1381         if (unlikely(!thread_is_running(thread))) {
1382                 cfs_spin_unlock(&scrub->os_lock);
1383                 OBD_FREE_PTR(oii);
1384                 RETURN(-EAGAIN);
1385         }
1386
1387         if (cfs_list_empty(&scrub->os_inconsistent_items))
1388                 wakeup = 1;
1389         cfs_list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1390         cfs_spin_unlock(&scrub->os_lock);
1391
1392         if (wakeup != 0)
1393                 cfs_waitq_broadcast(&thread->t_ctl_waitq);
1394
1395         RETURN(0);
1396 }
1397
1398 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1399                    struct osd_inode_id *id)
1400 {
1401         struct osd_scrub             *scrub = &dev->od_scrub;
1402         struct osd_inconsistent_item *oii;
1403         ENTRY;
1404
1405         cfs_spin_lock(&scrub->os_lock);
1406         cfs_list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1407                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1408                         *id = oii->oii_cache.oic_lid;
1409                         cfs_spin_unlock(&scrub->os_lock);
1410                         RETURN(0);
1411                 }
1412         }
1413         cfs_spin_unlock(&scrub->os_lock);
1414
1415         RETURN(-ENOENT);
1416 }
1417
1418 static const char *scrub_status_names[] = {
1419         "init",
1420         "scanning",
1421         "completed",
1422         "failed",
1423         "stopped",
1424         "paused",
1425         "crashed",
1426         NULL
1427 };
1428
1429 static const char *scrub_flags_names[] = {
1430         "recreated",
1431         "inconsistent",
1432         "auto",
1433         NULL
1434 };
1435
1436 static const char *scrub_param_names[] = {
1437         "failout",
1438         NULL
1439 };
1440
1441 static int scrub_bits_dump(char **buf, int *len, int bits, const char *names[],
1442                            const char *prefix)
1443 {
1444         int save = *len;
1445         int flag;
1446         int rc;
1447         int i;
1448
1449         rc = snprintf(*buf, *len, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1450         if (rc <= 0)
1451                 return -ENOSPC;
1452
1453         *buf += rc;
1454         *len -= rc;
1455         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1456                 if (flag & bits) {
1457                         bits &= ~flag;
1458                         rc = snprintf(*buf, *len, "%s%c", names[i],
1459                                       bits != 0 ? ',' : '\n');
1460                         if (rc <= 0)
1461                                 return -ENOSPC;
1462
1463                         *buf += rc;
1464                         *len -= rc;
1465                 }
1466         }
1467         return save - *len;
1468 }
1469
1470 static int scrub_time_dump(char **buf, int *len, __u64 time, const char *prefix)
1471 {
1472         int rc;
1473
1474         if (time != 0)
1475                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
1476                               cfs_time_current_sec() - time);
1477         else
1478                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
1479         if (rc <= 0)
1480                 return -ENOSPC;
1481
1482         *buf += rc;
1483         *len -= rc;
1484         return rc;
1485 }
1486
1487 static int scrub_pos_dump(char **buf, int *len, __u64 pos, const char *prefix)
1488 {
1489         int rc;
1490
1491         if (pos != 0)
1492                 rc = snprintf(*buf, *len, "%s: "LPU64"\n", prefix, pos);
1493         else
1494                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
1495         if (rc <= 0)
1496                 return -ENOSPC;
1497
1498         *buf += rc;
1499         *len -= rc;
1500         return rc;
1501 }
1502
1503 int osd_scrub_dump(struct osd_device *dev, char *buf, int len)
1504 {
1505         struct osd_scrub  *scrub   = &dev->od_scrub;
1506         struct scrub_file *sf      = &scrub->os_file;
1507         __u64              checked;
1508         __u64              speed;
1509         int                save    = len;
1510         int                ret     = -ENOSPC;
1511         int                rc;
1512
1513         cfs_down_read(&scrub->os_rwsem);
1514         rc = snprintf(buf, len,
1515                       "name: OI scrub\n"
1516                       "magic: 0x%x\n"
1517                       "oi_files: %d\n"
1518                       "status: %s\n",
1519                       sf->sf_magic, (int)sf->sf_oi_count,
1520                       scrub_status_names[sf->sf_status]);
1521         if (rc <= 0)
1522                 goto out;
1523
1524         buf += rc;
1525         len -= rc;
1526         rc = scrub_bits_dump(&buf, &len, sf->sf_flags, scrub_flags_names,
1527                              "flags");
1528         if (rc < 0)
1529                 goto out;
1530
1531         rc = scrub_bits_dump(&buf, &len, sf->sf_param, scrub_param_names,
1532                              "param");
1533         if (rc < 0)
1534                 goto out;
1535
1536         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_complete,
1537                              "time_since_last_completed");
1538         if (rc < 0)
1539                 goto out;
1540
1541         rc = scrub_time_dump(&buf, &len, sf->sf_time_latest_start,
1542                              "time_since_latest_start");
1543         if (rc < 0)
1544                 goto out;
1545
1546         rc = scrub_time_dump(&buf, &len, sf->sf_time_last_checkpoint,
1547                              "time_since_last_checkpoint");
1548         if (rc < 0)
1549                 goto out;
1550
1551         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_latest_start,
1552                             "latest_start_position");
1553         if (rc < 0)
1554                 goto out;
1555
1556         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_last_checkpoint,
1557                             "last_checkpoint_position");
1558         if (rc < 0)
1559                 goto out;
1560
1561         rc = scrub_pos_dump(&buf, &len, sf->sf_pos_first_inconsistent,
1562                             "first_failure_position");
1563         if (rc < 0)
1564                 goto out;
1565
1566         checked = sf->sf_items_checked + scrub->os_new_checked;
1567         rc = snprintf(buf, len,
1568                       "checked: "LPU64"\n"
1569                       "updated: "LPU64"\n"
1570                       "failed: "LPU64"\n"
1571                       "prior_updated: "LPU64"\n"
1572                       "success_count: %u\n",
1573                       checked, sf->sf_items_updated, sf->sf_items_failed,
1574                       sf->sf_items_updated_prior, sf->sf_success_count);
1575         if (rc <= 0)
1576                 goto out;
1577
1578         buf += rc;
1579         len -= rc;
1580         speed = checked;
1581         if (thread_is_running(&scrub->os_thread)) {
1582                 cfs_duration_t duration = cfs_time_current() -
1583                                           scrub->os_time_last_checkpoint;
1584                 __u64 new_checked = scrub->os_new_checked * CFS_HZ;
1585                 __u32 rtime = sf->sf_run_time +
1586                               cfs_duration_sec(duration + HALF_SEC);
1587
1588                 if (duration != 0)
1589                         do_div(new_checked, duration);
1590                 if (rtime != 0)
1591                         do_div(speed, rtime);
1592                 rc = snprintf(buf, len,
1593                               "run_time: %u seconds\n"
1594                               "average_speed: "LPU64" objects/sec\n"
1595                               "real-time_speed: "LPU64" objects/sec\n"
1596                               "current_position: %u\n",
1597                               rtime, speed, new_checked, scrub->os_pos_current);
1598         } else {
1599                 if (sf->sf_run_time != 0)
1600                         do_div(speed, sf->sf_run_time);
1601                 rc = snprintf(buf, len,
1602                               "run_time: %u seconds\n"
1603                               "average_speed: "LPU64" objects/sec\n"
1604                               "real-time_speed: N/A\n"
1605                               "current_position: N/A\n",
1606                               sf->sf_run_time, speed);
1607         }
1608         if (rc <= 0)
1609                 goto out;
1610
1611         buf += rc;
1612         len -= rc;
1613         ret = save - len;
1614
1615 out:
1616         cfs_up_read(&scrub->os_rwsem);
1617         return ret;
1618 }