Whamcloud - gitweb
b=14149
[fs/lustre-release.git] / lustre / llite / llite_nfs.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *   NFS export of Lustre Light File System 
5  *
6  *   Copyright (c) 2002, 2006 Cluster File Systems, Inc.
7  *
8  *   Author: Yury Umanets <umka@clusterfs.com>
9  *           Huang Hua <huanghua@clusterfs.com>
10  *
11  *   This file is part of Lustre, http://www.lustre.org.
12  *
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.
16  *
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.
21  *
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.
25  */
26
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>
32 #endif
33
34 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
35 {
36         return lu_fid_eq(&ll_i2info(inode)->lli_fid,
37                          (struct lu_fid *)opaque);
38 }
39
40 static struct inode *search_inode_for_lustre(struct super_block *sb,
41                                              struct lu_fid *fid,
42                                              int mode)
43 {
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;
48         int                   eadatalen = 0;
49         ino_t                 ino = ll_fid_build_ino(sbi, fid);
50         int                   rc;
51         ENTRY;
52
53         CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
54
55         inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
56         if (inode)
57                 RETURN(inode);
58
59         if (S_ISREG(mode)) {
60                 rc = ll_get_max_mdsize(sbi, &eadatalen);
61                 if (rc) 
62                         RETURN(ERR_PTR(rc)); 
63                 valid |= OBD_MD_FLEASIZE;
64         }
65
66         rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
67         if (rc) {
68                 CERROR("can't get object attrs, fid "DFID", rc %d\n",
69                        PFID(fid), rc);
70                 RETURN(ERR_PTR(rc));
71         }
72
73         rc = ll_prep_inode(&inode, req, sb);
74         ptlrpc_req_finished(req);
75         if (rc)
76                 RETURN(ERR_PTR(rc));
77
78         RETURN(inode);
79 }
80
81 extern struct dentry_operations ll_d_ops;
82
83 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
84                                       struct lu_fid *fid,
85                                       umode_t mode)
86 {
87         struct inode  *inode;
88         struct dentry *result;
89         ENTRY;
90
91         CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
92         if (!fid_is_sane(fid))
93                 RETURN(ERR_PTR(-ESTALE));
94
95         inode = search_inode_for_lustre(sb, fid, mode);
96         if (IS_ERR(inode))
97                 RETURN(ERR_PTR(PTR_ERR(inode)));
98
99         if (is_bad_inode(inode)) {
100                 /* we didn't find the right inode.. */
101                 CERROR("can't get inode by fid "DFID"\n",
102                        PFID(fid));
103                 iput(inode);
104                 RETURN(ERR_PTR(-ESTALE));
105         }
106
107         result = d_alloc_anon(inode);
108         if (!result) {
109                 iput(inode);
110                 RETURN(ERR_PTR(-ENOMEM));
111         }
112         ll_set_dd(result);
113         result->d_op = &ll_d_ops;
114         RETURN(result);
115 }
116
117 /*
118  * This length is counted as amount of __u32,
119  *  It is composed of a fid and a mode 
120  */
121 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
122
123 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
124                                    int fh_type,
125                                    int (*acceptable)(void *, struct dentry *),
126                                    void *context)
127 {
128         struct lu_fid *parent = NULL;
129         struct lu_fid *child;
130         struct dentry *entry;
131         ENTRY;
132
133         CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n", 
134                 PFID((struct lu_fid*)fh), fh_len, fh_type);
135
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));
140
141         child = (struct lu_fid*)fh;
142         if (fh_type == 2)
143                 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
144                 
145         entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
146                                                       acceptable, context);
147         RETURN(entry);
148 }
149
150 /* The return value is file handle type:
151  * 1 -- contains child file handle;
152  * 2 -- contains child file handle and parent file handle;
153  * 255 -- error.
154  */
155 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
156 {
157         struct inode    *inode = de->d_inode;
158         struct lu_fid   *fid = ll_inode2fid(inode);
159         ENTRY;
160
161         CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
162                        inode->i_ino, PFID(fid), *plen, ONE_FH_LEN);
163
164         if (*plen < ONE_FH_LEN)
165                 RETURN(255);
166
167         memcpy((char*)fh, fid, sizeof(*fid));
168         *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
169
170         if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
171                 struct inode *parent = de->d_parent->d_inode;
172                 fh += ONE_FH_LEN;
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;
176                 RETURN(2);
177         } else {
178                 *plen = ONE_FH_LEN;
179                 RETURN(1);
180         }
181 }
182
183 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
184 {
185         struct lu_fid      *fid;
186         struct dentry      *entry;
187         __u32               mode;
188         ENTRY;
189
190         fid = (struct lu_fid *)data;
191         mode = *((__u32*)data + ONE_FH_LEN - 1);
192         
193         entry = ll_iget_for_nfs(sb, fid, mode);
194         RETURN(entry);
195 }
196
197 static struct dentry *ll_get_parent(struct dentry *dchild)
198 {
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[] = "..";
205         int                   rc;
206         ENTRY;
207         
208         LASSERT(dir && S_ISDIR(dir->i_mode));
209         
210         sbi = ll_s2sbi(dir->i_sb);
211  
212         CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n", 
213                         dir->i_ino, PFID(ll_inode2fid(dir)));
214
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);
218         if (rc) {
219                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
220                 RETURN(ERR_PTR(rc));
221         }
222         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
223         LASSERT(body->valid & OBD_MD_FLID);
224         
225         CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n", 
226                 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
227
228         result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
229
230         ptlrpc_req_finished(req);
231         RETURN(result);
232
233
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,
239 };