4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2017, Intel Corporation.
26 * lustre/osd-zfs/osd_scrub.c
28 * Top-level entry points into osd module
30 * The OI scrub is used for rebuilding Object Index files when restores MDT from
33 * The otable based iterator scans ZFS objects to feed up layer LFSCK.
35 * Author: Fan Yong <fan.yong@intel.com>
38 #define DEBUG_SUBSYSTEM S_LFSCK
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>
51 #include <sys/zap_leaf.h>
53 #include "osd_internal.h"
55 #define OSD_OTABLE_MAX_HASH ((1ULL << 48) - 1)
56 #define OTABLE_PREFETCH 256
58 static inline bool osd_scrub_has_window(struct osd_otable_it *it)
60 return it->ooi_prefetched < OTABLE_PREFETCH;
64 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
66 * \retval 1, changed nothing
67 * \retval 0, changed successfully
68 * \retval -ve, on error
70 int osd_scrub_refresh_mapping(const struct lu_env *env,
71 struct osd_device *dev,
72 const struct lu_fid *fid,
73 uint64_t oid, enum dt_txn_op ops,
74 bool force, const char *name)
76 struct osd_thread_info *info = osd_oti_get(env);
77 struct zpl_direntry *zde = &info->oti_zde.lzd_reg;
78 char *buf = info->oti_str;
85 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN && !force)
88 tx = dmu_tx_create(dev->od_os);
90 GOTO(log, rc = -ENOMEM);
92 zapid = osd_get_name_n_idx(env, dev, fid, buf,
93 sizeof(info->oti_str), &dn);
94 osd_tx_hold_zap(tx, zapid, dn,
95 ops == DTO_INDEX_INSERT ? TRUE : FALSE, NULL);
96 rc = -dmu_tx_assign(tx, TXG_WAIT);
103 case DTO_INDEX_UPDATE:
105 zde->zde_dnode = oid;
106 zde->zde_type = 0; /* The type in OI mapping is useless. */
107 rc = -zap_update(dev->od_os, zapid, buf, 8, sizeof(*zde) / 8,
109 if (unlikely(rc == -ENOENT)) {
110 /* Some unlink thread may removed the OI mapping. */
114 case DTO_INDEX_INSERT:
116 zde->zde_dnode = oid;
117 zde->zde_type = 0; /* The type in OI mapping is useless. */
118 rc = osd_zap_add(dev, zapid, dn, buf, 8, sizeof(*zde) / 8,
120 if (unlikely(rc == -EEXIST))
123 case DTO_INDEX_DELETE:
124 rc = osd_zap_remove(dev, zapid, dn, buf, tx);
126 /* It is normal that the unlink thread has removed the
127 * OI mapping already. */
132 LASSERTF(0, "Unexpected ops %d\n", ops);
141 CDEBUG(D_LFSCK, "%s: refresh OI map for scrub, op %d, force %s, "
142 DFID" => %llu (%s): rc = %d\n", osd_name(dev), ops,
143 force ? "yes" : "no", PFID(fid), oid, name ? name : "null", rc);
149 osd_scrub_check_update(const struct lu_env *env, struct osd_device *dev,
150 const struct lu_fid *fid, uint64_t oid, int val)
152 struct lustre_scrub *scrub = &dev->od_scrub;
153 struct scrub_file *sf = &scrub->os_file;
154 struct osd_inconsistent_item *oii = NULL;
155 nvlist_t *nvbuf = NULL;
158 int ops = DTO_INDEX_UPDATE;
162 down_write(&scrub->os_rwsem);
163 scrub->os_new_checked++;
167 if (scrub->os_in_prior)
168 oii = list_entry(scrub->os_inconsistent_items.next,
169 struct osd_inconsistent_item, oii_list);
171 if (oid < sf->sf_pos_latest_start && !oii)
174 if (oii && oii->oii_insert) {
175 ops = DTO_INDEX_INSERT;
179 rc = osd_fid_lookup(env, dev, fid, &oid2);
184 ops = DTO_INDEX_INSERT;
187 rc = __osd_obj2dnode(dev->od_os, oid, &dn);
189 /* Someone removed the object by race. */
190 if (rc == -ENOENT || rc == -EEXIST)
195 spin_lock(&scrub->os_lock);
196 scrub->os_full_speed = 1;
197 spin_unlock(&scrub->os_lock);
199 sf->sf_flags |= SF_INCONSISTENT;
200 } else if (oid == oid2) {
203 struct lustre_mdt_attrs *lma = NULL;
206 rc = __osd_xattr_load_by_oid(dev, oid2, &nvbuf);
207 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
212 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
213 (uchar_t **)&lma, &size);
214 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
219 lustre_lma_swab(lma);
220 if (unlikely(lu_fid_eq(&lma->lma_self_fid, fid))) {
221 CDEBUG(D_LFSCK, "%s: the FID "DFID" is used by "
222 "two objects: %llu and %llu (in OI)\n",
223 osd_name(dev), PFID(fid), oid, oid2);
225 GOTO(out, rc = -EEXIST);
229 spin_lock(&scrub->os_lock);
230 scrub->os_full_speed = 1;
231 spin_unlock(&scrub->os_lock);
232 sf->sf_flags |= SF_INCONSISTENT;
235 rc = osd_scrub_refresh_mapping(env, dev, fid, oid, ops, false, NULL);
237 if (scrub->os_in_prior)
238 sf->sf_items_updated_prior++;
240 sf->sf_items_updated++;
250 sf->sf_items_failed++;
251 if (sf->sf_pos_first_inconsistent == 0 ||
252 sf->sf_pos_first_inconsistent > oid)
253 sf->sf_pos_first_inconsistent = oid;
258 /* There may be conflict unlink during the OI scrub,
259 * if happend, then remove the new added OI mapping. */
260 if (ops == DTO_INDEX_INSERT && dn && dn->dn_free_txg)
261 osd_scrub_refresh_mapping(env, dev, fid, oid,
262 DTO_INDEX_DELETE, false, NULL);
263 up_write(&scrub->os_rwsem);
269 spin_lock(&scrub->os_lock);
270 if (likely(!list_empty(&oii->oii_list)))
271 list_del(&oii->oii_list);
272 spin_unlock(&scrub->os_lock);
276 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
279 static int osd_scrub_prep(const struct lu_env *env, struct osd_device *dev)
281 struct lustre_scrub *scrub = &dev->od_scrub;
282 struct scrub_file *sf = &scrub->os_file;
283 __u32 flags = scrub->os_start_flags;
285 bool drop_dryrun = false;
288 CDEBUG(D_LFSCK, "%s: OI scrub prep, flags = 0x%x\n",
289 scrub->os_name, flags);
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;
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;
304 if (flags & SS_RESET)
305 scrub_file_reset(scrub, dev->od_uuid, 0);
307 spin_lock(&scrub->os_lock);
308 scrub->os_partial_scan = 0;
309 if (flags & SS_AUTO_FULL) {
310 scrub->os_full_speed = 1;
311 sf->sf_flags |= SF_AUTO;
312 } else if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
314 scrub->os_full_speed = 1;
316 scrub->os_full_speed = 0;
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;
331 sf->sf_pos_latest_start = 1;
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);
340 spin_lock(&scrub->os_lock);
341 scrub->os_running = 1;
342 spin_unlock(&scrub->os_lock);
345 up_write(&scrub->os_rwsem);
350 static int osd_scrub_post(const struct lu_env *env, struct osd_device *dev,
353 struct lustre_scrub *scrub = &dev->od_scrub;
354 struct scrub_file *sf = &scrub->os_file;
358 CDEBUG(D_LFSCK, "%s: OI scrub post with result = %d\n",
359 scrub->os_name, result);
361 down_write(&scrub->os_rwsem);
362 spin_lock(&scrub->os_lock);
363 scrub->os_running = 0;
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;
370 sf->sf_time_last_checkpoint = ktime_get_real_seconds();
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);
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;
384 sf->sf_status = SS_STOPPED;
386 sf->sf_status = SS_FAILED;
388 sf->sf_run_time += ktime_get_seconds() -
389 scrub->os_time_last_checkpoint;
391 rc = scrub_file_store(env, scrub);
392 up_write(&scrub->os_rwsem);
394 RETURN(rc < 0 ? rc : result);
397 /* iteration engine */
400 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
402 spin_lock(&scrub->os_lock);
403 if (osd_scrub_has_window(it) ||
404 !list_empty(&scrub->os_inconsistent_items) ||
405 it->ooi_waiting || kthread_should_stop())
406 scrub->os_waiting = 0;
408 scrub->os_waiting = 1;
409 spin_unlock(&scrub->os_lock);
411 return !scrub->os_waiting;
414 static int osd_scrub_next(const struct lu_env *env, struct osd_device *dev,
415 struct lu_fid *fid, uint64_t *oid)
417 struct lustre_scrub *scrub = &dev->od_scrub;
418 struct osd_otable_it *it = dev->od_otable_it;
419 struct lustre_mdt_attrs *lma = NULL;
420 nvlist_t *nvbuf = NULL;
425 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
426 wait_var_event_timeout(
428 !list_empty(&scrub->os_inconsistent_items) ||
429 kthread_should_stop(),
430 cfs_time_seconds(cfs_fail_val));
432 if (kthread_should_stop())
433 RETURN(SCRUB_NEXT_EXIT);
436 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
437 spin_lock(&scrub->os_lock);
438 scrub->os_running = 0;
439 spin_unlock(&scrub->os_lock);
440 RETURN(SCRUB_NEXT_CRASH);
443 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
444 RETURN(SCRUB_NEXT_FATAL);
453 if (!list_empty(&scrub->os_inconsistent_items)) {
454 spin_lock(&scrub->os_lock);
455 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
456 struct osd_inconsistent_item *oii;
458 oii = list_entry(scrub->os_inconsistent_items.next,
459 struct osd_inconsistent_item, oii_list);
460 *fid = oii->oii_cache.oic_fid;
461 *oid = oii->oii_cache.oic_dnode;
462 scrub->os_in_prior = 1;
463 spin_unlock(&scrub->os_lock);
467 spin_unlock(&scrub->os_lock);
470 if (!scrub->os_full_speed && !osd_scrub_has_window(it))
471 wait_var_event(scrub, osd_scrub_wakeup(scrub, it));
473 if (kthread_should_stop())
474 GOTO(out, rc = SCRUB_NEXT_EXIT);
476 rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
478 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
480 rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
481 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
487 LASSERT(nvbuf != NULL);
488 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
489 (uchar_t **)&lma, &size);
491 lustre_lma_swab(lma);
492 if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
493 !(lma->lma_incompat & LMAI_AGENT))) {
494 *fid = lma->lma_self_fid;
495 *oid = scrub->os_pos_current;
501 if (!scrub->os_full_speed) {
502 spin_lock(&scrub->os_lock);
503 it->ooi_prefetched++;
504 if (it->ooi_waiting) {
508 spin_unlock(&scrub->os_lock);
520 static int osd_scrub_exec(const struct lu_env *env, struct osd_device *dev,
521 const struct lu_fid *fid, uint64_t oid, int rc)
523 struct lustre_scrub *scrub = &dev->od_scrub;
524 struct osd_otable_it *it = dev->od_otable_it;
526 rc = osd_scrub_check_update(env, dev, fid, oid, rc);
527 if (!scrub->os_in_prior) {
528 if (!scrub->os_full_speed) {
529 spin_lock(&scrub->os_lock);
530 it->ooi_prefetched++;
531 if (it->ooi_waiting) {
535 spin_unlock(&scrub->os_lock);
538 spin_lock(&scrub->os_lock);
539 scrub->os_in_prior = 0;
540 spin_unlock(&scrub->os_lock);
546 rc = scrub_checkpoint(env, scrub);
548 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
549 "rc = %d\n", scrub->os_name, scrub->os_pos_current, rc);
550 /* Continue, as long as the scrub itself can go ahead. */
556 static int osd_scrub_main(void *args)
559 struct osd_device *dev = (struct osd_device *)args;
560 struct lustre_scrub *scrub = &dev->od_scrub;
566 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
568 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
573 rc = osd_scrub_prep(&env, dev);
575 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
580 if (!scrub->os_full_speed) {
581 struct osd_otable_it *it = dev->od_otable_it;
583 wait_var_event(scrub,
584 it->ooi_user_ready ||
585 kthread_should_stop());
587 if (kthread_should_stop())
590 scrub->os_pos_current = it->ooi_pos;
593 CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
594 scrub->os_name, scrub->os_start_flags,
595 scrub->os_pos_current);
597 fid = &osd_oti_get(&env)->oti_fid;
598 while (!rc && !kthread_should_stop()) {
599 rc = osd_scrub_next(&env, dev, fid, &oid);
601 case SCRUB_NEXT_EXIT:
603 case SCRUB_NEXT_CRASH:
604 spin_lock(&scrub->os_lock);
605 scrub->os_running = 0;
606 spin_unlock(&scrub->os_lock);
607 GOTO(out, rc = -EINVAL);
608 case SCRUB_NEXT_FATAL:
609 GOTO(post, rc = -EINVAL);
610 case SCRUB_NEXT_BREAK:
614 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
620 rc = osd_scrub_post(&env, dev, rc);
621 CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
622 scrub->os_name, scrub->os_pos_current, rc);
625 while (!list_empty(&scrub->os_inconsistent_items)) {
626 struct osd_inconsistent_item *oii;
628 oii = list_entry(scrub->os_inconsistent_items.next,
629 struct osd_inconsistent_item, oii_list);
630 list_del_init(&oii->oii_list);
637 spin_lock(&scrub->os_lock);
638 scrub->os_running = 0;
639 spin_unlock(&scrub->os_lock);
640 if (xchg(&scrub->os_task, NULL) == NULL)
641 /* scrub_stop is waiting, we need to synchronize */
642 wait_var_event(scrub, kthread_should_stop());
647 /* initial OI scrub */
651 typedef int (*handle_dirent_t)(const struct lu_env *, struct osd_device *,
652 const char *, uint64_t, uint64_t,
653 enum osd_lf_flags, bool);
654 static int osd_ios_varfid_hd(const struct lu_env *, struct osd_device *,
655 const char *, uint64_t, uint64_t,
656 enum osd_lf_flags, bool);
657 static int osd_ios_uld_hd(const struct lu_env *, struct osd_device *,
658 const char *, uint64_t, uint64_t,
659 enum osd_lf_flags, bool);
661 typedef int (*scan_dir_t)(const struct lu_env *, struct osd_device *,
662 uint64_t, handle_dirent_t, enum osd_lf_flags);
663 static int osd_ios_general_sd(const struct lu_env *, struct osd_device *,
664 uint64_t, handle_dirent_t, enum osd_lf_flags);
665 static int osd_ios_ROOT_sd(const struct lu_env *, struct osd_device *,
666 uint64_t, handle_dirent_t, enum osd_lf_flags);
670 struct lu_fid olm_fid;
671 enum osd_lf_flags olm_flags;
672 scan_dir_t olm_scan_dir;
673 handle_dirent_t olm_handle_dirent;
676 /* Add the new introduced local files in the list in the future. */
677 static const struct osd_lf_map osd_lf_maps[] = {
680 .olm_name = MOUNT_CONFIGS_DIR,
682 .f_seq = FID_SEQ_LOCAL_FILE,
683 .f_oid = MGS_CONFIGS_OID,
685 .olm_flags = OLF_SCAN_SUBITEMS,
686 .olm_scan_dir = osd_ios_general_sd,
687 .olm_handle_dirent = osd_ios_varfid_hd,
690 /* NIDTBL_VERSIONS */
692 .olm_name = MGS_NIDTBL_DIR,
693 .olm_flags = OLF_SCAN_SUBITEMS,
694 .olm_scan_dir = osd_ios_general_sd,
695 .olm_handle_dirent = osd_ios_varfid_hd,
700 .olm_name = MDT_ORPHAN_DIR,
707 .f_seq = FID_SEQ_ROOT,
708 .f_oid = FID_OID_ROOT,
710 .olm_flags = OLF_SCAN_SUBITEMS,
711 .olm_scan_dir = osd_ios_ROOT_sd,
718 .f_seq = FID_SEQ_LOCAL_FILE,
719 .f_oid = FLD_INDEX_OID,
723 /* changelog_catalog */
725 .olm_name = CHANGELOG_CATALOG,
728 /* changelog_users */
730 .olm_name = CHANGELOG_USERS,
736 .olm_flags = OLF_SCAN_SUBITEMS,
737 .olm_scan_dir = osd_ios_general_sd,
738 .olm_handle_dirent = osd_ios_varfid_hd,
744 .olm_flags = OLF_SCAN_SUBITEMS,
745 .olm_scan_dir = osd_ios_general_sd,
746 .olm_handle_dirent = osd_ios_varfid_hd,
751 .olm_name = LFSCK_DIR,
752 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
753 .olm_scan_dir = osd_ios_general_sd,
754 .olm_handle_dirent = osd_ios_varfid_hd,
759 .olm_name = LFSCK_BOOKMARK,
764 .olm_name = LFSCK_LAYOUT,
767 /* lfsck_namespace */
769 .olm_name = LFSCK_NAMESPACE,
772 /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
773 * and f_oid = index for their log files. See lu_update_log{_dir}_fid()
774 * for more details. */
778 .olm_name = "update_log",
780 .f_seq = FID_SEQ_UPDATE_LOG,
782 .olm_flags = OLF_IDX_IN_FID,
787 .olm_name = "update_log_dir",
789 .f_seq = FID_SEQ_UPDATE_LOG_DIR,
791 .olm_flags = OLF_SCAN_SUBITEMS | OLF_IDX_IN_FID,
792 .olm_scan_dir = osd_ios_general_sd,
793 .olm_handle_dirent = osd_ios_uld_hd,
798 .olm_name = HSM_ACTIONS,
803 .olm_name = LUSTRE_NODEMAP_NAME,
808 .olm_name = INDEX_BACKUP_DIR,
810 .f_seq = FID_SEQ_LOCAL_FILE,
811 .f_oid = INDEX_BACKUP_OID,
813 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
814 .olm_scan_dir = osd_ios_general_sd,
815 .olm_handle_dirent = osd_ios_varfid_hd,
823 /* Add the new introduced files under .lustre/ in the list in the future. */
824 static const struct osd_lf_map osd_dl_maps[] = {
829 .f_seq = FID_SEQ_DOT_LUSTRE,
830 .f_oid = FID_OID_DOT_LUSTRE_OBF,
834 /* .lustre/lost+found */
836 .olm_name = "lost+found",
838 .f_seq = FID_SEQ_DOT_LUSTRE,
839 .f_oid = FID_OID_DOT_LUSTRE_LPF,
848 struct osd_ios_item {
849 struct list_head oii_list;
851 enum osd_lf_flags oii_flags;
852 scan_dir_t oii_scan_dir;
853 handle_dirent_t oii_handle_dirent;
856 static int osd_ios_new_item(struct osd_device *dev, uint64_t parent,
857 enum osd_lf_flags flags, scan_dir_t scan_dir,
858 handle_dirent_t handle_dirent)
860 struct osd_ios_item *item;
864 CWARN("%s: initial OI scrub failed to add item for %llu\n",
865 osd_name(dev), parent);
869 INIT_LIST_HEAD(&item->oii_list);
870 item->oii_parent = parent;
871 item->oii_flags = flags;
872 item->oii_scan_dir = scan_dir;
873 item->oii_handle_dirent = handle_dirent;
874 list_add_tail(&item->oii_list, &dev->od_ios_list);
879 static bool osd_index_need_recreate(const struct lu_env *env,
880 struct osd_device *dev, uint64_t oid)
882 struct osd_thread_info *info = osd_oti_get(env);
883 zap_attribute_t *za = &info->oti_za2;
884 zap_cursor_t *zc = &info->oti_zc2;
888 zap_cursor_init_serialized(zc, dev->od_os, oid, 0);
889 rc = -zap_cursor_retrieve(zc, za);
891 if (rc && rc != -ENOENT)
897 static void osd_ios_index_register(const struct lu_env *env,
898 struct osd_device *osd,
899 const struct lu_fid *fid, uint64_t oid)
901 struct osd_thread_info *info = osd_oti_get(env);
902 zap_attribute_t *za = &info->oti_za2;
903 zap_cursor_t *zc = &info->oti_zc2;
904 struct zap_leaf_entry *le;
913 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
914 if (rc == -EEXIST || rc == -ENOENT)
920 if (!osd_object_is_zap(dn))
923 rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &hdl);
927 rc = -sa_lookup(hdl, SA_ZPL_MODE(osd), &mode, sizeof(mode));
928 sa_handle_destroy(hdl);
935 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
936 rc = -zap_cursor_retrieve(zc, za);
938 /* Skip empty index object */
939 GOTO(fini, rc = (rc == -ENOENT ? 1 : rc));
941 if (zc->zc_zap->zap_ismicro ||
942 !(zap_f_phys(zc->zc_zap)->zap_flags & ZAP_FLAG_UINT64_KEY))
945 le = ZAP_LEAF_ENTRY(zc->zc_leaf, 0);
946 keysize = le->le_name_numints * 8;
947 recsize = za->za_integer_length * za->za_num_integers;
948 if (likely(keysize && recsize))
949 rc = osd_index_register(osd, fid, keysize, recsize);
960 CWARN("%s: failed to register index "DFID" (%u/%u): rc = %d\n",
961 osd_name(osd), PFID(fid), keysize, recsize, rc);
963 CDEBUG(D_LFSCK, "%s: registered index "DFID" (%u/%u)\n",
964 osd_name(osd), PFID(fid), keysize, recsize);
967 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
968 struct lustre_index_restore_unit *liru, void *buf,
971 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
972 struct lu_fid *tgt_fid = &liru->liru_cfid;
973 struct lu_fid bak_fid;
977 lustre_fid2lbx(buf, tgt_fid, bufsize);
978 rc = -zap_lookup(dev->od_os, dev->od_index_backup_id, buf, 8,
979 sizeof(*zde) / 8, (void *)zde);
983 rc = osd_get_fid_by_oid(env, dev, zde->lzd_reg.zde_dnode, &bak_fid);
987 /* The OI mapping for index may be invalid, since it will be
988 * re-created, not update the OI mapping, just cache it in RAM. */
989 rc = osd_idc_find_and_init_with_oid(env, dev, tgt_fid,
992 rc = lustre_index_restore(env, &dev->od_dt_dev,
993 &liru->liru_pfid, tgt_fid, &bak_fid,
994 liru->liru_name, &dev->od_index_backup_list,
995 &dev->od_lock, buf, bufsize);
999 CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
1000 osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
1004 * verify FID-in-LMA and OI entry for one object
1006 * ios: Initial OI Scrub.
1008 static int osd_ios_scan_one(const struct lu_env *env, struct osd_device *dev,
1009 const struct lu_fid *fid, uint64_t parent,
1010 uint64_t oid, const char *name,
1011 enum osd_lf_flags flags)
1013 struct lustre_scrub *scrub = &dev->od_scrub;
1014 struct scrub_file *sf = &scrub->os_file;
1015 struct lustre_mdt_attrs *lma = NULL;
1016 nvlist_t *nvbuf = NULL;
1025 rc = __osd_xattr_load_by_oid(dev, oid, &nvbuf);
1026 if (unlikely(rc == -ENOENT || rc == -EEXIST))
1029 if (rc && rc != -ENODATA) {
1030 CWARN("%s: initial OI scrub failed to get lma for %llu: "
1031 "rc = %d\n", osd_name(dev), oid, rc);
1037 LASSERT(nvbuf != NULL);
1038 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1039 (uchar_t **)&lma, &size);
1040 if (rc || size == 0) {
1041 LASSERT(lma == NULL);
1044 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1045 lustre_lma_swab(lma);
1046 if (lma->lma_compat & LMAC_NOT_IN_OI) {
1051 if (lma->lma_compat & LMAC_IDX_BACKUP &&
1052 osd_index_need_recreate(env, dev, oid)) {
1053 if (parent == dev->od_root) {
1054 lu_local_obj_fid(&tfid,
1057 rc = osd_get_fid_by_oid(env, dev,
1065 rc = lustre_liru_new(
1066 &dev->od_index_restore_list,
1067 &tfid, &lma->lma_self_fid, oid,
1068 name, strlen(name));
1073 tfid = lma->lma_self_fid;
1074 if (!(flags & OLF_NOT_BACKUP))
1075 osd_ios_index_register(env, dev, &tfid, oid);
1080 if (rc == -ENODATA) {
1082 /* Skip the object without FID-in-LMA */
1083 CDEBUG(D_LFSCK, "%s: %llu has no FID-in-LMA, skip it\n",
1084 osd_name(dev), oid);
1089 LASSERT(!fid_is_zero(fid));
1092 if (flags & OLF_IDX_IN_FID) {
1093 LASSERT(dev->od_index >= 0);
1095 tfid.f_oid = dev->od_index;
1099 rc = osd_fid_lookup(env, dev, &tfid, &oid2);
1101 if (rc != -ENOENT) {
1102 CWARN("%s: initial OI scrub failed to lookup fid for "
1103 DFID"=>%llu: rc = %d\n",
1104 osd_name(dev), PFID(&tfid), oid, rc);
1109 flag = SF_RECREATED;
1110 op = DTO_INDEX_INSERT;
1115 flag = SF_INCONSISTENT;
1116 op = DTO_INDEX_UPDATE;
1119 if (!(sf->sf_flags & flag)) {
1120 scrub_file_reset(scrub, dev->od_uuid, flag);
1121 rc = scrub_file_store(env, scrub);
1126 rc = osd_scrub_refresh_mapping(env, dev, &tfid, oid, op, true, name);
1128 RETURN(rc > 0 ? 0 : rc);
1131 static int osd_ios_varfid_hd(const struct lu_env *env, struct osd_device *dev,
1132 const char *name, uint64_t parent, uint64_t oid,
1133 enum osd_lf_flags flags, bool is_dir)
1138 rc = osd_ios_scan_one(env, dev, NULL, parent, oid, name, 0);
1140 rc = osd_ios_new_item(dev, oid, flags, osd_ios_general_sd,
1146 static int osd_ios_uld_hd(const struct lu_env *env, struct osd_device *dev,
1147 const char *name, uint64_t parent, uint64_t oid,
1148 enum osd_lf_flags flags, bool is_dir)
1154 /* skip any non-DFID format name */
1158 /* skip the start '[' */
1159 sscanf(&name[1], SFID, RFID(&tfid));
1160 if (fid_is_sane(&tfid))
1161 rc = osd_ios_scan_one(env, dev, &tfid, parent, oid, name, 0);
1169 * General scanner for the directories execpt /ROOT during initial OI scrub.
1170 * It scans the name entries under the given directory one by one. For each
1171 * entry, verifies its OI mapping via the given @handle_dirent.
1173 static int osd_ios_general_sd(const struct lu_env *env, struct osd_device *dev,
1174 uint64_t parent, handle_dirent_t handle_dirent,
1175 enum osd_lf_flags flags)
1177 struct osd_thread_info *info = osd_oti_get(env);
1178 struct luz_direntry *zde = &info->oti_zde;
1179 zap_attribute_t *za = &info->oti_za;
1180 zap_cursor_t *zc = &info->oti_zc;
1184 zap_cursor_init_serialized(zc, dev->od_os, parent, 0);
1185 rc = -zap_cursor_retrieve(zc, za);
1187 zap_cursor_advance(zc);
1192 rc = -zap_cursor_retrieve(zc, za);
1194 GOTO(log, rc = (rc == -ENOENT ? 0 : rc));
1196 /* skip the entry started with '.' */
1197 if (likely(za->za_name[0] != '.')) {
1198 rc = osd_zap_lookup(dev, parent, NULL, za->za_name,
1199 za->za_integer_length,
1200 sizeof(*zde) / za->za_integer_length,
1203 CWARN("%s: initial OI scrub failed to lookup "
1204 "%s under %llu: rc = %d\n",
1205 osd_name(dev), za->za_name, parent, rc);
1209 rc = handle_dirent(env, dev, za->za_name, parent,
1210 zde->lzd_reg.zde_dnode, flags,
1211 S_ISDIR(DTTOIF(zde->lzd_reg.zde_type)) ?
1213 CDEBUG(D_LFSCK, "%s: initial OI scrub handled %s under "
1215 osd_name(dev), za->za_name, parent, rc);
1218 zap_cursor_advance(zc);
1223 CWARN("%s: initial OI scrub failed to scan the directory %llu: "
1224 "rc = %d\n", osd_name(dev), parent, rc);
1225 zap_cursor_fini(zc);
1231 * The scanner for /ROOT directory. It is not all the items under /ROOT will
1232 * be scanned during the initial OI scrub, instead, only the .lustre and the
1233 * sub-items under .lustre will be handled.
1235 static int osd_ios_ROOT_sd(const struct lu_env *env, struct osd_device *dev,
1236 uint64_t parent, handle_dirent_t handle_dirent,
1237 enum osd_lf_flags flags)
1239 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1240 const struct osd_lf_map *map;
1246 rc = osd_zap_lookup(dev, parent, NULL, dot_lustre_name, 8,
1247 sizeof(*zde) / 8, (void *)zde);
1248 if (rc == -ENOENT) {
1249 /* The .lustre directory is lost. That is not fatal. It can
1250 * be re-created in the subsequent MDT start processing. */
1255 CWARN("%s: initial OI scrub failed to find .lustre: "
1256 "rc = %d\n", osd_name(dev), rc);
1261 oid = zde->lzd_reg.zde_dnode;
1262 rc = osd_ios_scan_one(env, dev, &LU_DOT_LUSTRE_FID, parent, oid,
1263 dot_lustre_name, 0);
1267 for (map = osd_dl_maps; map->olm_name; map++) {
1268 rc = osd_zap_lookup(dev, oid, NULL, map->olm_name, 8,
1269 sizeof(*zde) / 8, (void *)zde);
1272 CWARN("%s: initial OI scrub failed to find 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,
1278 DTO_INDEX_DELETE, true,
1283 rc = osd_ios_scan_one(env, dev, &map->olm_fid, oid,
1284 zde->lzd_reg.zde_dnode, map->olm_name,
1293 static void osd_initial_OI_scrub(const struct lu_env *env,
1294 struct osd_device *dev)
1296 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1297 const struct osd_lf_map *map;
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);
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,
1313 DTO_INDEX_DELETE, true,
1318 rc = osd_ios_scan_one(env, dev, &map->olm_fid, dev->od_root,
1319 zde->lzd_reg.zde_dnode, map->olm_name,
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);
1327 while (!list_empty(&dev->od_ios_list)) {
1328 struct osd_ios_item *item;
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);
1338 if (!list_empty(&dev->od_index_restore_list)) {
1341 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1343 CERROR("%s: not enough RAM for rebuild index\n",
1346 while (!list_empty(&dev->od_index_restore_list)) {
1347 struct lustre_index_restore_unit *liru;
1349 liru = list_entry(dev->od_index_restore_list.next,
1350 struct lustre_index_restore_unit,
1352 list_del(&liru->liru_link);
1354 osd_index_restore(env, dev, liru, buf,
1355 INDEX_BACKUP_BUFSIZE);
1356 OBD_FREE(liru, liru->liru_len);
1360 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1366 /* OI scrub start/stop */
1368 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
1374 if (dev->od_dt_dev.dd_rdonly)
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);
1382 RETURN(rc == -EALREADY ? 0 : rc);
1385 void osd_scrub_stop(struct osd_device *dev)
1387 struct lustre_scrub *scrub = &dev->od_scrub;
1390 /* od_otable_sem: prevent concurrent start/stop */
1391 down(&dev->od_otable_sem);
1392 spin_lock(&scrub->os_lock);
1393 scrub->os_paused = 1;
1394 spin_unlock(&scrub->os_lock);
1396 up(&dev->od_otable_sem);
1401 /* OI scrub setup/cleanup */
1403 static const char osd_scrub_name[] = "OI_scrub";
1405 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
1408 struct osd_thread_info *info = osd_oti_get(env);
1409 struct lustre_scrub *scrub = &dev->od_scrub;
1410 struct scrub_file *sf = &scrub->os_file;
1411 struct lu_fid *fid = &info->oti_fid;
1412 struct dt_object *obj;
1418 memcpy(dev->od_uuid.b,
1419 &dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid,
1420 sizeof(dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid));
1421 memset(&dev->od_scrub, 0, sizeof(struct lustre_scrub));
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);
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;
1431 fid->f_oid = ((1 << 31) | dev->od_index) + 1;
1433 fid->f_oid = dev->od_index + 1;
1435 rc = osd_obj_find_or_create(env, dev, dev->od_root,
1436 osd_scrub_name, &oid, fid, false);
1440 rc = osd_idc_find_and_init_with_oid(env, dev, fid, oid);
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);
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);
1454 } else if (rc < 0) {
1455 GOTO(cleanup_obj, rc);
1457 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
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);
1463 } else if (sf->sf_status == SS_SCANNING) {
1464 sf->sf_status = SS_CRASHED;
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,
1472 sf->sf_oi_count = osd_oi_count;
1477 if (sf->sf_pos_last_checkpoint != 0)
1478 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1480 scrub->os_pos_current = 1;
1483 rc = scrub_file_store(env, scrub);
1485 GOTO(cleanup_obj, rc);
1488 /* Initialize OI files. */
1489 rc = osd_oi_init(env, dev, resetoi);
1491 GOTO(cleanup_obj, rc);
1493 if (!dev->od_dt_dev.dd_rdonly)
1494 osd_initial_OI_scrub(env, dev);
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 |
1505 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1508 GOTO(cleanup_oi, rc);
1513 osd_oi_fini(env, dev);
1515 dt_object_put_nocache(env, scrub->os_obj);
1516 scrub->os_obj = NULL;
1521 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1523 struct lustre_scrub *scrub = &dev->od_scrub;
1525 LASSERT(!dev->od_otable_it);
1527 if (scrub->os_obj) {
1528 osd_scrub_stop(dev);
1529 dt_object_put_nocache(env, scrub->os_obj);
1530 scrub->os_obj = NULL;
1533 if (dev->od_oi_table)
1534 osd_oi_fini(env, dev);
1537 /* object table based iteration APIs */
1539 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1540 struct dt_object *dt, __u32 attr)
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;
1551 if (dev->od_dt_dev.dd_rdonly)
1552 RETURN(ERR_PTR(-EROFS));
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));
1561 GOTO(out, it = ERR_PTR(-ENOMEM));
1563 if (flags & DOIF_OUTUSED)
1564 it->ooi_used_outside = 1;
1566 if (flags & DOIF_RESET)
1569 if (valid & DOIV_ERROR_HANDLE) {
1570 if (flags & DOIF_FAILOUT)
1571 start |= SS_SET_FAILOUT;
1573 start |= SS_CLEAR_FAILOUT;
1576 if (valid & DOIV_DRYRUN) {
1577 if (flags & DOIF_DRYRUN)
1578 start |= SS_SET_DRYRUN;
1580 start |= SS_CLEAR_DRYRUN;
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);
1588 dev->od_otable_it = it;
1590 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1591 if (rc == -EALREADY) {
1593 } else if (rc < 0) {
1594 dev->od_otable_it = NULL;
1598 it->ooi_pos = scrub->os_pos_current;
1604 up(&dev->od_otable_sem);
1605 return (struct dt_it *)it;
1608 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1610 struct osd_otable_it *it = (struct osd_otable_it *)di;
1611 struct osd_device *dev = it->ooi_dev;
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);
1618 dev->od_otable_it = NULL;
1619 up(&dev->od_otable_sem);
1623 static int osd_otable_it_get(const struct lu_env *env,
1624 struct dt_it *di, const struct dt_key *key)
1629 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1633 static void osd_otable_it_preload(const struct lu_env *env,
1634 struct osd_otable_it *it)
1636 struct osd_device *dev = it->ooi_dev;
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;
1644 if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1647 if (it->ooi_prefetched_dnode == 0)
1648 it->ooi_prefetched_dnode = it->ooi_pos;
1650 while (it->ooi_prefetched < OTABLE_PREFETCH) {
1651 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1656 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1657 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1658 it->ooi_prefetched++;
1663 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
1665 spin_lock(&scrub->os_lock);
1666 if (it->ooi_pos < scrub->os_pos_current || scrub->os_waiting ||
1668 it->ooi_waiting = 0;
1670 it->ooi_waiting = 1;
1671 spin_unlock(&scrub->os_lock);
1673 return !it->ooi_waiting;
1676 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
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 lustre_mdt_attrs *lma = NULL;
1682 nvlist_t *nvbuf = NULL;
1687 LASSERT(it->ooi_user_ready);
1688 fid_zero(&it->ooi_fid);
1690 if (unlikely(it->ooi_all_cached))
1701 if (it->ooi_pos >= scrub->os_pos_current)
1702 wait_var_event(scrub,
1703 osd_otable_it_wakeup(scrub, it));
1705 if (!scrub->os_running && !it->ooi_used_outside)
1708 rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1710 if (unlikely(rc == -ESRCH)) {
1711 it->ooi_all_cached = 1;
1718 rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
1721 if (!scrub->os_full_speed) {
1722 spin_lock(&scrub->os_lock);
1725 it->ooi_prefetched--;
1726 if (!scrub->os_full_speed) {
1727 if (scrub->os_waiting) {
1728 scrub->os_waiting = 0;
1733 spin_unlock(&scrub->os_lock);
1735 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
1741 LASSERT(nvbuf != NULL);
1742 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1743 (uchar_t **)&lma, &size);
1744 if (rc || size == 0)
1745 /* It is either non-Lustre object or OSD internal object,
1746 * ignore it, go ahead */
1749 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1750 lustre_lma_swab(lma);
1751 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI ||
1752 lma->lma_incompat & LMAI_AGENT))
1755 it->ooi_fid = lma->lma_self_fid;
1763 if (!rc && scrub->os_full_speed)
1764 osd_otable_it_preload(env, it);
1769 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1770 const struct dt_it *di)
1775 static int osd_otable_it_key_size(const struct lu_env *env,
1776 const struct dt_it *di)
1778 return sizeof(__u64);
1781 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1782 struct dt_rec *rec, __u32 attr)
1784 struct osd_otable_it *it = (struct osd_otable_it *)di;
1785 struct lu_fid *fid = (struct lu_fid *)rec;
1791 static __u64 osd_otable_it_store(const struct lu_env *env,
1792 const struct dt_it *di)
1794 struct osd_otable_it *it = (struct osd_otable_it *)di;
1800 * Set the OSD layer iteration start position as the specified hash.
1802 static int osd_otable_it_load(const struct lu_env *env,
1803 const struct dt_it *di, __u64 hash)
1805 struct osd_otable_it *it = (struct osd_otable_it *)di;
1806 struct osd_device *dev = it->ooi_dev;
1807 struct lustre_scrub *scrub = &dev->od_scrub;
1811 /* Forbid to set iteration position after iteration started. */
1812 if (it->ooi_user_ready)
1815 if (hash > OSD_OTABLE_MAX_HASH)
1816 hash = OSD_OTABLE_MAX_HASH;
1818 /* The hash is the last checkpoint position,
1819 * we will start from the next one. */
1820 it->ooi_pos = hash + 1;
1821 it->ooi_prefetched = 0;
1822 it->ooi_prefetched_dnode = 0;
1823 it->ooi_user_ready = 1;
1824 if (!scrub->os_full_speed)
1827 /* Unplug OSD layer iteration by the first next() call. */
1828 rc = osd_otable_it_next(env, (struct dt_it *)it);
1833 static int osd_otable_it_key_rec(const struct lu_env *env,
1834 const struct dt_it *di, void *key_rec)
1839 const struct dt_index_operations osd_otable_ops = {
1841 .init = osd_otable_it_init,
1842 .fini = osd_otable_it_fini,
1843 .get = osd_otable_it_get,
1844 .put = osd_otable_it_put,
1845 .next = osd_otable_it_next,
1846 .key = osd_otable_it_key,
1847 .key_size = osd_otable_it_key_size,
1848 .rec = osd_otable_it_rec,
1849 .store = osd_otable_it_store,
1850 .load = osd_otable_it_load,
1851 .key_rec = osd_otable_it_key_rec,
1855 /* high priority inconsistent items list APIs */
1857 int osd_oii_insert(const struct lu_env *env, struct osd_device *dev,
1858 const struct lu_fid *fid, uint64_t oid, bool insert)
1860 struct lustre_scrub *scrub = &dev->od_scrub;
1861 struct osd_inconsistent_item *oii;
1862 bool wakeup = false;
1865 osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1870 INIT_LIST_HEAD(&oii->oii_list);
1871 oii->oii_cache.oic_dev = dev;
1872 oii->oii_cache.oic_fid = *fid;
1873 oii->oii_cache.oic_dnode = oid;
1874 oii->oii_insert = insert;
1876 spin_lock(&scrub->os_lock);
1877 if (!scrub->os_running) {
1878 spin_unlock(&scrub->os_lock);
1883 if (list_empty(&scrub->os_inconsistent_items))
1885 list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1886 spin_unlock(&scrub->os_lock);
1894 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1897 struct lustre_scrub *scrub = &dev->od_scrub;
1898 struct osd_inconsistent_item *oii;
1902 spin_lock(&scrub->os_lock);
1903 list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1904 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1905 *oid = oii->oii_cache.oic_dnode;
1910 spin_unlock(&scrub->os_lock);