+/* If there are more than one MDT-objects claim as the OST-object's parent,
+ * and the OST-object only recognizes one of them, then we need to generate
+ * new OST-object(s) with new fid(s) for the non-recognized MDT-object(s). */
+static int lfsck_layout_repair_multiple_references(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_layout_req *llr,
+ struct lu_attr *la,
+ struct lu_buf *buf)
+{
+ struct lfsck_thread_info *info = lfsck_env_info(env);
+ struct dt_allocation_hint *hint = &info->lti_hint;
+ struct dt_object_format *dof = &info->lti_dof;
+ struct dt_device *pdev = com->lc_lfsck->li_next;
+ struct ost_id *oi = &info->lti_oi;
+ struct dt_object *parent = llr->llr_parent->llo_obj;
+ struct dt_device *cdev = lfsck_obj2dt_dev(llr->llr_child);
+ struct dt_object *child = NULL;
+ struct lu_device *d = &cdev->dd_lu_dev;
+ struct lu_object *o = NULL;
+ struct thandle *handle;
+ struct lov_mds_md_v1 *lmm;
+ struct lov_ost_data_v1 *objs;
+ struct lustre_handle lh = { 0 };
+ __u32 magic;
+ int rc;
+ ENTRY;
+
+ CDEBUG(D_LFSCK, "Repair multiple references for: parent "DFID
+ ", OST-index %u, stripe-index %u, owner %u:%u\n",
+ PFID(lfsck_dto2fid(parent)), llr->llr_ost_idx,
+ llr->llr_lov_idx, la->la_uid, la->la_gid);
+
+ rc = lfsck_layout_lock(env, com, parent, &lh,
+ MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR);
+ if (rc != 0)
+ RETURN(rc);
+
+ handle = dt_trans_create(env, pdev);
+ if (IS_ERR(handle))
+ GOTO(unlock1, rc = PTR_ERR(handle));
+
+ o = lu_object_anon(env, d, NULL);
+ if (IS_ERR(o))
+ GOTO(stop, rc = PTR_ERR(o));
+
+ child = container_of(o, struct dt_object, do_lu);
+ o = lu_object_locate(o->lo_header, d->ld_type);
+ if (unlikely(o == NULL))
+ GOTO(stop, rc = -EINVAL);
+
+ child = container_of(o, struct dt_object, do_lu);
+ la->la_valid = LA_UID | LA_GID;
+ hint->dah_parent = NULL;
+ hint->dah_mode = 0;
+ dof->dof_type = DFT_REGULAR;
+ rc = dt_declare_create(env, child, la, NULL, NULL, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
+ LU_XATTR_REPLACE, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = dt_trans_start(env, pdev, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ dt_write_lock(env, parent, 0);
+ if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
+ GOTO(unlock2, rc = 0);
+
+ rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV, BYPASS_CAPA);
+ if (unlikely(rc == 0 || rc == -ENODATA || rc == -ERANGE))
+ GOTO(unlock2, rc = 0);
+
+ lmm = buf->lb_buf;
+ rc = lfsck_layout_verify_header(lmm);
+ if (rc != 0)
+ GOTO(unlock2, rc);
+
+ /* Someone change layout during the LFSCK, no need to repair then. */
+ if (le16_to_cpu(lmm->lmm_layout_gen) != llr->llr_parent->llo_gen)
+ GOTO(unlock2, rc = 0);
+
+ rc = dt_create(env, child, la, hint, dof, handle);
+ if (rc != 0)
+ GOTO(unlock2, 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];
+ }
+
+ lmm->lmm_layout_gen = cpu_to_le16(llr->llr_parent->llo_gen + 1);
+ fid_to_ostid(lu_object_fid(&child->do_lu), oi);
+ ostid_cpu_to_le(oi, &objs[llr->llr_lov_idx].l_ost_oi);
+ objs[llr->llr_lov_idx].l_ost_gen = cpu_to_le32(0);
+ objs[llr->llr_lov_idx].l_ost_idx = cpu_to_le32(llr->llr_ost_idx);
+ rc = dt_xattr_set(env, parent, buf, XATTR_NAME_LOV,
+ LU_XATTR_REPLACE, handle, BYPASS_CAPA);
+
+ GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
+
+unlock2:
+ dt_write_unlock(env, parent);
+
+stop:
+ if (child != NULL)
+ lu_object_put(env, &child->do_lu);
+
+ dt_trans_stop(env, pdev, handle);
+
+unlock1:
+ lfsck_layout_unlock(&lh);
+
+ return rc;
+}
+
+/* If the MDT-object and the OST-object have different owner information,
+ * then trust the MDT-object, because the normal chown/chgrp handle order
+ * is from MDT to OST, and it is possible that some chown/chgrp operation
+ * is partly done. */
+static int lfsck_layout_repair_owner(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct lfsck_layout_req *llr,
+ struct lu_attr *pla)
+{
+ struct lfsck_thread_info *info = lfsck_env_info(env);
+ struct lu_attr *tla = &info->lti_la3;
+ struct dt_object *parent = llr->llr_parent->llo_obj;
+ struct dt_object *child = llr->llr_child;
+ struct dt_device *dev = lfsck_obj2dt_dev(child);
+ struct thandle *handle;
+ int rc;
+ ENTRY;
+
+ CDEBUG(D_LFSCK, "Repair inconsistent file owner for: parent "DFID
+ ", child "DFID", OST-index %u, stripe-index %u, owner %u:%u\n",
+ PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
+ llr->llr_ost_idx, llr->llr_lov_idx, pla->la_uid, pla->la_gid);
+
+ handle = dt_trans_create(env, dev);
+ if (IS_ERR(handle))
+ RETURN(PTR_ERR(handle));
+
+ tla->la_uid = pla->la_uid;
+ tla->la_gid = pla->la_gid;
+ tla->la_valid = LA_UID | LA_GID;
+ rc = dt_declare_attr_set(env, child, tla, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = dt_trans_start(env, dev, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ /* Use the dt_object lock to serialize with destroy and attr_set. */
+ dt_read_lock(env, parent, 0);
+ if (unlikely(lu_object_is_dying(parent->do_lu.lo_header)))
+ GOTO(unlock, rc = 1);
+
+ /* Get the latest parent's owner. */
+ rc = dt_attr_get(env, parent, tla, BYPASS_CAPA);
+ if (rc != 0) {
+ CWARN("%s: fail to get the latest parent's ("DFID") owner, "
+ "not sure whether some others chown/chgrp during the "
+ "LFSCK: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
+ PFID(lfsck_dto2fid(parent)), rc);
+
+ GOTO(unlock, rc);
+ }
+
+ /* Some others chown/chgrp during the LFSCK, needs to do nothing. */
+ if (unlikely(tla->la_uid != pla->la_uid ||
+ tla->la_gid != pla->la_gid))
+ GOTO(unlock, rc = 1);
+
+ tla->la_valid = LA_UID | LA_GID;
+ rc = dt_attr_set(env, child, tla, handle, BYPASS_CAPA);
+
+ GOTO(unlock, rc);
+
+unlock:
+ dt_read_unlock(env, parent);
+
+stop:
+ rc = lfsck_layout_trans_stop(env, dev, handle, rc);
+
+ return rc;
+}
+