Whamcloud - gitweb
restore get_uuid2int from b1_6.
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lustre/llite/llite_nfs.c
37  *
38  * NFS export of Lustre Light File System
39  *
40  * Author: Yury Umanets <umka@clusterfs.com>
41  * Author: Huang Hua <huanghua@clusterfs.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LLITE
45 #include <lustre_lite.h>
46 #include "llite_internal.h"
47 #ifdef HAVE_LINUX_EXPORTFS_H
48 #include <linux/exportfs.h>
49 #endif
50
51 __u32 get_uuid2int(const char *name, int len)
52 {
53         __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
54         while (len--) {
55                 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
56                 if (key & 0x80000000) key -= 0x7fffffff;
57                 key1 = key0;
58                 key0 = key;
59         }
60         return (key0 << 1);
61 }
62
63 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
64 {
65         return lu_fid_eq(&ll_i2info(inode)->lli_fid,
66                          (struct lu_fid *)opaque);
67 }
68
69 static struct inode *search_inode_for_lustre(struct super_block *sb,
70                                              struct lu_fid *fid,
71                                              int mode)
72 {
73         struct ll_sb_info     *sbi = ll_s2sbi(sb);
74         struct ptlrpc_request *req = NULL;
75         struct inode          *inode = NULL;
76         unsigned long         valid = 0;
77         int                   eadatalen = 0;
78         ino_t                 ino = ll_fid_build_ino(sbi, fid);
79         int                   rc;
80         ENTRY;
81
82         CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
83
84         inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
85         if (inode)
86                 RETURN(inode);
87
88         if (S_ISREG(mode)) {
89                 rc = ll_get_max_mdsize(sbi, &eadatalen);
90                 if (rc) 
91                         RETURN(ERR_PTR(rc)); 
92                 valid |= OBD_MD_FLEASIZE;
93         }
94
95         rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
96         if (rc) {
97                 CERROR("can't get object attrs, fid "DFID", rc %d\n",
98                        PFID(fid), rc);
99                 RETURN(ERR_PTR(rc));
100         }
101
102         rc = ll_prep_inode(&inode, req, sb);
103         ptlrpc_req_finished(req);
104         if (rc)
105                 RETURN(ERR_PTR(rc));
106
107         RETURN(inode);
108 }
109
110 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
111                                       struct lu_fid *fid,
112                                       umode_t mode)
113 {
114         struct inode  *inode;
115         struct dentry *result;
116         ENTRY;
117
118         CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
119         if (!fid_is_sane(fid))
120                 RETURN(ERR_PTR(-ESTALE));
121
122         inode = search_inode_for_lustre(sb, fid, mode);
123         if (IS_ERR(inode))
124                 RETURN(ERR_PTR(PTR_ERR(inode)));
125
126         if (is_bad_inode(inode)) {
127                 /* we didn't find the right inode.. */
128                 CERROR("can't get inode by fid "DFID"\n",
129                        PFID(fid));
130                 iput(inode);
131                 RETURN(ERR_PTR(-ESTALE));
132         }
133
134         result = d_alloc_anon(inode);
135         if (!result) {
136                 iput(inode);
137                 RETURN(ERR_PTR(-ENOMEM));
138         }
139
140         ll_set_dd(result);
141
142         lock_dentry(result);
143         if (unlikely(result->d_op == &ll_init_d_ops)) {
144                 result->d_op = &ll_d_ops;
145                 unlock_dentry(result);
146                 smp_wmb();
147                 ll_d_wakeup(result);
148         } else {
149                 result->d_op = &ll_d_ops;
150                 unlock_dentry(result);
151         }
152
153         RETURN(result);
154 }
155
156 /*
157  * This length is counted as amount of __u32,
158  *  It is composed of a fid and a mode 
159  */
160 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
161
162 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
163                                    int fh_type,
164                                    int (*acceptable)(void *, struct dentry *),
165                                    void *context)
166 {
167         struct lu_fid *parent = NULL;
168         struct lu_fid *child;
169         struct dentry *entry;
170         ENTRY;
171
172         CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n", 
173                 PFID((struct lu_fid*)fh), fh_len, fh_type);
174
175         if (fh_type != 1 && fh_type != 2)
176                 RETURN(ERR_PTR(-ESTALE));
177         if (fh_len < ONE_FH_LEN * fh_type)
178                 RETURN(ERR_PTR(-ESTALE));
179
180         child = (struct lu_fid*)fh;
181         if (fh_type == 2)
182                 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
183                 
184         entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
185                                                       acceptable, context);
186         RETURN(entry);
187 }
188
189 /* The return value is file handle type:
190  * 1 -- contains child file handle;
191  * 2 -- contains child file handle and parent file handle;
192  * 255 -- error.
193  */
194 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
195 {
196         struct inode    *inode = de->d_inode;
197         struct lu_fid   *fid = ll_inode2fid(inode);
198         ENTRY;
199
200         CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
201                        inode->i_ino, PFID(fid), *plen, (int)ONE_FH_LEN);
202
203         if (*plen < ONE_FH_LEN)
204                 RETURN(255);
205
206         memcpy((char*)fh, fid, sizeof(*fid));
207         *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
208
209         if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
210                 struct inode *parent = de->d_parent->d_inode;
211                 fh += ONE_FH_LEN;
212                 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
213                 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
214                 *plen = ONE_FH_LEN * 2;
215                 RETURN(2);
216         } else {
217                 *plen = ONE_FH_LEN;
218                 RETURN(1);
219         }
220 }
221
222 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
223 {
224         struct lu_fid      *fid;
225         struct dentry      *entry;
226         __u32               mode;
227         ENTRY;
228
229         fid = (struct lu_fid *)data;
230         mode = *((__u32*)data + ONE_FH_LEN - 1);
231         
232         entry = ll_iget_for_nfs(sb, fid, mode);
233         RETURN(entry);
234 }
235
236 static struct dentry *ll_get_parent(struct dentry *dchild)
237 {
238         struct ptlrpc_request *req = NULL;
239         struct inode          *dir = dchild->d_inode;
240         struct ll_sb_info     *sbi;
241         struct dentry         *result = NULL;
242         struct mdt_body       *body;
243         static char           dotdot[] = "..";
244         int                   rc;
245         ENTRY;
246         
247         LASSERT(dir && S_ISDIR(dir->i_mode));
248         
249         sbi = ll_s2sbi(dir->i_sb);
250  
251         CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n", 
252                         dir->i_ino, PFID(ll_inode2fid(dir)));
253
254         rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
255                              dotdot, strlen(dotdot) + 1, 0, 0,
256                              ll_i2suppgid(dir), &req);
257         if (rc) {
258                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
259                 RETURN(ERR_PTR(rc));
260         }
261         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
262         LASSERT(body->valid & OBD_MD_FLID);
263         
264         CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n", 
265                 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
266
267         result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
268
269         ptlrpc_req_finished(req);
270         RETURN(result);
271
272
273 struct export_operations lustre_export_operations = {
274        .get_parent = ll_get_parent,
275        .get_dentry = ll_get_dentry,
276        .encode_fh  = ll_encode_fh,
277        .decode_fh  = ll_decode_fh,
278 };