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