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 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
86 struct dentry *result;
89 CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
90 if (!fid_is_sane(fid))
91 RETURN(ERR_PTR(-ESTALE));
93 inode = search_inode_for_lustre(sb, fid, mode);
95 RETURN(ERR_PTR(PTR_ERR(inode)));
97 if (is_bad_inode(inode)) {
98 /* we didn't find the right inode.. */
99 CERROR("can't get inode by fid "DFID"\n",
102 RETURN(ERR_PTR(-ESTALE));
105 result = d_alloc_anon(inode);
108 RETURN(ERR_PTR(-ENOMEM));
114 if (unlikely(result->d_op == &ll_init_d_ops)) {
115 result->d_op = &ll_d_ops;
116 unlock_dentry(result);
120 result->d_op = &ll_d_ops;
121 unlock_dentry(result);
128 * This length is counted as amount of __u32,
129 * It is composed of a fid and a mode
131 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
133 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
135 int (*acceptable)(void *, struct dentry *),
138 struct lu_fid *parent = NULL;
139 struct lu_fid *child;
140 struct dentry *entry;
143 CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n",
144 PFID((struct lu_fid*)fh), fh_len, fh_type);
146 if (fh_type != 1 && fh_type != 2)
147 RETURN(ERR_PTR(-ESTALE));
148 if (fh_len < ONE_FH_LEN * fh_type)
149 RETURN(ERR_PTR(-ESTALE));
151 child = (struct lu_fid*)fh;
153 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
155 entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
156 acceptable, context);
160 /* The return value is file handle type:
161 * 1 -- contains child file handle;
162 * 2 -- contains child file handle and parent file handle;
165 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
167 struct inode *inode = de->d_inode;
168 struct lu_fid *fid = ll_inode2fid(inode);
171 CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
172 inode->i_ino, PFID(fid), *plen, ONE_FH_LEN);
174 if (*plen < ONE_FH_LEN)
177 memcpy((char*)fh, fid, sizeof(*fid));
178 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
180 if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
181 struct inode *parent = de->d_parent->d_inode;
183 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
184 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
185 *plen = ONE_FH_LEN * 2;
193 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
196 struct dentry *entry;
200 fid = (struct lu_fid *)data;
201 mode = *((__u32*)data + ONE_FH_LEN - 1);
203 entry = ll_iget_for_nfs(sb, fid, mode);
207 static struct dentry *ll_get_parent(struct dentry *dchild)
209 struct ptlrpc_request *req = NULL;
210 struct inode *dir = dchild->d_inode;
211 struct ll_sb_info *sbi;
212 struct dentry *result = NULL;
213 struct mdt_body *body;
214 static char dotdot[] = "..";
218 LASSERT(dir && S_ISDIR(dir->i_mode));
220 sbi = ll_s2sbi(dir->i_sb);
222 CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n",
223 dir->i_ino, PFID(ll_inode2fid(dir)));
225 rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
226 dotdot, strlen(dotdot) + 1, 0, 0,
227 ll_i2suppgid(dir), &req);
229 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
232 body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
233 LASSERT(body->valid & OBD_MD_FLID);
235 CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n",
236 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
238 result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
240 ptlrpc_req_finished(req);
244 struct export_operations lustre_export_operations = {
245 .get_parent = ll_get_parent,
246 .get_dentry = ll_get_dentry,
247 .encode_fh = ll_encode_fh,
248 .decode_fh = ll_decode_fh,