1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/llite/llite_nfs.c
38 * NFS export of Lustre Light File System
41 #define DEBUG_SUBSYSTEM S_LLITE
42 #include <lustre_lite.h>
43 #include "llite_internal.h"
44 #ifdef HAVE_LINUX_EXPORTFS_H
45 #include <linux/exportfs.h>
48 __u32 get_uuid2int(const char *name, int len)
50 __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
52 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
53 if (key & 0x80000000) key -= 0x7fffffff;
60 #if THREAD_SIZE >= 8192 /* see bug 17630 */
62 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
64 struct ll_fid *iid = opaque;
66 if (inode->i_ino == iid->id && inode->i_generation == iid->generation)
72 static struct inode * search_inode_for_lustre(struct super_block *sb,
75 struct ptlrpc_request *req = NULL;
76 struct ll_sb_info *sbi = ll_s2sbi(sb);
77 unsigned long valid = 0;
78 int eadatalen = 0, rc;
79 struct inode *inode = NULL;
82 inode = ILOOKUP(sb, iid->id, ll_nfs_test_inode, iid);
87 rc = ll_get_max_mdsize(sbi, &eadatalen);
91 valid |= OBD_MD_FLEASIZE;
93 /* mds_fid2dentry is ignore f_type */
94 rc = mdc_getattr(sbi->ll_mdc_exp, iid, valid, eadatalen, &req);
96 CERROR("failure %d inode "LPU64"\n", rc, iid->id);
100 rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, REPLY_REC_OFF, sb);
102 ptlrpc_req_finished(req);
105 ptlrpc_req_finished(req);
110 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
114 struct dentry *result;
118 RETURN(ERR_PTR(-ESTALE));
120 inode = search_inode_for_lustre(sb, iid);
122 RETURN(ERR_PTR(PTR_ERR(inode)));
124 if (is_bad_inode(inode) ||
125 (iid->generation && inode->i_generation != iid->generation)) {
126 /* we didn't find the right inode.. */
127 CERROR("Inode %lu, Bad count: %lu %d or version %u %u\n",
128 inode->i_ino, (unsigned long)inode->i_nlink,
129 atomic_read(&inode->i_count), inode->i_generation,
132 RETURN(ERR_PTR(-ESTALE));
135 result = d_obtain_alias(inode);
138 RETURN(ERR_PTR(-ENOMEM));
140 ll_dops_init(result, 1);
145 #define LUSTRE_NFS_FID 0x94
147 struct lustre_nfs_fid {
149 struct ll_fid parent;
153 /* plen is in 32 bit units!
154 * The return value is file handle type:
155 * 1 -- contains child file handle;
156 * 2 -- contains child file handle and parent file handle;
159 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen,
162 struct inode *inode = de->d_inode;
163 struct inode *parent = de->d_parent->d_inode;
164 struct lustre_nfs_fid *nfs_fid = (void *)fh;
167 CDEBUG(D_INFO, "encoding for (%lu) maxlen=%d minlen=%u\n",
168 inode->i_ino, *plen*4,
169 (int)sizeof(struct lustre_nfs_fid));
171 if (*plen*4 < sizeof(struct lustre_nfs_fid))
174 ll_inode2fid(&nfs_fid->child, inode);
175 ll_inode2fid(&nfs_fid->parent, parent);
177 nfs_fid->mode = (S_IFMT & inode->i_mode);
178 *plen = sizeof(struct lustre_nfs_fid)/4;
180 RETURN(LUSTRE_NFS_FID);
183 #ifdef HAVE_FH_TO_DENTRY
184 static struct dentry *ll_fh_to_dentry(struct super_block *sb, struct fid *fid,
185 int fh_len, int fh_type)
187 struct lustre_nfs_fid *nfs_fid = (struct lustre_nfs_fid *)fid;
189 if (fh_type != LUSTRE_NFS_FID)
190 RETURN(ERR_PTR(-EINVAL));
192 RETURN(ll_iget_for_nfs(sb, &nfs_fid->child));
194 static struct dentry *ll_fh_to_parent(struct super_block *sb, struct fid *fid,
195 int fh_len, int fh_type)
197 struct lustre_nfs_fid *nfs_fid = (struct lustre_nfs_fid *)fid;
199 if (fh_type != LUSTRE_NFS_FID)
200 RETURN(ERR_PTR(-EINVAL));
201 RETURN(ll_iget_for_nfs(sb, &nfs_fid->parent));
206 * This length is counted as amount of __u32,
207 * It is composed of a fid and a mode
209 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
211 int (*acceptable)(void *, struct dentry *),
214 struct lustre_nfs_fid *nfs_fid = (void *)fh;
215 struct dentry *entry;
218 CDEBUG(D_INFO, "decoding for "LPU64" fh_len=%d fh_type=%x\n",
219 nfs_fid->child.id, fh_len, fh_type);
221 if (fh_type != LUSTRE_NFS_FID)
222 RETURN(ERR_PTR(-ESTALE));
224 entry = sb->s_export_op->find_exported_dentry(sb, &nfs_fid->child,
226 acceptable, context);
231 struct dentry *ll_get_dentry(struct super_block *sb, void *data)
233 struct lustre_nfs_fid *fid = data;
236 RETURN(ll_iget_for_nfs(sb, &fid->child));
242 struct dentry *ll_get_parent(struct dentry *dchild)
244 struct ptlrpc_request *req = NULL;
245 struct inode *dir = dchild->d_inode;
246 struct ll_sb_info *sbi;
247 struct dentry *result = NULL;
249 struct mds_body *body;
250 char dotdot[] = "..";
254 LASSERT(dir && S_ISDIR(dir->i_mode));
256 sbi = ll_s2sbi(dir->i_sb);
258 fid.id = (__u64)dir->i_ino;
259 fid.generation = dir->i_generation;
260 fid.f_type = S_IFDIR;
262 rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, dotdot, strlen(dotdot) + 1,
265 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
268 body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (*body));
270 LASSERT((body->valid & OBD_MD_FLGENER) && (body->valid & OBD_MD_FLID));
272 fid.generation = body->generation;
273 result = ll_iget_for_nfs(dir->i_sb, &fid);
276 rc = PTR_ERR(result);
278 ptlrpc_req_finished(req);
284 struct export_operations lustre_export_operations = {
285 .encode_fh = ll_encode_fh,
286 .get_parent = ll_get_parent,
287 #ifdef HAVE_FH_TO_DENTRY
288 .fh_to_dentry = ll_fh_to_dentry,
289 .fh_to_parent = ll_fh_to_parent,
291 .get_dentry = ll_get_dentry,
292 .decode_fh = ll_decode_fh,
295 #endif /* THREAD_SIZE >= 8192 */