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/lustre/llite/llite_nfs.c
38 * NFS export of Lustre Light File System
40 * Author: Yury Umanets <umka@clusterfs.com>
41 * Author: Huang Hua <huanghua@clusterfs.com>
44 #define DEBUG_SUBSYSTEM S_LLITE
45 #include <lustre_lite.h>
46 #include "llite_internal.h"
47 #ifdef HAVE_LINUX_EXPORTFS_H
48 #include <linux/exportfs.h>
51 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
53 return lu_fid_eq(&ll_i2info(inode)->lli_fid,
54 (struct lu_fid *)opaque);
57 static struct inode *search_inode_for_lustre(struct super_block *sb,
61 struct ll_sb_info *sbi = ll_s2sbi(sb);
62 struct ptlrpc_request *req = NULL;
63 struct inode *inode = NULL;
64 unsigned long valid = 0;
66 ino_t ino = ll_fid_build_ino(sbi, fid);
70 CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
72 inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
77 rc = ll_get_max_mdsize(sbi, &eadatalen);
80 valid |= OBD_MD_FLEASIZE;
83 rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
85 CERROR("can't get object attrs, fid "DFID", rc %d\n",
90 rc = ll_prep_inode(&inode, req, sb);
91 ptlrpc_req_finished(req);
98 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
103 struct dentry *result;
106 CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
107 if (!fid_is_sane(fid))
108 RETURN(ERR_PTR(-ESTALE));
110 inode = search_inode_for_lustre(sb, fid, mode);
112 RETURN(ERR_PTR(PTR_ERR(inode)));
114 if (is_bad_inode(inode)) {
115 /* we didn't find the right inode.. */
116 CERROR("can't get inode by fid "DFID"\n",
119 RETURN(ERR_PTR(-ESTALE));
122 result = d_alloc_anon(inode);
125 RETURN(ERR_PTR(-ENOMEM));
131 if (unlikely(result->d_op == &ll_init_d_ops)) {
132 result->d_op = &ll_d_ops;
133 unlock_dentry(result);
137 result->d_op = &ll_d_ops;
138 unlock_dentry(result);
145 * This length is counted as amount of __u32,
146 * It is composed of a fid and a mode
148 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
150 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
152 int (*acceptable)(void *, struct dentry *),
155 struct lu_fid *parent = NULL;
156 struct lu_fid *child;
157 struct dentry *entry;
160 CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n",
161 PFID((struct lu_fid*)fh), fh_len, fh_type);
163 if (fh_type != 1 && fh_type != 2)
164 RETURN(ERR_PTR(-ESTALE));
165 if (fh_len < ONE_FH_LEN * fh_type)
166 RETURN(ERR_PTR(-ESTALE));
168 child = (struct lu_fid*)fh;
170 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
172 entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
173 acceptable, context);
177 /* The return value is file handle type:
178 * 1 -- contains child file handle;
179 * 2 -- contains child file handle and parent file handle;
182 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
184 struct inode *inode = de->d_inode;
185 struct lu_fid *fid = ll_inode2fid(inode);
188 CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
189 inode->i_ino, PFID(fid), *plen, (int)ONE_FH_LEN);
191 if (*plen < ONE_FH_LEN)
194 memcpy((char*)fh, fid, sizeof(*fid));
195 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
197 if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
198 struct inode *parent = de->d_parent->d_inode;
200 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
201 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
202 *plen = ONE_FH_LEN * 2;
210 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
213 struct dentry *entry;
217 fid = (struct lu_fid *)data;
218 mode = *((__u32*)data + ONE_FH_LEN - 1);
220 entry = ll_iget_for_nfs(sb, fid, mode);
224 static struct dentry *ll_get_parent(struct dentry *dchild)
226 struct ptlrpc_request *req = NULL;
227 struct inode *dir = dchild->d_inode;
228 struct ll_sb_info *sbi;
229 struct dentry *result = NULL;
230 struct mdt_body *body;
231 static char dotdot[] = "..";
235 LASSERT(dir && S_ISDIR(dir->i_mode));
237 sbi = ll_s2sbi(dir->i_sb);
239 CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n",
240 dir->i_ino, PFID(ll_inode2fid(dir)));
242 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
243 dotdot, strlen(dotdot) + 1, 0, 0,
244 ll_i2suppgid(dir), &req);
246 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
249 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
250 LASSERT(body->valid & OBD_MD_FLID);
252 CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n",
253 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
255 result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
257 ptlrpc_req_finished(req);
261 struct export_operations lustre_export_operations = {
262 .get_parent = ll_get_parent,
263 .get_dentry = ll_get_dentry,
264 .encode_fh = ll_encode_fh,
265 .decode_fh = ll_decode_fh,