+#include <linux/exportfs.h>
+
+u32 get_uuid2int(const char *name, int len)
+{
+ u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
+
+ while (len--) {
+ u32 key = key1 + (key0 ^ (*name++ * 7152373));
+
+ if (key & 0x80000000)
+ key -= 0x7fffffff;
+
+ key1 = key0;
+ key0 = key;
+ }
+ return (key0 << 1);
+}
+
+struct inode *search_inode_for_lustre(struct super_block *sb,
+ const struct lu_fid *fid)
+{
+ struct ll_sb_info *sbi = ll_s2sbi(sb);
+ struct ptlrpc_request *req = NULL;
+ struct inode *inode = NULL;
+ int eadatalen = 0;
+ unsigned long hash = cl_fid_build_ino(fid, ll_need_32bit_api(sbi));
+ struct md_op_data *op_data;
+ int rc;
+
+ ENTRY;
+
+ CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", hash, PFID(fid));
+
+ inode = ilookup5(sb, hash, ll_test_inode_by_fid, (void *)fid);
+ if (inode)
+ RETURN(inode);
+
+ rc = ll_get_default_mdsize(sbi, &eadatalen);
+ if (rc)
+ RETURN(ERR_PTR(rc));
+
+ /*
+ * Because inode is NULL, ll_prep_md_op_data can not
+ * be used here. So we allocate op_data ourselves
+ */
+ OBD_ALLOC_PTR(op_data);
+ if (!op_data)
+ return ERR_PTR(-ENOMEM);
+
+ op_data->op_fid1 = *fid;
+ op_data->op_mode = eadatalen;
+ op_data->op_valid = OBD_MD_FLEASIZE;
+
+ /* mds_fid2dentry ignores f_type */
+ rc = md_getattr(sbi->ll_md_exp, op_data, &req);
+ OBD_FREE_PTR(op_data);
+ if (rc) {
+ /*
+ * Suppress erroneous/confusing messages when NFS
+ * is out of sync and requests old data.
+ */
+ CDEBUG(D_INFO, "can't get object attrs, fid "DFID", rc %d\n",
+ PFID(fid), rc);
+ RETURN(ERR_PTR(rc));
+ }
+ rc = ll_prep_inode(&inode, req, sb, NULL);
+ ptlrpc_req_finished(req);
+ if (rc)
+ RETURN(ERR_PTR(rc));
+
+ RETURN(inode);
+}
+
+static struct dentry *
+ll_iget_for_nfs(struct super_block *sb, struct lu_fid *fid, struct lu_fid *parent)
+{
+ struct inode *inode;
+ struct dentry *result;
+
+ ENTRY;
+
+ if (!fid_is_sane(fid))
+ RETURN(ERR_PTR(-ESTALE));
+
+ CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
+
+ inode = search_inode_for_lustre(sb, fid);
+ if (IS_ERR(inode))
+ RETURN(ERR_PTR(PTR_ERR(inode)));
+
+ if (is_bad_inode(inode)) {
+ /* we didn't find the right inode.. */
+ iput(inode);
+ RETURN(ERR_PTR(-ESTALE));
+ }
+
+ /* N.B. d_obtain_alias() drops inode ref on error */
+ result = d_obtain_alias(inode);
+ if (!IS_ERR(result)) {
+ int rc;
+
+ rc = ll_d_init(result);
+ if (rc < 0) {
+ dput(result);
+ result = ERR_PTR(rc);
+ } else {
+ struct ll_dentry_data *ldd = ll_d2d(result);
+
+ /*
+ * Need to signal to the ll_file_open that
+ * we came from NFS and so opencache needs to be
+ * enabled for this one
+ */
+ spin_lock(&result->d_lock);
+ ldd->lld_nfs_dentry = 1;
+ spin_unlock(&result->d_lock);
+ }
+ }
+
+ RETURN(result);
+}
+
+#ifndef FILEID_INVALID
+#define FILEID_INVALID 0xff
+#endif
+#ifndef FILEID_LUSTRE
+#define FILEID_LUSTRE 0x97
+#endif
+
+/**
+ * \a connectable - is nfsd will connect himself or this should be done
+ * at lustre
+ *
+ * The return value is file handle type:
+ * 1 -- contains child file handle;
+ * 2 -- contains child file handle and parent file handle;
+ * 255 -- error.
+ */
+static int ll_encode_fh(struct inode *inode, u32 *fh, int *plen,
+ struct inode *parent)
+{
+ int fileid_len = sizeof(struct lustre_file_handle) / 4;
+ struct lustre_file_handle *lfh = (void *)fh;
+
+ ENTRY;
+
+ CDEBUG(D_INFO, "%s: encoding for ("DFID") maxlen=%d minlen=%d\n",
+ ll_i2sbi(inode)->ll_fsname,
+ PFID(ll_inode2fid(inode)), *plen, fileid_len);
+
+ if (*plen < fileid_len) {
+ *plen = fileid_len;
+ RETURN(FILEID_INVALID);
+ }