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