blob: 89f23eddb8f6dfba6c5f52d5356321fde8178418 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/nfs4callback.c
3 *
4 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/list.h>
38#include <linux/inet.h>
39#include <linux/errno.h>
40#include <linux/delay.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040041#include <linux/sched.h>
J. Bruce Fields2b47eec2007-07-27 18:06:50 -040042#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/xdr.h>
44#include <linux/sunrpc/svc.h>
45#include <linux/sunrpc/clnt.h>
Andy Adamson38524ab2009-09-10 12:25:59 +030046#include <linux/sunrpc/svcsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfsd/nfsd.h>
48#include <linux/nfsd/state.h>
49#include <linux/sunrpc/sched.h>
50#include <linux/nfs4.h>
51
52#define NFSDDBG_FACILITY NFSDDBG_PROC
53
54#define NFSPROC4_CB_NULL 0
55#define NFSPROC4_CB_COMPOUND 1
Andy Adamson38524ab2009-09-10 12:25:59 +030056#define NFS4_STATEID_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Index of predefined Linux callback client operations */
59
60enum {
Benny Halevy4be36ca2009-09-10 12:25:46 +030061 NFSPROC4_CLNT_CB_NULL = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 NFSPROC4_CLNT_CB_RECALL,
Andy Adamson38524ab2009-09-10 12:25:59 +030063 NFSPROC4_CLNT_CB_SEQUENCE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66enum nfs_cb_opnum4 {
67 OP_CB_RECALL = 4,
Andy Adamson38524ab2009-09-10 12:25:59 +030068 OP_CB_SEQUENCE = 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71#define NFS4_MAXTAGLEN 20
72
73#define NFS4_enc_cb_null_sz 0
74#define NFS4_dec_cb_null_sz 0
75#define cb_compound_enc_hdr_sz 4
76#define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
Andy Adamson38524ab2009-09-10 12:25:59 +030077#define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
78#define cb_sequence_enc_sz (sessionid_sz + 4 + \
79 1 /* no referring calls list yet */)
80#define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define op_enc_sz 1
83#define op_dec_sz 2
84#define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
Benny Halevy0ac68d12007-07-17 04:04:37 -070085#define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030087 cb_sequence_enc_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 1 + enc_stateid_sz + \
89 enc_nfs4_fh_sz)
90
91#define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030092 cb_sequence_dec_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 op_dec_sz)
94
Ricardo Labiaga132f9772009-09-10 12:26:12 +030095struct nfs4_rpc_args {
96 void *args_op;
97 struct nfsd4_cb_sequence args_seq;
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/*
101* Generic encode routines from fs/nfs/nfs4xdr.c
102*/
Al Virof00f3282006-10-19 23:29:01 -0700103static inline __be32 *
104xdr_writemem(__be32 *p, const void *ptr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 int tmp = XDR_QUADLEN(nbytes);
107 if (!tmp)
108 return p;
109 p[tmp-1] = 0;
110 memcpy(p, ptr, nbytes);
111 return p + tmp;
112}
113
114#define WRITE32(n) *p++ = htonl(n)
115#define WRITEMEM(ptr,nbytes) do { \
116 p = xdr_writemem(p, ptr, nbytes); \
117} while (0)
118#define RESERVE_SPACE(nbytes) do { \
119 p = xdr_reserve_space(xdr, nbytes); \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700120 if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 BUG_ON(!p); \
122} while (0)
123
124/*
125 * Generic decode routines from fs/nfs/nfs4xdr.c
126 */
127#define DECODE_TAIL \
128 status = 0; \
129out: \
130 return status; \
131xdr_error: \
132 dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
133 status = -EIO; \
134 goto out
135
136#define READ32(x) (x) = ntohl(*p++)
137#define READ64(x) do { \
138 (x) = (u64)ntohl(*p++) << 32; \
139 (x) |= ntohl(*p++); \
140} while (0)
141#define READTIME(x) do { \
142 p++; \
143 (x.tv_sec) = ntohl(*p++); \
144 (x.tv_nsec) = ntohl(*p++); \
145} while (0)
146#define READ_BUF(nbytes) do { \
147 p = xdr_inline_decode(xdr, nbytes); \
148 if (!p) { \
Greg Banks3e3b4802006-10-02 02:17:41 -0700149 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700150 __func__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EIO; \
152 } \
153} while (0)
154
155struct nfs4_cb_compound_hdr {
Andy Adamson38524ab2009-09-10 12:25:59 +0300156 /* args */
157 u32 ident; /* minorversion 0 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 u32 nops;
Andy Adamsonef52bff2009-06-16 04:20:50 +0300159 __be32 *nops_p;
Andy Adamsonab52ae62009-06-16 04:20:53 +0300160 u32 minorversion;
Andy Adamson38524ab2009-09-10 12:25:59 +0300161 /* res */
162 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 u32 taglen;
Andy Adamsonef52bff2009-06-16 04:20:50 +0300164 char *tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165};
166
167static struct {
168int stat;
169int errno;
170} nfs_cb_errtbl[] = {
171 { NFS4_OK, 0 },
172 { NFS4ERR_PERM, EPERM },
173 { NFS4ERR_NOENT, ENOENT },
174 { NFS4ERR_IO, EIO },
175 { NFS4ERR_NXIO, ENXIO },
176 { NFS4ERR_ACCESS, EACCES },
177 { NFS4ERR_EXIST, EEXIST },
178 { NFS4ERR_XDEV, EXDEV },
179 { NFS4ERR_NOTDIR, ENOTDIR },
180 { NFS4ERR_ISDIR, EISDIR },
181 { NFS4ERR_INVAL, EINVAL },
182 { NFS4ERR_FBIG, EFBIG },
183 { NFS4ERR_NOSPC, ENOSPC },
184 { NFS4ERR_ROFS, EROFS },
185 { NFS4ERR_MLINK, EMLINK },
186 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
187 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
188 { NFS4ERR_DQUOT, EDQUOT },
189 { NFS4ERR_STALE, ESTALE },
190 { NFS4ERR_BADHANDLE, EBADHANDLE },
191 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
192 { NFS4ERR_NOTSUPP, ENOTSUPP },
193 { NFS4ERR_TOOSMALL, ETOOSMALL },
194 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
195 { NFS4ERR_BADTYPE, EBADTYPE },
196 { NFS4ERR_LOCKED, EAGAIN },
197 { NFS4ERR_RESOURCE, EREMOTEIO },
198 { NFS4ERR_SYMLINK, ELOOP },
199 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
200 { NFS4ERR_DEADLOCK, EDEADLK },
201 { -1, EIO }
202};
203
204static int
205nfs_cb_stat_to_errno(int stat)
206{
207 int i;
208 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
209 if (nfs_cb_errtbl[i].stat == stat)
210 return nfs_cb_errtbl[i].errno;
211 }
212 /* If we cannot translate the error, the recovery routines should
213 * handle it.
214 * Note: remaining NFSv4 error codes have values > 10000, so should
215 * not conflict with native Linux error codes.
216 */
217 return stat;
218}
219
220/*
221 * XDR encode
222 */
223
Andy Adamsonef52bff2009-06-16 04:20:50 +0300224static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
226{
Al Virof00f3282006-10-19 23:29:01 -0700227 __be32 * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 RESERVE_SPACE(16);
230 WRITE32(0); /* tag length is always 0 */
Andy Adamsonab52ae62009-06-16 04:20:53 +0300231 WRITE32(hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 WRITE32(hdr->ident);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300233 hdr->nops_p = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 WRITE32(hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Andy Adamsonef52bff2009-06-16 04:20:50 +0300237static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
238{
239 *hdr->nops_p = htonl(hdr->nops);
240}
241
242static void
243encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
244 struct nfs4_cb_compound_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Al Virof00f3282006-10-19 23:29:01 -0700246 __be32 *p;
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400247 int len = dp->dl_fh.fh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400249 RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 WRITE32(OP_CB_RECALL);
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400251 WRITE32(dp->dl_stateid.si_generation);
252 WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
J. Bruce Fields6707bd32009-05-01 19:57:46 -0400253 WRITE32(0); /* truncate optimization not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 WRITE32(len);
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400255 WRITEMEM(&dp->dl_fh.fh_base, len);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300256 hdr->nops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Benny Halevy2af73582009-09-10 12:26:51 +0300259static void
260encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *args,
261 struct nfs4_cb_compound_hdr *hdr)
262{
263 __be32 *p;
264
265 if (hdr->minorversion == 0)
266 return;
267
268 RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
269
270 WRITE32(OP_CB_SEQUENCE);
271 WRITEMEM(args->cbs_clp->cl_sessionid.data, NFS4_MAX_SESSIONID_LEN);
272 WRITE32(args->cbs_clp->cl_cb_seq_nr);
273 WRITE32(0); /* slotid, always 0 */
274 WRITE32(0); /* highest slotid always 0 */
275 WRITE32(0); /* cachethis always 0 */
276 WRITE32(0); /* FIXME: support referring_call_lists */
277 hdr->nops++;
278}
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280static int
Al Virof00f3282006-10-19 23:29:01 -0700281nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct xdr_stream xdrs, *xdr = &xdrs;
284
285 xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
286 RESERVE_SPACE(0);
287 return 0;
288}
289
290static int
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300291nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
292 struct nfs4_rpc_args *rpc_args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct xdr_stream xdr;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300295 struct nfs4_delegation *args = rpc_args->args_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 struct nfs4_cb_compound_hdr hdr = {
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400297 .ident = args->dl_ident,
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300298 .minorversion = rpc_args->args_seq.cbs_minorversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 };
300
301 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
302 encode_cb_compound_hdr(&xdr, &hdr);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300303 encode_cb_sequence(&xdr, &rpc_args->args_seq, &hdr);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300304 encode_cb_recall(&xdr, args, &hdr);
305 encode_cb_nops(&hdr);
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309
310static int
311decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
Al Virof00f3282006-10-19 23:29:01 -0700312 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 READ_BUF(8);
315 READ32(hdr->status);
316 READ32(hdr->taglen);
317 READ_BUF(hdr->taglen + 4);
318 hdr->tag = (char *)p;
319 p += XDR_QUADLEN(hdr->taglen);
320 READ32(hdr->nops);
321 return 0;
322}
323
324static int
325decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
326{
Al Virof00f3282006-10-19 23:29:01 -0700327 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u32 op;
329 int32_t nfserr;
330
331 READ_BUF(8);
332 READ32(op);
333 if (op != expected) {
334 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
335 " operation %d but we issued a request for %d\n",
336 op, expected);
337 return -EIO;
338 }
339 READ32(nfserr);
340 if (nfserr != NFS_OK)
341 return -nfs_cb_stat_to_errno(nfserr);
342 return 0;
343}
344
Benny Halevy2af73582009-09-10 12:26:51 +0300345/*
346 * Our current back channel implmentation supports a single backchannel
347 * with a single slot.
348 */
349static int
350decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *res,
351 struct rpc_rqst *rqstp)
352{
353 struct nfs4_sessionid id;
354 int status;
355 u32 dummy;
356 __be32 *p;
357
358 if (res->cbs_minorversion == 0)
359 return 0;
360
361 status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
362 if (status)
363 return status;
364
365 /*
366 * If the server returns different values for sessionID, slotID or
367 * sequence number, the server is looney tunes.
368 */
369 status = -ESERVERFAULT;
370
371 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
372 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
373 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
374 if (memcmp(id.data, res->cbs_clp->cl_sessionid.data,
375 NFS4_MAX_SESSIONID_LEN)) {
376 dprintk("%s Invalid session id\n", __func__);
377 goto out;
378 }
379 READ32(dummy);
380 if (dummy != res->cbs_clp->cl_cb_seq_nr) {
381 dprintk("%s Invalid sequence number\n", __func__);
382 goto out;
383 }
384 READ32(dummy); /* slotid must be 0 */
385 if (dummy != 0) {
386 dprintk("%s Invalid slotid\n", __func__);
387 goto out;
388 }
389 /* FIXME: process highest slotid and target highest slotid */
390 status = 0;
391out:
392 return status;
393}
394
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396static int
Al Virof00f3282006-10-19 23:29:01 -0700397nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 return 0;
400}
401
402static int
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300403nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
404 struct nfsd4_cb_sequence *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct xdr_stream xdr;
407 struct nfs4_cb_compound_hdr hdr;
408 int status;
409
410 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
411 status = decode_cb_compound_hdr(&xdr, &hdr);
412 if (status)
413 goto out;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300414 if (seq) {
415 status = decode_cb_sequence(&xdr, seq, rqstp);
416 if (status)
417 goto out;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
420out:
421 return status;
422}
423
424/*
425 * RPC procedure tables
426 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#define PROC(proc, call, argtype, restype) \
428[NFSPROC4_CLNT_##proc] = { \
429 .p_proc = NFSPROC4_CB_##call, \
430 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
431 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -0400432 .p_arglen = NFS4_##argtype##_sz, \
433 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -0500434 .p_statidx = NFSPROC4_CB_##call, \
435 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
NeilBrownfd39ca92005-06-23 22:04:03 -0700438static struct rpc_procinfo nfs4_cb_procedures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
440 PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
441};
442
NeilBrownfd39ca92005-06-23 22:04:03 -0700443static struct rpc_version nfs_cb_version4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 .number = 1,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800445 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 .procs = nfs4_cb_procedures
447};
448
449static struct rpc_version * nfs_cb_version[] = {
450 NULL,
451 &nfs_cb_version4,
452};
453
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400454static struct rpc_program cb_program;
455
456static struct rpc_stat cb_stats = {
457 .program = &cb_program
458};
459
460#define NFS4_CALLBACK 0x40000000
461static struct rpc_program cb_program = {
462 .name = "nfs4_cb",
463 .number = NFS4_CALLBACK,
464 .nrvers = ARRAY_SIZE(nfs_cb_version),
465 .version = nfs_cb_version,
466 .stats = &cb_stats,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500467 .pipe_dir_name = "/nfsd4_cb",
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400468};
469
J. Bruce Fields595947a2009-03-05 17:18:10 -0500470static int max_cb_time(void)
471{
472 return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
473}
474
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400475/* Reference counting, callback cleanup, etc., all look racy as heck.
476 * And why is cb_set an atomic? */
477
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800478int setup_callback_client(struct nfs4_client *clp)
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400479{
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400480 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
Chuck Leverae5c7942006-08-22 20:06:21 -0400481 struct rpc_timeout timeparms = {
J. Bruce Fields595947a2009-03-05 17:18:10 -0500482 .to_initval = max_cb_time(),
483 .to_retries = 0,
Chuck Leverae5c7942006-08-22 20:06:21 -0400484 };
Chuck Leverae5c7942006-08-22 20:06:21 -0400485 struct rpc_create_args args = {
486 .protocol = IPPROTO_TCP,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -0400487 .address = (struct sockaddr *) &cb->cb_addr,
488 .addrsize = cb->cb_addrlen,
Chuck Leverae5c7942006-08-22 20:06:21 -0400489 .timeout = &timeparms,
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400490 .program = &cb_program,
Benny Halevyd5b337b2008-09-28 09:21:26 +0300491 .prognumber = cb->cb_prog,
Chuck Leverae5c7942006-08-22 20:06:21 -0400492 .version = nfs_cb_version[1]->number,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500493 .authflavor = clp->cl_flavor,
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400494 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500495 .client_name = clp->cl_principal,
Chuck Leverae5c7942006-08-22 20:06:21 -0400496 };
J. Bruce Fields63c86712007-10-25 19:00:26 -0400497 struct rpc_clnt *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
J. Bruce Fields418cd20a2009-02-22 15:52:13 -0800499 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800500 return -EINVAL;
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500501
Chuck Leverae5c7942006-08-22 20:06:21 -0400502 /* Create RPC client */
J. Bruce Fields63c86712007-10-25 19:00:26 -0400503 client = rpc_create(&args);
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800504 if (IS_ERR(client)) {
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800505 dprintk("NFSD: couldn't create callback client: %ld\n",
506 PTR_ERR(client));
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800507 return PTR_ERR(client);
508 }
509 cb->cb_client = client;
510 return 0;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800511
512}
513
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800514static void warn_no_callback_path(struct nfs4_client *clp, int reason)
515{
516 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
517 (int)clp->cl_name.len, clp->cl_name.data, reason);
518}
519
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500520static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
521{
522 struct nfs4_client *clp = calldata;
523
524 if (task->tk_status)
525 warn_no_callback_path(clp, task->tk_status);
526 else
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400527 atomic_set(&clp->cl_cb_conn.cb_set, 1);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500528 put_nfs4_client(clp);
529}
530
531static const struct rpc_call_ops nfsd4_cb_probe_ops = {
532 .rpc_call_done = nfsd4_cb_probe_done,
533};
534
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400535static struct rpc_cred *callback_cred;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800536
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400537int set_callback_cred(void)
538{
539 callback_cred = rpc_lookup_machine_cred();
540 if (!callback_cred)
541 return -ENOMEM;
542 return 0;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800543}
544
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400545
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500546void do_probe_callback(struct nfs4_client *clp)
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800547{
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400548 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800549 struct rpc_message msg = {
550 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
551 .rpc_argp = clp,
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400552 .rpc_cred = callback_cred
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800553 };
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800554 int status;
555
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500556 status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_SOFT,
557 &nfsd4_cb_probe_ops, (void *)clp);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500558 if (status) {
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800559 warn_no_callback_path(clp, status);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500560 put_nfs4_client(clp);
561 }
J. Bruce Fields63c86712007-10-25 19:00:26 -0400562}
563
564/*
565 * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
566 */
567void
568nfsd4_probe_callback(struct nfs4_client *clp)
569{
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800570 int status;
J. Bruce Fields63c86712007-10-25 19:00:26 -0400571
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400572 BUG_ON(atomic_read(&clp->cl_cb_conn.cb_set));
J. Bruce Fields63c86712007-10-25 19:00:26 -0400573
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800574 status = setup_callback_client(clp);
575 if (status) {
576 warn_no_callback_path(clp, status);
577 return;
578 }
579
NeilBrown541e0e02006-04-10 22:55:38 -0700580 /* the task holds a reference to the nfs4_client struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 atomic_inc(&clp->cl_count);
582
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500583 do_probe_callback(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300586/*
587 * There's currently a single callback channel slot.
588 * If the slot is available, then mark it busy. Otherwise, set the
589 * thread for sleeping on the callback RPC wait queue.
590 */
591static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
592 struct rpc_task *task)
593{
594 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
595 u32 *ptr = (u32 *)clp->cl_sessionid.data;
596 int status = 0;
597
598 dprintk("%s: %u:%u:%u:%u\n", __func__,
599 ptr[0], ptr[1], ptr[2], ptr[3]);
600
601 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
602 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
603 dprintk("%s slot is busy\n", __func__);
604 status = -EAGAIN;
605 goto out;
606 }
607
608 /*
609 * We'll need the clp during XDR encoding and decoding,
610 * and the sequence during decoding to verify the reply
611 */
612 args->args_seq.cbs_clp = clp;
613 task->tk_msg.rpc_resp = &args->args_seq;
614
615out:
616 dprintk("%s status=%d\n", __func__, status);
617 return status;
618}
619
620/*
621 * TODO: cb_sequence should support referring call lists, cachethis, multiple
622 * slots, and mark callback channel down on communication errors.
623 */
624static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
625{
626 struct nfs4_delegation *dp = calldata;
627 struct nfs4_client *clp = dp->dl_client;
628 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
629 u32 minorversion = clp->cl_cb_conn.cb_minorversion;
630 int status = 0;
631
632 args->args_seq.cbs_minorversion = minorversion;
633 if (minorversion) {
634 status = nfsd41_cb_setup_sequence(clp, task);
635 if (status) {
636 if (status != -EAGAIN) {
637 /* terminate rpc task */
638 task->tk_status = status;
639 task->tk_action = NULL;
640 }
641 return;
642 }
643 }
644 rpc_call_start(task);
645}
646
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300647static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
648{
649 struct nfs4_delegation *dp = calldata;
650 struct nfs4_client *clp = dp->dl_client;
651
652 dprintk("%s: minorversion=%d\n", __func__,
653 clp->cl_cb_conn.cb_minorversion);
654
655 if (clp->cl_cb_conn.cb_minorversion) {
656 /* No need for lock, access serialized in nfsd4_cb_prepare */
657 ++clp->cl_cb_seq_nr;
658 clear_bit(0, &clp->cl_cb_slot_busy);
659 rpc_wake_up_next(&clp->cl_cb_waitq);
660 dprintk("%s: freed slot, new seqid=%d\n", __func__,
661 clp->cl_cb_seq_nr);
662
663 /* We're done looking into the sequence information */
664 task->tk_msg.rpc_resp = NULL;
665 }
666}
667
J. Bruce Fields63e48632009-05-01 22:36:55 -0400668static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
669{
670 struct nfs4_delegation *dp = calldata;
671 struct nfs4_client *clp = dp->dl_client;
672
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300673 nfsd4_cb_done(task, calldata);
674
J. Bruce Fields63e48632009-05-01 22:36:55 -0400675 switch (task->tk_status) {
676 case -EIO:
677 /* Network partition? */
678 atomic_set(&clp->cl_cb_conn.cb_set, 0);
679 warn_no_callback_path(clp, task->tk_status);
680 case -EBADHANDLE:
681 case -NFS4ERR_BAD_STATEID:
682 /* Race: client probably got cb_recall
683 * before open reply granting delegation */
684 break;
685 default:
686 /* success, or error we can't handle */
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300687 goto done;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400688 }
689 if (dp->dl_retries--) {
690 rpc_delay(task, 2*HZ);
691 task->tk_status = 0;
692 rpc_restart_call(task);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300693 return;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400694 } else {
695 atomic_set(&clp->cl_cb_conn.cb_set, 0);
696 warn_no_callback_path(clp, task->tk_status);
697 }
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300698done:
699 kfree(task->tk_msg.rpc_argp);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400700}
701
702static void nfsd4_cb_recall_release(void *calldata)
703{
704 struct nfs4_delegation *dp = calldata;
705 struct nfs4_client *clp = dp->dl_client;
706
707 nfs4_put_delegation(dp);
708 put_nfs4_client(clp);
709}
710
711static const struct rpc_call_ops nfsd4_cb_recall_ops = {
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300712 .rpc_call_prepare = nfsd4_cb_prepare,
J. Bruce Fields63e48632009-05-01 22:36:55 -0400713 .rpc_call_done = nfsd4_cb_recall_done,
714 .rpc_release = nfsd4_cb_recall_release,
715};
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717/*
718 * called with dp->dl_count inc'ed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 */
720void
721nfsd4_cb_recall(struct nfs4_delegation *dp)
722{
723 struct nfs4_client *clp = dp->dl_client;
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400724 struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300725 struct nfs4_rpc_args *args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 struct rpc_message msg = {
727 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400728 .rpc_cred = callback_cred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 };
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300730 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300732 args = kzalloc(sizeof(*args), GFP_KERNEL);
733 if (!args)
734 goto out;
735 args->args_op = dp;
736 msg.rpc_argp = args;
J. Bruce Fields3aea09d2009-05-01 20:11:12 -0400737 dp->dl_retries = 1;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400738 status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
739 &nfsd4_cb_recall_ops, dp);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300740out:
J. Bruce Fields63e48632009-05-01 22:36:55 -0400741 if (status) {
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300742 kfree(args);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400743 put_nfs4_client(clp);
744 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}