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