Whamcloud - gitweb
5b6f3a03e38bb4b45cbcef032bcbdbf55ce6842b
[fs/lustre-release.git] / lustre / osd-zfs / 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) 2017, Intel Corporation.
24  */
25 /*
26  * lustre/osd-zfs/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 ZFS objects to feed up layer LFSCK.
34  *
35  * Author: Fan Yong <fan.yong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LFSCK
39
40 #include <linux/kthread.h>
41 #include <uapi/linux/lustre/lustre_idl.h>
42 #include <lustre_disk.h>
43 #include <dt_object.h>
44 #include <linux/xattr.h>
45 #include <lustre_scrub.h>
46 #include <obd_class.h>
47 #include <lustre_nodemap.h>
48 #include <sys/dsl_dataset.h>
49 #include <sys/zap_impl.h>
50 #include <sys/zap.h>
51 #include <sys/zap_leaf.h>
52
53 #include "osd_internal.h"
54
55 #define OSD_OTABLE_MAX_HASH             ((1ULL << 48) - 1)
56 #define OTABLE_PREFETCH                 256
57
58 #define DTO_INDEX_INSERT                1
59 #define DTO_INDEX_DELETE                2
60 #define DTO_INDEX_UPDATE                3
61
62 static inline bool osd_scrub_has_window(struct osd_otable_it *it)
63 {
64         return it->ooi_prefetched < OTABLE_PREFETCH;
65 }
66
67 /**
68  * update/insert/delete the specified OI mapping (@fid @id) according to the ops
69  *
70  * \retval   1, changed nothing
71  * \retval   0, changed successfully
72  * \retval -ve, on error
73  */
74 static int osd_scrub_refresh_mapping(const struct lu_env *env,
75                                      struct osd_device *dev,
76                                      const struct lu_fid *fid,
77                                      uint64_t oid, int ops,
78                                      bool force, const char *name)
79 {
80         struct osd_thread_info *info = osd_oti_get(env);
81         struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
82         char *buf = info->oti_str;
83         dmu_tx_t *tx = NULL;
84         dnode_t *dn = NULL;
85         uint64_t zapid;
86         int rc;
87         ENTRY;
88
89         if (dev->od_scrub.os_file.sf_param & SP_DRYRUN && !force)
90                 GOTO(log, rc = 0);
91
92         tx = dmu_tx_create(dev->od_os);
93         if (!tx)
94                 GOTO(log, rc = -ENOMEM);
95
96         zapid = osd_get_name_n_idx(env, dev, fid, buf,
97                                    sizeof(info->oti_str), &dn);
98         osd_tx_hold_zap(tx, zapid, dn,
99                         ops == DTO_INDEX_INSERT ? TRUE : FALSE, NULL);
100         rc = -dmu_tx_assign(tx, TXG_WAIT);
101         if (rc) {
102                 dmu_tx_abort(tx);
103                 GOTO(log, rc);
104         }
105
106         switch (ops) {
107         case DTO_INDEX_UPDATE:
108                 zde->zde_pad = 0;
109                 zde->zde_dnode = oid;
110                 zde->zde_type = 0; /* The type in OI mapping is useless. */
111                 rc = -zap_update(dev->od_os, zapid, buf, 8, sizeof(*zde) / 8,
112                                  zde, tx);
113                 if (unlikely(rc == -ENOENT)) {
114                         /* Some unlink thread may removed the OI mapping. */
115                         rc = 1;
116                 }
117                 break;
118         case DTO_INDEX_INSERT:
119                 zde->zde_pad = 0;
120                 zde->zde_dnode = oid;
121                 zde->zde_type = 0; /* The type in OI mapping is useless. */
122                 rc = osd_zap_add(dev, zapid, dn, buf, 8, sizeof(*zde) / 8,
123                                  zde, tx);
124                 if (unlikely(rc == -EEXIST))
125                         rc = 1;
126                 break;
127         case DTO_INDEX_DELETE:
128                 rc = osd_zap_remove(dev, zapid, dn, buf, tx);
129                 if (rc == -ENOENT) {
130                         /* It is normal that the unlink thread has removed the
131                          * OI mapping already. */
132                         rc = 1;
133                 }
134                 break;
135         default:
136                 LASSERTF(0, "Unexpected ops %d\n", ops);
137                 rc = -EINVAL;
138                 break;
139         }
140
141         dmu_tx_commit(tx);
142         GOTO(log, rc);
143
144 log:
145         CDEBUG(D_LFSCK, "%s: refresh OI map for scrub, op %d, force %s, "
146                DFID" => %llu (%s): rc = %d\n", osd_name(dev), ops,
147                force ? "yes" : "no", PFID(fid), oid, name ? name : "null", rc);
148
149         return rc;
150 }
151
152 static int
153 osd_scrub_check_update(const struct lu_env *env, struct osd_device *dev,
154                        const struct lu_fid *fid, uint64_t oid, int val)
155 {
156         struct lustre_scrub *scrub = &dev->od_scrub;
157         struct scrub_file *sf = &scrub->os_file;
158         struct osd_inconsistent_item *oii = NULL;
159         nvlist_t *nvbuf = NULL;
160         dnode_t *dn = NULL;
161         uint64_t oid2;
162         int ops = DTO_INDEX_UPDATE;
163         int rc;
164         ENTRY;
165
166         down_write(&scrub->os_rwsem);
167         scrub->os_new_checked++;
168         if (val < 0)
169                 GOTO(out, rc = val);
170
171         if (scrub->os_in_prior)
172                 oii = list_entry(scrub->os_inconsistent_items.next,
173                                  struct osd_inconsistent_item, oii_list);
174
175         if (oid < sf->sf_pos_latest_start && !oii)
176                 GOTO(out, rc = 0);
177
178         if (oii && oii->oii_insert) {
179                 ops = DTO_INDEX_INSERT;
180                 goto zget;
181         }
182
183         rc = osd_fid_lookup(env, dev, fid, &oid2);
184         if (rc) {
185                 if (rc != -ENOENT)
186                         GOTO(out, rc);
187
188                 ops = DTO_INDEX_INSERT;
189
190 zget:
191                 rc = __osd_obj2dnode(dev->od_os, oid, &dn);
192                 if (rc) {
193                         /* Someone removed the object by race. */
194                         if (rc == -ENOENT || rc == -EEXIST)
195                                 rc = 0;
196                         GOTO(out, rc);
197                 }
198
199                 scrub->os_full_speed = 1;
200                 sf->sf_flags |= SF_INCONSISTENT;
201         } else if (oid == oid2) {
202                 GOTO(out, rc = 0);
203         } else {
204                 struct lustre_mdt_attrs *lma = NULL;
205                 int size;
206
207                 rc = __osd_xattr_load_by_oid(dev, oid2, &nvbuf);
208                 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
209                         goto update;
210                 if (rc)
211                         GOTO(out, rc);
212
213                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
214                                                (uchar_t **)&lma, &size);
215                 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
216                         goto update;
217                 if (rc)
218                         GOTO(out, rc);
219
220                 lustre_lma_swab(lma);
221                 if (unlikely(lu_fid_eq(&lma->lma_self_fid, fid))) {
222                         CDEBUG(D_LFSCK, "%s: the FID "DFID" is used by "
223                                "two objects: %llu and %llu (in OI)\n",
224                                osd_name(dev), PFID(fid), oid, oid2);
225
226                         GOTO(out, rc = -EEXIST);
227                 }
228
229 update:
230                 scrub->os_full_speed = 1;
231                 sf->sf_flags |= SF_INCONSISTENT;
232         }
233
234         rc = osd_scrub_refresh_mapping(env, dev, fid, oid, ops, false, NULL);
235         if (!rc) {
236                 if (scrub->os_in_prior)
237                         sf->sf_items_updated_prior++;
238                 else
239                         sf->sf_items_updated++;
240         }
241
242         GOTO(out, rc);
243
244 out:
245         if (nvbuf)
246                 nvlist_free(nvbuf);
247
248         if (rc < 0) {
249                 sf->sf_items_failed++;
250                 if (sf->sf_pos_first_inconsistent == 0 ||
251                     sf->sf_pos_first_inconsistent > oid)
252                         sf->sf_pos_first_inconsistent = oid;
253         } else {
254                 rc = 0;
255         }
256
257         /* There may be conflict unlink during the OI scrub,
258          * if happend, then remove the new added OI mapping. */
259         if (ops == DTO_INDEX_INSERT && dn && dn->dn_free_txg)
260                 osd_scrub_refresh_mapping(env, dev, fid, oid,
261                                           DTO_INDEX_DELETE, false, NULL);
262         up_write(&scrub->os_rwsem);
263
264         if (dn)
265                 osd_dnode_rele(dn);
266
267         if (oii) {
268                 spin_lock(&scrub->os_lock);
269                 if (likely(!list_empty(&oii->oii_list)))
270                         list_del(&oii->oii_list);
271                 spin_unlock(&scrub->os_lock);
272                 OBD_FREE_PTR(oii);
273         }
274
275         RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
276 }
277
278 static int osd_scrub_prep(const struct lu_env *env, struct osd_device *dev)
279 {
280         struct lustre_scrub *scrub = &dev->od_scrub;
281         struct ptlrpc_thread *thread = &scrub->os_thread;
282         struct scrub_file *sf = &scrub->os_file;
283         __u32 flags = scrub->os_start_flags;
284         int rc;
285         bool drop_dryrun = false;
286         ENTRY;
287
288         CDEBUG(D_LFSCK, "%s: OI scrub prep, flags = 0x%x\n",
289                scrub->os_name, flags);
290
291         down_write(&scrub->os_rwsem);
292         if (flags & SS_SET_FAILOUT)
293                 sf->sf_param |= SP_FAILOUT;
294         else if (flags & SS_CLEAR_FAILOUT)
295                 sf->sf_param &= ~SP_FAILOUT;
296
297         if (flags & SS_SET_DRYRUN) {
298                 sf->sf_param |= SP_DRYRUN;
299         } else if (flags & SS_CLEAR_DRYRUN && sf->sf_param & SP_DRYRUN) {
300                 sf->sf_param &= ~SP_DRYRUN;
301                 drop_dryrun = true;
302         }
303
304         if (flags & SS_RESET)
305                 scrub_file_reset(scrub, dev->od_uuid, 0);
306
307         scrub->os_partial_scan = 0;
308         if (flags & SS_AUTO_FULL) {
309                 scrub->os_full_speed = 1;
310                 sf->sf_flags |= SF_AUTO;
311         } else if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
312                                    SF_UPGRADE)) {
313                 scrub->os_full_speed = 1;
314         } else {
315                 scrub->os_full_speed = 0;
316         }
317
318         spin_lock(&scrub->os_lock);
319         scrub->os_in_prior = 0;
320         scrub->os_waiting = 0;
321         scrub->os_paused = 0;
322         scrub->os_in_join = 0;
323         scrub->os_full_scrub = 0;
324         spin_unlock(&scrub->os_lock);
325         scrub->os_new_checked = 0;
326         if (drop_dryrun && sf->sf_pos_first_inconsistent != 0)
327                 sf->sf_pos_latest_start = sf->sf_pos_first_inconsistent;
328         else if (sf->sf_pos_last_checkpoint != 0)
329                 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
330         else
331                 sf->sf_pos_latest_start = 1;
332
333         scrub->os_pos_current = sf->sf_pos_latest_start;
334         sf->sf_status = SS_SCANNING;
335         sf->sf_time_latest_start = ktime_get_real_seconds();
336         sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
337         sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
338         rc = scrub_file_store(env, scrub);
339         if (!rc) {
340                 spin_lock(&scrub->os_lock);
341                 thread_set_flags(thread, SVC_RUNNING);
342                 spin_unlock(&scrub->os_lock);
343                 wake_up_all(&thread->t_ctl_waitq);
344         }
345         up_write(&scrub->os_rwsem);
346
347         RETURN(rc);
348 }
349
350 static int osd_scrub_post(const struct lu_env *env, struct osd_device *dev,
351                           int result)
352 {
353         struct lustre_scrub *scrub = &dev->od_scrub;
354         struct scrub_file *sf = &scrub->os_file;
355         int rc;
356         ENTRY;
357
358         CDEBUG(D_LFSCK, "%s: OI scrub post with result = %d\n",
359                scrub->os_name, result);
360
361         down_write(&scrub->os_rwsem);
362         spin_lock(&scrub->os_lock);
363         thread_set_flags(&scrub->os_thread, SVC_STOPPING);
364         spin_unlock(&scrub->os_lock);
365         if (scrub->os_new_checked > 0) {
366                 sf->sf_items_checked += scrub->os_new_checked;
367                 scrub->os_new_checked = 0;
368                 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
369         }
370         sf->sf_time_last_checkpoint = ktime_get_real_seconds();
371         if (result > 0) {
372                 sf->sf_status = SS_COMPLETED;
373                 if (!(sf->sf_param & SP_DRYRUN)) {
374                         memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
375                         sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
376                                           SF_UPGRADE | SF_AUTO);
377                 }
378                 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
379                 sf->sf_success_count++;
380         } else if (result == 0) {
381                 if (scrub->os_paused)
382                         sf->sf_status = SS_PAUSED;
383                 else
384                         sf->sf_status = SS_STOPPED;
385         } else {
386                 sf->sf_status = SS_FAILED;
387         }
388         sf->sf_run_time += ktime_get_seconds() -
389                            scrub->os_time_last_checkpoint;
390
391         rc = scrub_file_store(env, scrub);
392         up_write(&scrub->os_rwsem);
393
394         RETURN(rc < 0 ? rc : result);
395 }
396
397 /* iteration engine */
398
399 static inline int
400 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
401 {
402         spin_lock(&scrub->os_lock);
403         if (osd_scrub_has_window(it) ||
404             !list_empty(&scrub->os_inconsistent_items) ||
405             it->ooi_waiting || !thread_is_running(&scrub->os_thread))
406                 scrub->os_waiting = 0;
407         else
408                 scrub->os_waiting = 1;
409         spin_unlock(&scrub->os_lock);
410
411         return !scrub->os_waiting;
412 }
413
414 static int osd_scrub_next(const struct lu_env *env, struct osd_device *dev,
415                           struct lu_fid *fid, uint64_t *oid)
416 {
417         struct l_wait_info lwi = { 0 };
418         struct lustre_scrub *scrub = &dev->od_scrub;
419         struct ptlrpc_thread *thread = &scrub->os_thread;
420         struct osd_otable_it *it = dev->od_otable_it;
421         struct lustre_mdt_attrs *lma = NULL;
422         nvlist_t *nvbuf = NULL;
423         int size = 0;
424         int rc = 0;
425         ENTRY;
426
427         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
428                 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
429                 if (likely(lwi.lwi_timeout > 0)) {
430                         l_wait_event(thread->t_ctl_waitq,
431                                 !list_empty(&scrub->os_inconsistent_items) ||
432                                 !thread_is_running(thread),
433                                 &lwi);
434                         if (unlikely(!thread_is_running(thread)))
435                                 RETURN(SCRUB_NEXT_EXIT);
436                 }
437         }
438
439         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
440                 spin_lock(&scrub->os_lock);
441                 thread_set_flags(thread, SVC_STOPPING);
442                 spin_unlock(&scrub->os_lock);
443                 RETURN(SCRUB_NEXT_CRASH);
444         }
445
446         if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
447                 RETURN(SCRUB_NEXT_FATAL);
448
449 again:
450         if (nvbuf) {
451                 nvlist_free(nvbuf);
452                 nvbuf = NULL;
453                 lma = NULL;
454         }
455
456         if (!list_empty(&scrub->os_inconsistent_items)) {
457                 spin_lock(&scrub->os_lock);
458                 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
459                         struct osd_inconsistent_item *oii;
460
461                         oii = list_entry(scrub->os_inconsistent_items.next,
462                                 struct osd_inconsistent_item, oii_list);
463                         *fid = oii->oii_cache.oic_fid;
464                         *oid = oii->oii_cache.oic_dnode;
465                         scrub->os_in_prior = 1;
466                         spin_unlock(&scrub->os_lock);
467
468                         GOTO(out, rc = 0);
469                 }
470                 spin_unlock(&scrub->os_lock);
471         }
472
473         if (!scrub->os_full_speed && !osd_scrub_has_window(it))
474                 wait_event_idle(thread->t_ctl_waitq,
475                                 osd_scrub_wakeup(scrub, it));
476
477         if (unlikely(!thread_is_running(thread)))
478                 GOTO(out, rc = SCRUB_NEXT_EXIT);
479
480         rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
481         if (rc)
482                 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
483
484         rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
485         if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
486                 goto again;
487
488         if (rc)
489                 GOTO(out, rc);
490
491         LASSERT(nvbuf != NULL);
492         rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
493                                        (uchar_t **)&lma, &size);
494         if (!rc) {
495                 lustre_lma_swab(lma);
496                 if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
497                            !(lma->lma_incompat & LMAI_AGENT))) {
498                         *fid = lma->lma_self_fid;
499                         *oid = scrub->os_pos_current;
500
501                         GOTO(out, rc = 0);
502                 }
503         }
504
505         if (!scrub->os_full_speed) {
506                 spin_lock(&scrub->os_lock);
507                 it->ooi_prefetched++;
508                 if (it->ooi_waiting) {
509                         it->ooi_waiting = 0;
510                         wake_up_all(&thread->t_ctl_waitq);
511                 }
512                 spin_unlock(&scrub->os_lock);
513         }
514
515         goto again;
516
517 out:
518         if (nvbuf)
519                 nvlist_free(nvbuf);
520
521         return rc;
522 }
523
524 static int osd_scrub_exec(const struct lu_env *env, struct osd_device *dev,
525                           const struct lu_fid *fid, uint64_t oid, int rc)
526 {
527         struct lustre_scrub *scrub = &dev->od_scrub;
528         struct ptlrpc_thread *thread = &scrub->os_thread;
529         struct osd_otable_it *it = dev->od_otable_it;
530
531         rc = osd_scrub_check_update(env, dev, fid, oid, rc);
532         if (!scrub->os_in_prior) {
533                 if (!scrub->os_full_speed) {
534                         spin_lock(&scrub->os_lock);
535                         it->ooi_prefetched++;
536                         if (it->ooi_waiting) {
537                                 it->ooi_waiting = 0;
538                                 wake_up_all(&thread->t_ctl_waitq);
539                         }
540                         spin_unlock(&scrub->os_lock);
541                 }
542         } else {
543                 scrub->os_in_prior = 0;
544         }
545
546         if (rc)
547                 return rc;
548
549         rc = scrub_checkpoint(env, scrub);
550         if (rc) {
551                 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
552                        "rc = %d\n", scrub->os_name, scrub->os_pos_current, rc);
553                 /* Continue, as long as the scrub itself can go ahead. */
554         }
555
556         return 0;
557 }
558
559 static int osd_scrub_main(void *args)
560 {
561         struct lu_env env;
562         struct osd_device *dev = (struct osd_device *)args;
563         struct lustre_scrub *scrub = &dev->od_scrub;
564         struct ptlrpc_thread *thread = &scrub->os_thread;
565         struct lu_fid *fid;
566         uint64_t oid;
567         int rc = 0;
568         ENTRY;
569
570         rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
571         if (rc) {
572                 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
573                        scrub->os_name, rc);
574                 GOTO(noenv, rc);
575         }
576
577         rc = osd_scrub_prep(&env, dev);
578         if (rc) {
579                 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
580                        scrub->os_name, rc);
581                 GOTO(out, rc);
582         }
583
584         if (!scrub->os_full_speed) {
585                 struct osd_otable_it *it = dev->od_otable_it;
586
587                 wait_event_idle(thread->t_ctl_waitq,
588                                 it->ooi_user_ready ||
589                                 !thread_is_running(thread));
590
591                 if (unlikely(!thread_is_running(thread)))
592                         GOTO(post, rc = 0);
593
594                 scrub->os_pos_current = it->ooi_pos;
595         }
596
597         CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
598                scrub->os_name, scrub->os_start_flags,
599                scrub->os_pos_current);
600
601         fid = &osd_oti_get(&env)->oti_fid;
602         while (!rc && thread_is_running(thread)) {
603                 rc = osd_scrub_next(&env, dev, fid, &oid);
604                 switch (rc) {
605                 case SCRUB_NEXT_EXIT:
606                         GOTO(post, rc = 0);
607                 case SCRUB_NEXT_CRASH:
608                         spin_lock(&scrub->os_lock);
609                         thread_set_flags(&scrub->os_thread, SVC_STOPPING);
610                         spin_unlock(&scrub->os_lock);
611                         GOTO(out, rc = -EINVAL);
612                 case SCRUB_NEXT_FATAL:
613                         GOTO(post, rc = -EINVAL);
614                 case SCRUB_NEXT_BREAK:
615                         GOTO(post, rc = 1);
616                 }
617
618                 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
619         }
620
621         GOTO(post, rc);
622
623 post:
624         rc = osd_scrub_post(&env, dev, rc);
625         CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
626                scrub->os_name, scrub->os_pos_current, rc);
627
628 out:
629         while (!list_empty(&scrub->os_inconsistent_items)) {
630                 struct osd_inconsistent_item *oii;
631
632                 oii = list_entry(scrub->os_inconsistent_items.next,
633                                  struct osd_inconsistent_item, oii_list);
634                 list_del_init(&oii->oii_list);
635                 OBD_FREE_PTR(oii);
636         }
637
638         lu_env_fini(&env);
639
640 noenv:
641         spin_lock(&scrub->os_lock);
642         thread_set_flags(thread, SVC_STOPPED);
643         wake_up_all(&thread->t_ctl_waitq);
644         spin_unlock(&scrub->os_lock);
645         return rc;
646 }
647
648 /* initial OI scrub */
649
650 struct osd_lf_map;
651
652 typedef int (*handle_dirent_t)(const struct lu_env *, struct osd_device *,
653                                const char *, uint64_t, uint64_t,
654                                enum osd_lf_flags, bool);
655 static int osd_ios_varfid_hd(const struct lu_env *, struct osd_device *,
656                              const char *, uint64_t, uint64_t,
657                              enum osd_lf_flags, bool);
658 static int osd_ios_uld_hd(const struct lu_env *, struct osd_device *,
659                           const char *, uint64_t, uint64_t,
660                           enum osd_lf_flags, bool);
661
662 typedef int (*scan_dir_t)(const struct lu_env *, struct osd_device *,
663                           uint64_t, handle_dirent_t, enum osd_lf_flags);
664 static int osd_ios_general_sd(const struct lu_env *, struct osd_device *,
665                               uint64_t, handle_dirent_t, enum osd_lf_flags);
666 static int osd_ios_ROOT_sd(const struct lu_env *, struct osd_device *,
667                            uint64_t, handle_dirent_t, enum osd_lf_flags);
668
669 struct osd_lf_map {
670         char                    *olm_name;
671         struct lu_fid            olm_fid;
672         enum osd_lf_flags        olm_flags;
673         scan_dir_t               olm_scan_dir;
674         handle_dirent_t          olm_handle_dirent;
675 };
676
677 /* Add the new introduced local files in the list in the future. */
678 static const struct osd_lf_map osd_lf_maps[] = {
679         /* CONFIGS */
680         {
681                 .olm_name               = MOUNT_CONFIGS_DIR,
682                 .olm_fid                = {
683                         .f_seq  = FID_SEQ_LOCAL_FILE,
684                         .f_oid  = MGS_CONFIGS_OID,
685                 },
686                 .olm_flags              = OLF_SCAN_SUBITEMS,
687                 .olm_scan_dir           = osd_ios_general_sd,
688                 .olm_handle_dirent      = osd_ios_varfid_hd,
689         },
690
691         /* NIDTBL_VERSIONS */
692         {
693                 .olm_name               = MGS_NIDTBL_DIR,
694                 .olm_flags              = OLF_SCAN_SUBITEMS,
695                 .olm_scan_dir           = osd_ios_general_sd,
696                 .olm_handle_dirent      = osd_ios_varfid_hd,
697         },
698
699         /* PENDING */
700         {
701                 .olm_name               = MDT_ORPHAN_DIR,
702         },
703
704         /* ROOT */
705         {
706                 .olm_name               = "ROOT",
707                 .olm_fid                = {
708                         .f_seq  = FID_SEQ_ROOT,
709                         .f_oid  = FID_OID_ROOT,
710                 },
711                 .olm_flags              = OLF_SCAN_SUBITEMS,
712                 .olm_scan_dir           = osd_ios_ROOT_sd,
713         },
714
715         /* fld */
716         {
717                 .olm_name               = "fld",
718                 .olm_fid                = {
719                         .f_seq  = FID_SEQ_LOCAL_FILE,
720                         .f_oid  = FLD_INDEX_OID,
721                 },
722         },
723
724         /* changelog_catalog */
725         {
726                 .olm_name               = CHANGELOG_CATALOG,
727         },
728
729         /* changelog_users */
730         {
731                 .olm_name               = CHANGELOG_USERS,
732         },
733
734         /* quota_master */
735         {
736                 .olm_name               = QMT_DIR,
737                 .olm_flags              = OLF_SCAN_SUBITEMS,
738                 .olm_scan_dir           = osd_ios_general_sd,
739                 .olm_handle_dirent      = osd_ios_varfid_hd,
740         },
741
742         /* quota_slave */
743         {
744                 .olm_name               = QSD_DIR,
745                 .olm_flags              = OLF_SCAN_SUBITEMS,
746                 .olm_scan_dir           = osd_ios_general_sd,
747                 .olm_handle_dirent      = osd_ios_varfid_hd,
748         },
749
750         /* LFSCK */
751         {
752                 .olm_name               = LFSCK_DIR,
753                 .olm_flags              = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
754                 .olm_scan_dir           = osd_ios_general_sd,
755                 .olm_handle_dirent      = osd_ios_varfid_hd,
756         },
757
758         /* lfsck_bookmark */
759         {
760                 .olm_name               = LFSCK_BOOKMARK,
761         },
762
763         /* lfsck_layout */
764         {
765                 .olm_name               = LFSCK_LAYOUT,
766         },
767
768         /* lfsck_namespace */
769         {
770                 .olm_name               = LFSCK_NAMESPACE,
771         },
772
773         /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
774          * and f_oid = index for their log files.  See lu_update_log{_dir}_fid()
775          * for more details. */
776
777         /* update_log */
778         {
779                 .olm_name               = "update_log",
780                 .olm_fid                = {
781                         .f_seq  = FID_SEQ_UPDATE_LOG,
782                 },
783                 .olm_flags              = OLF_IDX_IN_FID,
784         },
785
786         /* update_log_dir */
787         {
788                 .olm_name               = "update_log_dir",
789                 .olm_fid        = {
790                         .f_seq  = FID_SEQ_UPDATE_LOG_DIR,
791                 },
792                 .olm_flags              = OLF_SCAN_SUBITEMS | OLF_IDX_IN_FID,
793                 .olm_scan_dir           = osd_ios_general_sd,
794                 .olm_handle_dirent      = osd_ios_uld_hd,
795         },
796
797         /* hsm_actions */
798         {
799                 .olm_name               = HSM_ACTIONS,
800         },
801
802         /* nodemap */
803         {
804                 .olm_name               = LUSTRE_NODEMAP_NAME,
805         },
806
807         /* index_backup */
808         {
809                 .olm_name               = INDEX_BACKUP_DIR,
810                 .olm_fid                = {
811                         .f_seq  = FID_SEQ_LOCAL_FILE,
812                         .f_oid  = INDEX_BACKUP_OID,
813                 },
814                 .olm_flags              = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
815                 .olm_scan_dir           = osd_ios_general_sd,
816                 .olm_handle_dirent      = osd_ios_varfid_hd,
817         },
818
819         {
820                 .olm_name               = NULL
821         }
822 };
823
824 /* Add the new introduced files under .lustre/ in the list in the future. */
825 static const struct osd_lf_map osd_dl_maps[] = {
826         /* .lustre/fid */
827         {
828                 .olm_name               = "fid",
829                 .olm_fid                = {
830                         .f_seq  = FID_SEQ_DOT_LUSTRE,
831                         .f_oid  = FID_OID_DOT_LUSTRE_OBF,
832                 },
833         },
834
835         /* .lustre/lost+found */
836         {
837                 .olm_name               = "lost+found",
838                 .olm_fid                = {
839                         .f_seq  = FID_SEQ_DOT_LUSTRE,
840                         .f_oid  = FID_OID_DOT_LUSTRE_LPF,
841                 },
842         },
843
844         {
845                 .olm_name               = NULL
846         }
847 };
848
849 struct osd_ios_item {
850         struct list_head        oii_list;
851         uint64_t                oii_parent;
852         enum osd_lf_flags       oii_flags;
853         scan_dir_t              oii_scan_dir;
854         handle_dirent_t         oii_handle_dirent;
855 };
856
857 static int osd_ios_new_item(struct osd_device *dev, uint64_t parent,
858                             enum osd_lf_flags flags, scan_dir_t scan_dir,
859                             handle_dirent_t handle_dirent)
860 {
861         struct osd_ios_item *item;
862
863         OBD_ALLOC_PTR(item);
864         if (!item) {
865                 CWARN("%s: initial OI scrub failed to add item for %llu\n",
866                       osd_name(dev), parent);
867                 return -ENOMEM;
868         }
869
870         INIT_LIST_HEAD(&item->oii_list);
871         item->oii_parent = parent;
872         item->oii_flags = flags;
873         item->oii_scan_dir = scan_dir;
874         item->oii_handle_dirent = handle_dirent;
875         list_add_tail(&item->oii_list, &dev->od_ios_list);
876
877         return 0;
878 }
879
880 static bool osd_index_need_recreate(const struct lu_env *env,
881                                     struct osd_device *dev, uint64_t oid)
882 {
883         struct osd_thread_info *info = osd_oti_get(env);
884         zap_attribute_t *za = &info->oti_za2;
885         zap_cursor_t *zc = &info->oti_zc2;
886         int rc;
887         ENTRY;
888
889         zap_cursor_init_serialized(zc, dev->od_os, oid, 0);
890         rc = -zap_cursor_retrieve(zc, za);
891         zap_cursor_fini(zc);
892         if (rc && rc != -ENOENT)
893                 RETURN(true);
894
895         RETURN(false);
896 }
897
898 static void osd_ios_index_register(const struct lu_env *env,
899                                    struct osd_device *osd,
900                                    const struct lu_fid *fid, uint64_t oid)
901 {
902         struct osd_thread_info *info = osd_oti_get(env);
903         zap_attribute_t *za = &info->oti_za2;
904         zap_cursor_t *zc = &info->oti_zc2;
905         struct zap_leaf_entry *le;
906         dnode_t *dn = NULL;
907         sa_handle_t *hdl;
908         __u64 mode = 0;
909         __u32 keysize = 0;
910         __u32 recsize = 0;
911         int rc;
912         ENTRY;
913
914         rc = __osd_obj2dnode(osd->od_os, oid, &dn);
915         if (rc == -EEXIST || rc == -ENOENT)
916                 RETURN_EXIT;
917
918         if (rc < 0)
919                 GOTO(log, rc);
920
921         if (!osd_object_is_zap(dn))
922                 GOTO(log, rc = 1);
923
924         rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &hdl);
925         if (rc)
926                 GOTO(log, rc);
927
928         rc = -sa_lookup(hdl, SA_ZPL_MODE(osd), &mode, sizeof(mode));
929         sa_handle_destroy(hdl);
930         if (rc)
931                 GOTO(log, rc);
932
933         if (!S_ISREG(mode))
934                 GOTO(log, rc = 1);
935
936         zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
937         rc = -zap_cursor_retrieve(zc, za);
938         if (rc)
939                 /* Skip empty index object */
940                 GOTO(fini, rc = (rc == -ENOENT ? 1 : rc));
941
942         if (zc->zc_zap->zap_ismicro ||
943             !(zap_f_phys(zc->zc_zap)->zap_flags & ZAP_FLAG_UINT64_KEY))
944                 GOTO(fini, rc = 1);
945
946         le = ZAP_LEAF_ENTRY(zc->zc_leaf, 0);
947         keysize = le->le_name_numints * 8;
948         recsize = za->za_integer_length * za->za_num_integers;
949         if (likely(keysize && recsize))
950                 rc = osd_index_register(osd, fid, keysize, recsize);
951
952         GOTO(fini, rc);
953
954 fini:
955         zap_cursor_fini(zc);
956
957 log:
958         if (dn)
959                 osd_dnode_rele(dn);
960         if (rc < 0)
961                 CWARN("%s: failed to register index "DFID" (%u/%u): rc = %d\n",
962                       osd_name(osd), PFID(fid), keysize, recsize, rc);
963         else if (!rc)
964                 CDEBUG(D_LFSCK, "%s: registered index "DFID" (%u/%u)\n",
965                        osd_name(osd), PFID(fid), keysize, recsize);
966 }
967
968 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
969                               struct lustre_index_restore_unit *liru, void *buf,
970                               int bufsize)
971 {
972         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
973         struct lu_fid *tgt_fid = &liru->liru_cfid;
974         struct lu_fid bak_fid;
975         int rc;
976         ENTRY;
977
978         lustre_fid2lbx(buf, tgt_fid, bufsize);
979         rc = -zap_lookup(dev->od_os, dev->od_index_backup_id, buf, 8,
980                          sizeof(*zde) / 8, (void *)zde);
981         if (rc)
982                 GOTO(log, rc);
983
984         rc = osd_get_fid_by_oid(env, dev, zde->lzd_reg.zde_dnode, &bak_fid);
985         if (rc)
986                 GOTO(log, rc);
987
988         /* The OI mapping for index may be invalid, since it will be
989          * re-created, not update the OI mapping, just cache it in RAM. */
990         rc = osd_idc_find_and_init_with_oid(env, dev, tgt_fid,
991                                             liru->liru_clid);
992         if (!rc)
993                 rc = lustre_index_restore(env, &dev->od_dt_dev,
994                                 &liru->liru_pfid, tgt_fid, &bak_fid,
995                                 liru->liru_name, &dev->od_index_backup_list,
996                                 &dev->od_lock, buf, bufsize);
997         GOTO(log, rc);
998
999 log:
1000         CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
1001                osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
1002 }
1003
1004 /**
1005  * verify FID-in-LMA and OI entry for one object
1006  *
1007  * ios: Initial OI Scrub.
1008  */
1009 static int osd_ios_scan_one(const struct lu_env *env, struct osd_device *dev,
1010                             const struct lu_fid *fid, uint64_t parent,
1011                             uint64_t oid, const char *name,
1012                             enum osd_lf_flags flags)
1013 {
1014         struct lustre_scrub *scrub = &dev->od_scrub;
1015         struct scrub_file *sf = &scrub->os_file;
1016         struct lustre_mdt_attrs *lma = NULL;
1017         nvlist_t *nvbuf = NULL;
1018         struct lu_fid tfid;
1019         uint64_t oid2 = 0;
1020         __u64 flag = 0;
1021         int size = 0;
1022         int op = 0;
1023         int rc;
1024         ENTRY;
1025
1026         rc = __osd_xattr_load_by_oid(dev, oid, &nvbuf);
1027         if (unlikely(rc == -ENOENT || rc == -EEXIST))
1028                 RETURN(0);
1029
1030         if (rc && rc != -ENODATA) {
1031                 CWARN("%s: initial OI scrub failed to get lma for %llu: "
1032                       "rc = %d\n", osd_name(dev), oid, rc);
1033
1034                 RETURN(rc);
1035         }
1036
1037         if (!rc) {
1038                 LASSERT(nvbuf != NULL);
1039                 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1040                                                (uchar_t **)&lma, &size);
1041                 if (rc || size == 0) {
1042                         LASSERT(lma == NULL);
1043                         rc = -ENODATA;
1044                 } else {
1045                         LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1046                         lustre_lma_swab(lma);
1047                         if (lma->lma_compat & LMAC_NOT_IN_OI) {
1048                                 nvlist_free(nvbuf);
1049                                 RETURN(0);
1050                         }
1051
1052                         if (lma->lma_compat & LMAC_IDX_BACKUP &&
1053                             osd_index_need_recreate(env, dev, oid)) {
1054                                 if (parent == dev->od_root) {
1055                                         lu_local_obj_fid(&tfid,
1056                                                          OSD_FS_ROOT_OID);
1057                                 } else {
1058                                         rc = osd_get_fid_by_oid(env, dev,
1059                                                                 parent, &tfid);
1060                                         if (rc) {
1061                                                 nvlist_free(nvbuf);
1062                                                 RETURN(rc);
1063                                         }
1064                                 }
1065
1066                                 rc = lustre_liru_new(
1067                                                 &dev->od_index_restore_list,
1068                                                 &tfid, &lma->lma_self_fid, oid,
1069                                                 name, strlen(name));
1070                                 nvlist_free(nvbuf);
1071                                 RETURN(rc);
1072                         }
1073
1074                         tfid = lma->lma_self_fid;
1075                         if (!(flags & OLF_NOT_BACKUP))
1076                                 osd_ios_index_register(env, dev, &tfid, oid);
1077                 }
1078                 nvlist_free(nvbuf);
1079         }
1080
1081         if (rc == -ENODATA) {
1082                 if (!fid) {
1083                         /* Skip the object without FID-in-LMA */
1084                         CDEBUG(D_LFSCK, "%s: %llu has no FID-in-LMA, skip it\n",
1085                                osd_name(dev), oid);
1086
1087                         RETURN(0);
1088                 }
1089
1090                 LASSERT(!fid_is_zero(fid));
1091
1092                 tfid = *fid;
1093                 if (flags & OLF_IDX_IN_FID) {
1094                         LASSERT(dev->od_index >= 0);
1095
1096                         tfid.f_oid = dev->od_index;
1097                 }
1098         }
1099
1100         rc = osd_fid_lookup(env, dev, &tfid, &oid2);
1101         if (rc) {
1102                 if (rc != -ENOENT) {
1103                         CWARN("%s: initial OI scrub failed to lookup fid for "
1104                               DFID"=>%llu: rc = %d\n",
1105                               osd_name(dev), PFID(&tfid), oid, rc);
1106
1107                         RETURN(rc);
1108                 }
1109
1110                 flag = SF_RECREATED;
1111                 op = DTO_INDEX_INSERT;
1112         } else {
1113                 if (oid == oid2)
1114                         RETURN(0);
1115
1116                 flag = SF_INCONSISTENT;
1117                 op = DTO_INDEX_UPDATE;
1118         }
1119
1120         if (!(sf->sf_flags & flag)) {
1121                 scrub_file_reset(scrub, dev->od_uuid, flag);
1122                 rc = scrub_file_store(env, scrub);
1123                 if (rc)
1124                         RETURN(rc);
1125         }
1126
1127         rc = osd_scrub_refresh_mapping(env, dev, &tfid, oid, op, true, name);
1128
1129         RETURN(rc > 0 ? 0 : rc);
1130 }
1131
1132 static int osd_ios_varfid_hd(const struct lu_env *env, struct osd_device *dev,
1133                              const char *name, uint64_t parent, uint64_t oid,
1134                              enum osd_lf_flags flags, bool is_dir)
1135 {
1136         int rc;
1137         ENTRY;
1138
1139         rc = osd_ios_scan_one(env, dev, NULL, parent, oid, name, 0);
1140         if (!rc && is_dir)
1141                 rc = osd_ios_new_item(dev, oid, flags, osd_ios_general_sd,
1142                                       osd_ios_varfid_hd);
1143
1144         RETURN(rc);
1145 }
1146
1147 static int osd_ios_uld_hd(const struct lu_env *env, struct osd_device *dev,
1148                           const char *name, uint64_t parent, uint64_t oid,
1149                           enum osd_lf_flags flags, bool is_dir)
1150 {
1151         struct lu_fid tfid;
1152         int rc;
1153         ENTRY;
1154
1155         /* skip any non-DFID format name */
1156         if (name[0] != '[')
1157                 RETURN(0);
1158
1159         /* skip the start '[' */
1160         sscanf(&name[1], SFID, RFID(&tfid));
1161         if (fid_is_sane(&tfid))
1162                 rc = osd_ios_scan_one(env, dev, &tfid, parent, oid, name, 0);
1163         else
1164                 rc = -EIO;
1165
1166         RETURN(rc);
1167 }
1168
1169 /*
1170  * General scanner for the directories execpt /ROOT during initial OI scrub.
1171  * It scans the name entries under the given directory one by one. For each
1172  * entry, verifies its OI mapping via the given @handle_dirent.
1173  */
1174 static int osd_ios_general_sd(const struct lu_env *env, struct osd_device *dev,
1175                               uint64_t parent, handle_dirent_t handle_dirent,
1176                               enum osd_lf_flags flags)
1177 {
1178         struct osd_thread_info *info = osd_oti_get(env);
1179         struct luz_direntry *zde = &info->oti_zde;
1180         zap_attribute_t *za = &info->oti_za;
1181         zap_cursor_t *zc = &info->oti_zc;
1182         int rc;
1183         ENTRY;
1184
1185         zap_cursor_init_serialized(zc, dev->od_os, parent, 0);
1186         rc = -zap_cursor_retrieve(zc, za);
1187         if (rc == -ENOENT)
1188                 zap_cursor_advance(zc);
1189         else if (rc)
1190                 GOTO(log, rc);
1191
1192         while (1) {
1193                 rc = -zap_cursor_retrieve(zc, za);
1194                 if (rc)
1195                         GOTO(log, rc = (rc == -ENOENT ? 0 : rc));
1196
1197                 /* skip the entry started with '.' */
1198                 if (likely(za->za_name[0] != '.')) {
1199                         rc = osd_zap_lookup(dev, parent, NULL, za->za_name,
1200                                         za->za_integer_length,
1201                                         sizeof(*zde) / za->za_integer_length,
1202                                         (void *)zde);
1203                         if (rc) {
1204                                 CWARN("%s: initial OI scrub failed to lookup "
1205                                       "%s under %llu: rc = %d\n",
1206                                       osd_name(dev), za->za_name, parent, rc);
1207                                 continue;
1208                         }
1209
1210                         rc = handle_dirent(env, dev, za->za_name, parent,
1211                                         zde->lzd_reg.zde_dnode, flags,
1212                                         S_ISDIR(DTTOIF(zde->lzd_reg.zde_type)) ?
1213                                         true : false);
1214                         CDEBUG(D_LFSCK, "%s: initial OI scrub handled %s under "
1215                                "%llu: rc = %d\n",
1216                                osd_name(dev), za->za_name, parent, rc);
1217                 }
1218
1219                 zap_cursor_advance(zc);
1220         }
1221
1222 log:
1223         if (rc)
1224                 CWARN("%s: initial OI scrub failed to scan the directory %llu: "
1225                       "rc = %d\n", osd_name(dev), parent, rc);
1226         zap_cursor_fini(zc);
1227
1228         return rc;
1229 }
1230
1231 /*
1232  * The scanner for /ROOT directory. It is not all the items under /ROOT will
1233  * be scanned during the initial OI scrub, instead, only the .lustre and the
1234  * sub-items under .lustre will be handled.
1235  */
1236 static int osd_ios_ROOT_sd(const struct lu_env *env, struct osd_device *dev,
1237                            uint64_t parent, handle_dirent_t handle_dirent,
1238                            enum osd_lf_flags flags)
1239 {
1240         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1241         const struct osd_lf_map *map;
1242         uint64_t oid;
1243         int rc;
1244         int rc1 = 0;
1245         ENTRY;
1246
1247         rc = osd_zap_lookup(dev, parent, NULL, dot_lustre_name, 8,
1248                             sizeof(*zde) / 8, (void *)zde);
1249         if (rc == -ENOENT) {
1250                 /* The .lustre directory is lost. That is not fatal. It can
1251                  * be re-created in the subsequent MDT start processing. */
1252                 RETURN(0);
1253         }
1254
1255         if (rc) {
1256                 CWARN("%s: initial OI scrub failed to find .lustre: "
1257                       "rc = %d\n", osd_name(dev), rc);
1258
1259                 RETURN(rc);
1260         }
1261
1262         oid = zde->lzd_reg.zde_dnode;
1263         rc = osd_ios_scan_one(env, dev, &LU_DOT_LUSTRE_FID, parent, oid,
1264                               dot_lustre_name, 0);
1265         if (rc)
1266                 RETURN(rc);
1267
1268         for (map = osd_dl_maps; map->olm_name; map++) {
1269                 rc = osd_zap_lookup(dev, oid, NULL, map->olm_name, 8,
1270                                     sizeof(*zde) / 8, (void *)zde);
1271                 if (rc) {
1272                         if (rc != -ENOENT)
1273                                 CWARN("%s: initial OI scrub failed to find"
1274                                       "the entry %s under .lustre: rc = %d\n",
1275                                       osd_name(dev), map->olm_name, rc);
1276                         else if (!fid_is_zero(&map->olm_fid))
1277                                 /* Try to remove the stale OI mapping. */
1278                                 osd_scrub_refresh_mapping(env, dev,
1279                                                 &map->olm_fid, 0,
1280                                                 DTO_INDEX_DELETE, true,
1281                                                 map->olm_name);
1282                         continue;
1283                 }
1284
1285                 rc = osd_ios_scan_one(env, dev, &map->olm_fid, oid,
1286                                       zde->lzd_reg.zde_dnode, map->olm_name,
1287                                       map->olm_flags);
1288                 if (rc)
1289                         rc1 = rc;
1290         }
1291
1292         RETURN(rc1);
1293 }
1294
1295 static void osd_initial_OI_scrub(const struct lu_env *env,
1296                                  struct osd_device *dev)
1297 {
1298         struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1299         const struct osd_lf_map *map;
1300         int rc;
1301         ENTRY;
1302
1303         for (map = osd_lf_maps; map->olm_name; map++) {
1304                 rc = osd_zap_lookup(dev, dev->od_root, NULL, map->olm_name, 8,
1305                                     sizeof(*zde) / 8, (void *)zde);
1306                 if (rc) {
1307                         if (rc != -ENOENT)
1308                                 CWARN("%s: initial OI scrub failed "
1309                                       "to find the entry %s: rc = %d\n",
1310                                       osd_name(dev), map->olm_name, rc);
1311                         else if (!fid_is_zero(&map->olm_fid))
1312                                 /* Try to remove the stale OI mapping. */
1313                                 osd_scrub_refresh_mapping(env, dev,
1314                                                 &map->olm_fid, 0,
1315                                                 DTO_INDEX_DELETE, true,
1316                                                 map->olm_name);
1317                         continue;
1318                 }
1319
1320                 rc = osd_ios_scan_one(env, dev, &map->olm_fid, dev->od_root,
1321                                       zde->lzd_reg.zde_dnode, map->olm_name,
1322                                       map->olm_flags);
1323                 if (!rc && map->olm_flags & OLF_SCAN_SUBITEMS)
1324                         osd_ios_new_item(dev, zde->lzd_reg.zde_dnode,
1325                                          map->olm_flags, map->olm_scan_dir,
1326                                          map->olm_handle_dirent);
1327         }
1328
1329         while (!list_empty(&dev->od_ios_list)) {
1330                 struct osd_ios_item *item;
1331
1332                 item = list_entry(dev->od_ios_list.next,
1333                                   struct osd_ios_item, oii_list);
1334                 list_del_init(&item->oii_list);
1335                 item->oii_scan_dir(env, dev, item->oii_parent,
1336                                    item->oii_handle_dirent, item->oii_flags);
1337                 OBD_FREE_PTR(item);
1338         }
1339
1340         if (!list_empty(&dev->od_index_restore_list)) {
1341                 char *buf;
1342
1343                 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1344                 if (!buf)
1345                         CERROR("%s: not enough RAM for rebuild index\n",
1346                                osd_name(dev));
1347
1348                 while (!list_empty(&dev->od_index_restore_list)) {
1349                         struct lustre_index_restore_unit *liru;
1350
1351                         liru = list_entry(dev->od_index_restore_list.next,
1352                                           struct lustre_index_restore_unit,
1353                                           liru_link);
1354                         list_del(&liru->liru_link);
1355                         if (buf)
1356                                 osd_index_restore(env, dev, liru, buf,
1357                                                   INDEX_BACKUP_BUFSIZE);
1358                         OBD_FREE(liru, liru->liru_len);
1359                 }
1360
1361                 if (buf)
1362                         OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1363         }
1364
1365         EXIT;
1366 }
1367
1368 /* OI scrub start/stop */
1369
1370 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
1371                     __u32 flags)
1372 {
1373         int rc;
1374         ENTRY;
1375
1376         if (dev->od_dt_dev.dd_rdonly)
1377                 RETURN(-EROFS);
1378
1379         /* od_otable_sem: prevent concurrent start/stop */
1380         down(&dev->od_otable_sem);
1381         rc = scrub_start(osd_scrub_main, &dev->od_scrub, dev, flags);
1382         up(&dev->od_otable_sem);
1383
1384         RETURN(rc == -EALREADY ? 0 : rc);
1385 }
1386
1387 void osd_scrub_stop(struct osd_device *dev)
1388 {
1389         struct lustre_scrub *scrub = &dev->od_scrub;
1390         ENTRY;
1391
1392         /* od_otable_sem: prevent concurrent start/stop */
1393         down(&dev->od_otable_sem);
1394         scrub->os_paused = 1;
1395         scrub_stop(scrub);
1396         up(&dev->od_otable_sem);
1397
1398         EXIT;
1399 }
1400
1401 /* OI scrub setup/cleanup */
1402
1403 static const char osd_scrub_name[] = "OI_scrub";
1404
1405 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
1406 {
1407         struct osd_thread_info *info = osd_oti_get(env);
1408         struct lustre_scrub *scrub = &dev->od_scrub;
1409         struct scrub_file *sf = &scrub->os_file;
1410         struct lu_fid *fid = &info->oti_fid;
1411         struct dt_object *obj;
1412         uint64_t oid;
1413         int rc = 0;
1414         bool dirty = false;
1415         ENTRY;
1416
1417         memcpy(dev->od_uuid.b,
1418                &dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid,
1419                sizeof(dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid));
1420         memset(&dev->od_scrub, 0, sizeof(struct lustre_scrub));
1421         init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
1422         init_rwsem(&scrub->os_rwsem);
1423         spin_lock_init(&scrub->os_lock);
1424         INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1425         scrub->os_name = osd_name(dev);
1426
1427         /* 'What the @fid is' is not imporatant, because the object
1428          * has no OI mapping, and only is visible inside the OSD.*/
1429         fid->f_seq = FID_SEQ_IGIF_MAX;
1430         if (dev->od_is_ost)
1431                 fid->f_oid = ((1 << 31) | dev->od_index) + 1;
1432         else
1433                 fid->f_oid = dev->od_index + 1;
1434         fid->f_ver = 0;
1435         rc = osd_obj_find_or_create(env, dev, dev->od_root,
1436                                     osd_scrub_name, &oid, fid, false);
1437         if (rc)
1438                 RETURN(rc);
1439
1440         rc = osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1441         if (rc)
1442                 RETURN(rc);
1443
1444         obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
1445         if (IS_ERR_OR_NULL(obj))
1446                 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
1447
1448         obj->do_body_ops = &osd_body_scrub_ops;
1449         scrub->os_obj = obj;
1450         rc = scrub_file_load(env, scrub);
1451         if (rc == -ENOENT || rc == -EFAULT) {
1452                 scrub_file_init(scrub, dev->od_uuid);
1453                 dirty = true;
1454         } else if (rc < 0) {
1455                 GOTO(cleanup_obj, rc);
1456         } else {
1457                 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
1458                         CDEBUG(D_LFSCK,
1459                                "%s: UUID has been changed from %pU to %pU\n",
1460                                osd_name(dev), &sf->sf_uuid, &dev->od_uuid);
1461                         scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1462                         dirty = true;
1463                 } else if (sf->sf_status == SS_SCANNING) {
1464                         sf->sf_status = SS_CRASHED;
1465                         dirty = true;
1466                 }
1467
1468                 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
1469                         LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
1470                                       osd_name(dev), sf->sf_oi_count,
1471                                       osd_oi_count);
1472                         sf->sf_oi_count = osd_oi_count;
1473                         dirty = true;
1474                 }
1475         }
1476
1477         if (sf->sf_pos_last_checkpoint != 0)
1478                 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1479         else
1480                 scrub->os_pos_current = 1;
1481
1482         if (dirty) {
1483                 rc = scrub_file_store(env, scrub);
1484                 if (rc)
1485                         GOTO(cleanup_obj, rc);
1486         }
1487
1488         /* Initialize OI files. */
1489         rc = osd_oi_init(env, dev);
1490         if (rc < 0)
1491                 GOTO(cleanup_obj, rc);
1492
1493         if (!dev->od_dt_dev.dd_rdonly)
1494                 osd_initial_OI_scrub(env, dev);
1495
1496         if (!dev->od_dt_dev.dd_rdonly &&
1497             dev->od_auto_scrub_interval != AS_NEVER &&
1498             ((sf->sf_status == SS_PAUSED) ||
1499              (sf->sf_status == SS_CRASHED &&
1500               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1501                               SF_UPGRADE | SF_AUTO)) ||
1502              (sf->sf_status == SS_INIT &&
1503               sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1504                               SF_UPGRADE))))
1505                 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1506
1507         if (rc)
1508                 GOTO(cleanup_oi, rc);
1509
1510         RETURN(0);
1511
1512 cleanup_oi:
1513         osd_oi_fini(env, dev);
1514 cleanup_obj:
1515         dt_object_put_nocache(env, scrub->os_obj);
1516         scrub->os_obj = NULL;
1517
1518         return rc;
1519 }
1520
1521 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1522 {
1523         struct lustre_scrub *scrub = &dev->od_scrub;
1524
1525         LASSERT(!dev->od_otable_it);
1526
1527         if (scrub->os_obj) {
1528                 osd_scrub_stop(dev);
1529                 dt_object_put_nocache(env, scrub->os_obj);
1530                 scrub->os_obj = NULL;
1531         }
1532
1533         if (dev->od_oi_table)
1534                 osd_oi_fini(env, dev);
1535 }
1536
1537 /* object table based iteration APIs */
1538
1539 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1540                                        struct dt_object *dt, __u32 attr)
1541 {
1542         enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1543         enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1544         struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1545         struct lustre_scrub *scrub = &dev->od_scrub;
1546         struct osd_otable_it *it;
1547         __u32 start = 0;
1548         int rc;
1549         ENTRY;
1550
1551         if (dev->od_dt_dev.dd_rdonly)
1552                 RETURN(ERR_PTR(-EROFS));
1553
1554         /* od_otable_sem: prevent concurrent init/fini */
1555         down(&dev->od_otable_sem);
1556         if (dev->od_otable_it)
1557                 GOTO(out, it = ERR_PTR(-EALREADY));
1558
1559         OBD_ALLOC_PTR(it);
1560         if (!it)
1561                 GOTO(out, it = ERR_PTR(-ENOMEM));
1562
1563         if (flags & DOIF_OUTUSED)
1564                 it->ooi_used_outside = 1;
1565
1566         if (flags & DOIF_RESET)
1567                 start |= SS_RESET;
1568
1569         if (valid & DOIV_ERROR_HANDLE) {
1570                 if (flags & DOIF_FAILOUT)
1571                         start |= SS_SET_FAILOUT;
1572                 else
1573                         start |= SS_CLEAR_FAILOUT;
1574         }
1575
1576         if (valid & DOIV_DRYRUN) {
1577                 if (flags & DOIF_DRYRUN)
1578                         start |= SS_SET_DRYRUN;
1579                 else
1580                         start |= SS_CLEAR_DRYRUN;
1581         }
1582
1583         /* XXX: dmu_object_next() does NOT find dnodes allocated
1584          *      in the current non-committed txg, so we force txg
1585          *      commit to find all existing dnodes ... */
1586         txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1587
1588         dev->od_otable_it = it;
1589         it->ooi_dev = dev;
1590         rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1591         if (rc == -EALREADY) {
1592                 it->ooi_pos = 1;
1593         } else if (rc < 0) {
1594                 dev->od_otable_it = NULL;
1595                 OBD_FREE_PTR(it);
1596                 it = ERR_PTR(rc);
1597         } else {
1598                 it->ooi_pos = scrub->os_pos_current;
1599         }
1600
1601         GOTO(out, it);
1602
1603 out:
1604         up(&dev->od_otable_sem);
1605         return (struct dt_it *)it;
1606 }
1607
1608 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1609 {
1610         struct osd_otable_it *it = (struct osd_otable_it *)di;
1611         struct osd_device *dev = it->ooi_dev;
1612
1613         /* od_otable_sem: prevent concurrent init/fini */
1614         down(&dev->od_otable_sem);
1615         scrub_stop(&dev->od_scrub);
1616         LASSERT(dev->od_otable_it == it);
1617
1618         dev->od_otable_it = NULL;
1619         up(&dev->od_otable_sem);
1620         OBD_FREE_PTR(it);
1621 }
1622
1623 static int osd_otable_it_get(const struct lu_env *env,
1624                              struct dt_it *di, const struct dt_key *key)
1625 {
1626         return 0;
1627 }
1628
1629 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1630 {
1631 }
1632
1633 static void osd_otable_it_preload(const struct lu_env *env,
1634                                   struct osd_otable_it *it)
1635 {
1636         struct osd_device *dev = it->ooi_dev;
1637         int rc;
1638
1639         /* can go negative on the very first access to the iterator
1640          * or if some non-Lustre objects were found */
1641         if (unlikely(it->ooi_prefetched < 0))
1642                 it->ooi_prefetched = 0;
1643
1644         if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1645                 return;
1646
1647         if (it->ooi_prefetched_dnode == 0)
1648                 it->ooi_prefetched_dnode = it->ooi_pos;
1649
1650         while (it->ooi_prefetched < OTABLE_PREFETCH) {
1651                 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1652                                       B_FALSE, 0);
1653                 if (rc)
1654                         break;
1655
1656                 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1657                                  0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1658                 it->ooi_prefetched++;
1659         }
1660 }
1661
1662 static inline int
1663 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
1664 {
1665         spin_lock(&scrub->os_lock);
1666         if (it->ooi_pos < scrub->os_pos_current || scrub->os_waiting ||
1667             !thread_is_running(&scrub->os_thread))
1668                 it->ooi_waiting = 0;
1669         else
1670                 it->ooi_waiting = 1;
1671         spin_unlock(&scrub->os_lock);
1672
1673         return !it->ooi_waiting;
1674 }
1675
1676 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1677 {
1678         struct osd_otable_it *it = (struct osd_otable_it *)di;
1679         struct osd_device *dev = it->ooi_dev;
1680         struct lustre_scrub *scrub = &dev->od_scrub;
1681         struct ptlrpc_thread *thread = &scrub->os_thread;
1682         struct lustre_mdt_attrs *lma = NULL;
1683         nvlist_t *nvbuf = NULL;
1684         int size = 0;
1685         int rc;
1686         ENTRY;
1687
1688         LASSERT(it->ooi_user_ready);
1689         fid_zero(&it->ooi_fid);
1690
1691         if (unlikely(it->ooi_all_cached))
1692                 RETURN(1);
1693
1694 again:
1695         if (nvbuf) {
1696                 nvlist_free(nvbuf);
1697                 nvbuf = NULL;
1698                 lma = NULL;
1699                 size = 0;
1700         }
1701
1702         if (it->ooi_pos >= scrub->os_pos_current)
1703                 wait_event_idle(thread->t_ctl_waitq,
1704                                 osd_otable_it_wakeup(scrub, it));
1705
1706         if (!thread_is_running(thread) && !it->ooi_used_outside)
1707                 GOTO(out, rc = 1);
1708
1709         rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1710         if (rc) {
1711                 if (unlikely(rc == -ESRCH)) {
1712                         it->ooi_all_cached = 1;
1713                         rc = 1;
1714                 }
1715
1716                 GOTO(out, rc);
1717         }
1718
1719         rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
1720
1721         if (!scrub->os_full_speed)
1722                 spin_lock(&scrub->os_lock);
1723         it->ooi_prefetched--;
1724         if (!scrub->os_full_speed) {
1725                 if (scrub->os_waiting) {
1726                         scrub->os_waiting = 0;
1727                         wake_up_all(&thread->t_ctl_waitq);
1728                 }
1729                 spin_unlock(&scrub->os_lock);
1730         }
1731
1732         if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
1733                 goto again;
1734
1735         if (rc)
1736                 GOTO(out, rc);
1737
1738         LASSERT(nvbuf != NULL);
1739         rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1740                                        (uchar_t **)&lma, &size);
1741         if (rc || size == 0)
1742                 /* It is either non-Lustre object or OSD internal object,
1743                  * ignore it, go ahead */
1744                 goto again;
1745
1746         LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1747         lustre_lma_swab(lma);
1748         if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI ||
1749                      lma->lma_incompat & LMAI_AGENT))
1750                 goto again;
1751
1752         it->ooi_fid = lma->lma_self_fid;
1753
1754         GOTO(out, rc = 0);
1755
1756 out:
1757         if (nvbuf)
1758                 nvlist_free(nvbuf);
1759
1760         if (!rc && scrub->os_full_speed)
1761                 osd_otable_it_preload(env, it);
1762
1763         return rc;
1764 }
1765
1766 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1767                                         const struct dt_it *di)
1768 {
1769         return NULL;
1770 }
1771
1772 static int osd_otable_it_key_size(const struct lu_env *env,
1773                                   const struct dt_it *di)
1774 {
1775         return sizeof(__u64);
1776 }
1777
1778 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1779                              struct dt_rec *rec, __u32 attr)
1780 {
1781         struct osd_otable_it *it  = (struct osd_otable_it *)di;
1782         struct lu_fid *fid = (struct lu_fid *)rec;
1783
1784         *fid = it->ooi_fid;
1785         return 0;
1786 }
1787
1788 static __u64 osd_otable_it_store(const struct lu_env *env,
1789                                  const struct dt_it *di)
1790 {
1791         struct osd_otable_it *it = (struct osd_otable_it *)di;
1792
1793         return it->ooi_pos;
1794 }
1795
1796 /**
1797  * Set the OSD layer iteration start position as the specified hash.
1798  */
1799 static int osd_otable_it_load(const struct lu_env *env,
1800                               const struct dt_it *di, __u64 hash)
1801 {
1802         struct osd_otable_it *it = (struct osd_otable_it *)di;
1803         struct osd_device *dev = it->ooi_dev;
1804         struct lustre_scrub *scrub = &dev->od_scrub;
1805         int rc;
1806         ENTRY;
1807
1808         /* Forbid to set iteration position after iteration started. */
1809         if (it->ooi_user_ready)
1810                 RETURN(-EPERM);
1811
1812         if (hash > OSD_OTABLE_MAX_HASH)
1813                 hash = OSD_OTABLE_MAX_HASH;
1814
1815         /* The hash is the last checkpoint position,
1816          * we will start from the next one. */
1817         it->ooi_pos = hash + 1;
1818         it->ooi_prefetched = 0;
1819         it->ooi_prefetched_dnode = 0;
1820         it->ooi_user_ready = 1;
1821         if (!scrub->os_full_speed)
1822                 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1823
1824         /* Unplug OSD layer iteration by the first next() call. */
1825         rc = osd_otable_it_next(env, (struct dt_it *)it);
1826
1827         RETURN(rc);
1828 }
1829
1830 static int osd_otable_it_key_rec(const struct lu_env *env,
1831                                  const struct dt_it *di, void *key_rec)
1832 {
1833         return 0;
1834 }
1835
1836 const struct dt_index_operations osd_otable_ops = {
1837         .dio_it = {
1838                 .init     = osd_otable_it_init,
1839                 .fini     = osd_otable_it_fini,
1840                 .get      = osd_otable_it_get,
1841                 .put      = osd_otable_it_put,
1842                 .next     = osd_otable_it_next,
1843                 .key      = osd_otable_it_key,
1844                 .key_size = osd_otable_it_key_size,
1845                 .rec      = osd_otable_it_rec,
1846                 .store    = osd_otable_it_store,
1847                 .load     = osd_otable_it_load,
1848                 .key_rec  = osd_otable_it_key_rec,
1849         }
1850 };
1851
1852 /* high priority inconsistent items list APIs */
1853
1854 int osd_oii_insert(const struct lu_env *env, struct osd_device *dev,
1855                    const struct lu_fid *fid, uint64_t oid, bool insert)
1856 {
1857         struct lustre_scrub *scrub = &dev->od_scrub;
1858         struct ptlrpc_thread *thread = &scrub->os_thread;
1859         struct osd_inconsistent_item *oii;
1860         bool wakeup = false;
1861         ENTRY;
1862
1863         osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1864         OBD_ALLOC_PTR(oii);
1865         if (unlikely(!oii))
1866                 RETURN(-ENOMEM);
1867
1868         INIT_LIST_HEAD(&oii->oii_list);
1869         oii->oii_cache.oic_dev = dev;
1870         oii->oii_cache.oic_fid = *fid;
1871         oii->oii_cache.oic_dnode = oid;
1872         oii->oii_insert = insert;
1873
1874         spin_lock(&scrub->os_lock);
1875         if (unlikely(!thread_is_running(thread))) {
1876                 spin_unlock(&scrub->os_lock);
1877                 OBD_FREE_PTR(oii);
1878                 RETURN(-EAGAIN);
1879         }
1880
1881         if (list_empty(&scrub->os_inconsistent_items))
1882                 wakeup = true;
1883         list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1884         spin_unlock(&scrub->os_lock);
1885
1886         if (wakeup)
1887                 wake_up_all(&thread->t_ctl_waitq);
1888
1889         RETURN(0);
1890 }
1891
1892 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1893                    uint64_t *oid)
1894 {
1895         struct lustre_scrub *scrub = &dev->od_scrub;
1896         struct osd_inconsistent_item *oii;
1897         int ret = -ENOENT;
1898         ENTRY;
1899
1900         spin_lock(&scrub->os_lock);
1901         list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1902                 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1903                         *oid = oii->oii_cache.oic_dnode;
1904                         ret = 0;
1905                         break;
1906                 }
1907         }
1908         spin_unlock(&scrub->os_lock);
1909
1910         RETURN(ret);
1911 }