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