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