Whamcloud - gitweb
- implemented almost all Nikita's notes in seq mgr and same ones in fld;
[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, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define DEBUG_SUBSYSTEM S_LLITE
25 #include <lustre_lite.h>
26 #include "llite_internal.h"
27
28 __u32 get_uuid2int(const char *name, int len)
29 {
30         __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
31         while (len--) {
32                 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
33                 if (key & 0x80000000) key -= 0x7fffffff;
34                 key1 = key0;
35                 key0 = key;
36         }
37         return (key0 << 1);
38 }
39
40 struct ll_ino {
41         unsigned long ino;
42         unsigned long gen;
43 };
44
45 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
46 static int ll_nfs_test_inode(struct inode *inode, unsigned long ino, void *opaque)
47 #else
48 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
49 #endif
50 {
51         struct lu_fid *ifid = &ll_i2info(inode)->lli_fid;
52         struct lu_fid *lfid = opaque;
53
54         if (memcmp(ifid, lfid, sizeof(struct lu_fid)) == 0)
55                 return 1;
56
57         return 0;
58 }
59
60 static struct inode *search_inode_for_lustre(struct super_block *sb,
61                                              struct lu_fid *fid,
62                                              int mode)
63 {
64         struct ll_sb_info *sbi = ll_s2sbi(sb);
65         struct ptlrpc_request *req = NULL;
66         struct inode *inode = NULL;
67         unsigned long valid = 0;
68         int eadatalen = 0, rc;
69
70         inode = ILOOKUP(sb, ll_fid_build_ino(sbi, fid),
71                         ll_nfs_test_inode, fid);
72         if (inode)
73                 return inode;
74
75         if (S_ISREG(mode)) {
76                 rc = ll_get_max_mdsize(sbi, &eadatalen);
77                 if (rc) 
78                         return ERR_PTR(rc); 
79                 valid |= OBD_MD_FLEASIZE;
80         }
81
82         rc = md_getattr(sbi->ll_md_exp, fid, valid, eadatalen, &req);
83         if (rc) {
84                 CERROR("can't get object attrs, fid "DFID", rc %d\n",
85                        PFID(fid), rc);
86                 return ERR_PTR(rc);
87         }
88
89         rc = ll_prep_inode(&inode, req, 0, sb);
90         if (rc) {
91                 ptlrpc_req_finished(req);
92                 return ERR_PTR(rc);
93         }
94         ptlrpc_req_finished(req);
95
96         return inode;
97 }
98
99 extern struct dentry_operations ll_d_ops;
100
101 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
102                                       struct lu_fid *fid, umode_t mode)
103 {
104         struct inode *inode;
105         struct dentry *result;
106 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
107         struct list_head *lp;
108 #endif
109
110         if (!fid_is_sane(fid))
111                 return ERR_PTR(-ESTALE);
112
113         inode = search_inode_for_lustre(sb, fid, mode);
114         if (IS_ERR(inode))
115                 return ERR_PTR(PTR_ERR(inode));
116
117         if (is_bad_inode(inode)) {
118                 /* we didn't find the right inode.. */
119                 CERROR("can't get inode by fid "DFID"\n",
120                        PFID(fid));
121                 iput(inode);
122                 return ERR_PTR(-ESTALE);
123         }
124
125 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
126         result = d_alloc_anon(inode);
127         if (!result) {
128                 iput(inode);
129                 return ERR_PTR(-ENOMEM);
130         }
131 #else
132         /* now to find a dentry.
133          * If possible, get a well-connected one
134          */
135         spin_lock(&dcache_lock);
136         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
137                 result = list_entry(lp,struct dentry, d_alias);
138                 lock_dentry(result);
139                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
140                         dget_locked(result);
141                         ll_set_dflags(result, DCACHE_REFERENCED);
142                         unlock_dentry(result);
143                         spin_unlock(&dcache_lock);
144                         iput(inode);
145                         return result;
146                 }
147                 unlock_dentry(result);
148         }
149         spin_unlock(&dcache_lock);
150         result = d_alloc_root(inode);
151         if (result == NULL) {
152                 iput(inode);
153                 return ERR_PTR(-ENOMEM);
154         }
155         result->d_flags |= DCACHE_DISCONNECTED;
156
157 #endif
158         ll_set_dd(result);
159         result->d_op = &ll_d_ops;
160         return result;
161 }
162
163 static void ll_fh_to_fid(struct lu_fid *fid, __u32 *mode, __u32 *datap)
164 {
165         /* unpacking ->f_seq */
166         fid->f_seq = datap[0];
167         fid->f_seq = (fid->f_seq << 32) | datap[1];
168
169         /* unpacking ->f_num */
170         fid->f_ver = datap[2];
171         fid->f_oid = datap[3];
172
173         *mode = datap[4];
174 }
175
176 static void ll_fid_to_fh(struct lu_fid *fid, __u32 *mode, __u32 *datap)
177 {
178         /* packing ->f_seq */
179         *datap++ = (__u32)(fid_seq(fid) >> 32);
180         *datap++ = (__u32)(fid_seq(fid) & 0x00000000ffffffff);
181
182         /* packing ->f_num */
183         *datap++ = fid_ver(fid);
184         *datap++ = fid_oid(fid);
185
186         /* packing inode mode */
187         *datap++ = (__u32)(S_IFMT & *mode);
188 }
189
190 struct dentry *ll_fh_to_dentry(struct super_block *sb, __u32 *data, int len,
191                                int fhtype, int parent)
192 {
193         struct lu_fid fid;
194         __u32 mode;
195         
196         switch (fhtype) {
197                 case 2:
198                         if (len < 10)
199                                 break;
200                         if (parent) {
201                                 /* getting fid from parent's patr of @fh. That
202                                  * is (data + 5) */
203                                 ll_fh_to_fid(&fid, &mode, data + 5);
204                                 return ll_iget_for_nfs(sb, &fid, mode);
205                         }
206                 case 1:
207                         if (len < 5)
208                                 break;
209                         if (parent)
210                                 break;
211                         ll_fh_to_fid(&fid, &mode, data);
212                         return ll_iget_for_nfs(sb, &fid, mode);
213                 default: break;
214         }
215         return ERR_PTR(-EINVAL);
216 }
217
218 int ll_dentry_to_fh(struct dentry *dentry, __u32 *datap, int *lenp,
219                     int need_parent)
220 {
221         struct inode *child = dentry->d_inode;
222         
223         if (*lenp < 5)
224                 return 255;
225
226         /* XXX: there is suspection that @datap is 5*4 bytes max long, so that
227          * 10*4 bytes (two fids + two times mode) does not fit into it. Not sure
228          * how to fix it though. */
229         ll_fid_to_fh(&ll_i2info(child)->lli_fid, 
230                      (__u32 *)&child->i_mode, datap);
231
232         if (*lenp == 5 || S_ISDIR(child->i_mode)) {
233                 *lenp = 5;
234                 return 1;
235         }
236         if (dentry->d_parent) {
237                 struct inode *parent = dentry->d_parent->d_inode;
238                 
239                 ll_fid_to_fh(&ll_i2info(parent)->lli_fid,
240                              (__u32 *)&parent->i_mode, datap);
241                 *lenp = 10;
242                 return 2;
243         }
244         *lenp = 5;
245         return 1;
246 }
247
248 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
249 struct dentry *ll_get_dentry(struct super_block *sb, void *data)
250 {
251         __u32 *inump = (__u32*)data;
252         struct lu_fid fid;
253         
254         /* FIXME: seems this is not enough */
255         fid.f_seq = inump[0];
256         fid.f_oid = inump[1];
257         
258         return ll_iget_for_nfs(sb, &fid, S_IFREG);
259 }
260
261 struct dentry *ll_get_parent(struct dentry *dchild)
262 {
263         struct ptlrpc_request *req = NULL;
264         struct inode *dir = dchild->d_inode;
265         struct ll_sb_info *sbi;
266         struct dentry *result = NULL;
267         struct mds_body *body;
268         char dotdot[] = "..";
269         int  rc = 0;
270         ENTRY;
271         
272         LASSERT(dir && S_ISDIR(dir->i_mode));
273         
274         sbi = ll_s2sbi(dir->i_sb);
275  
276         rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir),
277                              dotdot, strlen(dotdot) + 1,
278                              0, 0, &req);
279         if (rc) {
280                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
281                 return ERR_PTR(rc);
282         }
283         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body)); 
284        
285         LASSERT((body->valid & OBD_MD_FLGENER) && (body->valid & OBD_MD_FLID));
286         
287         result = ll_iget_for_nfs(dir->i_sb, ll_inode2fid(dir), S_IFDIR);
288
289         if (IS_ERR(result))
290                 rc = PTR_ERR(result);
291
292         ptlrpc_req_finished(req);
293         if (rc)
294                 return ERR_PTR(rc);
295         RETURN(result);
296
297
298 struct export_operations lustre_export_operations = {
299        .get_parent = ll_get_parent,
300        .get_dentry = ll_get_dentry, 
301 };
302 #endif