Merge 4.7-rc6 into staging-next
[cascardo/linux.git] / drivers / staging / lustre / lustre / llite / llite_nfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lustre/llite/llite_nfs.c
33  *
34  * NFS export of Lustre Light File System
35  *
36  * Author: Yury Umanets <umka@clusterfs.com>
37  * Author: Huang Hua <huanghua@clusterfs.com>
38  */
39
40 #define DEBUG_SUBSYSTEM S_LLITE
41 #include "../include/lustre_lite.h"
42 #include "llite_internal.h"
43 #include <linux/exportfs.h>
44
45 __u32 get_uuid2int(const char *name, int len)
46 {
47         __u32 key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
48
49         while (len--) {
50                 __u32 key = key1 + (key0 ^ (*name++ * 7152373));
51
52                 if (key & 0x80000000)
53                         key -= 0x7fffffff;
54                 key1 = key0;
55                 key0 = key;
56         }
57         return (key0 << 1);
58 }
59
60 void get_uuid2fsid(const char *name, int len, __kernel_fsid_t *fsid)
61 {
62         __u64 key = 0, key0 = 0x12a3fe2d, key1 = 0x37abe8f9;
63
64         while (len--) {
65                 key = key1 + (key0 ^ (*name++ * 7152373));
66                 if (key & 0x8000000000000000ULL)
67                         key -= 0x7fffffffffffffffULL;
68                 key1 = key0;
69                 key0 = key;
70         }
71
72         fsid->val[0] = key;
73         fsid->val[1] = key >> 32;
74 }
75
76 static int ll_nfs_test_inode(struct inode *inode, void *opaque)
77 {
78         return lu_fid_eq(&ll_i2info(inode)->lli_fid, opaque);
79 }
80
81 struct inode *search_inode_for_lustre(struct super_block *sb,
82                                       const struct lu_fid *fid)
83 {
84         struct ll_sb_info     *sbi = ll_s2sbi(sb);
85         struct ptlrpc_request *req = NULL;
86         struct inode      *inode = NULL;
87         int                eadatalen = 0;
88         unsigned long         hash = cl_fid_build_ino(fid,
89                                                       ll_need_32bit_api(sbi));
90         struct  md_op_data    *op_data;
91         int                rc;
92
93         CDEBUG(D_INFO, "searching inode for:(%lu,"DFID")\n", hash, PFID(fid));
94
95         inode = ilookup5(sb, hash, ll_nfs_test_inode, (void *)fid);
96         if (inode)
97                 return inode;
98
99         rc = ll_get_default_mdsize(sbi, &eadatalen);
100         if (rc)
101                 return ERR_PTR(rc);
102
103         /* Because inode is NULL, ll_prep_md_op_data can not
104          * be used here. So we allocate op_data ourselves
105          */
106         op_data = kzalloc(sizeof(*op_data), GFP_NOFS);
107         if (!op_data)
108                 return ERR_PTR(-ENOMEM);
109
110         op_data->op_fid1 = *fid;
111         op_data->op_mode = eadatalen;
112         op_data->op_valid = OBD_MD_FLEASIZE;
113
114         /* mds_fid2dentry ignores f_type */
115         rc = md_getattr(sbi->ll_md_exp, op_data, &req);
116         kfree(op_data);
117         if (rc) {
118                 CDEBUG(D_INFO, "can't get object attrs, fid "DFID", rc %d\n",
119                        PFID(fid), rc);
120                 return ERR_PTR(rc);
121         }
122         rc = ll_prep_inode(&inode, req, sb, NULL);
123         ptlrpc_req_finished(req);
124         if (rc)
125                 return ERR_PTR(rc);
126
127         return inode;
128 }
129
130 struct lustre_nfs_fid {
131         struct lu_fid   lnf_child;
132         struct lu_fid   lnf_parent;
133 };
134
135 static struct dentry *
136 ll_iget_for_nfs(struct super_block *sb, struct lu_fid *fid, struct lu_fid *parent)
137 {
138         struct inode  *inode;
139         struct dentry *result;
140
141         if (!fid_is_sane(fid))
142                 return ERR_PTR(-ESTALE);
143
144         CDEBUG(D_INFO, "Get dentry for fid: " DFID "\n", PFID(fid));
145
146         inode = search_inode_for_lustre(sb, fid);
147         if (IS_ERR(inode))
148                 return ERR_CAST(inode);
149
150         if (is_bad_inode(inode)) {
151                 /* we didn't find the right inode.. */
152                 iput(inode);
153                 return ERR_PTR(-ESTALE);
154         }
155
156         /**
157          * It is an anonymous dentry without OST objects created yet.
158          * We have to find the parent to tell MDS how to init lov objects.
159          */
160         if (S_ISREG(inode->i_mode) && !ll_i2info(inode)->lli_has_smd &&
161             parent && !fid_is_zero(parent)) {
162                 struct ll_inode_info *lli = ll_i2info(inode);
163
164                 spin_lock(&lli->lli_lock);
165                 lli->lli_pfid = *parent;
166                 spin_unlock(&lli->lli_lock);
167         }
168
169         /* N.B. d_obtain_alias() drops inode ref on error */
170         result = d_obtain_alias(inode);
171         if (!IS_ERR(result)) {
172                 int rc;
173
174                 rc = ll_d_init(result);
175                 if (rc < 0) {
176                         dput(result);
177                         result = ERR_PTR(rc);
178                 } else {
179                         struct ll_dentry_data *ldd = ll_d2d(result);
180
181                         /*
182                          * Need to signal to the ll_intent_file_open that
183                          * we came from NFS and so opencache needs to be
184                          * enabled for this one
185                          */
186                         ldd->lld_nfs_dentry = 1;
187                 }
188         }
189
190         return result;
191 }
192
193 /**
194  * \a connectable - is nfsd will connect himself or this should be done
195  *                at lustre
196  *
197  * The return value is file handle type:
198  * 1 -- contains child file handle;
199  * 2 -- contains child file handle and parent file handle;
200  * 255 -- error.
201  */
202 static int ll_encode_fh(struct inode *inode, __u32 *fh, int *plen,
203                         struct inode *parent)
204 {
205         int fileid_len = sizeof(struct lustre_nfs_fid) / 4;
206         struct lustre_nfs_fid *nfs_fid = (void *)fh;
207
208         CDEBUG(D_INFO, "%s: encoding for ("DFID") maxlen=%d minlen=%d\n",
209                ll_get_fsname(inode->i_sb, NULL, 0),
210                PFID(ll_inode2fid(inode)), *plen, fileid_len);
211
212         if (*plen < fileid_len) {
213                 *plen = fileid_len;
214                 return FILEID_INVALID;
215         }
216
217         nfs_fid->lnf_child = *ll_inode2fid(inode);
218         if (parent)
219                 nfs_fid->lnf_parent = *ll_inode2fid(parent);
220         else
221                 fid_zero(&nfs_fid->lnf_parent);
222         *plen = fileid_len;
223
224         return FILEID_LUSTRE;
225 }
226
227 static int ll_nfs_get_name_filldir(struct dir_context *ctx, const char *name,
228                                    int namelen, loff_t hash, u64 ino,
229                                    unsigned type)
230 {
231         /* It is hack to access lde_fid for comparison with lgd_fid.
232          * So the input 'name' must be part of the 'lu_dirent'.
233          */
234         struct lu_dirent *lde = container_of0(name, struct lu_dirent, lde_name);
235         struct ll_getname_data *lgd =
236                 container_of(ctx, struct ll_getname_data, ctx);
237         struct lu_fid fid;
238
239         fid_le_to_cpu(&fid, &lde->lde_fid);
240         if (lu_fid_eq(&fid, &lgd->lgd_fid)) {
241                 memcpy(lgd->lgd_name, name, namelen);
242                 lgd->lgd_name[namelen] = 0;
243                 lgd->lgd_found = 1;
244         }
245         return lgd->lgd_found;
246 }
247
248 static int ll_get_name(struct dentry *dentry, char *name,
249                        struct dentry *child)
250 {
251         struct inode *dir = d_inode(dentry);
252         int rc;
253         struct ll_getname_data lgd = {
254                 .lgd_name = name,
255                 .lgd_fid = ll_i2info(d_inode(child))->lli_fid,
256                 .ctx.actor = ll_nfs_get_name_filldir,
257         };
258
259         if (!dir || !S_ISDIR(dir->i_mode)) {
260                 rc = -ENOTDIR;
261                 goto out;
262         }
263
264         if (!dir->i_fop) {
265                 rc = -EINVAL;
266                 goto out;
267         }
268
269         inode_lock(dir);
270         rc = ll_dir_read(dir, &lgd.ctx);
271         inode_unlock(dir);
272         if (!rc && !lgd.lgd_found)
273                 rc = -ENOENT;
274 out:
275         return rc;
276 }
277
278 static struct dentry *ll_fh_to_dentry(struct super_block *sb, struct fid *fid,
279                                       int fh_len, int fh_type)
280 {
281         struct lustre_nfs_fid *nfs_fid = (struct lustre_nfs_fid *)fid;
282
283         if (fh_type != FILEID_LUSTRE)
284                 return ERR_PTR(-EPROTO);
285
286         return ll_iget_for_nfs(sb, &nfs_fid->lnf_child, &nfs_fid->lnf_parent);
287 }
288
289 static struct dentry *ll_fh_to_parent(struct super_block *sb, struct fid *fid,
290                                       int fh_len, int fh_type)
291 {
292         struct lustre_nfs_fid *nfs_fid = (struct lustre_nfs_fid *)fid;
293
294         if (fh_type != FILEID_LUSTRE)
295                 return ERR_PTR(-EPROTO);
296
297         return ll_iget_for_nfs(sb, &nfs_fid->lnf_parent, NULL);
298 }
299
300 static struct dentry *ll_get_parent(struct dentry *dchild)
301 {
302         struct ptlrpc_request *req = NULL;
303         struct inode      *dir = d_inode(dchild);
304         struct ll_sb_info     *sbi;
305         struct dentry    *result = NULL;
306         struct mdt_body       *body;
307         static char        dotdot[] = "..";
308         struct md_op_data     *op_data;
309         int                rc;
310         int                   lmmsize;
311
312         LASSERT(dir && S_ISDIR(dir->i_mode));
313
314         sbi = ll_s2sbi(dir->i_sb);
315
316         CDEBUG(D_INFO, "%s: getting parent for ("DFID")\n",
317                ll_get_fsname(dir->i_sb, NULL, 0),
318                PFID(ll_inode2fid(dir)));
319
320         rc = ll_get_default_mdsize(sbi, &lmmsize);
321         if (rc != 0)
322                 return ERR_PTR(rc);
323
324         op_data = ll_prep_md_op_data(NULL, dir, NULL, dotdot,
325                                      strlen(dotdot), lmmsize,
326                                      LUSTRE_OPC_ANY, NULL);
327         if (IS_ERR(op_data))
328                 return (void *)op_data;
329
330         rc = md_getattr_name(sbi->ll_md_exp, op_data, &req);
331         ll_finish_md_op_data(op_data);
332         if (rc) {
333                 CERROR("%s: failure inode "DFID" get parent: rc = %d\n",
334                        ll_get_fsname(dir->i_sb, NULL, 0),
335                        PFID(ll_inode2fid(dir)), rc);
336                 return ERR_PTR(rc);
337         }
338         body = req_capsule_server_get(&req->rq_pill, &RMF_MDT_BODY);
339         /*
340          * LU-3952: MDT may lost the FID of its parent, we should not crash
341          * the NFS server, ll_iget_for_nfs() will handle the error.
342          */
343         if (body->valid & OBD_MD_FLID) {
344                 CDEBUG(D_INFO, "parent for " DFID " is " DFID "\n",
345                        PFID(ll_inode2fid(dir)), PFID(&body->fid1));
346         }
347         result = ll_iget_for_nfs(dir->i_sb, &body->fid1, NULL);
348
349         ptlrpc_req_finished(req);
350         return result;
351 }
352
353 const struct export_operations lustre_export_operations = {
354         .get_parent = ll_get_parent,
355         .encode_fh  = ll_encode_fh,
356         .get_name   = ll_get_name,
357         .fh_to_dentry = ll_fh_to_dentry,
358         .fh_to_parent = ll_fh_to_parent,
359 };