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