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++;
246 if (dev->od_is_ost) {
248 uint64_t nlink, mode;
250 rc = -sa_handle_get(dev->od_os, oid, NULL, SA_HDL_PRIVATE,
255 rc = -sa_lookup(hdl, SA_ZPL_MODE(dev), &mode, sizeof(mode));
256 if (rc || !S_ISREG(mode)) {
257 sa_handle_destroy(hdl);
261 rc = -sa_lookup(hdl, SA_ZPL_LINKS(dev), &nlink, sizeof(nlink));
262 if (rc == 0 && nlink > 1)
263 scrub->os_has_ml_file = 1;
265 sa_handle_destroy(hdl);
273 sf->sf_items_failed++;
274 if (sf->sf_pos_first_inconsistent == 0 ||
275 sf->sf_pos_first_inconsistent > oid)
276 sf->sf_pos_first_inconsistent = oid;
281 /* There may be conflict unlink during the OI scrub,
282 * if happend, then remove the new added OI mapping. */
283 if (ops == DTO_INDEX_INSERT && dn && dn->dn_free_txg)
284 osd_scrub_refresh_mapping(env, dev, fid, oid,
285 DTO_INDEX_DELETE, false, NULL);
286 up_write(&scrub->os_rwsem);
292 spin_lock(&scrub->os_lock);
293 if (likely(!list_empty(&oii->oii_list)))
294 list_del(&oii->oii_list);
295 spin_unlock(&scrub->os_lock);
299 RETURN(sf->sf_param & SP_FAILOUT ? rc : 0);
302 /* iteration engine */
305 osd_scrub_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
307 spin_lock(&scrub->os_lock);
308 if (osd_scrub_has_window(it) ||
309 !list_empty(&scrub->os_inconsistent_items) ||
310 it->ooi_waiting || kthread_should_stop())
311 scrub->os_waiting = 0;
313 scrub->os_waiting = 1;
314 spin_unlock(&scrub->os_lock);
316 return !scrub->os_waiting;
319 static int osd_scrub_next(const struct lu_env *env, struct osd_device *dev,
320 struct lu_fid *fid, uint64_t *oid)
322 struct lustre_scrub *scrub = &dev->od_scrub;
323 struct osd_otable_it *it = dev->od_otable_it;
324 struct lustre_mdt_attrs *lma = NULL;
325 nvlist_t *nvbuf = NULL;
330 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_DELAY) && cfs_fail_val > 0) {
331 wait_var_event_timeout(
333 !list_empty(&scrub->os_inconsistent_items) ||
334 kthread_should_stop(),
335 cfs_time_seconds(cfs_fail_val));
337 if (kthread_should_stop())
338 RETURN(SCRUB_NEXT_EXIT);
341 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_CRASH)) {
342 spin_lock(&scrub->os_lock);
343 scrub->os_running = 0;
344 spin_unlock(&scrub->os_lock);
345 RETURN(SCRUB_NEXT_CRASH);
348 if (OBD_FAIL_CHECK(OBD_FAIL_OSD_SCRUB_FATAL))
349 RETURN(SCRUB_NEXT_FATAL);
358 if (!list_empty(&scrub->os_inconsistent_items)) {
359 spin_lock(&scrub->os_lock);
360 if (likely(!list_empty(&scrub->os_inconsistent_items))) {
361 struct osd_inconsistent_item *oii;
363 oii = list_entry(scrub->os_inconsistent_items.next,
364 struct osd_inconsistent_item, oii_list);
365 *fid = oii->oii_cache.oic_fid;
366 *oid = oii->oii_cache.oic_dnode;
367 scrub->os_in_prior = 1;
368 spin_unlock(&scrub->os_lock);
372 spin_unlock(&scrub->os_lock);
375 if (!scrub->os_full_speed && !osd_scrub_has_window(it))
376 wait_var_event(scrub, osd_scrub_wakeup(scrub, it));
378 if (kthread_should_stop())
379 GOTO(out, rc = SCRUB_NEXT_EXIT);
381 rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
383 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
385 rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
386 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
392 LASSERT(nvbuf != NULL);
393 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
394 (uchar_t **)&lma, &size);
396 lustre_lma_swab(lma);
397 if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
398 !(lma->lma_incompat & LMAI_AGENT))) {
399 *fid = lma->lma_self_fid;
400 *oid = scrub->os_pos_current;
406 if (!scrub->os_full_speed) {
407 spin_lock(&scrub->os_lock);
408 it->ooi_prefetched++;
409 if (it->ooi_waiting) {
413 spin_unlock(&scrub->os_lock);
425 static int osd_scrub_exec(const struct lu_env *env, struct osd_device *dev,
426 const struct lu_fid *fid, uint64_t oid, int rc)
428 struct lustre_scrub *scrub = &dev->od_scrub;
429 struct osd_otable_it *it = dev->od_otable_it;
431 rc = osd_scrub_check_update(env, dev, fid, oid, rc);
432 if (!scrub->os_in_prior) {
433 if (!scrub->os_full_speed) {
434 spin_lock(&scrub->os_lock);
435 it->ooi_prefetched++;
436 if (it->ooi_waiting) {
440 spin_unlock(&scrub->os_lock);
443 spin_lock(&scrub->os_lock);
444 scrub->os_in_prior = 0;
445 spin_unlock(&scrub->os_lock);
451 rc = scrub_checkpoint(env, scrub);
453 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
454 "rc = %d\n", scrub->os_name, scrub->os_pos_current, rc);
455 /* Continue, as long as the scrub itself can go ahead. */
461 static int osd_scan_ml_file_main(const struct lu_env *env,
462 struct osd_device *dev);
464 static int osd_scrub_main(void *args)
467 struct osd_device *dev = (struct osd_device *)args;
468 struct lustre_scrub *scrub = &dev->od_scrub;
474 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
476 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
481 rc = scrub_thread_prep(&env, scrub, dev->od_uuid, 1);
483 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
488 if (!scrub->os_full_speed) {
489 struct osd_otable_it *it = dev->od_otable_it;
491 wait_var_event(scrub,
492 it->ooi_user_ready ||
493 kthread_should_stop());
495 if (kthread_should_stop())
498 scrub->os_pos_current = it->ooi_pos;
501 CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
502 scrub->os_name, scrub->os_start_flags,
503 scrub->os_pos_current);
505 fid = &osd_oti_get(&env)->oti_fid;
506 while (!rc && !kthread_should_stop()) {
507 rc = osd_scrub_next(&env, dev, fid, &oid);
509 case SCRUB_NEXT_EXIT:
511 case SCRUB_NEXT_CRASH:
512 spin_lock(&scrub->os_lock);
513 scrub->os_running = 0;
514 spin_unlock(&scrub->os_lock);
515 GOTO(out, rc = -EINVAL);
516 case SCRUB_NEXT_FATAL:
517 GOTO(post, rc = -EINVAL);
518 case SCRUB_NEXT_BREAK:
522 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
528 if (scrub->os_has_ml_file) {
529 ret = osd_scan_ml_file_main(&env, dev);
534 rc = scrub_thread_post(&env, &dev->od_scrub, rc);
535 CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
536 scrub->os_name, scrub->os_pos_current, rc);
539 while (!list_empty(&scrub->os_inconsistent_items)) {
540 struct osd_inconsistent_item *oii;
542 oii = list_entry(scrub->os_inconsistent_items.next,
543 struct osd_inconsistent_item, oii_list);
544 list_del_init(&oii->oii_list);
551 spin_lock(&scrub->os_lock);
552 scrub->os_running = 0;
553 spin_unlock(&scrub->os_lock);
554 if (xchg(&scrub->os_task, NULL) == NULL)
555 /* scrub_stop is waiting, we need to synchronize */
556 wait_var_event(scrub, kthread_should_stop());
561 /* initial OI scrub */
565 typedef int (*handle_dirent_t)(const struct lu_env *, struct osd_device *,
566 const char *, uint64_t, uint64_t,
567 enum osd_lf_flags, bool);
568 static int osd_ios_varfid_hd(const struct lu_env *, struct osd_device *,
569 const char *, uint64_t, uint64_t,
570 enum osd_lf_flags, bool);
571 static int osd_ios_uld_hd(const struct lu_env *, struct osd_device *,
572 const char *, uint64_t, uint64_t,
573 enum osd_lf_flags, bool);
575 typedef int (*scan_dir_t)(const struct lu_env *, struct osd_device *,
576 uint64_t, handle_dirent_t, enum osd_lf_flags);
577 static int osd_ios_general_sd(const struct lu_env *, struct osd_device *,
578 uint64_t, handle_dirent_t, enum osd_lf_flags);
579 static int osd_ios_ROOT_sd(const struct lu_env *, struct osd_device *,
580 uint64_t, handle_dirent_t, enum osd_lf_flags);
584 struct lu_fid olm_fid;
585 enum osd_lf_flags olm_flags;
586 scan_dir_t olm_scan_dir;
587 handle_dirent_t olm_handle_dirent;
590 /* Add the new introduced local files in the list in the future. */
591 static const struct osd_lf_map osd_lf_maps[] = {
594 .olm_name = MOUNT_CONFIGS_DIR,
596 .f_seq = FID_SEQ_LOCAL_FILE,
597 .f_oid = MGS_CONFIGS_OID,
599 .olm_flags = OLF_SCAN_SUBITEMS,
600 .olm_scan_dir = osd_ios_general_sd,
601 .olm_handle_dirent = osd_ios_varfid_hd,
604 /* NIDTBL_VERSIONS */
606 .olm_name = MGS_NIDTBL_DIR,
607 .olm_flags = OLF_SCAN_SUBITEMS,
608 .olm_scan_dir = osd_ios_general_sd,
609 .olm_handle_dirent = osd_ios_varfid_hd,
614 .olm_name = MDT_ORPHAN_DIR,
621 .f_seq = FID_SEQ_ROOT,
622 .f_oid = FID_OID_ROOT,
624 .olm_flags = OLF_SCAN_SUBITEMS,
625 .olm_scan_dir = osd_ios_ROOT_sd,
632 .f_seq = FID_SEQ_LOCAL_FILE,
633 .f_oid = FLD_INDEX_OID,
637 /* changelog_catalog */
639 .olm_name = CHANGELOG_CATALOG,
642 /* changelog_users */
644 .olm_name = CHANGELOG_USERS,
650 .olm_flags = OLF_SCAN_SUBITEMS,
651 .olm_scan_dir = osd_ios_general_sd,
652 .olm_handle_dirent = osd_ios_varfid_hd,
658 .olm_flags = OLF_SCAN_SUBITEMS,
659 .olm_scan_dir = osd_ios_general_sd,
660 .olm_handle_dirent = osd_ios_varfid_hd,
665 .olm_name = LFSCK_DIR,
666 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
667 .olm_scan_dir = osd_ios_general_sd,
668 .olm_handle_dirent = osd_ios_varfid_hd,
673 .olm_name = LFSCK_BOOKMARK,
678 .olm_name = LFSCK_LAYOUT,
681 /* lfsck_namespace */
683 .olm_name = LFSCK_NAMESPACE,
686 /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
687 * and f_oid = index for their log files. See lu_update_log{_dir}_fid()
688 * for more details. */
692 .olm_name = "update_log",
694 .f_seq = FID_SEQ_UPDATE_LOG,
696 .olm_flags = OLF_IDX_IN_FID,
701 .olm_name = "update_log_dir",
703 .f_seq = FID_SEQ_UPDATE_LOG_DIR,
705 .olm_flags = OLF_SCAN_SUBITEMS | OLF_IDX_IN_FID,
706 .olm_scan_dir = osd_ios_general_sd,
707 .olm_handle_dirent = osd_ios_uld_hd,
712 .olm_name = HSM_ACTIONS,
717 .olm_name = LUSTRE_NODEMAP_NAME,
722 .olm_name = INDEX_BACKUP_DIR,
724 .f_seq = FID_SEQ_LOCAL_FILE,
725 .f_oid = INDEX_BACKUP_OID,
727 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
728 .olm_scan_dir = osd_ios_general_sd,
729 .olm_handle_dirent = osd_ios_varfid_hd,
737 /* Add the new introduced files under .lustre/ in the list in the future. */
738 static const struct osd_lf_map osd_dl_maps[] = {
743 .f_seq = FID_SEQ_DOT_LUSTRE,
744 .f_oid = FID_OID_DOT_LUSTRE_OBF,
748 /* .lustre/lost+found */
750 .olm_name = "lost+found",
752 .f_seq = FID_SEQ_DOT_LUSTRE,
753 .f_oid = FID_OID_DOT_LUSTRE_LPF,
762 struct osd_ios_item {
763 struct list_head oii_list;
765 enum osd_lf_flags oii_flags;
766 scan_dir_t oii_scan_dir;
767 handle_dirent_t oii_handle_dirent;
770 static int osd_ios_new_item(struct osd_device *dev, uint64_t parent,
771 enum osd_lf_flags flags, scan_dir_t scan_dir,
772 handle_dirent_t handle_dirent)
774 struct osd_ios_item *item;
778 CWARN("%s: initial OI scrub failed to add item for %llu\n",
779 osd_name(dev), parent);
783 INIT_LIST_HEAD(&item->oii_list);
784 item->oii_parent = parent;
785 item->oii_flags = flags;
786 item->oii_scan_dir = scan_dir;
787 item->oii_handle_dirent = handle_dirent;
788 list_add_tail(&item->oii_list, &dev->od_ios_list);
793 static bool osd_index_need_recreate(const struct lu_env *env,
794 struct osd_device *dev, uint64_t oid)
796 struct osd_thread_info *info = osd_oti_get(env);
797 zap_attribute_t *za = &info->oti_za2;
798 zap_cursor_t *zc = &info->oti_zc2;
802 zap_cursor_init_serialized(zc, dev->od_os, oid, 0);
803 rc = -zap_cursor_retrieve(zc, za);
805 if (rc && rc != -ENOENT)
811 static void osd_ios_index_register(const struct lu_env *env,
812 struct osd_device *osd,
813 const struct lu_fid *fid, uint64_t oid)
815 struct osd_thread_info *info = osd_oti_get(env);
816 zap_attribute_t *za = &info->oti_za2;
817 zap_cursor_t *zc = &info->oti_zc2;
818 struct zap_leaf_entry *le;
827 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
828 if (rc == -EEXIST || rc == -ENOENT)
834 if (!osd_object_is_zap(dn))
837 rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &hdl);
841 rc = -sa_lookup(hdl, SA_ZPL_MODE(osd), &mode, sizeof(mode));
842 sa_handle_destroy(hdl);
849 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
850 rc = -zap_cursor_retrieve(zc, za);
852 /* Skip empty index object */
853 GOTO(fini, rc = (rc == -ENOENT ? 1 : rc));
855 if (zc->zc_zap->zap_ismicro ||
856 !(zap_f_phys(zc->zc_zap)->zap_flags & ZAP_FLAG_UINT64_KEY))
859 le = ZAP_LEAF_ENTRY(zc->zc_leaf, 0);
860 keysize = le->le_name_numints * 8;
861 recsize = za->za_integer_length * za->za_num_integers;
862 if (likely(keysize && recsize))
863 rc = osd_index_register(osd, fid, keysize, recsize);
874 CWARN("%s: failed to register index "DFID" (%u/%u): rc = %d\n",
875 osd_name(osd), PFID(fid), keysize, recsize, rc);
877 CDEBUG(D_LFSCK, "%s: registered index "DFID" (%u/%u)\n",
878 osd_name(osd), PFID(fid), keysize, recsize);
881 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
882 struct lustre_index_restore_unit *liru, void *buf,
885 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
886 struct lu_fid *tgt_fid = &liru->liru_cfid;
887 struct lu_fid bak_fid;
891 lustre_fid2lbx(buf, tgt_fid, bufsize);
892 rc = -zap_lookup(dev->od_os, dev->od_index_backup_id, buf, 8,
893 sizeof(*zde) / 8, (void *)zde);
897 rc = osd_get_fid_by_oid(env, dev, zde->lzd_reg.zde_dnode, &bak_fid);
901 /* The OI mapping for index may be invalid, since it will be
902 * re-created, not update the OI mapping, just cache it in RAM. */
903 rc = osd_idc_find_and_init_with_oid(env, dev, tgt_fid,
906 rc = lustre_index_restore(env, &dev->od_dt_dev,
907 &liru->liru_pfid, tgt_fid, &bak_fid,
908 liru->liru_name, &dev->od_index_backup_list,
909 &dev->od_lock, buf, bufsize);
913 CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
914 osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
918 * verify FID-in-LMA and OI entry for one object
920 * ios: Initial OI Scrub.
922 static int osd_ios_scan_one(const struct lu_env *env, struct osd_device *dev,
923 const struct lu_fid *fid, uint64_t parent,
924 uint64_t oid, const char *name,
925 enum osd_lf_flags flags)
927 struct lustre_scrub *scrub = &dev->od_scrub;
928 struct scrub_file *sf = &scrub->os_file;
929 struct lustre_mdt_attrs *lma = NULL;
930 nvlist_t *nvbuf = NULL;
939 rc = __osd_xattr_load_by_oid(dev, oid, &nvbuf);
940 if (unlikely(rc == -ENOENT || rc == -EEXIST))
943 if (rc && rc != -ENODATA) {
944 CWARN("%s: initial OI scrub failed to get lma for %llu: "
945 "rc = %d\n", osd_name(dev), oid, rc);
951 LASSERT(nvbuf != NULL);
952 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
953 (uchar_t **)&lma, &size);
954 if (rc || size == 0) {
955 LASSERT(lma == NULL);
958 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
959 lustre_lma_swab(lma);
960 if (lma->lma_compat & LMAC_NOT_IN_OI) {
965 if (lma->lma_compat & LMAC_IDX_BACKUP &&
966 osd_index_need_recreate(env, dev, oid)) {
967 if (parent == dev->od_root) {
968 lu_local_obj_fid(&tfid,
971 rc = osd_get_fid_by_oid(env, dev,
979 rc = lustre_liru_new(
980 &dev->od_index_restore_list,
981 &tfid, &lma->lma_self_fid, oid,
987 tfid = lma->lma_self_fid;
988 if (!(flags & OLF_NOT_BACKUP))
989 osd_ios_index_register(env, dev, &tfid, oid);
994 if (rc == -ENODATA) {
996 /* Skip the object without FID-in-LMA */
997 CDEBUG(D_LFSCK, "%s: %llu has no FID-in-LMA, skip it\n",
1003 LASSERT(!fid_is_zero(fid));
1006 if (flags & OLF_IDX_IN_FID) {
1007 LASSERT(dev->od_index >= 0);
1009 tfid.f_oid = dev->od_index;
1013 rc = osd_fid_lookup(env, dev, &tfid, &oid2);
1015 if (rc != -ENOENT) {
1016 CWARN("%s: initial OI scrub failed to lookup fid for "
1017 DFID"=>%llu: rc = %d\n",
1018 osd_name(dev), PFID(&tfid), oid, rc);
1023 flag = SF_RECREATED;
1024 op = DTO_INDEX_INSERT;
1029 flag = SF_INCONSISTENT;
1030 op = DTO_INDEX_UPDATE;
1033 if (!(sf->sf_flags & flag)) {
1034 scrub_file_reset(scrub, dev->od_uuid, flag);
1035 rc = scrub_file_store(env, scrub);
1040 rc = osd_scrub_refresh_mapping(env, dev, &tfid, oid, op, true, name);
1042 RETURN(rc > 0 ? 0 : rc);
1045 static int osd_ios_varfid_hd(const struct lu_env *env, struct osd_device *dev,
1046 const char *name, uint64_t parent, uint64_t oid,
1047 enum osd_lf_flags flags, bool is_dir)
1052 rc = osd_ios_scan_one(env, dev, NULL, parent, oid, name, 0);
1054 rc = osd_ios_new_item(dev, oid, flags, osd_ios_general_sd,
1060 static int osd_ios_uld_hd(const struct lu_env *env, struct osd_device *dev,
1061 const char *name, uint64_t parent, uint64_t oid,
1062 enum osd_lf_flags flags, bool is_dir)
1068 /* skip any non-DFID format name */
1072 /* skip the start '[' */
1073 sscanf(&name[1], SFID, RFID(&tfid));
1074 if (fid_is_sane(&tfid))
1075 rc = osd_ios_scan_one(env, dev, &tfid, parent, oid, name, 0);
1083 * General scanner for the directories execpt /ROOT during initial OI scrub.
1084 * It scans the name entries under the given directory one by one. For each
1085 * entry, verifies its OI mapping via the given @handle_dirent.
1087 static int osd_ios_general_sd(const struct lu_env *env, struct osd_device *dev,
1088 uint64_t parent, handle_dirent_t handle_dirent,
1089 enum osd_lf_flags flags)
1091 struct osd_thread_info *info = osd_oti_get(env);
1092 struct luz_direntry *zde = &info->oti_zde;
1093 zap_attribute_t *za = &info->oti_za;
1094 zap_cursor_t *zc = &info->oti_zc;
1098 zap_cursor_init_serialized(zc, dev->od_os, parent, 0);
1099 rc = -zap_cursor_retrieve(zc, za);
1101 zap_cursor_advance(zc);
1106 rc = -zap_cursor_retrieve(zc, za);
1108 GOTO(log, rc = (rc == -ENOENT ? 0 : rc));
1110 /* skip the entry started with '.' */
1111 if (likely(za->za_name[0] != '.')) {
1112 rc = osd_zap_lookup(dev, parent, NULL, za->za_name,
1113 za->za_integer_length,
1114 sizeof(*zde) / za->za_integer_length,
1117 CWARN("%s: initial OI scrub failed to lookup "
1118 "%s under %llu: rc = %d\n",
1119 osd_name(dev), za->za_name, parent, rc);
1123 rc = handle_dirent(env, dev, za->za_name, parent,
1124 zde->lzd_reg.zde_dnode, flags,
1125 S_ISDIR(DTTOIF(zde->lzd_reg.zde_type)) ?
1127 CDEBUG(D_LFSCK, "%s: initial OI scrub handled %s under "
1129 osd_name(dev), za->za_name, parent, rc);
1132 zap_cursor_advance(zc);
1137 CWARN("%s: initial OI scrub failed to scan the directory %llu: "
1138 "rc = %d\n", osd_name(dev), parent, rc);
1139 zap_cursor_fini(zc);
1145 * The scanner for /ROOT directory. It is not all the items under /ROOT will
1146 * be scanned during the initial OI scrub, instead, only the .lustre and the
1147 * sub-items under .lustre will be handled.
1149 static int osd_ios_ROOT_sd(const struct lu_env *env, struct osd_device *dev,
1150 uint64_t parent, handle_dirent_t handle_dirent,
1151 enum osd_lf_flags flags)
1153 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1154 const struct osd_lf_map *map;
1160 rc = osd_zap_lookup(dev, parent, NULL, dot_lustre_name, 8,
1161 sizeof(*zde) / 8, (void *)zde);
1162 if (rc == -ENOENT) {
1163 /* The .lustre directory is lost. That is not fatal. It can
1164 * be re-created in the subsequent MDT start processing. */
1169 CWARN("%s: initial OI scrub failed to find .lustre: "
1170 "rc = %d\n", osd_name(dev), rc);
1175 oid = zde->lzd_reg.zde_dnode;
1176 rc = osd_ios_scan_one(env, dev, &LU_DOT_LUSTRE_FID, parent, oid,
1177 dot_lustre_name, 0);
1181 for (map = osd_dl_maps; map->olm_name; map++) {
1182 rc = osd_zap_lookup(dev, oid, NULL, map->olm_name, 8,
1183 sizeof(*zde) / 8, (void *)zde);
1186 CWARN("%s: initial OI scrub failed to find the entry %s under .lustre: rc = %d\n",
1187 osd_name(dev), map->olm_name, rc);
1188 else if (!fid_is_zero(&map->olm_fid))
1189 /* Try to remove the stale OI mapping. */
1190 osd_scrub_refresh_mapping(env, dev,
1192 DTO_INDEX_DELETE, true,
1197 rc = osd_ios_scan_one(env, dev, &map->olm_fid, oid,
1198 zde->lzd_reg.zde_dnode, map->olm_name,
1207 static void osd_initial_OI_scrub(const struct lu_env *env,
1208 struct osd_device *dev)
1210 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1211 const struct osd_lf_map *map;
1215 for (map = osd_lf_maps; map->olm_name; map++) {
1216 rc = osd_zap_lookup(dev, dev->od_root, NULL, map->olm_name, 8,
1217 sizeof(*zde) / 8, (void *)zde);
1220 CWARN("%s: initial OI scrub failed "
1221 "to find the entry %s: rc = %d\n",
1222 osd_name(dev), map->olm_name, rc);
1223 else if (!fid_is_zero(&map->olm_fid))
1224 /* Try to remove the stale OI mapping. */
1225 osd_scrub_refresh_mapping(env, dev,
1227 DTO_INDEX_DELETE, true,
1232 rc = osd_ios_scan_one(env, dev, &map->olm_fid, dev->od_root,
1233 zde->lzd_reg.zde_dnode, map->olm_name,
1235 if (!rc && map->olm_flags & OLF_SCAN_SUBITEMS)
1236 osd_ios_new_item(dev, zde->lzd_reg.zde_dnode,
1237 map->olm_flags, map->olm_scan_dir,
1238 map->olm_handle_dirent);
1241 while (!list_empty(&dev->od_ios_list)) {
1242 struct osd_ios_item *item;
1244 item = list_entry(dev->od_ios_list.next,
1245 struct osd_ios_item, oii_list);
1246 list_del_init(&item->oii_list);
1247 item->oii_scan_dir(env, dev, item->oii_parent,
1248 item->oii_handle_dirent, item->oii_flags);
1252 if (!list_empty(&dev->od_index_restore_list)) {
1255 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1257 CERROR("%s: not enough RAM for rebuild index\n",
1260 while (!list_empty(&dev->od_index_restore_list)) {
1261 struct lustre_index_restore_unit *liru;
1263 liru = list_entry(dev->od_index_restore_list.next,
1264 struct lustre_index_restore_unit,
1266 list_del(&liru->liru_link);
1268 osd_index_restore(env, dev, liru, buf,
1269 INDEX_BACKUP_BUFSIZE);
1270 OBD_FREE(liru, liru->liru_len);
1274 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1280 /* OI scrub start/stop */
1282 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
1288 if (dev->od_dt_dev.dd_rdonly)
1291 /* od_otable_sem: prevent concurrent start/stop */
1292 down(&dev->od_otable_sem);
1293 rc = scrub_start(osd_scrub_main, &dev->od_scrub, dev, flags);
1294 up(&dev->od_otable_sem);
1296 RETURN(rc == -EALREADY ? 0 : rc);
1299 void osd_scrub_stop(struct osd_device *dev)
1301 struct lustre_scrub *scrub = &dev->od_scrub;
1304 /* od_otable_sem: prevent concurrent start/stop */
1305 down(&dev->od_otable_sem);
1306 spin_lock(&scrub->os_lock);
1307 scrub->os_paused = 1;
1308 spin_unlock(&scrub->os_lock);
1310 up(&dev->od_otable_sem);
1315 /* OI scrub setup/cleanup */
1317 static const char osd_scrub_name[] = "OI_scrub";
1319 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
1322 struct osd_thread_info *info = osd_oti_get(env);
1323 struct lustre_scrub *scrub = &dev->od_scrub;
1324 struct scrub_file *sf = &scrub->os_file;
1325 struct lu_fid *fid = &info->oti_fid;
1326 struct dt_object *obj;
1332 memcpy(dev->od_uuid.b,
1333 &dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid,
1334 sizeof(dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid));
1335 memset(&dev->od_scrub, 0, sizeof(struct lustre_scrub));
1336 init_rwsem(&scrub->os_rwsem);
1337 spin_lock_init(&scrub->os_lock);
1338 INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1339 scrub->os_name = osd_name(dev);
1341 /* 'What the @fid is' is not imporatant, because the object
1342 * has no OI mapping, and only is visible inside the OSD.*/
1343 fid->f_seq = FID_SEQ_IGIF_MAX;
1345 fid->f_oid = ((1 << 31) | dev->od_index) + 1;
1347 fid->f_oid = dev->od_index + 1;
1349 rc = osd_obj_find_or_create(env, dev, dev->od_root,
1350 osd_scrub_name, &oid, fid, false);
1354 rc = osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1358 obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
1359 if (IS_ERR_OR_NULL(obj))
1360 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
1362 obj->do_body_ops = &osd_body_scrub_ops;
1363 scrub->os_obj = obj;
1364 rc = scrub_file_load(env, scrub);
1365 if (rc == -ENOENT || rc == -EFAULT) {
1366 scrub_file_init(scrub, dev->od_uuid);
1368 } else if (rc < 0) {
1369 GOTO(cleanup_obj, rc);
1371 if (!uuid_equal(&sf->sf_uuid, &dev->od_uuid)) {
1373 "%s: UUID has been changed from %pU to %pU\n",
1374 osd_name(dev), &sf->sf_uuid, &dev->od_uuid);
1375 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1377 } else if (sf->sf_status == SS_SCANNING) {
1378 sf->sf_status = SS_CRASHED;
1382 if ((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0) {
1383 LCONSOLE_WARN("%s: invalid oi count %d, set it to %d\n",
1384 osd_name(dev), sf->sf_oi_count,
1386 sf->sf_oi_count = osd_oi_count;
1391 if (sf->sf_pos_last_checkpoint != 0)
1392 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1394 scrub->os_pos_current = 1;
1397 rc = scrub_file_store(env, scrub);
1399 GOTO(cleanup_obj, rc);
1402 /* Initialize OI files. */
1403 rc = osd_oi_init(env, dev, resetoi);
1405 GOTO(cleanup_obj, rc);
1407 if (!dev->od_dt_dev.dd_rdonly)
1408 osd_initial_OI_scrub(env, dev);
1410 if (!dev->od_dt_dev.dd_rdonly &&
1411 dev->od_auto_scrub_interval != AS_NEVER &&
1412 ((sf->sf_status == SS_PAUSED) ||
1413 (sf->sf_status == SS_CRASHED &&
1414 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1415 SF_UPGRADE | SF_AUTO)) ||
1416 (sf->sf_status == SS_INIT &&
1417 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1419 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1422 GOTO(cleanup_oi, rc);
1427 osd_oi_fini(env, dev);
1429 dt_object_put_nocache(env, scrub->os_obj);
1430 scrub->os_obj = NULL;
1435 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1437 struct lustre_scrub *scrub = &dev->od_scrub;
1439 LASSERT(!dev->od_otable_it);
1441 if (scrub->os_obj) {
1442 osd_scrub_stop(dev);
1443 dt_object_put_nocache(env, scrub->os_obj);
1444 scrub->os_obj = NULL;
1447 if (dev->od_oi_table)
1448 osd_oi_fini(env, dev);
1451 /* object table based iteration APIs */
1453 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1454 struct dt_object *dt, __u32 attr)
1456 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1457 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1458 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1459 struct lustre_scrub *scrub = &dev->od_scrub;
1460 struct osd_otable_it *it;
1465 if (dev->od_dt_dev.dd_rdonly)
1466 RETURN(ERR_PTR(-EROFS));
1468 /* od_otable_sem: prevent concurrent init/fini */
1469 down(&dev->od_otable_sem);
1470 if (dev->od_otable_it)
1471 GOTO(out, it = ERR_PTR(-EALREADY));
1475 GOTO(out, it = ERR_PTR(-ENOMEM));
1477 if (flags & DOIF_OUTUSED)
1478 it->ooi_used_outside = 1;
1480 if (flags & DOIF_RESET)
1483 if (valid & DOIV_ERROR_HANDLE) {
1484 if (flags & DOIF_FAILOUT)
1485 start |= SS_SET_FAILOUT;
1487 start |= SS_CLEAR_FAILOUT;
1490 if (valid & DOIV_DRYRUN) {
1491 if (flags & DOIF_DRYRUN)
1492 start |= SS_SET_DRYRUN;
1494 start |= SS_CLEAR_DRYRUN;
1497 /* XXX: dmu_object_next() does NOT find dnodes allocated
1498 * in the current non-committed txg, so we force txg
1499 * commit to find all existing dnodes ... */
1500 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1502 dev->od_otable_it = it;
1504 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1505 if (rc == -EALREADY) {
1507 } else if (rc < 0) {
1508 dev->od_otable_it = NULL;
1512 it->ooi_pos = scrub->os_pos_current;
1518 up(&dev->od_otable_sem);
1519 return (struct dt_it *)it;
1522 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1524 struct osd_otable_it *it = (struct osd_otable_it *)di;
1525 struct osd_device *dev = it->ooi_dev;
1527 /* od_otable_sem: prevent concurrent init/fini */
1528 down(&dev->od_otable_sem);
1529 scrub_stop(&dev->od_scrub);
1530 LASSERT(dev->od_otable_it == it);
1532 dev->od_otable_it = NULL;
1533 up(&dev->od_otable_sem);
1537 static int osd_otable_it_get(const struct lu_env *env,
1538 struct dt_it *di, const struct dt_key *key)
1543 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1547 static void osd_otable_it_preload(const struct lu_env *env,
1548 struct osd_otable_it *it)
1550 struct osd_device *dev = it->ooi_dev;
1553 /* can go negative on the very first access to the iterator
1554 * or if some non-Lustre objects were found */
1555 if (unlikely(it->ooi_prefetched < 0))
1556 it->ooi_prefetched = 0;
1558 if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1561 if (it->ooi_prefetched_dnode == 0)
1562 it->ooi_prefetched_dnode = it->ooi_pos;
1564 while (it->ooi_prefetched < OTABLE_PREFETCH) {
1565 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1570 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1571 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1572 it->ooi_prefetched++;
1577 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
1579 spin_lock(&scrub->os_lock);
1580 if (it->ooi_pos < scrub->os_pos_current || scrub->os_waiting ||
1582 it->ooi_waiting = 0;
1584 it->ooi_waiting = 1;
1585 spin_unlock(&scrub->os_lock);
1587 return !it->ooi_waiting;
1590 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1592 struct osd_otable_it *it = (struct osd_otable_it *)di;
1593 struct osd_device *dev = it->ooi_dev;
1594 struct lustre_scrub *scrub = &dev->od_scrub;
1595 struct lustre_mdt_attrs *lma = NULL;
1596 nvlist_t *nvbuf = NULL;
1601 LASSERT(it->ooi_user_ready);
1602 fid_zero(&it->ooi_fid);
1604 if (unlikely(it->ooi_all_cached))
1615 if (it->ooi_pos >= scrub->os_pos_current)
1616 wait_var_event(scrub,
1617 osd_otable_it_wakeup(scrub, it));
1619 if (!scrub->os_running && !it->ooi_used_outside)
1622 rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1624 if (unlikely(rc == -ESRCH)) {
1625 it->ooi_all_cached = 1;
1632 rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
1635 if (!scrub->os_full_speed) {
1636 spin_lock(&scrub->os_lock);
1639 it->ooi_prefetched--;
1640 if (!scrub->os_full_speed) {
1641 if (scrub->os_waiting) {
1642 scrub->os_waiting = 0;
1647 spin_unlock(&scrub->os_lock);
1649 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
1655 LASSERT(nvbuf != NULL);
1656 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1657 (uchar_t **)&lma, &size);
1658 if (rc || size == 0)
1659 /* It is either non-Lustre object or OSD internal object,
1660 * ignore it, go ahead */
1663 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1664 lustre_lma_swab(lma);
1665 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI ||
1666 lma->lma_incompat & LMAI_AGENT))
1669 it->ooi_fid = lma->lma_self_fid;
1677 if (!rc && scrub->os_full_speed)
1678 osd_otable_it_preload(env, it);
1683 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1684 const struct dt_it *di)
1689 static int osd_otable_it_key_size(const struct lu_env *env,
1690 const struct dt_it *di)
1692 return sizeof(__u64);
1695 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1696 struct dt_rec *rec, __u32 attr)
1698 struct osd_otable_it *it = (struct osd_otable_it *)di;
1699 struct lu_fid *fid = (struct lu_fid *)rec;
1705 static __u64 osd_otable_it_store(const struct lu_env *env,
1706 const struct dt_it *di)
1708 struct osd_otable_it *it = (struct osd_otable_it *)di;
1714 * Set the OSD layer iteration start position as the specified hash.
1716 static int osd_otable_it_load(const struct lu_env *env,
1717 const struct dt_it *di, __u64 hash)
1719 struct osd_otable_it *it = (struct osd_otable_it *)di;
1720 struct osd_device *dev = it->ooi_dev;
1721 struct lustre_scrub *scrub = &dev->od_scrub;
1725 /* Forbid to set iteration position after iteration started. */
1726 if (it->ooi_user_ready)
1729 if (hash > OSD_OTABLE_MAX_HASH)
1730 hash = OSD_OTABLE_MAX_HASH;
1732 /* The hash is the last checkpoint position,
1733 * we will start from the next one. */
1734 it->ooi_pos = hash + 1;
1735 it->ooi_prefetched = 0;
1736 it->ooi_prefetched_dnode = 0;
1737 it->ooi_user_ready = 1;
1738 if (!scrub->os_full_speed)
1741 /* Unplug OSD layer iteration by the first next() call. */
1742 rc = osd_otable_it_next(env, (struct dt_it *)it);
1747 static int osd_otable_it_key_rec(const struct lu_env *env,
1748 const struct dt_it *di, void *key_rec)
1753 const struct dt_index_operations osd_otable_ops = {
1755 .init = osd_otable_it_init,
1756 .fini = osd_otable_it_fini,
1757 .get = osd_otable_it_get,
1758 .put = osd_otable_it_put,
1759 .next = osd_otable_it_next,
1760 .key = osd_otable_it_key,
1761 .key_size = osd_otable_it_key_size,
1762 .rec = osd_otable_it_rec,
1763 .store = osd_otable_it_store,
1764 .load = osd_otable_it_load,
1765 .key_rec = osd_otable_it_key_rec,
1769 /* high priority inconsistent items list APIs */
1771 int osd_oii_insert(const struct lu_env *env, struct osd_device *dev,
1772 const struct lu_fid *fid, uint64_t oid, bool insert)
1774 struct lustre_scrub *scrub = &dev->od_scrub;
1775 struct osd_inconsistent_item *oii;
1776 bool wakeup = false;
1779 osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1784 INIT_LIST_HEAD(&oii->oii_list);
1785 oii->oii_cache.oic_dev = dev;
1786 oii->oii_cache.oic_fid = *fid;
1787 oii->oii_cache.oic_dnode = oid;
1788 oii->oii_insert = insert;
1790 spin_lock(&scrub->os_lock);
1791 if (!scrub->os_running) {
1792 spin_unlock(&scrub->os_lock);
1797 if (list_empty(&scrub->os_inconsistent_items))
1799 list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1800 spin_unlock(&scrub->os_lock);
1808 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1811 struct lustre_scrub *scrub = &dev->od_scrub;
1812 struct osd_inconsistent_item *oii;
1816 spin_lock(&scrub->os_lock);
1817 list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1818 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1819 *oid = oii->oii_cache.oic_dnode;
1824 spin_unlock(&scrub->os_lock);
1829 typedef int (*scan_dir_helper_t)(const struct lu_env *env,
1830 struct osd_device *dev, uint64_t dir_oid,
1831 struct osd_zap_it *ozi);
1833 static int osd_scan_dir(const struct lu_env *env, struct osd_device *dev,
1834 uint64_t id, scan_dir_helper_t cb)
1836 struct osd_zap_it *it;
1837 struct luz_direntry *zde;
1838 zap_attribute_t *za;
1843 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
1847 rc = osd_zap_cursor_init(&it->ozi_zc, dev->od_os, id, 0);
1854 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1862 if (name_is_dot_or_dotdot(za->za_name, strlen(za->za_name))) {
1863 zap_cursor_advance(it->ozi_zc);
1867 strncpy(it->ozi_name, za->za_name, sizeof(it->ozi_name));
1868 if (za->za_integer_length != 8) {
1873 rc = osd_zap_lookup(dev, it->ozi_zc->zc_zapobj, NULL,
1874 za->za_name, za->za_integer_length,
1875 sizeof(*zde) / za->za_integer_length, zde);
1879 rc = cb(env, dev, id, it);
1883 zap_cursor_advance(it->ozi_zc);
1885 osd_zap_cursor_fini(it->ozi_zc);
1888 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
1892 static int osd_remove_ml_file(const struct lu_env *env, struct osd_device *dev,
1893 uint64_t dir, uint64_t id, struct lu_fid *fid,
1896 struct osd_thread_info *info = osd_oti_get(env);
1897 struct dt_object *dt;
1898 struct osd_object *obj = NULL;
1904 rc = -sa_handle_get(dev->od_os, id, NULL, SA_HDL_PRIVATE, &hdl);
1908 dt = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
1910 RETURN(PTR_ERR(dt));
1913 obj = osd_dt_obj(dt);
1914 down_read(&obj->oo_guard);
1917 rc = -sa_lookup(hdl, SA_ZPL_LINKS(dev), &nlink, sizeof(nlink));
1922 CERROR("%s: multi-link file O/%s/%s/%s has nlink %llu\n",
1923 osd_name(dev), info->oti_seq_name, info->oti_dir_name,
1928 tx = dmu_tx_create(dev->od_os);
1930 CERROR("%s: fail to create tx to remove multi-link file!\n",
1932 GOTO(out, rc = -ENOMEM);
1935 dmu_tx_hold_zap(tx, dir, FALSE, NULL);
1936 rc = -dmu_tx_assign(tx, TXG_WAIT);
1941 rc = -sa_update(hdl, SA_ZPL_LINKS(dev), &nlink, sizeof(nlink), tx);
1945 rc = -zap_remove(dev->od_os, dir, name, tx);
1957 up_read(&obj->oo_guard);
1958 dt_object_put_nocache(env, dt);
1961 sa_handle_destroy(hdl);
1965 static int osd_scan_ml_file(const struct lu_env *env, struct osd_device *dev,
1966 uint64_t dir_oid, struct osd_zap_it *ozi)
1968 struct osd_thread_info *info = osd_oti_get(env);
1969 struct lu_fid *fid = &info->oti_fid;
1970 struct ost_id *ostid = &info->oti_ostid;
1977 rc = osd_get_fid_by_oid(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode, fid);
1982 fid_to_ostid(fid, ostid);
1984 snprintf(name, sizeof(name), (fid_seq_is_rsvd(seq) ||
1985 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
1986 fid_seq_is_idif(seq) ? 0 : seq);
1987 if (strcmp(info->oti_seq_name, name) != 0)
1990 snprintf(name, sizeof(name), "d%d",
1991 (int)ostid_id(ostid) % OSD_OST_MAP_SIZE);
1992 if (strcmp(info->oti_dir_name, name) != 0)
1995 snprintf(name, sizeof(name), "%llu", ostid_id(ostid));
1996 if (strcmp(ozi->ozi_name, name) == 0)
2000 CDEBUG(D_LFSCK, "%s: the file O/%s/%s/%s is corrupted\n",
2001 osd_name(dev), info->oti_seq_name, info->oti_dir_name,
2004 rc = osd_remove_ml_file(env, dev, dir_oid,
2005 ozi->ozi_zde.lzd_reg.zde_dnode, fid,
2010 static int osd_scan_ml_file_dir(const struct lu_env *env,
2011 struct osd_device *dev, uint64_t dir_oid,
2012 struct osd_zap_it *ozi)
2014 struct osd_thread_info *info = osd_oti_get(env);
2016 if (!S_ISDIR(cpu_to_le16(DTTOIF(ozi->ozi_zde.lzd_reg.zde_type))))
2019 info->oti_dir_name = ozi->ozi_name;
2020 return osd_scan_dir(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode,
2024 static int osd_scan_ml_file_seq(const struct lu_env *env,
2025 struct osd_device *dev, uint64_t dir_oid,
2026 struct osd_zap_it *ozi)
2028 struct osd_thread_info *info = osd_oti_get(env);
2030 if (!S_ISDIR(cpu_to_le16(DTTOIF(ozi->ozi_zde.lzd_reg.zde_type))))
2033 info->oti_seq_name = ozi->ozi_name;
2034 return osd_scan_dir(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode,
2035 osd_scan_ml_file_dir);
2038 static int osd_scan_ml_file_main(const struct lu_env *env,
2039 struct osd_device *dev)
2041 return osd_scan_dir(env, dev, dev->od_O_id, osd_scan_ml_file_seq);