+/*
+ * \ret -ENODATA: unrecognized stripe
+ * \ret = 0 : recognized stripe
+ * \ret < 0 : other failures
+ */
+static int lfsck_layout_master_check_pairs(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lu_fid *cfid,
+ struct lu_fid *pfid)
+{
+ struct lfsck_thread_info *info = lfsck_env_info(env);
+ struct lu_buf *buf = &info->lti_big_buf;
+ struct ost_id *oi = &info->lti_oi;
+ struct dt_object *obj;
+ struct lov_mds_md_v1 *lmm;
+ struct lov_ost_data_v1 *objs;
+ __u32 idx = pfid->f_stripe_idx;
+ __u32 magic;
+ int rc = 0;
+ int i;
+ __u16 count;
+ ENTRY;
+
+ pfid->f_ver = 0;
+ obj = lfsck_object_find_by_dev(env, com->lc_lfsck->li_bottom, pfid);
+ if (IS_ERR(obj))
+ RETURN(PTR_ERR(obj));
+
+ dt_read_lock(env, obj, 0);
+ if (unlikely(!dt_object_exists(obj)))
+ GOTO(unlock, rc = -ENOENT);
+
+ rc = lfsck_layout_get_lovea(env, obj, buf, NULL);
+ if (rc < 0)
+ GOTO(unlock, rc);
+
+ if (rc == 0)
+ GOTO(unlock, rc = -ENODATA);
+
+ lmm = buf->lb_buf;
+ rc = lfsck_layout_verify_header(lmm);
+ if (rc != 0)
+ GOTO(unlock, rc);
+
+ /* Currently, we only support LOV_MAGIC_V1/LOV_MAGIC_V3 which has
+ * been verified in lfsck_layout_verify_header() already. If some
+ * new magic introduced in the future, then layout LFSCK needs to
+ * be updated also. */
+ magic = le32_to_cpu(lmm->lmm_magic);
+ if (magic == LOV_MAGIC_V1) {
+ objs = &lmm->lmm_objects[0];
+ } else {
+ LASSERT(magic == LOV_MAGIC_V3);
+ objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
+ }
+
+ fid_to_ostid(cfid, oi);
+ count = le16_to_cpu(lmm->lmm_stripe_count);
+ for (i = 0; i < count; i++, objs++) {
+ struct ost_id oi2;
+
+ ostid_le_to_cpu(&objs->l_ost_oi, &oi2);
+ if (memcmp(oi, &oi2, sizeof(*oi)) == 0)
+ GOTO(unlock, rc = (i != idx ? -ENODATA : 0));
+ }
+
+ GOTO(unlock, rc = -ENODATA);
+
+unlock:
+ dt_read_unlock(env, obj);
+ lu_object_put(env, &obj->do_lu);
+
+ return rc;
+}
+
+/*
+ * The LFSCK-on-OST will ask the LFSCK-on-MDT to check whether the given
+ * MDT-object/OST-object pairs match or not to aviod transfer MDT-object
+ * layout EA from MDT to OST. On one hand, the OST no need to understand
+ * the layout EA structure; on the other hand, it may cause trouble when
+ * transfer large layout EA from MDT to OST via normal OUT RPC.
+ *
+ * \ret > 0: unrecognized stripe
+ * \ret = 0: recognized stripe
+ * \ret < 0: other failures
+ */
+static int lfsck_layout_slave_check_pairs(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lu_fid *cfid,
+ struct lu_fid *pfid)
+{
+ struct lfsck_instance *lfsck = com->lc_lfsck;
+ struct obd_device *obd = lfsck->li_obd;
+ struct seq_server_site *ss =
+ lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
+ struct obd_export *exp = NULL;
+ struct ptlrpc_request *req = NULL;
+ struct lfsck_request *lr;
+ struct lu_seq_range range = { 0 };
+ int rc = 0;
+ ENTRY;
+
+ if (unlikely(fid_is_idif(pfid)))
+ RETURN(1);
+
+ fld_range_set_any(&range);
+ rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(pfid), &range);
+ if (rc != 0)
+ RETURN(rc == -ENOENT ? 1 : rc);
+
+ if (unlikely(!fld_range_is_mdt(&range)))
+ RETURN(1);
+
+ exp = lustre_find_lwp_by_index(obd->obd_name, range.lsr_index);
+ if (unlikely(exp == NULL))
+ RETURN(1);
+
+ if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
+ GOTO(out, rc = -EOPNOTSUPP);
+
+ req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
+ if (req == NULL)
+ GOTO(out, rc = -ENOMEM);
+
+ rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
+ if (rc != 0) {
+ ptlrpc_request_free(req);
+
+ GOTO(out, rc);
+ }
+
+ lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
+ memset(lr, 0, sizeof(*lr));
+ lr->lr_event = LE_PAIRS_VERIFY;
+ lr->lr_active = LFSCK_TYPE_LAYOUT;
+ lr->lr_fid = *cfid; /* OST-object itself FID. */
+ lr->lr_fid2 = *pfid; /* The claimed parent FID. */
+
+ ptlrpc_request_set_replen(req);
+ rc = ptlrpc_queue_wait(req);
+ ptlrpc_req_finished(req);
+
+ if (rc == -ENOENT || rc == -ENODATA)
+ rc = 1;
+
+ GOTO(out, rc);
+
+out:
+ if (exp != NULL)
+ class_export_put(exp);
+
+ return rc;
+}
+
+static int lfsck_layout_slave_repair_pfid(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_request *lr)
+{
+ struct lfsck_thread_info *info = lfsck_env_info(env);
+ struct filter_fid *ff = &info->lti_new_pfid;
+ struct lu_buf *buf;
+ struct dt_device *dev = com->lc_lfsck->li_bottom;
+ struct dt_object *obj;
+ struct thandle *th = NULL;
+ int rc = 0;
+ ENTRY;
+
+ obj = lfsck_object_find_by_dev(env, dev, &lr->lr_fid);
+ if (IS_ERR(obj))
+ GOTO(log, rc = PTR_ERR(obj));
+
+ fid_cpu_to_le(&ff->ff_parent, &lr->lr_fid2);
+ buf = lfsck_buf_get(env, ff, sizeof(*ff));
+ dt_write_lock(env, obj, 0);
+ if (unlikely(!dt_object_exists(obj)))
+ GOTO(unlock, rc = 0);
+
+ th = dt_trans_create(env, dev);
+ if (IS_ERR(th))
+ GOTO(unlock, rc = PTR_ERR(th));
+
+ rc = dt_declare_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = dt_trans_start_local(env, dev, th);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = dt_xattr_set(env, obj, buf, XATTR_NAME_FID, 0, th, BYPASS_CAPA);
+
+ GOTO(stop, rc);
+
+stop:
+ dt_trans_stop(env, dev, th);
+
+unlock:
+ dt_write_unlock(env, obj);
+ lu_object_put(env, &obj->do_lu);
+
+log:
+ CDEBUG(D_LFSCK, "%s: layout LFSCK slave repaired pfid for "DFID
+ ", parent "DFID": rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
+ PFID(&lr->lr_fid), PFID(&lr->lr_fid2), rc);
+
+ return rc;
+}
+