2 * -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3 * vim:expandtab:shiftwidth=8:tabstop=8:
7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 only,
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License version 2 for more details (a copy is included
17 * in the LICENSE file that accompanied this code).
19 * You should have received a copy of the GNU General Public License
20 * version 2 along with this program; If not, see [sun.com URL with a
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
24 * CA 95054 USA or visit www.sun.com if you need additional information or
30 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
31 * Use is subject to license terms.
34 * This file is part of Lustre, http://www.lustre.org/
35 * Lustre is a trademark of Sun Microsystems, Inc.
37 * lustre/lustre/llite/llite_nfs.c
39 * NFS export of Lustre Light File System
41 * Author: Yury Umanets <umka@clusterfs.com>
42 * Author: Huang Hua <huanghua@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_LLITE
46 #include <lustre_lite.h>
47 #include "llite_internal.h"
48 #ifdef HAVE_LINUX_EXPORTFS_H
49 #include <linux/exportfs.h>
52 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
54 return lu_fid_eq(&ll_i2info(inode)->lli_fid,
55 (struct lu_fid *)opaque);
58 static struct inode *search_inode_for_lustre(struct super_block *sb,
62 struct ll_sb_info *sbi = ll_s2sbi(sb);
63 struct ptlrpc_request *req = NULL;
64 struct inode *inode = NULL;
65 unsigned long valid = 0;
67 ino_t ino = ll_fid_build_ino(sbi, fid);
71 CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
73 inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
78 rc = ll_get_max_mdsize(sbi, &eadatalen);
81 valid |= OBD_MD_FLEASIZE;
84 rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
86 CERROR("can't get object attrs, fid "DFID", rc %d\n",
91 rc = ll_prep_inode(&inode, req, sb);
92 ptlrpc_req_finished(req);
99 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
104 struct dentry *result;
107 CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
108 if (!fid_is_sane(fid))
109 RETURN(ERR_PTR(-ESTALE));
111 inode = search_inode_for_lustre(sb, fid, mode);
113 RETURN(ERR_PTR(PTR_ERR(inode)));
115 if (is_bad_inode(inode)) {
116 /* we didn't find the right inode.. */
117 CERROR("can't get inode by fid "DFID"\n",
120 RETURN(ERR_PTR(-ESTALE));
123 result = d_alloc_anon(inode);
126 RETURN(ERR_PTR(-ENOMEM));
132 if (unlikely(result->d_op == &ll_init_d_ops)) {
133 result->d_op = &ll_d_ops;
134 unlock_dentry(result);
138 result->d_op = &ll_d_ops;
139 unlock_dentry(result);
146 * This length is counted as amount of __u32,
147 * It is composed of a fid and a mode
149 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
151 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
153 int (*acceptable)(void *, struct dentry *),
156 struct lu_fid *parent = NULL;
157 struct lu_fid *child;
158 struct dentry *entry;
161 CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n",
162 PFID((struct lu_fid*)fh), fh_len, fh_type);
164 if (fh_type != 1 && fh_type != 2)
165 RETURN(ERR_PTR(-ESTALE));
166 if (fh_len < ONE_FH_LEN * fh_type)
167 RETURN(ERR_PTR(-ESTALE));
169 child = (struct lu_fid*)fh;
171 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
173 entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
174 acceptable, context);
178 /* The return value is file handle type:
179 * 1 -- contains child file handle;
180 * 2 -- contains child file handle and parent file handle;
183 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
185 struct inode *inode = de->d_inode;
186 struct lu_fid *fid = ll_inode2fid(inode);
189 CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
190 inode->i_ino, PFID(fid), *plen, (int)ONE_FH_LEN);
192 if (*plen < ONE_FH_LEN)
195 memcpy((char*)fh, fid, sizeof(*fid));
196 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
198 if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
199 struct inode *parent = de->d_parent->d_inode;
201 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
202 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
203 *plen = ONE_FH_LEN * 2;
211 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
214 struct dentry *entry;
218 fid = (struct lu_fid *)data;
219 mode = *((__u32*)data + ONE_FH_LEN - 1);
221 entry = ll_iget_for_nfs(sb, fid, mode);
225 static struct dentry *ll_get_parent(struct dentry *dchild)
227 struct ptlrpc_request *req = NULL;
228 struct inode *dir = dchild->d_inode;
229 struct ll_sb_info *sbi;
230 struct dentry *result = NULL;
231 struct mdt_body *body;
232 static char dotdot[] = "..";
236 LASSERT(dir && S_ISDIR(dir->i_mode));
238 sbi = ll_s2sbi(dir->i_sb);
240 CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n",
241 dir->i_ino, PFID(ll_inode2fid(dir)));
243 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
244 dotdot, strlen(dotdot) + 1, 0, 0,
245 ll_i2suppgid(dir), &req);
247 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
250 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
251 LASSERT(body->valid & OBD_MD_FLID);
253 CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n",
254 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
256 result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
258 ptlrpc_req_finished(req);
262 struct export_operations lustre_export_operations = {
263 .get_parent = ll_get_parent,
264 .get_dentry = ll_get_dentry,
265 .encode_fh = ll_encode_fh,
266 .decode_fh = ll_decode_fh,