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 scrub->os_full_speed = 1;
196 sf->sf_flags |= SF_INCONSISTENT;
197 } else if (oid == oid2) {
200 struct lustre_mdt_attrs *lma = NULL;
203 rc = __osd_xattr_load_by_oid(dev, oid2, &nvbuf);
204 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
209 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
210 (uchar_t **)&lma, &size);
211 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
216 lustre_lma_swab(lma);
217 if (unlikely(lu_fid_eq(&lma->lma_self_fid, fid))) {
218 CDEBUG(D_LFSCK, "%s: the FID "DFID" is used by "
219 "two objects: %llu and %llu (in OI)\n",
220 osd_name(dev), PFID(fid), oid, oid2);
222 GOTO(out, rc = -EEXIST);
226 scrub->os_full_speed = 1;
227 sf->sf_flags |= SF_INCONSISTENT;
230 rc = osd_scrub_refresh_mapping(env, dev, fid, oid, ops, false, NULL);
232 if (scrub->os_in_prior)
233 sf->sf_items_updated_prior++;
235 sf->sf_items_updated++;
245 sf->sf_items_failed++;
246 if (sf->sf_pos_first_inconsistent == 0 ||
247 sf->sf_pos_first_inconsistent > oid)
248 sf->sf_pos_first_inconsistent = oid;
253 /* There may be conflict unlink during the OI scrub,
254 * if happend, then remove the new added OI mapping. */
255 if (ops == DTO_INDEX_INSERT && dn && dn->dn_free_txg)
256 osd_scrub_refresh_mapping(env, dev, fid, oid,
257 DTO_INDEX_DELETE, false, NULL);
258 up_write(&scrub->os_rwsem);
264 spin_lock(&scrub->os_lock);
265 if (likely(!list_empty(&oii->oii_list)))
266 list_del(&oii->oii_list);
267 spin_unlock(&scrub->os_lock);
271 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
274 static int osd_scrub_prep(const struct lu_env *env, struct osd_device *dev)
276 struct lustre_scrub *scrub = &dev->od_scrub;
277 struct ptlrpc_thread *thread = &scrub->os_thread;
278 struct scrub_file *sf = &scrub->os_file;
279 __u32 flags = scrub->os_start_flags;
281 bool drop_dryrun = false;
284 CDEBUG(D_LFSCK, "%s: OI scrub prep, flags = 0x%x\n",
285 scrub->os_name, flags);
287 down_write(&scrub->os_rwsem);
288 if (flags & SS_SET_FAILOUT)
289 sf->sf_param |= SP_FAILOUT;
290 else if (flags & SS_CLEAR_FAILOUT)
291 sf->sf_param &= ~SP_FAILOUT;
293 if (flags & SS_SET_DRYRUN) {
294 sf->sf_param |= SP_DRYRUN;
295 } else if (flags & SS_CLEAR_DRYRUN && sf->sf_param & SP_DRYRUN) {
296 sf->sf_param &= ~SP_DRYRUN;
300 if (flags & SS_RESET)
301 scrub_file_reset(scrub, dev->od_uuid, 0);
303 scrub->os_partial_scan = 0;
304 if (flags & SS_AUTO_FULL) {
305 scrub->os_full_speed = 1;
306 sf->sf_flags |= SF_AUTO;
307 } else if (sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
309 scrub->os_full_speed = 1;
311 scrub->os_full_speed = 0;
314 spin_lock(&scrub->os_lock);
315 scrub->os_in_prior = 0;
316 scrub->os_waiting = 0;
317 scrub->os_paused = 0;
318 scrub->os_in_join = 0;
319 scrub->os_full_scrub = 0;
320 spin_unlock(&scrub->os_lock);
321 scrub->os_new_checked = 0;
322 if (drop_dryrun && sf->sf_pos_first_inconsistent != 0)
323 sf->sf_pos_latest_start = sf->sf_pos_first_inconsistent;
324 else if (sf->sf_pos_last_checkpoint != 0)
325 sf->sf_pos_latest_start = sf->sf_pos_last_checkpoint + 1;
327 sf->sf_pos_latest_start = 1;
329 scrub->os_pos_current = sf->sf_pos_latest_start;
330 sf->sf_status = SS_SCANNING;
331 sf->sf_time_latest_start = ktime_get_real_seconds();
332 sf->sf_time_last_checkpoint = sf->sf_time_latest_start;
333 sf->sf_pos_last_checkpoint = sf->sf_pos_latest_start - 1;
334 rc = scrub_file_store(env, scrub);
336 spin_lock(&scrub->os_lock);
337 thread_set_flags(thread, SVC_RUNNING);
338 spin_unlock(&scrub->os_lock);
339 wake_up_all(&thread->t_ctl_waitq);
341 up_write(&scrub->os_rwsem);
346 static int osd_scrub_post(const struct lu_env *env, struct osd_device *dev,
349 struct lustre_scrub *scrub = &dev->od_scrub;
350 struct scrub_file *sf = &scrub->os_file;
354 CDEBUG(D_LFSCK, "%s: OI scrub post with result = %d\n",
355 scrub->os_name, result);
357 down_write(&scrub->os_rwsem);
358 spin_lock(&scrub->os_lock);
359 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
360 spin_unlock(&scrub->os_lock);
361 if (scrub->os_new_checked > 0) {
362 sf->sf_items_checked += scrub->os_new_checked;
363 scrub->os_new_checked = 0;
364 sf->sf_pos_last_checkpoint = scrub->os_pos_current;
366 sf->sf_time_last_checkpoint = ktime_get_real_seconds();
368 sf->sf_status = SS_COMPLETED;
369 if (!(sf->sf_param & SP_DRYRUN)) {
370 memset(sf->sf_oi_bitmap, 0, SCRUB_OI_BITMAP_SIZE);
371 sf->sf_flags &= ~(SF_RECREATED | SF_INCONSISTENT |
372 SF_UPGRADE | SF_AUTO);
374 sf->sf_time_last_complete = sf->sf_time_last_checkpoint;
375 sf->sf_success_count++;
376 } else if (result == 0) {
377 if (scrub->os_paused)
378 sf->sf_status = SS_PAUSED;
380 sf->sf_status = SS_STOPPED;
382 sf->sf_status = SS_FAILED;
384 sf->sf_run_time += ktime_get_seconds() -
385 scrub->os_time_last_checkpoint;
387 rc = scrub_file_store(env, scrub);
388 up_write(&scrub->os_rwsem);
390 RETURN(rc < 0 ? rc : result);
393 /* iteration engine */
396 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
398 spin_lock(&scrub->os_lock);
399 if (osd_scrub_has_window(it) ||
400 !list_empty(&scrub->os_inconsistent_items) ||
401 it->ooi_waiting || !thread_is_running(&scrub->os_thread))
402 scrub->os_waiting = 0;
404 scrub->os_waiting = 1;
405 spin_unlock(&scrub->os_lock);
407 return !scrub->os_waiting;
410 static int osd_scrub_next(const struct lu_env *env, struct osd_device *dev,
411 struct lu_fid *fid, uint64_t *oid)
413 struct l_wait_info lwi = { 0 };
414 struct lustre_scrub *scrub = &dev->od_scrub;
415 struct ptlrpc_thread *thread = &scrub->os_thread;
416 struct osd_otable_it *it = dev->od_otable_it;
417 struct lustre_mdt_attrs *lma = NULL;
418 nvlist_t *nvbuf = NULL;
423 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
424 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val), NULL, NULL);
425 if (likely(lwi.lwi_timeout > 0)) {
426 l_wait_event(thread->t_ctl_waitq,
427 !list_empty(&scrub->os_inconsistent_items) ||
428 !thread_is_running(thread),
430 if (unlikely(!thread_is_running(thread)))
431 RETURN(SCRUB_NEXT_EXIT);
435 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
436 spin_lock(&scrub->os_lock);
437 thread_set_flags(thread, SVC_STOPPING);
438 spin_unlock(&scrub->os_lock);
439 RETURN(SCRUB_NEXT_CRASH);
442 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
443 RETURN(SCRUB_NEXT_FATAL);
452 if (!list_empty(&scrub->os_inconsistent_items)) {
453 spin_lock(&scrub->os_lock);
454 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
455 struct osd_inconsistent_item *oii;
457 oii = list_entry(scrub->os_inconsistent_items.next,
458 struct osd_inconsistent_item, oii_list);
459 *fid = oii->oii_cache.oic_fid;
460 *oid = oii->oii_cache.oic_dnode;
461 scrub->os_in_prior = 1;
462 spin_unlock(&scrub->os_lock);
466 spin_unlock(&scrub->os_lock);
469 if (!scrub->os_full_speed && !osd_scrub_has_window(it)) {
470 memset(&lwi, 0, sizeof(lwi));
471 l_wait_event(thread->t_ctl_waitq,
472 osd_scrub_wakeup(scrub, it),
476 if (unlikely(!thread_is_running(thread)))
477 GOTO(out, rc = SCRUB_NEXT_EXIT);
479 rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
481 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
483 rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
484 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
490 LASSERT(nvbuf != NULL);
491 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
492 (uchar_t **)&lma, &size);
494 lustre_lma_swab(lma);
495 if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
496 !(lma->lma_incompat & LMAI_AGENT))) {
497 *fid = lma->lma_self_fid;
498 *oid = scrub->os_pos_current;
504 if (!scrub->os_full_speed) {
505 spin_lock(&scrub->os_lock);
506 it->ooi_prefetched++;
507 if (it->ooi_waiting) {
509 wake_up_all(&thread->t_ctl_waitq);
511 spin_unlock(&scrub->os_lock);
523 static int osd_scrub_exec(const struct lu_env *env, struct osd_device *dev,
524 const struct lu_fid *fid, uint64_t oid, int rc)
526 struct lustre_scrub *scrub = &dev->od_scrub;
527 struct ptlrpc_thread *thread = &scrub->os_thread;
528 struct osd_otable_it *it = dev->od_otable_it;
530 rc = osd_scrub_check_update(env, dev, fid, oid, rc);
531 if (!scrub->os_in_prior) {
532 if (!scrub->os_full_speed) {
533 spin_lock(&scrub->os_lock);
534 it->ooi_prefetched++;
535 if (it->ooi_waiting) {
537 wake_up_all(&thread->t_ctl_waitq);
539 spin_unlock(&scrub->os_lock);
542 scrub->os_in_prior = 0;
548 rc = scrub_checkpoint(env, scrub);
550 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
551 "rc = %d\n", scrub->os_name, scrub->os_pos_current, rc);
552 /* Continue, as long as the scrub itself can go ahead. */
558 static int osd_scrub_main(void *args)
561 struct osd_device *dev = (struct osd_device *)args;
562 struct lustre_scrub *scrub = &dev->od_scrub;
563 struct ptlrpc_thread *thread = &scrub->os_thread;
569 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
571 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
576 rc = osd_scrub_prep(&env, dev);
578 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
583 if (!scrub->os_full_speed) {
584 struct l_wait_info lwi = { 0 };
585 struct osd_otable_it *it = dev->od_otable_it;
587 l_wait_event(thread->t_ctl_waitq,
588 it->ooi_user_ready || !thread_is_running(thread),
590 if (unlikely(!thread_is_running(thread)))
593 scrub->os_pos_current = it->ooi_pos;
596 CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
597 scrub->os_name, scrub->os_start_flags,
598 scrub->os_pos_current);
600 fid = &osd_oti_get(&env)->oti_fid;
601 while (!rc && thread_is_running(thread)) {
602 rc = osd_scrub_next(&env, dev, fid, &oid);
604 case SCRUB_NEXT_EXIT:
606 case SCRUB_NEXT_CRASH:
607 spin_lock(&scrub->os_lock);
608 thread_set_flags(&scrub->os_thread, SVC_STOPPING);
609 spin_unlock(&scrub->os_lock);
610 GOTO(out, rc = -EINVAL);
611 case SCRUB_NEXT_FATAL:
612 GOTO(post, rc = -EINVAL);
613 case SCRUB_NEXT_BREAK:
617 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
623 rc = osd_scrub_post(&env, dev, rc);
624 CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
625 scrub->os_name, scrub->os_pos_current, rc);
628 while (!list_empty(&scrub->os_inconsistent_items)) {
629 struct osd_inconsistent_item *oii;
631 oii = list_entry(scrub->os_inconsistent_items.next,
632 struct osd_inconsistent_item, oii_list);
633 list_del_init(&oii->oii_list);
640 spin_lock(&scrub->os_lock);
641 thread_set_flags(thread, SVC_STOPPED);
642 wake_up_all(&thread->t_ctl_waitq);
643 spin_unlock(&scrub->os_lock);
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"
1273 "the entry %s under .lustre: rc = %d\n",
1274 osd_name(dev), map->olm_name, rc);
1275 else if (!fid_is_zero(&map->olm_fid))
1276 /* Try to remove the stale OI mapping. */
1277 osd_scrub_refresh_mapping(env, dev,
1279 DTO_INDEX_DELETE, true,
1284 rc = osd_ios_scan_one(env, dev, &map->olm_fid, oid,
1285 zde->lzd_reg.zde_dnode, map->olm_name,
1294 static void osd_initial_OI_scrub(const struct lu_env *env,
1295 struct osd_device *dev)
1297 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1298 const struct osd_lf_map *map;
1302 for (map = osd_lf_maps; map->olm_name; map++) {
1303 rc = osd_zap_lookup(dev, dev->od_root, NULL, map->olm_name, 8,
1304 sizeof(*zde) / 8, (void *)zde);
1307 CWARN("%s: initial OI scrub failed "
1308 "to find the entry %s: rc = %d\n",
1309 osd_name(dev), map->olm_name, rc);
1310 else if (!fid_is_zero(&map->olm_fid))
1311 /* Try to remove the stale OI mapping. */
1312 osd_scrub_refresh_mapping(env, dev,
1314 DTO_INDEX_DELETE, true,
1319 rc = osd_ios_scan_one(env, dev, &map->olm_fid, dev->od_root,
1320 zde->lzd_reg.zde_dnode, map->olm_name,
1322 if (!rc && map->olm_flags & OLF_SCAN_SUBITEMS)
1323 osd_ios_new_item(dev, zde->lzd_reg.zde_dnode,
1324 map->olm_flags, map->olm_scan_dir,
1325 map->olm_handle_dirent);
1328 while (!list_empty(&dev->od_ios_list)) {
1329 struct osd_ios_item *item;
1331 item = list_entry(dev->od_ios_list.next,
1332 struct osd_ios_item, oii_list);
1333 list_del_init(&item->oii_list);
1334 item->oii_scan_dir(env, dev, item->oii_parent,
1335 item->oii_handle_dirent, item->oii_flags);
1339 if (!list_empty(&dev->od_index_restore_list)) {
1342 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1344 CERROR("%s: not enough RAM for rebuild index\n",
1347 while (!list_empty(&dev->od_index_restore_list)) {
1348 struct lustre_index_restore_unit *liru;
1350 liru = list_entry(dev->od_index_restore_list.next,
1351 struct lustre_index_restore_unit,
1353 list_del(&liru->liru_link);
1355 osd_index_restore(env, dev, liru, buf,
1356 INDEX_BACKUP_BUFSIZE);
1357 OBD_FREE(liru, liru->liru_len);
1361 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1367 /* OI scrub start/stop */
1369 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
1375 if (dev->od_dt_dev.dd_rdonly)
1378 /* od_otable_sem: prevent concurrent start/stop */
1379 down(&dev->od_otable_sem);
1380 rc = scrub_start(osd_scrub_main, &dev->od_scrub, dev, flags);
1381 up(&dev->od_otable_sem);
1383 RETURN(rc == -EALREADY ? 0 : rc);
1386 void osd_scrub_stop(struct osd_device *dev)
1388 struct lustre_scrub *scrub = &dev->od_scrub;
1391 /* od_otable_sem: prevent concurrent start/stop */
1392 down(&dev->od_otable_sem);
1393 scrub->os_paused = 1;
1395 up(&dev->od_otable_sem);
1400 /* OI scrub setup/cleanup */
1402 static const char osd_scrub_name[] = "OI_scrub";
1404 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
1407 struct osd_thread_info *info = osd_oti_get(env);
1408 struct lustre_scrub *scrub = &dev->od_scrub;
1409 struct scrub_file *sf = &scrub->os_file;
1410 struct lu_fid *fid = &info->oti_fid;
1411 struct dt_object *obj;
1417 memcpy(dev->od_uuid,
1418 &dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid,
1419 sizeof(dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid));
1420 memset(&dev->od_scrub, 0, sizeof(struct lustre_scrub));
1421 init_waitqueue_head(&scrub->os_thread.t_ctl_waitq);
1422 init_rwsem(&scrub->os_rwsem);
1423 spin_lock_init(&scrub->os_lock);
1424 INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1425 scrub->os_name = osd_name(dev);
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 (memcmp(sf->sf_uuid, dev->od_uuid, 16) != 0) {
1458 struct obd_uuid *old_uuid;
1459 struct obd_uuid *new_uuid;
1461 OBD_ALLOC_PTR(old_uuid);
1462 OBD_ALLOC_PTR(new_uuid);
1463 if (!old_uuid || !new_uuid) {
1464 CERROR("%s: UUID has been changed, but"
1465 "failed to allocate RAM for report\n",
1468 class_uuid_unparse(sf->sf_uuid, old_uuid);
1469 class_uuid_unparse(dev->od_uuid, new_uuid);
1470 CDEBUG(D_LFSCK, "%s: UUID has been changed "
1471 "from %s to %s\n", osd_name(dev),
1472 old_uuid->uuid, new_uuid->uuid);
1474 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1477 OBD_FREE_PTR(old_uuid);
1479 OBD_FREE_PTR(new_uuid);
1480 } else if (sf->sf_status == SS_SCANNING) {
1481 sf->sf_status = SS_CRASHED;
1485 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
1486 LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
1487 osd_name(dev), sf->sf_oi_count,
1489 sf->sf_oi_count = osd_oi_count;
1494 if (sf->sf_pos_last_checkpoint != 0)
1495 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1497 scrub->os_pos_current = 1;
1500 rc = scrub_file_store(env, scrub);
1502 GOTO(cleanup_obj, rc);
1505 /* Initialize OI files. */
1506 rc = osd_oi_init(env, dev, resetoi);
1508 GOTO(cleanup_obj, rc);
1510 if (!dev->od_dt_dev.dd_rdonly)
1511 osd_initial_OI_scrub(env, dev);
1513 if (!dev->od_dt_dev.dd_rdonly &&
1514 dev->od_auto_scrub_interval != AS_NEVER &&
1515 ((sf->sf_status == SS_PAUSED) ||
1516 (sf->sf_status == SS_CRASHED &&
1517 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1518 SF_UPGRADE | SF_AUTO)) ||
1519 (sf->sf_status == SS_INIT &&
1520 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1522 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1525 GOTO(cleanup_oi, rc);
1530 osd_oi_fini(env, dev);
1532 dt_object_put_nocache(env, scrub->os_obj);
1533 scrub->os_obj = NULL;
1538 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1540 struct lustre_scrub *scrub = &dev->od_scrub;
1542 LASSERT(!dev->od_otable_it);
1544 if (scrub->os_obj) {
1545 osd_scrub_stop(dev);
1546 dt_object_put_nocache(env, scrub->os_obj);
1547 scrub->os_obj = NULL;
1550 if (dev->od_oi_table)
1551 osd_oi_fini(env, dev);
1554 /* object table based iteration APIs */
1556 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1557 struct dt_object *dt, __u32 attr)
1559 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1560 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1561 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1562 struct lustre_scrub *scrub = &dev->od_scrub;
1563 struct osd_otable_it *it;
1568 if (dev->od_dt_dev.dd_rdonly)
1569 RETURN(ERR_PTR(-EROFS));
1571 /* od_otable_sem: prevent concurrent init/fini */
1572 down(&dev->od_otable_sem);
1573 if (dev->od_otable_it)
1574 GOTO(out, it = ERR_PTR(-EALREADY));
1578 GOTO(out, it = ERR_PTR(-ENOMEM));
1580 if (flags & DOIF_OUTUSED)
1581 it->ooi_used_outside = 1;
1583 if (flags & DOIF_RESET)
1586 if (valid & DOIV_ERROR_HANDLE) {
1587 if (flags & DOIF_FAILOUT)
1588 start |= SS_SET_FAILOUT;
1590 start |= SS_CLEAR_FAILOUT;
1593 if (valid & DOIV_DRYRUN) {
1594 if (flags & DOIF_DRYRUN)
1595 start |= SS_SET_DRYRUN;
1597 start |= SS_CLEAR_DRYRUN;
1600 /* XXX: dmu_object_next() does NOT find dnodes allocated
1601 * in the current non-committed txg, so we force txg
1602 * commit to find all existing dnodes ... */
1603 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1605 dev->od_otable_it = it;
1607 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1608 if (rc == -EALREADY) {
1610 } else if (rc < 0) {
1611 dev->od_otable_it = NULL;
1615 it->ooi_pos = scrub->os_pos_current;
1621 up(&dev->od_otable_sem);
1622 return (struct dt_it *)it;
1625 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1627 struct osd_otable_it *it = (struct osd_otable_it *)di;
1628 struct osd_device *dev = it->ooi_dev;
1630 /* od_otable_sem: prevent concurrent init/fini */
1631 down(&dev->od_otable_sem);
1632 scrub_stop(&dev->od_scrub);
1633 LASSERT(dev->od_otable_it == it);
1635 dev->od_otable_it = NULL;
1636 up(&dev->od_otable_sem);
1640 static int osd_otable_it_get(const struct lu_env *env,
1641 struct dt_it *di, const struct dt_key *key)
1646 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1650 static void osd_otable_it_preload(const struct lu_env *env,
1651 struct osd_otable_it *it)
1653 struct osd_device *dev = it->ooi_dev;
1656 /* can go negative on the very first access to the iterator
1657 * or if some non-Lustre objects were found */
1658 if (unlikely(it->ooi_prefetched < 0))
1659 it->ooi_prefetched = 0;
1661 if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1664 if (it->ooi_prefetched_dnode == 0)
1665 it->ooi_prefetched_dnode = it->ooi_pos;
1667 while (it->ooi_prefetched < OTABLE_PREFETCH) {
1668 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1673 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1674 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1675 it->ooi_prefetched++;
1680 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
1682 spin_lock(&scrub->os_lock);
1683 if (it->ooi_pos < scrub->os_pos_current || scrub->os_waiting ||
1684 !thread_is_running(&scrub->os_thread))
1685 it->ooi_waiting = 0;
1687 it->ooi_waiting = 1;
1688 spin_unlock(&scrub->os_lock);
1690 return !it->ooi_waiting;
1693 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1695 struct osd_otable_it *it = (struct osd_otable_it *)di;
1696 struct osd_device *dev = it->ooi_dev;
1697 struct lustre_scrub *scrub = &dev->od_scrub;
1698 struct ptlrpc_thread *thread = &scrub->os_thread;
1699 struct l_wait_info lwi = { 0 };
1700 struct lustre_mdt_attrs *lma = NULL;
1701 nvlist_t *nvbuf = NULL;
1706 LASSERT(it->ooi_user_ready);
1707 fid_zero(&it->ooi_fid);
1709 if (unlikely(it->ooi_all_cached))
1720 if (it->ooi_pos >= scrub->os_pos_current)
1721 l_wait_event(thread->t_ctl_waitq,
1722 osd_otable_it_wakeup(scrub, it),
1725 if (!thread_is_running(thread) && !it->ooi_used_outside)
1728 rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1730 if (unlikely(rc == -ESRCH)) {
1731 it->ooi_all_cached = 1;
1738 rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
1740 if (!scrub->os_full_speed)
1741 spin_lock(&scrub->os_lock);
1742 it->ooi_prefetched--;
1743 if (!scrub->os_full_speed) {
1744 if (scrub->os_waiting) {
1745 scrub->os_waiting = 0;
1746 wake_up_all(&thread->t_ctl_waitq);
1748 spin_unlock(&scrub->os_lock);
1751 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
1757 LASSERT(nvbuf != NULL);
1758 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1759 (uchar_t **)&lma, &size);
1760 if (rc || size == 0)
1761 /* It is either non-Lustre object or OSD internal object,
1762 * ignore it, go ahead */
1765 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1766 lustre_lma_swab(lma);
1767 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI ||
1768 lma->lma_incompat & LMAI_AGENT))
1771 it->ooi_fid = lma->lma_self_fid;
1779 if (!rc && scrub->os_full_speed)
1780 osd_otable_it_preload(env, it);
1785 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1786 const struct dt_it *di)
1791 static int osd_otable_it_key_size(const struct lu_env *env,
1792 const struct dt_it *di)
1794 return sizeof(__u64);
1797 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1798 struct dt_rec *rec, __u32 attr)
1800 struct osd_otable_it *it = (struct osd_otable_it *)di;
1801 struct lu_fid *fid = (struct lu_fid *)rec;
1807 static __u64 osd_otable_it_store(const struct lu_env *env,
1808 const struct dt_it *di)
1810 struct osd_otable_it *it = (struct osd_otable_it *)di;
1816 * Set the OSD layer iteration start position as the specified hash.
1818 static int osd_otable_it_load(const struct lu_env *env,
1819 const struct dt_it *di, __u64 hash)
1821 struct osd_otable_it *it = (struct osd_otable_it *)di;
1822 struct osd_device *dev = it->ooi_dev;
1823 struct lustre_scrub *scrub = &dev->od_scrub;
1827 /* Forbid to set iteration position after iteration started. */
1828 if (it->ooi_user_ready)
1831 if (hash > OSD_OTABLE_MAX_HASH)
1832 hash = OSD_OTABLE_MAX_HASH;
1834 /* The hash is the last checkpoint position,
1835 * we will start from the next one. */
1836 it->ooi_pos = hash + 1;
1837 it->ooi_prefetched = 0;
1838 it->ooi_prefetched_dnode = 0;
1839 it->ooi_user_ready = 1;
1840 if (!scrub->os_full_speed)
1841 wake_up_all(&scrub->os_thread.t_ctl_waitq);
1843 /* Unplug OSD layer iteration by the first next() call. */
1844 rc = osd_otable_it_next(env, (struct dt_it *)it);
1849 static int osd_otable_it_key_rec(const struct lu_env *env,
1850 const struct dt_it *di, void *key_rec)
1855 const struct dt_index_operations osd_otable_ops = {
1857 .init = osd_otable_it_init,
1858 .fini = osd_otable_it_fini,
1859 .get = osd_otable_it_get,
1860 .put = osd_otable_it_put,
1861 .next = osd_otable_it_next,
1862 .key = osd_otable_it_key,
1863 .key_size = osd_otable_it_key_size,
1864 .rec = osd_otable_it_rec,
1865 .store = osd_otable_it_store,
1866 .load = osd_otable_it_load,
1867 .key_rec = osd_otable_it_key_rec,
1871 /* high priority inconsistent items list APIs */
1873 int osd_oii_insert(const struct lu_env *env, struct osd_device *dev,
1874 const struct lu_fid *fid, uint64_t oid, bool insert)
1876 struct lustre_scrub *scrub = &dev->od_scrub;
1877 struct ptlrpc_thread *thread = &scrub->os_thread;
1878 struct osd_inconsistent_item *oii;
1879 bool wakeup = false;
1882 osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1887 INIT_LIST_HEAD(&oii->oii_list);
1888 oii->oii_cache.oic_dev = dev;
1889 oii->oii_cache.oic_fid = *fid;
1890 oii->oii_cache.oic_dnode = oid;
1891 oii->oii_insert = insert;
1893 spin_lock(&scrub->os_lock);
1894 if (unlikely(!thread_is_running(thread))) {
1895 spin_unlock(&scrub->os_lock);
1900 if (list_empty(&scrub->os_inconsistent_items))
1902 list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1903 spin_unlock(&scrub->os_lock);
1906 wake_up_all(&thread->t_ctl_waitq);
1911 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1914 struct lustre_scrub *scrub = &dev->od_scrub;
1915 struct osd_inconsistent_item *oii;
1919 spin_lock(&scrub->os_lock);
1920 list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1921 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1922 *oid = oii->oii_cache.oic_dnode;
1927 spin_unlock(&scrub->os_lock);