+ /* temp buffer for path element, the buffer will be finally freed
+ * in mdt_thread_info_fini */
+ buf = lu_buf_check_and_alloc(buf, PATH_MAX);
+ if (buf->lb_buf == NULL)
+ RETURN(-ENOMEM);
+
+ ldata.ld_buf = buf;
+ ptr = pli->pli_path + pli->pli_pathlen - 1;
+ *ptr = 0;
+ --ptr;
+ pli->pli_fidcount = 0;
+ pli->pli_fids[0] = *(struct lu_fid *)mdt_object_fid(pli->pli_mdt_obj);
+
+ /* root FID only exists on MDT0, and fid2path should also ends at MDT0,
+ * so checking root_fid can only happen on MDT0. */
+ while (!lu_fid_eq(&mdt->mdt_md_root_fid,
+ &pli->pli_fids[pli->pli_fidcount])) {
+ mdt_obj = mdt_object_find(info->mti_env, mdt,
+ &pli->pli_fids[pli->pli_fidcount]);
+ if (IS_ERR(mdt_obj))
+ GOTO(out, rc = PTR_ERR(mdt_obj));
+ if (mdt_object_remote(mdt_obj)) {
+ mdt_object_put(info->mti_env, mdt_obj);
+ GOTO(remote_out, rc = -EREMOTE);
+ }
+ if (!mdt_object_exists(mdt_obj)) {
+ mdt_object_put(info->mti_env, mdt_obj);
+ GOTO(out, rc = -ENOENT);
+ }
+
+ rc = mdt_links_read(info, mdt_obj, &ldata);
+ mdt_object_put(info->mti_env, mdt_obj);
+ if (rc != 0)
+ GOTO(out, rc);
+
+ leh = buf->lb_buf;
+ lee = (struct link_ea_entry *)(leh + 1); /* link #0 */
+ linkea_entry_unpack(lee, &reclen, tmpname, tmpfid);
+ /* If set, use link #linkno for path lookup, otherwise use
+ link #0. Only do this for the final path element. */
+ if ((pli->pli_fidcount == 0) &&
+ (pli->pli_linkno < leh->leh_reccount)) {
+ int count;
+ for (count = 0; count < pli->pli_linkno; count++) {
+ lee = (struct link_ea_entry *)
+ ((char *)lee + reclen);
+ linkea_entry_unpack(lee, &reclen, tmpname,
+ tmpfid);
+ }
+ if (pli->pli_linkno < leh->leh_reccount - 1)
+ /* indicate to user there are more links */
+ pli->pli_linkno++;
+ }
+
+ /* Pack the name in the end of the buffer */
+ ptr -= tmpname->ln_namelen;
+ if (ptr - 1 <= pli->pli_path)
+ GOTO(out, rc = -EOVERFLOW);
+ strncpy(ptr, tmpname->ln_name, tmpname->ln_namelen);
+ *(--ptr) = '/';
+
+ /* Store the parent fid for historic lookup */
+ if (++pli->pli_fidcount >= MAX_PATH_DEPTH)
+ GOTO(out, rc = -EOVERFLOW);
+ pli->pli_fids[pli->pli_fidcount] = *tmpfid;
+ }
+
+remote_out:
+ ptr++; /* skip leading / */
+ memmove(pli->pli_path, ptr, pli->pli_path + pli->pli_pathlen - ptr);
+
+ EXIT;
+out:
+ return rc;
+}
+
+/* Returns the full path to this fid, as of changelog record recno. */
+static int mdt_path(struct mdt_thread_info *info, struct mdt_object *obj,
+ char *path, int pathlen, __u64 *recno, int *linkno,
+ struct lu_fid *fid)
+{
+ struct mdt_device *mdt = info->mti_mdt;
+ struct path_lookup_info *pli;
+ int tries = 3;
+ int rc = -EAGAIN;
+ ENTRY;
+
+ if (pathlen < 3)
+ RETURN(-EOVERFLOW);
+
+ if (lu_fid_eq(&mdt->mdt_md_root_fid, mdt_object_fid(obj))) {
+ path[0] = '\0';
+ RETURN(0);
+ }
+
+ OBD_ALLOC_PTR(pli);
+ if (pli == NULL)
+ RETURN(-ENOMEM);
+
+ pli->pli_mdt_obj = obj;
+ pli->pli_recno = *recno;
+ pli->pli_path = path;
+ pli->pli_pathlen = pathlen;
+ pli->pli_linkno = *linkno;
+
+ /* Retry multiple times in case file is being moved */
+ while (tries-- && rc == -EAGAIN)
+ rc = mdt_path_current(info, pli);
+
+ /* return the last resolved fids to the client, so the client will
+ * build the left path on another MDT for remote object */
+ *fid = pli->pli_fids[pli->pli_fidcount];
+
+ *recno = pli->pli_currec;
+ /* Return next link index to caller */
+ *linkno = pli->pli_linkno;
+
+ OBD_FREE_PTR(pli);
+
+ RETURN(rc);