+#include <lustre_lite.h>
+#include "llite_internal.h"
+
+static int ll_readlink_internal(struct inode *inode,
+ struct ptlrpc_request **request, char **symname)
+{
+ struct ll_inode_info *lli = ll_i2info(inode);
+ struct ll_sb_info *sbi = ll_i2sbi(inode);
+ int rc, symlen = i_size_read(inode) + 1;
+ struct mdt_body *body;
+ struct obd_capa *oc;
+ ENTRY;
+
+ *request = NULL;
+
+ if (lli->lli_symlink_name) {
+ *symname = lli->lli_symlink_name;
+ CDEBUG(D_INODE, "using cached symlink %s\n", *symname);
+ RETURN(0);
+ }
+
+ oc = ll_mdscapa_get(inode);
+ rc = md_getattr(sbi->ll_md_exp, ll_inode2fid(inode), oc,
+ OBD_MD_LINKNAME, symlen, request);
+ capa_put(oc);
+ if (rc) {
+ if (rc != -ENOENT)
+ CERROR("inode %lu: rc = %d\n", inode->i_ino, rc);
+ GOTO (failed, rc);
+ }
+
+ body = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF,
+ sizeof(*body));
+ LASSERT(body != NULL);
+ LASSERT(lustre_rep_swabbed(*request, REPLY_REC_OFF));
+
+ if ((body->valid & OBD_MD_LINKNAME) == 0) {
+ CERROR("OBD_MD_LINKNAME not set on reply\n");
+ GOTO(failed, rc = -EPROTO);
+ }
+
+ LASSERT(symlen != 0);
+ if (body->eadatasize != symlen) {
+ CERROR("inode %lu: symlink length %d not expected %d\n",
+ inode->i_ino, body->eadatasize - 1, symlen - 1);
+ GOTO(failed, rc = -EPROTO);
+ }
+
+ *symname = lustre_msg_buf((*request)->rq_repmsg, REPLY_REC_OFF + 1,
+ symlen);
+ if (*symname == NULL ||
+ strnlen (*symname, symlen) != symlen - 1) {
+ /* not full/NULL terminated */
+ CERROR("inode %lu: symlink not NULL terminated string"
+ "of length %d\n", inode->i_ino, symlen - 1);
+ GOTO(failed, rc = -EPROTO);
+ }
+
+ OBD_ALLOC(lli->lli_symlink_name, symlen);
+ /* do not return an error if we cannot cache the symlink locally */
+ if (lli->lli_symlink_name) {
+ memcpy(lli->lli_symlink_name, *symname, symlen);
+ ptlrpc_req_finished (*request);
+ *request = NULL;
+ *symname = lli->lli_symlink_name;
+ }
+
+ RETURN(0);
+
+ failed:
+ ptlrpc_req_finished (*request);
+ RETURN (rc);
+}