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_first_entry(&scrub->os_inconsistent_items,
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 (CFS_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 (CFS_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 (CFS_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_first_entry(&scrub->os_inconsistent_items,
364 struct osd_inconsistent_item,
366 *fid = oii->oii_cache.oic_fid;
367 *oid = oii->oii_cache.oic_dnode;
368 scrub->os_in_prior = 1;
369 spin_unlock(&scrub->os_lock);
373 spin_unlock(&scrub->os_lock);
376 if (!scrub->os_full_speed && !osd_scrub_has_window(it))
377 wait_var_event(scrub, osd_scrub_wakeup(scrub, it));
379 if (kthread_should_stop())
380 GOTO(out, rc = SCRUB_NEXT_EXIT);
382 rc = -dmu_object_next(dev->od_os, &scrub->os_pos_current, B_FALSE, 0);
384 GOTO(out, rc = (rc == -ESRCH ? SCRUB_NEXT_BREAK : rc));
386 rc = __osd_xattr_load_by_oid(dev, scrub->os_pos_current, &nvbuf);
387 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
393 LASSERT(nvbuf != NULL);
394 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
395 (uchar_t **)&lma, &size);
397 lustre_lma_swab(lma);
398 if (likely(!(lma->lma_compat & LMAC_NOT_IN_OI) &&
399 !(lma->lma_incompat & LMAI_AGENT))) {
400 *fid = lma->lma_self_fid;
401 *oid = scrub->os_pos_current;
407 if (!scrub->os_full_speed) {
408 spin_lock(&scrub->os_lock);
409 it->ooi_prefetched++;
410 if (it->ooi_waiting) {
414 spin_unlock(&scrub->os_lock);
426 static int osd_scrub_exec(const struct lu_env *env, struct osd_device *dev,
427 const struct lu_fid *fid, uint64_t oid, int rc)
429 struct lustre_scrub *scrub = &dev->od_scrub;
430 struct osd_otable_it *it = dev->od_otable_it;
432 rc = osd_scrub_check_update(env, dev, fid, oid, rc);
433 if (!scrub->os_in_prior) {
434 if (!scrub->os_full_speed) {
435 spin_lock(&scrub->os_lock);
436 it->ooi_prefetched++;
437 if (it->ooi_waiting) {
441 spin_unlock(&scrub->os_lock);
444 spin_lock(&scrub->os_lock);
445 scrub->os_in_prior = 0;
446 spin_unlock(&scrub->os_lock);
452 rc = scrub_checkpoint(env, scrub);
454 CDEBUG(D_LFSCK, "%s: fail to checkpoint, pos = %llu: "
455 "rc = %d\n", scrub->os_name, scrub->os_pos_current, rc);
456 /* Continue, as long as the scrub itself can go ahead. */
462 static int osd_scan_ml_file_main(const struct lu_env *env,
463 struct osd_device *dev);
465 static int osd_scrub_main(void *args)
468 struct osd_device *dev = (struct osd_device *)args;
469 struct lustre_scrub *scrub = &dev->od_scrub;
475 rc = lu_env_init(&env, LCT_LOCAL | LCT_DT_THREAD);
477 CDEBUG(D_LFSCK, "%s: OI scrub fail to init env: rc = %d\n",
482 rc = scrub_thread_prep(&env, scrub, dev->od_uuid, 1);
484 CDEBUG(D_LFSCK, "%s: OI scrub fail to scrub prep: rc = %d\n",
489 if (!scrub->os_full_speed) {
490 struct osd_otable_it *it = dev->od_otable_it;
492 wait_var_event(scrub,
493 it->ooi_user_ready ||
494 kthread_should_stop());
496 if (kthread_should_stop())
499 scrub->os_pos_current = it->ooi_pos;
502 CDEBUG(D_LFSCK, "%s: OI scrub start, flags = 0x%x, pos = %llu\n",
503 scrub->os_name, scrub->os_start_flags,
504 scrub->os_pos_current);
506 fid = &osd_oti_get(&env)->oti_fid;
507 while (!rc && !kthread_should_stop()) {
508 rc = osd_scrub_next(&env, dev, fid, &oid);
510 case SCRUB_NEXT_EXIT:
512 case SCRUB_NEXT_CRASH:
513 spin_lock(&scrub->os_lock);
514 scrub->os_running = 0;
515 spin_unlock(&scrub->os_lock);
516 GOTO(out, rc = -EINVAL);
517 case SCRUB_NEXT_FATAL:
518 GOTO(post, rc = -EINVAL);
519 case SCRUB_NEXT_BREAK:
523 rc = osd_scrub_exec(&env, dev, fid, oid, rc);
529 if (scrub->os_has_ml_file) {
530 ret = osd_scan_ml_file_main(&env, dev);
535 rc = scrub_thread_post(&env, &dev->od_scrub, rc);
536 CDEBUG(D_LFSCK, "%s: OI scrub: stop, pos = %llu: rc = %d\n",
537 scrub->os_name, scrub->os_pos_current, rc);
540 while (!list_empty(&scrub->os_inconsistent_items)) {
541 struct osd_inconsistent_item *oii;
543 oii = list_first_entry(&scrub->os_inconsistent_items,
544 struct osd_inconsistent_item, oii_list);
545 list_del_init(&oii->oii_list);
552 spin_lock(&scrub->os_lock);
553 scrub->os_running = 0;
554 spin_unlock(&scrub->os_lock);
555 if (xchg(&scrub->os_task, NULL) == NULL)
556 /* scrub_stop is waiting, we need to synchronize */
557 wait_var_event(scrub, kthread_should_stop());
562 /* initial OI scrub */
566 typedef int (*handle_dirent_t)(const struct lu_env *, struct osd_device *,
567 const char *, uint64_t, uint64_t,
568 enum osd_lf_flags, bool);
569 static int osd_ios_varfid_hd(const struct lu_env *, struct osd_device *,
570 const char *, uint64_t, uint64_t,
571 enum osd_lf_flags, bool);
572 static int osd_ios_uld_hd(const struct lu_env *, struct osd_device *,
573 const char *, uint64_t, uint64_t,
574 enum osd_lf_flags, bool);
576 typedef int (*scan_dir_t)(const struct lu_env *, struct osd_device *,
577 uint64_t, handle_dirent_t, enum osd_lf_flags);
578 static int osd_ios_general_sd(const struct lu_env *, struct osd_device *,
579 uint64_t, handle_dirent_t, enum osd_lf_flags);
580 static int osd_ios_ROOT_sd(const struct lu_env *, struct osd_device *,
581 uint64_t, handle_dirent_t, enum osd_lf_flags);
585 struct lu_fid olm_fid;
586 enum osd_lf_flags olm_flags;
587 scan_dir_t olm_scan_dir;
588 handle_dirent_t olm_handle_dirent;
591 /* Add the new introduced local files in the list in the future. */
592 static const struct osd_lf_map osd_lf_maps[] = {
595 .olm_name = MOUNT_CONFIGS_DIR,
597 .f_seq = FID_SEQ_LOCAL_FILE,
598 .f_oid = MGS_CONFIGS_OID,
600 .olm_flags = OLF_SCAN_SUBITEMS,
601 .olm_scan_dir = osd_ios_general_sd,
602 .olm_handle_dirent = osd_ios_varfid_hd,
605 /* NIDTBL_VERSIONS */
607 .olm_name = MGS_NIDTBL_DIR,
608 .olm_flags = OLF_SCAN_SUBITEMS,
609 .olm_scan_dir = osd_ios_general_sd,
610 .olm_handle_dirent = osd_ios_varfid_hd,
615 .olm_name = MDT_ORPHAN_DIR,
622 .f_seq = FID_SEQ_ROOT,
623 .f_oid = FID_OID_ROOT,
625 .olm_flags = OLF_SCAN_SUBITEMS,
626 .olm_scan_dir = osd_ios_ROOT_sd,
633 .f_seq = FID_SEQ_LOCAL_FILE,
634 .f_oid = FLD_INDEX_OID,
638 /* changelog_catalog */
640 .olm_name = CHANGELOG_CATALOG,
643 /* changelog_users */
645 .olm_name = CHANGELOG_USERS,
651 .olm_flags = OLF_SCAN_SUBITEMS,
652 .olm_scan_dir = osd_ios_general_sd,
653 .olm_handle_dirent = osd_ios_varfid_hd,
659 .olm_flags = OLF_SCAN_SUBITEMS,
660 .olm_scan_dir = osd_ios_general_sd,
661 .olm_handle_dirent = osd_ios_varfid_hd,
666 .olm_name = LFSCK_DIR,
667 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
668 .olm_scan_dir = osd_ios_general_sd,
669 .olm_handle_dirent = osd_ios_varfid_hd,
674 .olm_name = LFSCK_BOOKMARK,
679 .olm_name = LFSCK_LAYOUT,
682 /* lfsck_namespace */
684 .olm_name = LFSCK_NAMESPACE,
687 /* OSP update logs update_log{_dir} use f_seq = FID_SEQ_UPDATE_LOG{_DIR}
688 * and f_oid = index for their log files. See lu_update_log{_dir}_fid()
689 * for more details. */
693 .olm_name = "update_log",
695 .f_seq = FID_SEQ_UPDATE_LOG,
697 .olm_flags = OLF_IDX_IN_FID,
702 .olm_name = "update_log_dir",
704 .f_seq = FID_SEQ_UPDATE_LOG_DIR,
706 .olm_flags = OLF_SCAN_SUBITEMS | OLF_IDX_IN_FID,
707 .olm_scan_dir = osd_ios_general_sd,
708 .olm_handle_dirent = osd_ios_uld_hd,
713 .olm_name = HSM_ACTIONS,
718 .olm_name = LUSTRE_NODEMAP_NAME,
723 .olm_name = INDEX_BACKUP_DIR,
725 .f_seq = FID_SEQ_LOCAL_FILE,
726 .f_oid = INDEX_BACKUP_OID,
728 .olm_flags = OLF_SCAN_SUBITEMS | OLF_NOT_BACKUP,
729 .olm_scan_dir = osd_ios_general_sd,
730 .olm_handle_dirent = osd_ios_varfid_hd,
738 /* Add the new introduced files under .lustre/ in the list in the future. */
739 static const struct osd_lf_map osd_dl_maps[] = {
744 .f_seq = FID_SEQ_DOT_LUSTRE,
745 .f_oid = FID_OID_DOT_LUSTRE_OBF,
749 /* .lustre/lost+found */
751 .olm_name = "lost+found",
753 .f_seq = FID_SEQ_DOT_LUSTRE,
754 .f_oid = FID_OID_DOT_LUSTRE_LPF,
763 struct osd_ios_item {
764 struct list_head oii_list;
766 enum osd_lf_flags oii_flags;
767 scan_dir_t oii_scan_dir;
768 handle_dirent_t oii_handle_dirent;
771 static int osd_ios_new_item(struct osd_device *dev, uint64_t parent,
772 enum osd_lf_flags flags, scan_dir_t scan_dir,
773 handle_dirent_t handle_dirent)
775 struct osd_ios_item *item;
779 CWARN("%s: initial OI scrub failed to add item for %llu\n",
780 osd_name(dev), parent);
784 INIT_LIST_HEAD(&item->oii_list);
785 item->oii_parent = parent;
786 item->oii_flags = flags;
787 item->oii_scan_dir = scan_dir;
788 item->oii_handle_dirent = handle_dirent;
789 list_add_tail(&item->oii_list, &dev->od_ios_list);
794 static bool osd_index_need_recreate(const struct lu_env *env,
795 struct osd_device *dev, uint64_t oid)
797 struct osd_thread_info *info = osd_oti_get(env);
798 zap_attribute_t *za = &info->oti_za2;
799 zap_cursor_t *zc = &info->oti_zc2;
803 zap_cursor_init_serialized(zc, dev->od_os, oid, 0);
804 rc = -zap_cursor_retrieve(zc, za);
806 if (rc && rc != -ENOENT)
812 static void osd_ios_index_register(const struct lu_env *env,
813 struct osd_device *osd,
814 const struct lu_fid *fid, uint64_t oid)
816 struct osd_thread_info *info = osd_oti_get(env);
817 zap_attribute_t *za = &info->oti_za2;
818 zap_cursor_t *zc = &info->oti_zc2;
819 struct zap_leaf_entry *le;
828 rc = __osd_obj2dnode(osd->od_os, oid, &dn);
829 if (rc == -EEXIST || rc == -ENOENT)
835 if (!osd_object_is_zap(dn))
838 rc = -sa_handle_get(osd->od_os, oid, NULL, SA_HDL_PRIVATE, &hdl);
842 rc = -sa_lookup(hdl, SA_ZPL_MODE(osd), &mode, sizeof(mode));
843 sa_handle_destroy(hdl);
850 zap_cursor_init_serialized(zc, osd->od_os, oid, 0);
851 rc = -zap_cursor_retrieve(zc, za);
853 /* Skip empty index object */
854 GOTO(fini, rc = (rc == -ENOENT ? 1 : rc));
856 if (zc->zc_zap->zap_ismicro ||
857 !(zap_f_phys(zc->zc_zap)->zap_flags & ZAP_FLAG_UINT64_KEY))
860 le = ZAP_LEAF_ENTRY(zc->zc_leaf, 0);
861 keysize = le->le_name_numints * 8;
862 recsize = za->za_integer_length * za->za_num_integers;
863 if (likely(keysize && recsize))
864 rc = osd_index_register(osd, fid, keysize, recsize);
875 CWARN("%s: failed to register index "DFID" (%u/%u): rc = %d\n",
876 osd_name(osd), PFID(fid), keysize, recsize, rc);
878 CDEBUG(D_LFSCK, "%s: registered index "DFID" (%u/%u)\n",
879 osd_name(osd), PFID(fid), keysize, recsize);
882 static void osd_index_restore(const struct lu_env *env, struct osd_device *dev,
883 struct lustre_index_restore_unit *liru, void *buf,
886 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
887 struct lu_fid *tgt_fid = &liru->liru_cfid;
888 struct lu_fid bak_fid;
892 lustre_fid2lbx(buf, tgt_fid, bufsize);
893 rc = -zap_lookup(dev->od_os, dev->od_index_backup_id, buf, 8,
894 sizeof(*zde) / 8, (void *)zde);
898 rc = osd_get_fid_by_oid(env, dev, zde->lzd_reg.zde_dnode, &bak_fid);
902 /* The OI mapping for index may be invalid, since it will be
903 * re-created, not update the OI mapping, just cache it in RAM. */
904 rc = osd_idc_find_and_init_with_oid(env, dev, tgt_fid,
907 rc = lustre_index_restore(env, &dev->od_dt_dev,
908 &liru->liru_pfid, tgt_fid, &bak_fid,
909 liru->liru_name, &dev->od_index_backup_list,
910 &dev->od_lock, buf, bufsize);
914 CDEBUG(D_WARNING, "%s: restore index '%s' with "DFID": rc = %d\n",
915 osd_name(dev), liru->liru_name, PFID(tgt_fid), rc);
919 * verify FID-in-LMA and OI entry for one object
921 * ios: Initial OI Scrub.
923 static int osd_ios_scan_one(const struct lu_env *env, struct osd_device *dev,
924 const struct lu_fid *fid, uint64_t parent,
925 uint64_t oid, const char *name,
926 enum osd_lf_flags flags)
928 struct lustre_scrub *scrub = &dev->od_scrub;
929 struct scrub_file *sf = &scrub->os_file;
930 struct lustre_mdt_attrs *lma = NULL;
931 nvlist_t *nvbuf = NULL;
940 rc = __osd_xattr_load_by_oid(dev, oid, &nvbuf);
941 if (unlikely(rc == -ENOENT || rc == -EEXIST))
944 if (rc && rc != -ENODATA) {
945 CWARN("%s: initial OI scrub failed to get lma for %llu: "
946 "rc = %d\n", osd_name(dev), oid, rc);
952 LASSERT(nvbuf != NULL);
953 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
954 (uchar_t **)&lma, &size);
955 if (rc || size == 0) {
956 LASSERT(lma == NULL);
959 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
960 lustre_lma_swab(lma);
961 if (lma->lma_compat & LMAC_NOT_IN_OI) {
966 if (lma->lma_compat & LMAC_IDX_BACKUP &&
967 osd_index_need_recreate(env, dev, oid)) {
968 if (parent == dev->od_root) {
969 lu_local_obj_fid(&tfid,
972 rc = osd_get_fid_by_oid(env, dev,
980 rc = lustre_liru_new(
981 &dev->od_index_restore_list,
982 &tfid, &lma->lma_self_fid, oid,
988 tfid = lma->lma_self_fid;
989 if (!(flags & OLF_NOT_BACKUP))
990 osd_ios_index_register(env, dev, &tfid, oid);
995 if (rc == -ENODATA) {
997 /* Skip the object without FID-in-LMA */
998 CDEBUG(D_LFSCK, "%s: %llu has no FID-in-LMA, skip it\n",
1004 LASSERT(!fid_is_zero(fid));
1007 if (flags & OLF_IDX_IN_FID) {
1008 LASSERT(dev->od_index >= 0);
1010 tfid.f_oid = dev->od_index;
1014 rc = osd_fid_lookup(env, dev, &tfid, &oid2);
1016 if (rc != -ENOENT) {
1017 CWARN("%s: initial OI scrub failed to lookup fid for "
1018 DFID"=>%llu: rc = %d\n",
1019 osd_name(dev), PFID(&tfid), oid, rc);
1024 flag = SF_RECREATED;
1025 op = DTO_INDEX_INSERT;
1030 flag = SF_INCONSISTENT;
1031 op = DTO_INDEX_UPDATE;
1034 if (!(sf->sf_flags & flag)) {
1035 scrub_file_reset(scrub, dev->od_uuid, flag);
1036 rc = scrub_file_store(env, scrub);
1041 rc = osd_scrub_refresh_mapping(env, dev, &tfid, oid, op, true, name);
1043 RETURN(rc > 0 ? 0 : rc);
1046 static int osd_ios_varfid_hd(const struct lu_env *env, struct osd_device *dev,
1047 const char *name, uint64_t parent, uint64_t oid,
1048 enum osd_lf_flags flags, bool is_dir)
1053 rc = osd_ios_scan_one(env, dev, NULL, parent, oid, name, 0);
1055 rc = osd_ios_new_item(dev, oid, flags, osd_ios_general_sd,
1061 static int osd_ios_uld_hd(const struct lu_env *env, struct osd_device *dev,
1062 const char *name, uint64_t parent, uint64_t oid,
1063 enum osd_lf_flags flags, bool is_dir)
1069 /* skip any non-DFID format name */
1073 /* skip the start '[' */
1074 sscanf(&name[1], SFID, RFID(&tfid));
1075 if (fid_is_sane(&tfid))
1076 rc = osd_ios_scan_one(env, dev, &tfid, parent, oid, name, 0);
1084 * General scanner for the directories execpt /ROOT during initial OI scrub.
1085 * It scans the name entries under the given directory one by one. For each
1086 * entry, verifies its OI mapping via the given @handle_dirent.
1088 static int osd_ios_general_sd(const struct lu_env *env, struct osd_device *dev,
1089 uint64_t parent, handle_dirent_t handle_dirent,
1090 enum osd_lf_flags flags)
1092 struct osd_thread_info *info = osd_oti_get(env);
1093 struct luz_direntry *zde = &info->oti_zde;
1094 zap_attribute_t *za = &info->oti_za;
1095 zap_cursor_t *zc = &info->oti_zc;
1099 zap_cursor_init_serialized(zc, dev->od_os, parent, 0);
1100 rc = -zap_cursor_retrieve(zc, za);
1102 zap_cursor_advance(zc);
1107 rc = -zap_cursor_retrieve(zc, za);
1109 GOTO(log, rc = (rc == -ENOENT ? 0 : rc));
1111 /* skip the entry started with '.' */
1112 if (likely(za->za_name[0] != '.')) {
1113 rc = osd_zap_lookup(dev, parent, NULL, za->za_name,
1114 za->za_integer_length,
1115 sizeof(*zde) / za->za_integer_length,
1118 CWARN("%s: initial OI scrub failed to lookup "
1119 "%s under %llu: rc = %d\n",
1120 osd_name(dev), za->za_name, parent, rc);
1124 rc = handle_dirent(env, dev, za->za_name, parent,
1125 zde->lzd_reg.zde_dnode, flags,
1126 S_ISDIR(DTTOIF(zde->lzd_reg.zde_type)) ?
1128 CDEBUG(D_LFSCK, "%s: initial OI scrub handled %s under "
1130 osd_name(dev), za->za_name, parent, rc);
1133 zap_cursor_advance(zc);
1138 CWARN("%s: initial OI scrub failed to scan the directory %llu: "
1139 "rc = %d\n", osd_name(dev), parent, rc);
1140 zap_cursor_fini(zc);
1146 * The scanner for /ROOT directory. It is not all the items under /ROOT will
1147 * be scanned during the initial OI scrub, instead, only the .lustre and the
1148 * sub-items under .lustre will be handled.
1150 static int osd_ios_ROOT_sd(const struct lu_env *env, struct osd_device *dev,
1151 uint64_t parent, handle_dirent_t handle_dirent,
1152 enum osd_lf_flags flags)
1154 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1155 const struct osd_lf_map *map;
1161 rc = osd_zap_lookup(dev, parent, NULL, dot_lustre_name, 8,
1162 sizeof(*zde) / 8, (void *)zde);
1163 if (rc == -ENOENT) {
1164 /* The .lustre directory is lost. That is not fatal. It can
1165 * be re-created in the subsequent MDT start processing. */
1170 CWARN("%s: initial OI scrub failed to find .lustre: "
1171 "rc = %d\n", osd_name(dev), rc);
1176 oid = zde->lzd_reg.zde_dnode;
1177 rc = osd_ios_scan_one(env, dev, &LU_DOT_LUSTRE_FID, parent, oid,
1178 dot_lustre_name, 0);
1182 for (map = osd_dl_maps; map->olm_name; map++) {
1183 rc = osd_zap_lookup(dev, oid, NULL, map->olm_name, 8,
1184 sizeof(*zde) / 8, (void *)zde);
1187 CWARN("%s: initial OI scrub failed to find the entry %s under .lustre: rc = %d\n",
1188 osd_name(dev), map->olm_name, rc);
1189 else if (!fid_is_zero(&map->olm_fid))
1190 /* Try to remove the stale OI mapping. */
1191 osd_scrub_refresh_mapping(env, dev,
1193 DTO_INDEX_DELETE, true,
1198 rc = osd_ios_scan_one(env, dev, &map->olm_fid, oid,
1199 zde->lzd_reg.zde_dnode, map->olm_name,
1208 static void osd_initial_OI_scrub(const struct lu_env *env,
1209 struct osd_device *dev)
1211 struct luz_direntry *zde = &osd_oti_get(env)->oti_zde;
1212 const struct osd_lf_map *map;
1216 for (map = osd_lf_maps; map->olm_name; map++) {
1217 rc = osd_zap_lookup(dev, dev->od_root, NULL, map->olm_name, 8,
1218 sizeof(*zde) / 8, (void *)zde);
1221 CWARN("%s: initial OI scrub failed "
1222 "to find the entry %s: rc = %d\n",
1223 osd_name(dev), map->olm_name, rc);
1224 else if (!fid_is_zero(&map->olm_fid))
1225 /* Try to remove the stale OI mapping. */
1226 osd_scrub_refresh_mapping(env, dev,
1228 DTO_INDEX_DELETE, true,
1233 rc = osd_ios_scan_one(env, dev, &map->olm_fid, dev->od_root,
1234 zde->lzd_reg.zde_dnode, map->olm_name,
1236 if (!rc && map->olm_flags & OLF_SCAN_SUBITEMS)
1237 osd_ios_new_item(dev, zde->lzd_reg.zde_dnode,
1238 map->olm_flags, map->olm_scan_dir,
1239 map->olm_handle_dirent);
1242 while (!list_empty(&dev->od_ios_list)) {
1243 struct osd_ios_item *item;
1245 item = list_first_entry(&dev->od_ios_list,
1246 struct osd_ios_item, oii_list);
1247 list_del_init(&item->oii_list);
1248 item->oii_scan_dir(env, dev, item->oii_parent,
1249 item->oii_handle_dirent, item->oii_flags);
1253 if (!list_empty(&dev->od_index_restore_list)) {
1256 OBD_ALLOC_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1258 CERROR("%s: not enough RAM for rebuild index\n",
1261 while (!list_empty(&dev->od_index_restore_list)) {
1262 struct lustre_index_restore_unit *liru;
1264 liru = list_first_entry(&dev->od_index_restore_list,
1265 struct lustre_index_restore_unit,
1267 list_del(&liru->liru_link);
1269 osd_index_restore(env, dev, liru, buf,
1270 INDEX_BACKUP_BUFSIZE);
1271 OBD_FREE(liru, liru->liru_len);
1275 OBD_FREE_LARGE(buf, INDEX_BACKUP_BUFSIZE);
1281 /* OI scrub start/stop */
1283 int osd_scrub_start(const struct lu_env *env, struct osd_device *dev,
1289 if (dev->od_dt_dev.dd_rdonly)
1292 /* od_otable_sem: prevent concurrent start/stop */
1293 down(&dev->od_otable_sem);
1294 rc = scrub_start(osd_scrub_main, &dev->od_scrub, dev, flags);
1295 up(&dev->od_otable_sem);
1297 RETURN(rc == -EALREADY ? 0 : rc);
1300 void osd_scrub_stop(struct osd_device *dev)
1302 struct lustre_scrub *scrub = &dev->od_scrub;
1305 /* od_otable_sem: prevent concurrent start/stop */
1306 down(&dev->od_otable_sem);
1307 spin_lock(&scrub->os_lock);
1308 scrub->os_paused = 1;
1309 spin_unlock(&scrub->os_lock);
1311 up(&dev->od_otable_sem);
1316 /* OI scrub setup/cleanup */
1318 static const char osd_scrub_name[] = "OI_scrub";
1320 int osd_scrub_setup(const struct lu_env *env, struct osd_device *dev,
1321 time64_t interval, bool resetoi)
1323 struct osd_thread_info *info = osd_oti_get(env);
1324 struct lustre_scrub *scrub = &dev->od_scrub;
1325 struct scrub_file *sf = &scrub->os_file;
1326 struct lu_fid *fid = &info->oti_fid;
1327 struct dt_object *obj;
1333 memcpy(dev->od_uuid.b,
1334 &dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid,
1335 sizeof(dsl_dataset_phys(dev->od_os->os_dsl_dataset)->ds_guid));
1336 memset(&dev->od_scrub, 0, sizeof(struct lustre_scrub));
1337 init_rwsem(&scrub->os_rwsem);
1338 spin_lock_init(&scrub->os_lock);
1339 INIT_LIST_HEAD(&scrub->os_inconsistent_items);
1340 scrub->os_name = osd_name(dev);
1341 scrub->os_auto_scrub_interval = interval;
1343 /* 'What the @fid is' is not imporatant, because the object
1344 * has no OI mapping, and only is visible inside the OSD.*/
1345 fid->f_seq = FID_SEQ_IGIF_MAX;
1347 fid->f_oid = ((1 << 31) | dev->od_index) + 1;
1349 fid->f_oid = dev->od_index + 1;
1351 rc = osd_obj_find_or_create(env, dev, dev->od_root,
1352 osd_scrub_name, &oid, fid, false);
1356 rc = osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1360 obj = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
1361 if (IS_ERR_OR_NULL(obj))
1362 RETURN(obj ? PTR_ERR(obj) : -ENOENT);
1364 obj->do_body_ops = &osd_body_scrub_ops;
1365 scrub->os_obj = obj;
1366 rc = scrub_file_load(env, scrub);
1367 if (rc == -ENOENT || rc == -EFAULT) {
1368 scrub_file_init(scrub, dev->od_uuid);
1370 } else if (rc < 0) {
1371 GOTO(cleanup_obj, rc);
1373 if (!guid_equal(&sf->sf_uuid, &dev->od_uuid)) {
1375 "%s: UUID has been changed from %pU to %pU\n",
1376 osd_name(dev), &sf->sf_uuid, &dev->od_uuid);
1377 scrub_file_reset(scrub, dev->od_uuid, SF_INCONSISTENT);
1379 } else if (sf->sf_status == SS_SCANNING) {
1380 sf->sf_status = SS_CRASHED;
1384 if (unlikely((sf->sf_oi_count & (sf->sf_oi_count - 1)) != 0 ||
1385 sf->sf_oi_count > OSD_OI_FID_NR_MAX)) {
1386 LCONSOLE_WARN("%s: invalid OI count %u, reset to %u\n",
1387 osd_name(dev), sf->sf_oi_count,
1389 sf->sf_oi_count = osd_oi_count;
1394 if (sf->sf_pos_last_checkpoint != 0)
1395 scrub->os_pos_current = sf->sf_pos_last_checkpoint + 1;
1397 scrub->os_pos_current = 1;
1400 rc = scrub_file_store(env, scrub);
1402 GOTO(cleanup_obj, rc);
1405 /* Initialize OI files. */
1406 rc = osd_oi_init(env, dev, resetoi);
1408 GOTO(cleanup_obj, rc);
1410 if (!dev->od_dt_dev.dd_rdonly)
1411 osd_initial_OI_scrub(env, dev);
1413 if (!dev->od_dt_dev.dd_rdonly &&
1414 scrub->os_auto_scrub_interval != AS_NEVER &&
1415 ((sf->sf_status == SS_PAUSED) ||
1416 (sf->sf_status == SS_CRASHED &&
1417 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1418 SF_UPGRADE | SF_AUTO)) ||
1419 (sf->sf_status == SS_INIT &&
1420 sf->sf_flags & (SF_RECREATED | SF_INCONSISTENT |
1422 rc = osd_scrub_start(env, dev, SS_AUTO_FULL);
1425 GOTO(cleanup_oi, rc);
1430 osd_oi_fini(env, dev);
1432 dt_object_put_nocache(env, scrub->os_obj);
1433 scrub->os_obj = NULL;
1438 void osd_scrub_cleanup(const struct lu_env *env, struct osd_device *dev)
1440 struct lustre_scrub *scrub = &dev->od_scrub;
1442 LASSERT(!dev->od_otable_it);
1444 if (scrub->os_obj) {
1445 osd_scrub_stop(dev);
1446 dt_object_put_nocache(env, scrub->os_obj);
1447 scrub->os_obj = NULL;
1450 if (dev->od_oi_table)
1451 osd_oi_fini(env, dev);
1454 /* object table based iteration APIs */
1456 static struct dt_it *osd_otable_it_init(const struct lu_env *env,
1457 struct dt_object *dt, __u32 attr)
1459 enum dt_otable_it_flags flags = attr >> DT_OTABLE_IT_FLAGS_SHIFT;
1460 enum dt_otable_it_valid valid = attr & ~DT_OTABLE_IT_FLAGS_MASK;
1461 struct osd_device *dev = osd_dev(dt->do_lu.lo_dev);
1462 struct lustre_scrub *scrub = &dev->od_scrub;
1463 struct osd_otable_it *it;
1468 if (dev->od_dt_dev.dd_rdonly)
1469 RETURN(ERR_PTR(-EROFS));
1471 /* od_otable_sem: prevent concurrent init/fini */
1472 down(&dev->od_otable_sem);
1473 if (dev->od_otable_it)
1474 GOTO(out, it = ERR_PTR(-EALREADY));
1478 GOTO(out, it = ERR_PTR(-ENOMEM));
1480 if (flags & DOIF_OUTUSED)
1481 it->ooi_used_outside = 1;
1483 if (flags & DOIF_RESET)
1486 if (valid & DOIV_ERROR_HANDLE) {
1487 if (flags & DOIF_FAILOUT)
1488 start |= SS_SET_FAILOUT;
1490 start |= SS_CLEAR_FAILOUT;
1493 if (valid & DOIV_DRYRUN) {
1494 if (flags & DOIF_DRYRUN)
1495 start |= SS_SET_DRYRUN;
1497 start |= SS_CLEAR_DRYRUN;
1500 /* XXX: dmu_object_next() does NOT find dnodes allocated
1501 * in the current non-committed txg, so we force txg
1502 * commit to find all existing dnodes ... */
1503 txg_wait_synced(dmu_objset_pool(dev->od_os), 0ULL);
1505 dev->od_otable_it = it;
1507 rc = scrub_start(osd_scrub_main, scrub, dev, start & ~SS_AUTO_PARTIAL);
1508 if (rc == -EALREADY) {
1510 } else if (rc < 0) {
1511 dev->od_otable_it = NULL;
1515 it->ooi_pos = scrub->os_pos_current;
1521 up(&dev->od_otable_sem);
1522 return (struct dt_it *)it;
1525 static void osd_otable_it_fini(const struct lu_env *env, struct dt_it *di)
1527 struct osd_otable_it *it = (struct osd_otable_it *)di;
1528 struct osd_device *dev = it->ooi_dev;
1530 /* od_otable_sem: prevent concurrent init/fini */
1531 down(&dev->od_otable_sem);
1532 scrub_stop(&dev->od_scrub);
1533 LASSERT(dev->od_otable_it == it);
1535 dev->od_otable_it = NULL;
1536 up(&dev->od_otable_sem);
1540 static int osd_otable_it_get(const struct lu_env *env,
1541 struct dt_it *di, const struct dt_key *key)
1546 static void osd_otable_it_put(const struct lu_env *env, struct dt_it *di)
1550 static void osd_otable_it_preload(const struct lu_env *env,
1551 struct osd_otable_it *it)
1553 struct osd_device *dev = it->ooi_dev;
1556 /* can go negative on the very first access to the iterator
1557 * or if some non-Lustre objects were found */
1558 if (unlikely(it->ooi_prefetched < 0))
1559 it->ooi_prefetched = 0;
1561 if (it->ooi_prefetched >= (OTABLE_PREFETCH >> 1))
1564 if (it->ooi_prefetched_dnode == 0)
1565 it->ooi_prefetched_dnode = it->ooi_pos;
1567 while (it->ooi_prefetched < OTABLE_PREFETCH) {
1568 rc = -dmu_object_next(dev->od_os, &it->ooi_prefetched_dnode,
1573 osd_dmu_prefetch(dev->od_os, it->ooi_prefetched_dnode,
1574 0, 0, 0, ZIO_PRIORITY_ASYNC_READ);
1575 it->ooi_prefetched++;
1580 osd_otable_it_wakeup(struct lustre_scrub *scrub, struct osd_otable_it *it)
1582 spin_lock(&scrub->os_lock);
1583 if (it->ooi_pos < scrub->os_pos_current || scrub->os_waiting ||
1585 it->ooi_waiting = 0;
1587 it->ooi_waiting = 1;
1588 spin_unlock(&scrub->os_lock);
1590 return !it->ooi_waiting;
1593 static int osd_otable_it_next(const struct lu_env *env, struct dt_it *di)
1595 struct osd_otable_it *it = (struct osd_otable_it *)di;
1596 struct osd_device *dev = it->ooi_dev;
1597 struct lustre_scrub *scrub = &dev->od_scrub;
1598 struct lustre_mdt_attrs *lma = NULL;
1599 nvlist_t *nvbuf = NULL;
1604 LASSERT(it->ooi_user_ready);
1605 fid_zero(&it->ooi_fid);
1607 if (unlikely(it->ooi_all_cached))
1618 if (it->ooi_pos >= scrub->os_pos_current)
1619 wait_var_event(scrub,
1620 osd_otable_it_wakeup(scrub, it));
1622 if (!scrub->os_running && !it->ooi_used_outside)
1625 rc = -dmu_object_next(dev->od_os, &it->ooi_pos, B_FALSE, 0);
1627 if (unlikely(rc == -ESRCH)) {
1628 it->ooi_all_cached = 1;
1635 rc = __osd_xattr_load_by_oid(dev, it->ooi_pos, &nvbuf);
1638 if (!scrub->os_full_speed) {
1639 spin_lock(&scrub->os_lock);
1642 it->ooi_prefetched--;
1643 if (!scrub->os_full_speed) {
1644 if (scrub->os_waiting) {
1645 scrub->os_waiting = 0;
1650 spin_unlock(&scrub->os_lock);
1652 if (rc == -ENOENT || rc == -EEXIST || rc == -ENODATA)
1658 LASSERT(nvbuf != NULL);
1659 rc = -nvlist_lookup_byte_array(nvbuf, XATTR_NAME_LMA,
1660 (uchar_t **)&lma, &size);
1661 if (rc || size == 0)
1662 /* It is either non-Lustre object or OSD internal object,
1663 * ignore it, go ahead */
1666 LASSERTF(lma != NULL, "corrupted LMA, size %d\n", size);
1667 lustre_lma_swab(lma);
1668 if (unlikely(lma->lma_compat & LMAC_NOT_IN_OI ||
1669 lma->lma_incompat & LMAI_AGENT))
1672 it->ooi_fid = lma->lma_self_fid;
1680 if (!rc && scrub->os_full_speed)
1681 osd_otable_it_preload(env, it);
1686 static struct dt_key *osd_otable_it_key(const struct lu_env *env,
1687 const struct dt_it *di)
1692 static int osd_otable_it_key_size(const struct lu_env *env,
1693 const struct dt_it *di)
1695 return sizeof(__u64);
1698 static int osd_otable_it_rec(const struct lu_env *env, const struct dt_it *di,
1699 struct dt_rec *rec, __u32 attr)
1701 struct osd_otable_it *it = (struct osd_otable_it *)di;
1702 struct lu_fid *fid = (struct lu_fid *)rec;
1708 static __u64 osd_otable_it_store(const struct lu_env *env,
1709 const struct dt_it *di)
1711 struct osd_otable_it *it = (struct osd_otable_it *)di;
1717 * Set the OSD layer iteration start position as the specified hash.
1719 static int osd_otable_it_load(const struct lu_env *env,
1720 const struct dt_it *di, __u64 hash)
1722 struct osd_otable_it *it = (struct osd_otable_it *)di;
1723 struct osd_device *dev = it->ooi_dev;
1724 struct lustre_scrub *scrub = &dev->od_scrub;
1728 /* Forbid to set iteration position after iteration started. */
1729 if (it->ooi_user_ready)
1732 if (hash > OSD_OTABLE_MAX_HASH)
1733 hash = OSD_OTABLE_MAX_HASH;
1735 /* The hash is the last checkpoint position,
1736 * we will start from the next one. */
1737 it->ooi_pos = hash + 1;
1738 it->ooi_prefetched = 0;
1739 it->ooi_prefetched_dnode = 0;
1740 it->ooi_user_ready = 1;
1741 if (!scrub->os_full_speed)
1744 /* Unplug OSD layer iteration by the first next() call. */
1745 rc = osd_otable_it_next(env, (struct dt_it *)it);
1750 static int osd_otable_it_key_rec(const struct lu_env *env,
1751 const struct dt_it *di, void *key_rec)
1756 const struct dt_index_operations osd_otable_ops = {
1758 .init = osd_otable_it_init,
1759 .fini = osd_otable_it_fini,
1760 .get = osd_otable_it_get,
1761 .put = osd_otable_it_put,
1762 .next = osd_otable_it_next,
1763 .key = osd_otable_it_key,
1764 .key_size = osd_otable_it_key_size,
1765 .rec = osd_otable_it_rec,
1766 .store = osd_otable_it_store,
1767 .load = osd_otable_it_load,
1768 .key_rec = osd_otable_it_key_rec,
1772 /* high priority inconsistent items list APIs */
1774 int osd_oii_insert(const struct lu_env *env, struct osd_device *dev,
1775 const struct lu_fid *fid, uint64_t oid, bool insert)
1777 struct lustre_scrub *scrub = &dev->od_scrub;
1778 struct osd_inconsistent_item *oii;
1779 bool wakeup = false;
1782 osd_idc_find_and_init_with_oid(env, dev, fid, oid);
1787 INIT_LIST_HEAD(&oii->oii_list);
1788 oii->oii_cache.oic_dev = dev;
1789 oii->oii_cache.oic_fid = *fid;
1790 oii->oii_cache.oic_dnode = oid;
1791 oii->oii_insert = insert;
1793 spin_lock(&scrub->os_lock);
1794 if (!scrub->os_running) {
1795 spin_unlock(&scrub->os_lock);
1800 if (list_empty(&scrub->os_inconsistent_items))
1802 list_add_tail(&oii->oii_list, &scrub->os_inconsistent_items);
1803 spin_unlock(&scrub->os_lock);
1811 int osd_oii_lookup(struct osd_device *dev, const struct lu_fid *fid,
1814 struct lustre_scrub *scrub = &dev->od_scrub;
1815 struct osd_inconsistent_item *oii;
1819 spin_lock(&scrub->os_lock);
1820 list_for_each_entry(oii, &scrub->os_inconsistent_items, oii_list) {
1821 if (lu_fid_eq(fid, &oii->oii_cache.oic_fid)) {
1822 *oid = oii->oii_cache.oic_dnode;
1827 spin_unlock(&scrub->os_lock);
1832 typedef int (*scan_dir_helper_t)(const struct lu_env *env,
1833 struct osd_device *dev, uint64_t dir_oid,
1834 struct osd_zap_it *ozi);
1836 static int osd_scan_dir(const struct lu_env *env, struct osd_device *dev,
1837 uint64_t id, scan_dir_helper_t cb)
1839 struct osd_zap_it *it;
1840 struct luz_direntry *zde;
1841 zap_attribute_t *za;
1846 OBD_SLAB_ALLOC_PTR_GFP(it, osd_zapit_cachep, GFP_NOFS);
1850 rc = osd_zap_cursor_init(&it->ozi_zc, dev->od_os, id, 0);
1857 rc = -zap_cursor_retrieve(it->ozi_zc, za);
1865 if (name_is_dot_or_dotdot(za->za_name, strlen(za->za_name))) {
1866 zap_cursor_advance(it->ozi_zc);
1870 strncpy(it->ozi_name, za->za_name, sizeof(it->ozi_name));
1871 if (za->za_integer_length != 8) {
1876 rc = osd_zap_lookup(dev, it->ozi_zc->zc_zapobj, NULL,
1877 za->za_name, za->za_integer_length,
1878 sizeof(*zde) / za->za_integer_length, zde);
1882 rc = cb(env, dev, id, it);
1886 zap_cursor_advance(it->ozi_zc);
1888 osd_zap_cursor_fini(it->ozi_zc);
1891 OBD_SLAB_FREE_PTR(it, osd_zapit_cachep);
1895 static int osd_remove_ml_file(const struct lu_env *env, struct osd_device *dev,
1896 uint64_t dir, uint64_t id, struct lu_fid *fid,
1899 struct osd_thread_info *info = osd_oti_get(env);
1900 struct dt_object *dt;
1901 struct osd_object *obj = NULL;
1907 rc = -sa_handle_get(dev->od_os, id, NULL, SA_HDL_PRIVATE, &hdl);
1911 dt = lu2dt(lu_object_find_slice(env, osd2lu_dev(dev), fid, NULL));
1913 RETURN(PTR_ERR(dt));
1916 obj = osd_dt_obj(dt);
1917 down_read(&obj->oo_guard);
1920 rc = -sa_lookup(hdl, SA_ZPL_LINKS(dev), &nlink, sizeof(nlink));
1925 CERROR("%s: multi-link file O/%s/%s/%s has nlink %llu\n",
1926 osd_name(dev), info->oti_seq_name, info->oti_dir_name,
1931 tx = dmu_tx_create(dev->od_os);
1933 CERROR("%s: fail to create tx to remove multi-link file!\n",
1935 GOTO(out, rc = -ENOMEM);
1938 dmu_tx_hold_zap(tx, dir, FALSE, NULL);
1939 rc = -dmu_tx_assign(tx, TXG_WAIT);
1944 rc = -sa_update(hdl, SA_ZPL_LINKS(dev), &nlink, sizeof(nlink), tx);
1948 rc = -zap_remove(dev->od_os, dir, name, tx);
1960 up_read(&obj->oo_guard);
1961 dt_object_put_nocache(env, dt);
1964 sa_handle_destroy(hdl);
1968 static int osd_scan_ml_file(const struct lu_env *env, struct osd_device *dev,
1969 uint64_t dir_oid, struct osd_zap_it *ozi)
1971 struct osd_thread_info *info = osd_oti_get(env);
1972 struct lu_fid *fid = &info->oti_fid;
1973 struct ost_id *ostid = &info->oti_ostid;
1980 rc = osd_get_fid_by_oid(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode, fid);
1985 fid_to_ostid(fid, ostid);
1987 snprintf(name, sizeof(name), (fid_seq_is_rsvd(seq) ||
1988 fid_seq_is_mdt0(seq)) ? "%llu" : "%llx",
1989 fid_seq_is_idif(seq) ? 0 : seq);
1990 if (strcmp(info->oti_seq_name, name) != 0)
1993 snprintf(name, sizeof(name), "d%d",
1994 (int)ostid_id(ostid) % OSD_OST_MAP_SIZE);
1995 if (strcmp(info->oti_dir_name, name) != 0)
1998 snprintf(name, sizeof(name), "%llu", ostid_id(ostid));
1999 if (strcmp(ozi->ozi_name, name) == 0)
2003 CDEBUG(D_LFSCK, "%s: the file O/%s/%s/%s is corrupted\n",
2004 osd_name(dev), info->oti_seq_name, info->oti_dir_name,
2007 rc = osd_remove_ml_file(env, dev, dir_oid,
2008 ozi->ozi_zde.lzd_reg.zde_dnode, fid,
2013 static int osd_scan_ml_file_dir(const struct lu_env *env,
2014 struct osd_device *dev, uint64_t dir_oid,
2015 struct osd_zap_it *ozi)
2017 struct osd_thread_info *info = osd_oti_get(env);
2019 if (!S_ISDIR(cpu_to_le16(DTTOIF(ozi->ozi_zde.lzd_reg.zde_type))))
2022 info->oti_dir_name = ozi->ozi_name;
2023 return osd_scan_dir(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode,
2027 static int osd_scan_ml_file_seq(const struct lu_env *env,
2028 struct osd_device *dev, uint64_t dir_oid,
2029 struct osd_zap_it *ozi)
2031 struct osd_thread_info *info = osd_oti_get(env);
2033 if (!S_ISDIR(cpu_to_le16(DTTOIF(ozi->ozi_zde.lzd_reg.zde_type))))
2036 info->oti_seq_name = ozi->ozi_name;
2037 return osd_scan_dir(env, dev, ozi->ozi_zde.lzd_reg.zde_dnode,
2038 osd_scan_ml_file_dir);
2041 static int osd_scan_ml_file_main(const struct lu_env *env,
2042 struct osd_device *dev)
2044 return osd_scan_dir(env, dev, dev->od_O_id, osd_scan_ml_file_seq);