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