Whamcloud - gitweb
b=5498
[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
31 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
32 {
33         return lu_fid_eq(&ll_i2info(inode)->lli_fid,
34                          (struct lu_fid *)opaque);
35 }
36
37 static struct inode *search_inode_for_lustre(struct super_block *sb,
38                                              struct lu_fid *fid,
39                                              int mode)
40 {
41         struct ll_sb_info     *sbi = ll_s2sbi(sb);
42         struct ptlrpc_request *req = NULL;
43         struct inode          *inode = NULL;
44         unsigned long         valid = 0;
45         int                   eadatalen = 0;
46         ino_t                 ino = ll_fid_build_ino(sbi, fid);
47         int                   rc;
48         ENTRY;
49
50         CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", ino, PFID(fid));
51
52         inode = ILOOKUP(sb, ino, ll_nfs_test_inode, fid);
53         if (inode)
54                 RETURN(inode);
55
56         if (S_ISREG(mode)) {
57                 rc = ll_get_max_mdsize(sbi, &eadatalen);
58                 if (rc) 
59                         RETURN(ERR_PTR(rc)); 
60                 valid |= OBD_MD_FLEASIZE;
61         }
62
63         rc = md_getattr(sbi->ll_md_exp, fid, NULL, valid, eadatalen, &req);
64         if (rc) {
65                 CERROR("can't get object attrs, fid "DFID", rc %d\n",
66                        PFID(fid), rc);
67                 RETURN(ERR_PTR(rc));
68         }
69
70         rc = ll_prep_inode(&inode, req, REPLY_REC_OFF, sb);
71         ptlrpc_req_finished(req);
72         if (rc)
73                 RETURN(ERR_PTR(rc));
74
75         RETURN(inode);
76 }
77
78 extern struct dentry_operations ll_d_ops;
79
80 static struct dentry *ll_iget_for_nfs(struct super_block *sb,
81                                       struct lu_fid *fid,
82                                       umode_t mode)
83 {
84         struct inode  *inode;
85         struct dentry *result;
86         ENTRY;
87
88         CDEBUG(D_INFO, "Get dentry for fid: "DFID"\n", PFID(fid));
89         if (!fid_is_sane(fid))
90                 RETURN(ERR_PTR(-ESTALE));
91
92         inode = search_inode_for_lustre(sb, fid, mode);
93         if (IS_ERR(inode))
94                 RETURN(ERR_PTR(PTR_ERR(inode)));
95
96         if (is_bad_inode(inode)) {
97                 /* we didn't find the right inode.. */
98                 CERROR("can't get inode by fid "DFID"\n",
99                        PFID(fid));
100                 iput(inode);
101                 RETURN(ERR_PTR(-ESTALE));
102         }
103
104         result = d_alloc_anon(inode);
105         if (!result) {
106                 iput(inode);
107                 RETURN(ERR_PTR(-ENOMEM));
108         }
109         ll_set_dd(result);
110         result->d_op = &ll_d_ops;
111         RETURN(result);
112 }
113
114 /*
115  * This length is counted as amount of __u32,
116  *  It is composed of a fid and a mode 
117  */
118 #define ONE_FH_LEN (sizeof(struct lu_fid)/4 + 1)
119
120 static struct dentry *ll_decode_fh(struct super_block *sb, __u32 *fh, int fh_len,
121                                    int fh_type,
122                                    int (*acceptable)(void *, struct dentry *),
123                                    void *context)
124 {
125         struct lu_fid *parent = NULL;
126         struct lu_fid *child;
127         struct dentry *entry;
128         ENTRY;
129
130         CDEBUG(D_INFO, "decoding for "DFID" fh_len=%d fh_type=%d\n", 
131                 PFID((struct lu_fid*)fh), fh_len, fh_type);
132
133         if (fh_type != 1 && fh_type != 2)
134                 RETURN(ERR_PTR(-ESTALE));
135         if (fh_len < ONE_FH_LEN * fh_type)
136                 RETURN(ERR_PTR(-ESTALE));
137
138         child = (struct lu_fid*)fh;
139         if (fh_type == 2)
140                 parent = (struct lu_fid*)(fh + ONE_FH_LEN);
141                 
142         entry = sb->s_export_op->find_exported_dentry(sb, child, parent,
143                                                       acceptable, context);
144         RETURN(entry);
145 }
146
147 /* The return value is file handle type:
148  * 1 -- contains child file handle;
149  * 2 -- contains child file handle and parent file handle;
150  * 255 -- error.
151  */
152 static int ll_encode_fh(struct dentry *de, __u32 *fh, int *plen, int connectable)
153 {
154         struct inode    *inode = de->d_inode;
155         struct lu_fid   *fid = ll_inode2fid(inode);
156         ENTRY;
157
158         CDEBUG(D_INFO, "encoding for (%lu,"DFID") maxlen=%d minlen=%d\n",
159                        inode->i_ino, PFID(fid), *plen, ONE_FH_LEN);
160
161         if (*plen < ONE_FH_LEN)
162                 RETURN(255);
163
164         memcpy((char*)fh, fid, sizeof(*fid));
165         *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & inode->i_mode);
166
167         if (de->d_parent && *plen >= ONE_FH_LEN * 2) {
168                 struct inode *parent = de->d_parent->d_inode;
169                 fh += ONE_FH_LEN;
170                 memcpy((char*)fh, &ll_i2info(parent)->lli_fid, sizeof(*fid));
171                 *(fh + ONE_FH_LEN - 1) = (__u32)(S_IFMT & parent->i_mode);
172                 *plen = ONE_FH_LEN * 2;
173                 RETURN(2);
174         } else {
175                 *plen = ONE_FH_LEN;
176                 RETURN(1);
177         }
178 }
179
180 static struct dentry *ll_get_dentry(struct super_block *sb, void *data)
181 {
182         struct lu_fid      *fid;
183         struct dentry      *entry;
184         __u32               mode;
185         ENTRY;
186
187         fid = (struct lu_fid *)data;
188         mode = *((__u32*)data + ONE_FH_LEN - 1);
189         
190         entry = ll_iget_for_nfs(sb, fid, mode);
191         RETURN(entry);
192 }
193
194 static struct dentry *ll_get_parent(struct dentry *dchild)
195 {
196         struct ptlrpc_request *req = NULL;
197         struct inode          *dir = dchild->d_inode;
198         struct ll_sb_info     *sbi;
199         struct dentry         *result = NULL;
200         struct mdt_body       *body;
201         static char           dotdot[] = "..";
202         int                   rc;
203         ENTRY;
204         
205         LASSERT(dir && S_ISDIR(dir->i_mode));
206         
207         sbi = ll_s2sbi(dir->i_sb);
208  
209         CDEBUG(D_INFO, "getting parent for (%lu,"DFID")\n", 
210                         dir->i_ino, PFID(ll_inode2fid(dir)));
211
212         rc = md_getattr_name(sbi->ll_md_exp, ll_inode2fid(dir), NULL,
213                              dotdot, strlen(dotdot) + 1, 0, 0, &req);
214         if (rc) {
215                 CERROR("failure %d inode %lu get parent\n", rc, dir->i_ino);
216                 RETURN(ERR_PTR(rc));
217         }
218         body = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF, sizeof(*body)); 
219        
220         LASSERT(body->valid & OBD_MD_FLID);
221         
222         CDEBUG(D_INFO, "parent for "DFID" is "DFID"\n", 
223                 PFID(ll_inode2fid(dir)), PFID(&body->fid1));
224
225         result = ll_iget_for_nfs(dir->i_sb, &body->fid1, S_IFDIR);
226
227         ptlrpc_req_finished(req);
228         RETURN(result);
229
230
231 struct export_operations lustre_export_operations = {
232        .get_parent = ll_get_parent,
233        .get_dentry = ll_get_dentry,
234        .encode_fh  = ll_encode_fh,
235        .decode_fh  = ll_decode_fh,
236 };