Whamcloud - gitweb
land b1_5 onto 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, 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 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
41 static int ll_nfs_test_inode(struct inode *inode, unsigned long ino, void *opaque)
42 #else
43 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
44 #endif
45 {
46         struct ll_fid *iid = opaque;
47
48         if (inode->i_ino == iid->id && inode->i_generation == iid->generation)
49                 return 1;
50
51         return 0;
52 }
53
54 static struct inode * search_inode_for_lustre(struct super_block *sb,
55                                               unsigned long ino,
56                                               unsigned long generation,
57                                               int mode)
58 {
59         struct ptlrpc_request *req = NULL;
60         struct ll_sb_info *sbi = ll_s2sbi(sb);
61         struct ll_fid fid;
62         unsigned long valid = 0;
63         int eadatalen = 0, rc;
64         struct inode *inode = NULL;
65         struct ll_fid iid = { .id = ino, .generation = generation };
66
67         inode = ILOOKUP(sb, ino, ll_nfs_test_inode, &iid);
68
69         if (inode)
70                 return inode;
71         if (S_ISREG(mode)) {
72                 rc = ll_get_max_mdsize(sbi, &eadatalen);
73                 if (rc) 
74                         return ERR_PTR(rc); 
75                 valid |= OBD_MD_FLEASIZE;
76         }
77         fid.id = (__u64)ino;
78         fid.generation = generation;
79         fid.f_type = mode;
80
81         rc = mdc_getattr(sbi->ll_mdc_exp, &fid, valid, eadatalen, &req);
82         if (rc) {
83                 CERROR("failure %d inode %lu\n", rc, ino);
84                 return ERR_PTR(rc);
85         }
86
87         rc = ll_prep_inode(sbi->ll_osc_exp, &inode, req, REPLY_REC_OFF, sb);
88         if (rc) {
89                 ptlrpc_req_finished(req);
90                 return ERR_PTR(rc);
91         }
92         ptlrpc_req_finished(req);
93
94         return inode;
95 }
96
97 extern struct dentry_operations ll_d_ops;
98
99 static struct dentry *ll_iget_for_nfs(struct super_block *sb, unsigned long ino,
100                                       __u32 generation, umode_t mode)
101 {
102         struct inode *inode;
103         struct dentry *result;
104 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
105         struct list_head *lp;
106 #endif
107
108         if (ino == 0)
109                 return ERR_PTR(-ESTALE);
110
111         inode = search_inode_for_lustre(sb, ino, generation, mode);
112         if (IS_ERR(inode)) {
113                 return ERR_PTR(PTR_ERR(inode));
114         }
115         if (is_bad_inode(inode) ||
116             (generation && inode->i_generation != generation)){
117                 /* we didn't find the right inode.. */
118                 CERROR("Inode %lu, Bad count: %lu %d or version  %u %u\n",
119                        inode->i_ino, (unsigned long)inode->i_nlink,
120                        atomic_read(&inode->i_count), inode->i_generation,
121                        generation);
122                 iput(inode);
123                 return ERR_PTR(-ESTALE);
124         }
125
126 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
127         result = d_alloc_anon(inode);
128         if (!result) {
129                 iput(inode);
130                 return ERR_PTR(-ENOMEM);
131         }
132 #else
133         /* now to find a dentry.
134          * If possible, get a well-connected one
135          */
136         spin_lock(&dcache_lock);
137         for (lp = inode->i_dentry.next; lp != &inode->i_dentry ; lp=lp->next) {
138                 result = list_entry(lp,struct dentry, d_alias);
139                 lock_dentry(result);
140                 if (!(result->d_flags & DCACHE_DISCONNECTED)) {
141                         dget_locked(result);
142                         ll_set_dflags(result, DCACHE_REFERENCED);
143                         unlock_dentry(result);
144                         spin_unlock(&dcache_lock);
145                         iput(inode);
146                         return result;
147                 }
148                 unlock_dentry(result);
149         }
150         spin_unlock(&dcache_lock);
151         result = d_alloc_root(inode);
152         if (result == NULL) {
153                 iput(inode);
154                 return ERR_PTR(-ENOMEM);
155         }
156         result->d_flags |= DCACHE_DISCONNECTED;
157
158 #endif
159         ll_set_dd(result);
160         result->d_op = &ll_d_ops;
161         return result;
162 }
163
164 struct dentry *ll_fh_to_dentry(struct super_block *sb, __u32 *data, int len,
165                                int fhtype, int parent)
166 {
167         switch (fhtype) {
168                 case 2:
169                         if (len < 5)
170                                 break;
171                         if (parent)
172                                 return ll_iget_for_nfs(sb, data[3], 0, data[4]);
173                 case 1:
174                         if (len < 3)
175                                 break;
176                         if (parent)
177                                 break;
178                         return ll_iget_for_nfs(sb, data[0], data[1], data[2]);
179                 default: break;
180         }
181         return ERR_PTR(-EINVAL);
182 }
183
184 int ll_dentry_to_fh(struct dentry *dentry, __u32 *datap, int *lenp,
185                     int need_parent)
186 {
187         if (*lenp < 3)
188                 return 255;
189         *datap++ = dentry->d_inode->i_ino;
190         *datap++ = dentry->d_inode->i_generation;
191         *datap++ = (__u32)(S_IFMT & dentry->d_inode->i_mode);
192
193         if (*lenp == 3 || S_ISDIR(dentry->d_inode->i_mode)) {
194                 *lenp = 3;
195                 return 1;
196         }
197         if (dentry->d_parent) {
198                 *datap++ = dentry->d_parent->d_inode->i_ino;
199                 *datap++ = (__u32)(S_IFMT & dentry->d_parent->d_inode->i_mode);
200
201                 *lenp = 5;
202                 return 2;
203         }
204         *lenp = 3;
205         return 1;
206 }
207
208 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
209 struct dentry *ll_get_dentry(struct super_block *sb, void *data)
210 {
211         __u32 *inump = (__u32*)data;
212         return ll_iget_for_nfs(sb, inump[0], inump[1], S_IFREG);
213 }
214
215 struct dentry *ll_get_parent(struct dentry *dchild)
216 {
217         struct ptlrpc_request *req = NULL;
218         struct inode *dir = dchild->d_inode;
219         struct ll_sb_info *sbi;
220         struct dentry *result = NULL;
221         struct ll_fid fid;
222         struct mds_body *body;
223         char dotdot[] = "..";
224         int  rc = 0;
225         ENTRY;
226         
227         LASSERT(dir && S_ISDIR(dir->i_mode));
228         
229         sbi = ll_s2sbi(dir->i_sb);       
230  
231         fid.id = (__u64)dir->i_ino;
232         fid.generation = dir->i_generation;
233         fid.f_type = S_IFDIR;
234
235         rc = mdc_getattr_name(sbi->ll_mdc_exp, &fid, dotdot, strlen(dotdot) + 1,
236                               0, 0, &req);
237         if (rc) {
238                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
239                 return ERR_PTR(rc);
240         }
241         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof (*body)); 
242        
243         LASSERT((body->valid & OBD_MD_FLGENER) && (body->valid & OBD_MD_FLID));
244         
245         result = ll_iget_for_nfs(dir->i_sb, body->ino, body->generation, S_IFDIR);
246
247         if (IS_ERR(result))
248                 rc = PTR_ERR(result);
249
250         ptlrpc_req_finished(req);
251         if (rc)
252                 return ERR_PTR(rc);
253         RETURN(result);
254
255
256 struct export_operations lustre_export_operations = {
257        .get_parent = ll_get_parent,
258        .get_dentry = ll_get_dentry, 
259 };
260 #endif