1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * NFS export of Lustre Light File System
6 * Copyright (c) 2002, 2006 Cluster File Systems, Inc.
8 * Author: Yury Umanets <umka@clusterfs.com>
9 * Huang Hua <huanghua@clusterfs.com>
11 * This file is part of Lustre, http://www.lustre.org.
13 * Lustre is free software; you can redistribute it and/or
14 * modify it under the terms of version 2 of the GNU General Public
15 * License as published by the Free Software Foundation.
17 * Lustre is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with Lustre; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define DEBUG_SUBSYSTEM S_LLITE
28 #include <lustre_lite.h>
29 #include "llite_internal.h"
30 #ifdef HAVE_LINUX_EXPORTFS_H
31 #include <linux/exportfs.h>
34 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
36 return lu_fid_eq(&ll_i2info(inode)->lli_fid,
37 (struct lu_fid *)opaque);
40 static struct inode *search_inode_for_lustre(struct super_block *sb,
44 struct ll_sb_info *sbi = ll_s2sbi(sb);
45 struct ptlrpc_request *req = NULL;
46 struct inode *inode = NULL;
47 unsigned long valid = 0;
49 ino_t ino = ll_fid_build_ino(sbi, fid);
53 CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
55 inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
60 rc = ll_get_max_mdsize(sbi, &eadatalen);
63 valid |= OBD_MD_FLEASIZE;
66 rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
68 CERROR("can't get object attrs, fid "DFID", rc %d\n",
73 rc = ll_prep_inode(&inode, req, sb);
74 ptlrpc_req_finished(req);
81 extern struct dentry_operations ll_d_ops;
83 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
88 struct dentry *result;
91 CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
92 if (!fid_is_sane(fid))
93 RETURN(ERR_PTR(-ESTALE));
95 inode = search_inode_for_lustre(sb, fid, mode);
97 RETURN(ERR_PTR(PTR_ERR(inode)));
99 if (is_bad_inode(inode)) {
100 /* we didn't find the right inode.. */
101 CERROR("can't get inode by fid "DFID"\n",
104 RETURN(ERR_PTR(-ESTALE));
107 result = d_alloc_anon(inode);
110 RETURN(ERR_PTR(-ENOMEM));
113 result->d_op = &ll_d_ops;
118 * This length is counted as amount of __u32,
119 * It is composed of a fid and a mode
121 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
123 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
125 int (*acceptable)(void *, struct dentry *),
128 struct lu_fid *parent = NULL;
129 struct lu_fid *child;
130 struct dentry *entry;
133 CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n",
134 PFID((struct lu_fid*)fh), fh_len, fh_type);
136 if (fh_type != 1 && fh_type != 2)
137 RETURN(ERR_PTR(-ESTALE));
138 if (fh_len < ONE_FH_LEN * fh_type)
139 RETURN(ERR_PTR(-ESTALE));
141 child = (struct lu_fid*)fh;
143 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
145 entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
146 acceptable, context);
150 /* The return value is file handle type:
151 * 1 -- contains child file handle;
152 * 2 -- contains child file handle and parent file handle;
155 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
157 struct inode *inode = de->d_inode;
158 struct lu_fid *fid = ll_inode2fid(inode);
161 CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
162 inode->i_ino, PFID(fid), *plen, ONE_FH_LEN);
164 if (*plen < ONE_FH_LEN)
167 memcpy((char*)fh, fid, sizeof(*fid));
168 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
170 if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
171 struct inode *parent = de->d_parent->d_inode;
173 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
174 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
175 *plen = ONE_FH_LEN * 2;
183 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
186 struct dentry *entry;
190 fid = (struct lu_fid *)data;
191 mode = *((__u32*)data + ONE_FH_LEN - 1);
193 entry = ll_iget_for_nfs(sb, fid, mode);
197 static struct dentry *ll_get_parent(struct dentry *dchild)
199 struct ptlrpc_request *req = NULL;
200 struct inode *dir = dchild->d_inode;
201 struct ll_sb_info *sbi;
202 struct dentry *result = NULL;
203 struct mdt_body *body;
204 static char dotdot[] = "..";
208 LASSERT(dir && S_ISDIR(dir->i_mode));
210 sbi = ll_s2sbi(dir->i_sb);
212 CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n",
213 dir->i_ino, PFID(ll_inode2fid(dir)));
215 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
216 dotdot, strlen(dotdot) + 1, 0, 0,
217 ll_i2suppgid(dir), &req);
219 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
222 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
223 LASSERT(body->valid & OBD_MD_FLID);
225 CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n",
226 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
228 result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
230 ptlrpc_req_finished(req);
234 struct export_operations lustre_export_operations = {
235 .get_parent = ll_get_parent,
236 .get_dentry = ll_get_dentry,
237 .encode_fh = ll_encode_fh,
238 .decode_fh = ll_decode_fh,