+/**
+ * \retval +ve repaired
+ * \retval 0 no need to repair
+ * \retval -ve error cases
+ */
+static int mdd_lfsck_namespace_double_scan_one(const struct lu_env *env,
+ struct lfsck_component *com,
+ struct mdd_object *child,
+ __u8 flags)
+{
+ struct mdd_thread_info *info = mdd_env_info(env);
+ struct lu_attr *la = &info->mti_la;
+ struct lu_name *cname = &info->mti_name;
+ struct lu_fid *pfid = &info->mti_fid;
+ struct lu_fid *cfid = &info->mti_fid2;
+ struct md_lfsck *lfsck = com->lc_lfsck;
+ struct mdd_device *mdd = mdd_lfsck2mdd(lfsck);
+ struct lfsck_bookmark *bk = &lfsck->ml_bookmark_ram;
+ struct lfsck_namespace *ns =
+ (struct lfsck_namespace *)com->lc_file_ram;
+ struct mdd_link_data ldata = { 0 };
+ struct thandle *handle = NULL;
+ bool locked = false;
+ bool update = false;
+ int count;
+ int rc;
+ ENTRY;
+
+ if (com->lc_journal) {
+
+again:
+ LASSERT(!locked);
+
+ com->lc_journal = 1;
+ handle = mdd_trans_create(env, mdd);
+ if (IS_ERR(handle))
+ RETURN(rc = PTR_ERR(handle));
+
+ rc = mdd_declare_links_add(env, child, handle);;
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ rc = mdd_trans_start(env, mdd, handle);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ mdd_write_lock(env, child, MOR_TGT_CHILD);
+ locked = true;
+ }
+
+ if (unlikely(mdd_is_dead_obj(child)))
+ GOTO(stop, rc = 0);
+
+ rc = mdd_links_read(env, child, &ldata);
+ if (rc != 0) {
+ if ((bk->lb_param & LPF_DRYRUN) &&
+ (rc == -EINVAL || rc == -ENODATA))
+ rc = 1;
+
+ GOTO(stop, rc);
+ }
+
+ rc = mdd_la_get(env, child, la, BYPASS_CAPA);
+ if (rc != 0)
+ GOTO(stop, rc);
+
+ ldata.ml_lee = (struct link_ea_entry *)(ldata.ml_leh + 1);
+ count = ldata.ml_leh->leh_reccount;
+ while (count-- > 0) {
+ struct mdd_object *parent = NULL;
+ struct dt_object *dir;
+
+ mdd_lee_unpack(ldata.ml_lee, &ldata.ml_reclen, cname, pfid);
+ if (!fid_is_sane(pfid))
+ goto shrink;
+
+ parent = mdd_object_find(env, mdd, pfid);
+ if (parent == NULL)
+ goto shrink;
+ else if (IS_ERR(parent))
+ GOTO(stop, rc = PTR_ERR(parent));
+
+ if (!mdd_object_exists(parent))
+ goto shrink;
+
+ /* XXX: need more processing for remote object in the future. */
+ if (mdd_object_remote(parent)) {
+ mdd_object_put(env, parent);
+ ldata.ml_lee = (struct link_ea_entry *)
+ ((char *)ldata.ml_lee + ldata.ml_reclen);
+ continue;
+ }
+
+ dir = mdd_object_child(parent);
+ if (unlikely(!dt_try_as_dir(env, dir)))
+ goto shrink;
+
+ /* To guarantee the 'name' is terminated with '0'. */
+ memcpy(info->mti_key, cname->ln_name, cname->ln_namelen);
+ info->mti_key[cname->ln_namelen] = 0;
+ cname->ln_name = info->mti_key;
+ rc = dt_lookup(env, dir, (struct dt_rec *)cfid,
+ (const struct dt_key *)cname->ln_name,
+ BYPASS_CAPA);
+ if (rc != 0 && rc != -ENOENT) {
+ mdd_object_put(env, parent);
+ GOTO(stop, rc);
+ }
+
+ if (rc == 0) {
+ if (lu_fid_eq(cfid, mdo2fid(child))) {
+ mdd_object_put(env, parent);
+ ldata.ml_lee = (struct link_ea_entry *)
+ ((char *)ldata.ml_lee + ldata.ml_reclen);
+ continue;
+ }
+
+ goto shrink;
+ }
+
+ if (ldata.ml_leh->leh_reccount > la->la_nlink)
+ goto shrink;
+
+ /* XXX: For the case of there is linkea entry, but without name
+ * entry pointing to the object, and the object link count
+ * isn't less than the count of name entries, then add the
+ * name entry back to namespace.
+ *
+ * It is out of LFSCK 1.5 scope, will implement it in the
+ * future. Keep the linkEA entry. */
+ mdd_object_put(env, parent);
+ ldata.ml_lee = (struct link_ea_entry *)
+ ((char *)ldata.ml_lee + ldata.ml_reclen);
+ continue;
+
+shrink:
+ if (parent != NULL)
+ mdd_object_put(env, parent);
+ if (bk->lb_param & LPF_DRYRUN)
+ RETURN(1);
+
+ CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
+ PFID(mdo2fid(child)), cname->ln_namelen, cname->ln_name,
+ PFID(pfid));
+ mdd_links_del_buf(env, &ldata, cname);
+ update = true;
+ }
+
+ if (update) {
+ if (!com->lc_journal) {
+ com->lc_journal = 1;
+ goto again;
+ }
+
+ rc = mdd_links_write(env, child, &ldata, handle);
+ }
+
+ GOTO(stop, rc);
+
+stop:
+ if (locked)
+ mdd_write_unlock(env, child);
+
+ if (handle != NULL)
+ mdd_trans_stop(env, mdd, rc, handle);
+
+ if (rc == 0 && update) {
+ ns->ln_objs_nlink_repaired++;
+ rc = 1;
+ }
+ return rc;
+}
+