Whamcloud - gitweb
Branch HEAD
[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 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
82                                       struct lu_fid *fid,
83                                       umode_t mode)
84 {
85         struct inode  *inode;
86         struct dentry *result;
87         ENTRY;
88
89         CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
90         if (!fid_is_sane(fid))
91                 RETURN(ERR_PTR(-ESTALE));
92
93         inode = search_inode_for_lustre(sb, fid, mode);
94         if (IS_ERR(inode))
95                 RETURN(ERR_PTR(PTR_ERR(inode)));
96
97         if (is_bad_inode(inode)) {
98                 /* we didn't find the right inode.. */
99                 CERROR("can't get inode by fid "DFID"\n",
100                        PFID(fid));
101                 iput(inode);
102                 RETURN(ERR_PTR(-ESTALE));
103         }
104
105         result = d_alloc_anon(inode);
106         if (!result) {
107                 iput(inode);
108                 RETURN(ERR_PTR(-ENOMEM));
109         }
110
111         ll_set_dd(result);
112
113         lock_dentry(result);
114         if (unlikely(result->d_op == &ll_init_d_ops)) {
115                 result->d_op = &ll_d_ops;
116                 unlock_dentry(result);
117                 smp_wmb();
118                 ll_d_wakeup(result);
119         } else {
120                 result->d_op = &ll_d_ops;
121                 unlock_dentry(result);
122         }
123
124         RETURN(result);
125 }
126
127 /*
128  * This length is counted as amount of __u32,
129  *  It is composed of a fid and a mode 
130  */
131 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
132
133 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
134                                    int fh_type,
135                                    int (*acceptable)(void *, struct dentry *),
136                                    void *context)
137 {
138         struct lu_fid *parent = NULL;
139         struct lu_fid *child;
140         struct dentry *entry;
141         ENTRY;
142
143         CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n", 
144                 PFID((struct lu_fid*)fh), fh_len, fh_type);
145
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));
150
151         child = (struct lu_fid*)fh;
152         if (fh_type == 2)
153                 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
154                 
155         entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
156                                                       acceptable, context);
157         RETURN(entry);
158 }
159
160 /* The return value is file handle type:
161  * 1 -- contains child file handle;
162  * 2 -- contains child file handle and parent file handle;
163  * 255 -- error.
164  */
165 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
166 {
167         struct inode    *inode = de->d_inode;
168         struct lu_fid   *fid = ll_inode2fid(inode);
169         ENTRY;
170
171         CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
172                        inode->i_ino, PFID(fid), *plen, (int)ONE_FH_LEN);
173
174         if (*plen < ONE_FH_LEN)
175                 RETURN(255);
176
177         memcpy((char*)fh, fid, sizeof(*fid));
178         *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
179
180         if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
181                 struct inode *parent = de->d_parent->d_inode;
182                 fh += ONE_FH_LEN;
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;
186                 RETURN(2);
187         } else {
188                 *plen = ONE_FH_LEN;
189                 RETURN(1);
190         }
191 }
192
193 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
194 {
195         struct lu_fid      *fid;
196         struct dentry      *entry;
197         __u32               mode;
198         ENTRY;
199
200         fid = (struct lu_fid *)data;
201         mode = *((__u32*)data + ONE_FH_LEN - 1);
202         
203         entry = ll_iget_for_nfs(sb, fid, mode);
204         RETURN(entry);
205 }
206
207 static struct dentry *ll_get_parent(struct dentry *dchild)
208 {
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[] = "..";
215         int                   rc;
216         ENTRY;
217         
218         LASSERT(dir && S_ISDIR(dir->i_mode));
219         
220         sbi = ll_s2sbi(dir->i_sb);
221  
222         CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n", 
223                         dir->i_ino, PFID(ll_inode2fid(dir)));
224
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);
228         if (rc) {
229                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
230                 RETURN(ERR_PTR(rc));
231         }
232         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
233         LASSERT(body->valid & OBD_MD_FLID);
234         
235         CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n", 
236                 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
237
238         result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
239
240         ptlrpc_req_finished(req);
241         RETURN(result);
242
243
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,
249 };