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 #define DTO_INDEX_INSERT 1
59 #define DTO_INDEX_DELETE 2
60 #define DTO_INDEX_UPDATE 3
62 static inline bool osd_scrub_has_window(struct osd_otable_it *it)
64 return it->ooi_prefetched < OTABLE_PREFETCH;
68 * update/insert/delete the specified OI mapping (@fid @id) according to the ops
70 * \retval 1, changed nothing
71 * \retval 0, changed successfully
72 * \retval -ve, on error
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)
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;
89 if (dev->od_scrub.os_file.sf_param & SP_DRYRUN && !force)
92 tx = dmu_tx_create(dev->od_os);
94 GOTO(log, rc = -ENOMEM);
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);
107 case DTO_INDEX_UPDATE:
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,
113 if (unlikely(rc == -ENOENT)) {
114 /* Some unlink thread may removed the OI mapping. */
118 case DTO_INDEX_INSERT:
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,
124 if (unlikely(rc == -EEXIST))
127 case DTO_INDEX_DELETE:
128 rc = osd_zap_remove(dev, zapid, dn, buf, tx);
130 /* It is normal that the unlink thread has removed the
131 * OI mapping already. */
136 LASSERTF(0, "Unexpected ops %d\n", ops);
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);
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)
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;
162 int ops = DTO_INDEX_UPDATE;
166 down_write(&scrub->os_rwsem);
167 scrub->os_new_checked++;
171 if (scrub->os_in_prior)
172 oii = list_entry(scrub->os_inconsistent_items.next,
173 struct osd_inconsistent_item, oii_list);
175 if (oid < sf->sf_pos_latest_start && !oii)
178 if (oii && oii->oii_insert) {
179 ops = DTO_INDEX_INSERT;
183 rc = osd_fid_lookup(env, dev, fid, &oid2);
188 ops = DTO_INDEX_INSERT;
191 rc = __osd_obj2dnode(dev->od_os, oid, &dn);
193 /* Someone removed the object by race. */
194 if (rc == -ENOENT || rc == -EEXIST)
199 scrub->os_full_speed = 1;
200 sf->sf_flags |= SF_INCONSISTENT;
201 } else if (oid == oid2) {
204 struct lustre_mdt_attrs *lma = NULL;
207 rc = __osd_xattr_load_by_oid(dev, oid2, &nvbuf);
208 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
213 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
214 (uchar_t **)&lma, &size);
215 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
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);
226 GOTO(out, rc = -EEXIST);
230 scrub->os_full_speed = 1;
231 sf->sf_flags |= SF_INCONSISTENT;
234 rc = osd_scrub_refresh_mapping(env, dev, fid, oid, ops, false, NULL);
236 if (scrub->os_in_prior)
237 sf->sf_items_updated_prior++;
239 sf->sf_items_updated++;
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;
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);
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);
275 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
278 static int osd_scrub_prep(const struct lu_env *env, struct osd_device *dev)
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;
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 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 |
313 scrub->os_full_speed = 1;
315 scrub->os_full_speed = 0;
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;
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 thread_set_flags(thread, SVC_RUNNING);
342 spin_unlock(&scrub->os_lock);
343 wake_up_all(&thread->t_ctl_waitq);
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 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;
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 || !thread_is_running(&scrub->os_thread))
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 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;
426 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
427 wait_event_idle_timeout(
429 !list_empty(&scrub->os_inconsistent_items) ||
430 !thread_is_running(thread),
431 cfs_time_seconds(cfs_fail_val));
433 if (unlikely(!thread_is_running(thread)))
434 RETURN(SCRUB_NEXT_EXIT);
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);
444 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
445 RETURN(SCRUB_NEXT_FATAL);
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;
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);
468 spin_unlock(&scrub->os_lock);
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));
475 if (unlikely(!thread_is_running(thread)))
476 GOTO(out, rc = SCRUB_NEXT_EXIT);
478 rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
480 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
482 rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
483 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
489 LASSERT(nvbuf != NULL);
490 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
491 (uchar_t **)&lma, &size);
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;
503 if (!scrub->os_full_speed) {
504 spin_lock(&scrub->os_lock);
505 it->ooi_prefetched++;
506 if (it->ooi_waiting) {
508 wake_up_all(&thread->t_ctl_waitq);
510 spin_unlock(&scrub->os_lock);
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)
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;
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) {
536 wake_up_all(&thread->t_ctl_waitq);
538 spin_unlock(&scrub->os_lock);
541 scrub->os_in_prior = 0;
547 rc = scrub_checkpoint(env, scrub);
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. */
557 static int osd_scrub_main(void *args)
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;
568 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
570 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
575 rc = osd_scrub_prep(&env, dev);
577 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
582 if (!scrub->os_full_speed) {
583 struct osd_otable_it *it = dev->od_otable_it;
585 wait_event_idle(thread->t_ctl_waitq,
586 it->ooi_user_ready ||
587 !thread_is_running(thread));
589 if (unlikely(!thread_is_running(thread)))
592 scrub->os_pos_current = it->ooi_pos;
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);
599 fid = &osd_oti_get(&env)->oti_fid;
600 while (!rc && thread_is_running(thread)) {
601 rc = osd_scrub_next(&env, dev, fid, &oid);
603 case SCRUB_NEXT_EXIT:
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:
616 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
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);
627 while (!list_empty(&scrub->os_inconsistent_items)) {
628 struct osd_inconsistent_item *oii;
630 oii = list_entry(scrub->os_inconsistent_items.next,
631 struct osd_inconsistent_item, oii_list);
632 list_del_init(&oii->oii_list);
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);
646 /* initial OI scrub */
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);
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);
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;
675 /* Add the new introduced local files in the list in the future. */
676 static const struct osd_lf_map osd_lf_maps[] = {
679 .olm_name = MOUNT_CONFIGS_DIR,
681 .f_seq = FID_SEQ_LOCAL_FILE,
682 .f_oid = MGS_CONFIGS_OID,
684 .olm_flags = OLF_SCAN_SUBITEMS,
685 .olm_scan_dir = osd_ios_general_sd,
686 .olm_handle_dirent = osd_ios_varfid_hd,
689 /* NIDTBL_VERSIONS */
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,
699 .olm_name = MDT_ORPHAN_DIR,
706 .f_seq = FID_SEQ_ROOT,
707 .f_oid = FID_OID_ROOT,
709 .olm_flags = OLF_SCAN_SUBITEMS,
710 .olm_scan_dir = osd_ios_ROOT_sd,
717 .f_seq = FID_SEQ_LOCAL_FILE,
718 .f_oid = FLD_INDEX_OID,
722 /* changelog_catalog */
724 .olm_name = CHANGELOG_CATALOG,
727 /* changelog_users */
729 .olm_name = CHANGELOG_USERS,
735 .olm_flags = OLF_SCAN_SUBITEMS,
736 .olm_scan_dir = osd_ios_general_sd,
737 .olm_handle_dirent = osd_ios_varfid_hd,
743 .olm_flags = OLF_SCAN_SUBITEMS,
744 .olm_scan_dir = osd_ios_general_sd,
745 .olm_handle_dirent = osd_ios_varfid_hd,
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,
758 .olm_name = LFSCK_BOOKMARK,
763 .olm_name = LFSCK_LAYOUT,
766 /* lfsck_namespace */
768 .olm_name = LFSCK_NAMESPACE,
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. */
777 .olm_name = "update_log",
779 .f_seq = FID_SEQ_UPDATE_LOG,
781 .olm_flags = OLF_IDX_IN_FID,
786 .olm_name = "update_log_dir",
788 .f_seq = FID_SEQ_UPDATE_LOG_DIR,
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,
797 .olm_name = HSM_ACTIONS,
802 .olm_name = LUSTRE_NODEMAP_NAME,
807 .olm_name = INDEX_BACKUP_DIR,
809 .f_seq = FID_SEQ_LOCAL_FILE,
810 .f_oid = INDEX_BACKUP_OID,
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,
822 /* Add the new introduced files under .lustre/ in the list in the future. */
823 static const struct osd_lf_map osd_dl_maps[] = {
828 .f_seq = FID_SEQ_DOT_LUSTRE,
829 .f_oid = FID_OID_DOT_LUSTRE_OBF,
833 /* .lustre/lost+found */
835 .olm_name = "lost+found",
837 .f_seq = FID_SEQ_DOT_LUSTRE,
838 .f_oid = FID_OID_DOT_LUSTRE_LPF,
847 struct osd_ios_item {
848 struct list_head oii_list;
850 enum osd_lf_flags oii_flags;
851 scan_dir_t oii_scan_dir;
852 handle_dirent_t oii_handle_dirent;
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)
859 struct osd_ios_item *item;
863 CWARN("%s: initial OI scrub failed to add item for %llu\n",
864 osd_name(dev), parent);
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);
878 static bool osd_index_need_recreate(const struct lu_env *env,
879 struct osd_device *dev, uint64_t oid)
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;
887 zap_cursor_init_serialized(zc, dev->od_os, oid, 0);
888 rc = -zap_cursor_retrieve(zc, za);
890 if (rc && rc != -ENOENT)
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)
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;
912 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
913 if (rc == -EEXIST || rc == -ENOENT)
919 if (!osd_object_is_zap(dn))
922 rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &hdl);
926 rc = -sa_lookup(hdl, SA_ZPL_MODE(osd), &mode, sizeof(mode));
927 sa_handle_destroy(hdl);
934 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
935 rc = -zap_cursor_retrieve(zc, za);
937 /* Skip empty index object */
938 GOTO(fini, rc = (rc == -ENOENT ? 1 : rc));
940 if (zc->zc_zap->zap_ismicro ||
941 !(zap_f_phys(zc->zc_zap)->zap_flags & ZAP_FLAG_UINT64_KEY))
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);
959 CWARN("%s: failed to register index "DFID" (%u/%u): rc = %d\n",
960 osd_name(osd), PFID(fid), keysize, recsize, rc);
962 CDEBUG(D_LFSCK, "%s: registered index "DFID" (%u/%u)\n",
963 osd_name(osd), PFID(fid), keysize, recsize);
966 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
967 struct lustre_index_restore_unit *liru, void *buf,
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;
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);
982 rc = osd_get_fid_by_oid(env, dev, zde->lzd_reg.zde_dnode, &bak_fid);
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,
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);
998 CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
999 osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
1003 * verify FID-in-LMA and OI entry for one object
1005 * ios: Initial OI Scrub.
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)
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;
1024 rc = __osd_xattr_load_by_oid(dev, oid, &nvbuf);
1025 if (unlikely(rc == -ENOENT || rc == -EEXIST))
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);
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);
1043 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1044 lustre_lma_swab(lma);
1045 if (lma->lma_compat & LMAC_NOT_IN_OI) {
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,
1056 rc = osd_get_fid_by_oid(env, dev,
1064 rc = lustre_liru_new(
1065 &dev->od_index_restore_list,
1066 &tfid, &lma->lma_self_fid, oid,
1067 name, strlen(name));
1072 tfid = lma->lma_self_fid;
1073 if (!(flags & OLF_NOT_BACKUP))
1074 osd_ios_index_register(env, dev, &tfid, oid);
1079 if (rc == -ENODATA) {
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);
1088 LASSERT(!fid_is_zero(fid));
1091 if (flags & OLF_IDX_IN_FID) {
1092 LASSERT(dev->od_index >= 0);
1094 tfid.f_oid = dev->od_index;
1098 rc = osd_fid_lookup(env, dev, &tfid, &oid2);
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);
1108 flag = SF_RECREATED;
1109 op = DTO_INDEX_INSERT;
1114 flag = SF_INCONSISTENT;
1115 op = DTO_INDEX_UPDATE;
1118 if (!(sf->sf_flags & flag)) {
1119 scrub_file_reset(scrub, dev->od_uuid, flag);
1120 rc = scrub_file_store(env, scrub);
1125 rc = osd_scrub_refresh_mapping(env, dev, &tfid, oid, op, true, name);
1127 RETURN(rc > 0 ? 0 : rc);
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)
1137 rc = osd_ios_scan_one(env, dev, NULL, parent, oid, name, 0);
1139 rc = osd_ios_new_item(dev, oid, flags, osd_ios_general_sd,
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)
1153 /* skip any non-DFID format name */
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);
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.
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)
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;
1183 zap_cursor_init_serialized(zc, dev->od_os, parent, 0);
1184 rc = -zap_cursor_retrieve(zc, za);
1186 zap_cursor_advance(zc);
1191 rc = -zap_cursor_retrieve(zc, za);
1193 GOTO(log, rc = (rc == -ENOENT ? 0 : rc));
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,
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);
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)) ?
1212 CDEBUG(D_LFSCK, "%s: initial OI scrub handled %s under "
1214 osd_name(dev), za->za_name, parent, rc);
1217 zap_cursor_advance(zc);
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);
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.
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)
1238 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1239 const struct osd_lf_map *map;
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. */
1254 CWARN("%s: initial OI scrub failed to find .lustre: "
1255 "rc = %d\n", osd_name(dev), rc);
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);
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);
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,
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 scrub->os_paused = 1;
1394 up(&dev->od_otable_sem);
1399 /* OI scrub setup/cleanup */
1401 static const char osd_scrub_name[] = "OI_scrub";
1403 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev)
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;
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);
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;
1429 fid->f_oid = ((1 << 31) | dev->od_index) + 1;
1431 fid->f_oid = dev->od_index + 1;
1433 rc = osd_obj_find_or_create(env, dev, dev->od_root,
1434 osd_scrub_name, &oid, fid, false);
1438 rc = osd_idc_find_and_init_with_oid(env, dev, fid, oid);
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);
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);
1452 } else if (rc < 0) {
1453 GOTO(cleanup_obj, rc);
1455 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
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);
1461 } else if (sf->sf_status == SS_SCANNING) {
1462 sf->sf_status = SS_CRASHED;
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,
1470 sf->sf_oi_count = osd_oi_count;
1475 if (sf->sf_pos_last_checkpoint != 0)
1476 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1478 scrub->os_pos_current = 1;
1481 rc = scrub_file_store(env, scrub);
1483 GOTO(cleanup_obj, rc);
1486 /* Initialize OI files. */
1487 rc = osd_oi_init(env, dev);
1489 GOTO(cleanup_obj, rc);
1491 if (!dev->od_dt_dev.dd_rdonly)
1492 osd_initial_OI_scrub(env, dev);
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 |
1503 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1506 GOTO(cleanup_oi, rc);
1511 osd_oi_fini(env, dev);
1513 dt_object_put_nocache(env, scrub->os_obj);
1514 scrub->os_obj = NULL;
1519 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1521 struct lustre_scrub *scrub = &dev->od_scrub;
1523 LASSERT(!dev->od_otable_it);
1525 if (scrub->os_obj) {
1526 osd_scrub_stop(dev);
1527 dt_object_put_nocache(env, scrub->os_obj);
1528 scrub->os_obj = NULL;
1531 if (dev->od_oi_table)
1532 osd_oi_fini(env, dev);
1535 /* object table based iteration APIs */
1537 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1538 struct dt_object *dt, __u32 attr)
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;
1549 if (dev->od_dt_dev.dd_rdonly)
1550 RETURN(ERR_PTR(-EROFS));
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));
1559 GOTO(out, it = ERR_PTR(-ENOMEM));
1561 if (flags & DOIF_OUTUSED)
1562 it->ooi_used_outside = 1;
1564 if (flags & DOIF_RESET)
1567 if (valid & DOIV_ERROR_HANDLE) {
1568 if (flags & DOIF_FAILOUT)
1569 start |= SS_SET_FAILOUT;
1571 start |= SS_CLEAR_FAILOUT;
1574 if (valid & DOIV_DRYRUN) {
1575 if (flags & DOIF_DRYRUN)
1576 start |= SS_SET_DRYRUN;
1578 start |= SS_CLEAR_DRYRUN;
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);
1586 dev->od_otable_it = it;
1588 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1589 if (rc == -EALREADY) {
1591 } else if (rc < 0) {
1592 dev->od_otable_it = NULL;
1596 it->ooi_pos = scrub->os_pos_current;
1602 up(&dev->od_otable_sem);
1603 return (struct dt_it *)it;
1606 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1608 struct osd_otable_it *it = (struct osd_otable_it *)di;
1609 struct osd_device *dev = it->ooi_dev;
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);
1616 dev->od_otable_it = NULL;
1617 up(&dev->od_otable_sem);
1621 static int osd_otable_it_get(const struct lu_env *env,
1622 struct dt_it *di, const struct dt_key *key)
1627 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1631 static void osd_otable_it_preload(const struct lu_env *env,
1632 struct osd_otable_it *it)
1634 struct osd_device *dev = it->ooi_dev;
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;
1642 if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1645 if (it->ooi_prefetched_dnode == 0)
1646 it->ooi_prefetched_dnode = it->ooi_pos;
1648 while (it->ooi_prefetched < OTABLE_PREFETCH) {
1649 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1654 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1655 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1656 it->ooi_prefetched++;
1661 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
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;
1668 it->ooi_waiting = 1;
1669 spin_unlock(&scrub->os_lock);
1671 return !it->ooi_waiting;
1674 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
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;
1686 LASSERT(it->ooi_user_ready);
1687 fid_zero(&it->ooi_fid);
1689 if (unlikely(it->ooi_all_cached))
1700 if (it->ooi_pos >= scrub->os_pos_current)
1701 wait_event_idle(thread->t_ctl_waitq,
1702 osd_otable_it_wakeup(scrub, it));
1704 if (!thread_is_running(thread) && !it->ooi_used_outside)
1707 rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1709 if (unlikely(rc == -ESRCH)) {
1710 it->ooi_all_cached = 1;
1717 rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
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);
1727 spin_unlock(&scrub->os_lock);
1730 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
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 */
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))
1750 it->ooi_fid = lma->lma_self_fid;
1758 if (!rc && scrub->os_full_speed)
1759 osd_otable_it_preload(env, it);
1764 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1765 const struct dt_it *di)
1770 static int osd_otable_it_key_size(const struct lu_env *env,
1771 const struct dt_it *di)
1773 return sizeof(__u64);
1776 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1777 struct dt_rec *rec, __u32 attr)
1779 struct osd_otable_it *it = (struct osd_otable_it *)di;
1780 struct lu_fid *fid = (struct lu_fid *)rec;
1786 static __u64 osd_otable_it_store(const struct lu_env *env,
1787 const struct dt_it *di)
1789 struct osd_otable_it *it = (struct osd_otable_it *)di;
1795 * Set the OSD layer iteration start position as the specified hash.
1797 static int osd_otable_it_load(const struct lu_env *env,
1798 const struct dt_it *di, __u64 hash)
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;
1806 /* Forbid to set iteration position after iteration started. */
1807 if (it->ooi_user_ready)
1810 if (hash > OSD_OTABLE_MAX_HASH)
1811 hash = OSD_OTABLE_MAX_HASH;
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);
1822 /* Unplug OSD layer iteration by the first next() call. */
1823 rc = osd_otable_it_next(env, (struct dt_it *)it);
1828 static int osd_otable_it_key_rec(const struct lu_env *env,
1829 const struct dt_it *di, void *key_rec)
1834 const struct dt_index_operations osd_otable_ops = {
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,
1850 /* high priority inconsistent items list APIs */
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)
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;
1861 osd_idc_find_and_init_with_oid(env, dev, fid, oid);
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;
1872 spin_lock(&scrub->os_lock);
1873 if (unlikely(!thread_is_running(thread))) {
1874 spin_unlock(&scrub->os_lock);
1879 if (list_empty(&scrub->os_inconsistent_items))
1881 list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1882 spin_unlock(&scrub->os_lock);
1885 wake_up_all(&thread->t_ctl_waitq);
1890 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1893 struct lustre_scrub *scrub = &dev->od_scrub;
1894 struct osd_inconsistent_item *oii;
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;
1906 spin_unlock(&scrub->os_lock);