blob: 1e6497ed3e1219c4db2abcf07f0c380ab4b4007a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <andros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sunrpc/clnt.h>
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -050035#include <linux/sunrpc/svc_xprt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020037#include "nfsd.h"
38#include "state.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#define NFSDDBG_FACILITY NFSDDBG_PROC
41
42#define NFSPROC4_CB_NULL 0
43#define NFSPROC4_CB_COMPOUND 1
Andy Adamson38524ab2009-09-10 12:25:59 +030044#define NFS4_STATEID_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Index of predefined Linux callback client operations */
47
48enum {
Benny Halevy4be36ca2009-09-10 12:25:46 +030049 NFSPROC4_CLNT_CB_NULL = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 NFSPROC4_CLNT_CB_RECALL,
Andy Adamson38524ab2009-09-10 12:25:59 +030051 NFSPROC4_CLNT_CB_SEQUENCE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54enum nfs_cb_opnum4 {
55 OP_CB_RECALL = 4,
Andy Adamson38524ab2009-09-10 12:25:59 +030056 OP_CB_SEQUENCE = 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -070057};
58
59#define NFS4_MAXTAGLEN 20
60
61#define NFS4_enc_cb_null_sz 0
62#define NFS4_dec_cb_null_sz 0
63#define cb_compound_enc_hdr_sz 4
64#define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
Andy Adamson38524ab2009-09-10 12:25:59 +030065#define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
66#define cb_sequence_enc_sz (sessionid_sz + 4 + \
67 1 /* no referring calls list yet */)
68#define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
69
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define op_enc_sz 1
71#define op_dec_sz 2
72#define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
Benny Halevy0ac68d12007-07-17 04:04:37 -070073#define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074#define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030075 cb_sequence_enc_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 1 + enc_stateid_sz + \
77 enc_nfs4_fh_sz)
78
79#define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030080 cb_sequence_dec_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 op_dec_sz)
82
83/*
84* Generic encode routines from fs/nfs/nfs4xdr.c
85*/
Al Virof00f3282006-10-19 23:29:01 -070086static inline __be32 *
87xdr_writemem(__be32 *p, const void *ptr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 int tmp = XDR_QUADLEN(nbytes);
90 if (!tmp)
91 return p;
92 p[tmp-1] = 0;
93 memcpy(p, ptr, nbytes);
94 return p + tmp;
95}
96
97#define WRITE32(n) *p++ = htonl(n)
98#define WRITEMEM(ptr,nbytes) do { \
99 p = xdr_writemem(p, ptr, nbytes); \
100} while (0)
101#define RESERVE_SPACE(nbytes) do { \
102 p = xdr_reserve_space(xdr, nbytes); \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700103 if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 BUG_ON(!p); \
105} while (0)
106
107/*
108 * Generic decode routines from fs/nfs/nfs4xdr.c
109 */
110#define DECODE_TAIL \
111 status = 0; \
112out: \
113 return status; \
114xdr_error: \
115 dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
116 status = -EIO; \
117 goto out
118
119#define READ32(x) (x) = ntohl(*p++)
120#define READ64(x) do { \
121 (x) = (u64)ntohl(*p++) << 32; \
122 (x) |= ntohl(*p++); \
123} while (0)
124#define READTIME(x) do { \
125 p++; \
126 (x.tv_sec) = ntohl(*p++); \
127 (x.tv_nsec) = ntohl(*p++); \
128} while (0)
129#define READ_BUF(nbytes) do { \
130 p = xdr_inline_decode(xdr, nbytes); \
131 if (!p) { \
Greg Banks3e3b4802006-10-02 02:17:41 -0700132 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700133 __func__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -EIO; \
135 } \
136} while (0)
137
138struct nfs4_cb_compound_hdr {
Andy Adamson38524ab2009-09-10 12:25:59 +0300139 /* args */
140 u32 ident; /* minorversion 0 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 u32 nops;
Andy Adamsonef52bff2009-06-16 04:20:50 +0300142 __be32 *nops_p;
Andy Adamsonab52ae62009-06-16 04:20:53 +0300143 u32 minorversion;
Andy Adamson38524ab2009-09-10 12:25:59 +0300144 /* res */
145 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146};
147
148static struct {
149int stat;
150int errno;
151} nfs_cb_errtbl[] = {
152 { NFS4_OK, 0 },
153 { NFS4ERR_PERM, EPERM },
154 { NFS4ERR_NOENT, ENOENT },
155 { NFS4ERR_IO, EIO },
156 { NFS4ERR_NXIO, ENXIO },
157 { NFS4ERR_ACCESS, EACCES },
158 { NFS4ERR_EXIST, EEXIST },
159 { NFS4ERR_XDEV, EXDEV },
160 { NFS4ERR_NOTDIR, ENOTDIR },
161 { NFS4ERR_ISDIR, EISDIR },
162 { NFS4ERR_INVAL, EINVAL },
163 { NFS4ERR_FBIG, EFBIG },
164 { NFS4ERR_NOSPC, ENOSPC },
165 { NFS4ERR_ROFS, EROFS },
166 { NFS4ERR_MLINK, EMLINK },
167 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
168 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
169 { NFS4ERR_DQUOT, EDQUOT },
170 { NFS4ERR_STALE, ESTALE },
171 { NFS4ERR_BADHANDLE, EBADHANDLE },
172 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
173 { NFS4ERR_NOTSUPP, ENOTSUPP },
174 { NFS4ERR_TOOSMALL, ETOOSMALL },
175 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
176 { NFS4ERR_BADTYPE, EBADTYPE },
177 { NFS4ERR_LOCKED, EAGAIN },
178 { NFS4ERR_RESOURCE, EREMOTEIO },
179 { NFS4ERR_SYMLINK, ELOOP },
180 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
181 { NFS4ERR_DEADLOCK, EDEADLK },
182 { -1, EIO }
183};
184
185static int
186nfs_cb_stat_to_errno(int stat)
187{
188 int i;
189 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
190 if (nfs_cb_errtbl[i].stat == stat)
191 return nfs_cb_errtbl[i].errno;
192 }
193 /* If we cannot translate the error, the recovery routines should
194 * handle it.
195 * Note: remaining NFSv4 error codes have values > 10000, so should
196 * not conflict with native Linux error codes.
197 */
198 return stat;
199}
200
201/*
202 * XDR encode
203 */
204
Andy Adamsonef52bff2009-06-16 04:20:50 +0300205static void
Benny Halevy9303bbd2010-05-25 09:50:23 +0300206encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
207{
208 __be32 *p;
209
210 RESERVE_SPACE(sizeof(stateid_t));
211 WRITE32(sid->si_generation);
212 WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
213}
214
215static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
217{
Al Virof00f3282006-10-19 23:29:01 -0700218 __be32 * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 RESERVE_SPACE(16);
221 WRITE32(0); /* tag length is always 0 */
Andy Adamsonab52ae62009-06-16 04:20:53 +0300222 WRITE32(hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 WRITE32(hdr->ident);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300224 hdr->nops_p = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 WRITE32(hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Andy Adamsonef52bff2009-06-16 04:20:50 +0300228static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
229{
230 *hdr->nops_p = htonl(hdr->nops);
231}
232
233static void
234encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
235 struct nfs4_cb_compound_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Al Virof00f3282006-10-19 23:29:01 -0700237 __be32 *p;
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400238 int len = dp->dl_fh.fh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Benny Halevy9303bbd2010-05-25 09:50:23 +0300240 RESERVE_SPACE(4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 WRITE32(OP_CB_RECALL);
Benny Halevy9303bbd2010-05-25 09:50:23 +0300242 encode_stateid(xdr, &dp->dl_stateid);
243 RESERVE_SPACE(8 + (XDR_QUADLEN(len) << 2));
J. Bruce Fields6707bd32009-05-01 19:57:46 -0400244 WRITE32(0); /* truncate optimization not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 WRITE32(len);
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400246 WRITEMEM(&dp->dl_fh.fh_base, len);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300247 hdr->nops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
Benny Halevy2af73582009-09-10 12:26:51 +0300250static void
251encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *args,
252 struct nfs4_cb_compound_hdr *hdr)
253{
254 __be32 *p;
255
256 if (hdr->minorversion == 0)
257 return;
258
259 RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
260
261 WRITE32(OP_CB_SEQUENCE);
262 WRITEMEM(args->cbs_clp->cl_sessionid.data, NFS4_MAX_SESSIONID_LEN);
263 WRITE32(args->cbs_clp->cl_cb_seq_nr);
264 WRITE32(0); /* slotid, always 0 */
265 WRITE32(0); /* highest slotid always 0 */
266 WRITE32(0); /* cachethis always 0 */
267 WRITE32(0); /* FIXME: support referring_call_lists */
268 hdr->nops++;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271static int
Al Virof00f3282006-10-19 23:29:01 -0700272nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct xdr_stream xdrs, *xdr = &xdrs;
275
276 xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
277 RESERVE_SPACE(0);
278 return 0;
279}
280
281static int
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300282nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
283 struct nfs4_rpc_args *rpc_args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct xdr_stream xdr;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300286 struct nfs4_delegation *args = rpc_args->args_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct nfs4_cb_compound_hdr hdr = {
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400288 .ident = args->dl_ident,
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300289 .minorversion = rpc_args->args_seq.cbs_minorversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 };
291
292 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
293 encode_cb_compound_hdr(&xdr, &hdr);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300294 encode_cb_sequence(&xdr, &rpc_args->args_seq, &hdr);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300295 encode_cb_recall(&xdr, args, &hdr);
296 encode_cb_nops(&hdr);
297 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
300
301static int
302decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
Al Virof00f3282006-10-19 23:29:01 -0700303 __be32 *p;
J. Bruce Fields68a4b482010-05-27 09:30:39 -0400304 u32 taglen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306 READ_BUF(8);
307 READ32(hdr->status);
J. Bruce Fields68a4b482010-05-27 09:30:39 -0400308 /* We've got no use for the tag; ignore it: */
309 READ32(taglen);
310 READ_BUF(taglen + 4);
311 p += XDR_QUADLEN(taglen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 READ32(hdr->nops);
313 return 0;
314}
315
316static int
317decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
318{
Al Virof00f3282006-10-19 23:29:01 -0700319 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 u32 op;
321 int32_t nfserr;
322
323 READ_BUF(8);
324 READ32(op);
325 if (op != expected) {
326 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
327 " operation %d but we issued a request for %d\n",
328 op, expected);
329 return -EIO;
330 }
331 READ32(nfserr);
332 if (nfserr != NFS_OK)
333 return -nfs_cb_stat_to_errno(nfserr);
334 return 0;
335}
336
Benny Halevy2af73582009-09-10 12:26:51 +0300337/*
338 * Our current back channel implmentation supports a single backchannel
339 * with a single slot.
340 */
341static int
342decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *res,
343 struct rpc_rqst *rqstp)
344{
345 struct nfs4_sessionid id;
346 int status;
347 u32 dummy;
348 __be32 *p;
349
350 if (res->cbs_minorversion == 0)
351 return 0;
352
353 status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
354 if (status)
355 return status;
356
357 /*
358 * If the server returns different values for sessionID, slotID or
359 * sequence number, the server is looney tunes.
360 */
361 status = -ESERVERFAULT;
362
363 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
364 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
365 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
366 if (memcmp(id.data, res->cbs_clp->cl_sessionid.data,
367 NFS4_MAX_SESSIONID_LEN)) {
368 dprintk("%s Invalid session id\n", __func__);
369 goto out;
370 }
371 READ32(dummy);
372 if (dummy != res->cbs_clp->cl_cb_seq_nr) {
373 dprintk("%s Invalid sequence number\n", __func__);
374 goto out;
375 }
376 READ32(dummy); /* slotid must be 0 */
377 if (dummy != 0) {
378 dprintk("%s Invalid slotid\n", __func__);
379 goto out;
380 }
381 /* FIXME: process highest slotid and target highest slotid */
382 status = 0;
383out:
384 return status;
385}
386
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static int
Al Virof00f3282006-10-19 23:29:01 -0700389nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 return 0;
392}
393
394static int
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300395nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
396 struct nfsd4_cb_sequence *seq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 struct xdr_stream xdr;
399 struct nfs4_cb_compound_hdr hdr;
400 int status;
401
402 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
403 status = decode_cb_compound_hdr(&xdr, &hdr);
404 if (status)
405 goto out;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300406 if (seq) {
407 status = decode_cb_sequence(&xdr, seq, rqstp);
408 if (status)
409 goto out;
410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
412out:
413 return status;
414}
415
416/*
417 * RPC procedure tables
418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#define PROC(proc, call, argtype, restype) \
420[NFSPROC4_CLNT_##proc] = { \
421 .p_proc = NFSPROC4_CB_##call, \
422 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
423 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -0400424 .p_arglen = NFS4_##argtype##_sz, \
425 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -0500426 .p_statidx = NFSPROC4_CB_##call, \
427 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
NeilBrownfd39ca92005-06-23 22:04:03 -0700430static struct rpc_procinfo nfs4_cb_procedures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
432 PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
433};
434
NeilBrownfd39ca92005-06-23 22:04:03 -0700435static struct rpc_version nfs_cb_version4 = {
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400436/*
437 * Note on the callback rpc program version number: despite language in rfc
438 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
439 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
440 * in practice that appears to be what implementations use. The section
441 * 18.36.3 language is expected to be fixed in an erratum.
442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 .number = 1,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800444 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 .procs = nfs4_cb_procedures
446};
447
448static struct rpc_version * nfs_cb_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 &nfs_cb_version4,
450};
451
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400452static struct rpc_program cb_program;
453
454static struct rpc_stat cb_stats = {
455 .program = &cb_program
456};
457
458#define NFS4_CALLBACK 0x40000000
459static struct rpc_program cb_program = {
460 .name = "nfs4_cb",
461 .number = NFS4_CALLBACK,
462 .nrvers = ARRAY_SIZE(nfs_cb_version),
463 .version = nfs_cb_version,
464 .stats = &cb_stats,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500465 .pipe_dir_name = "/nfsd4_cb",
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400466};
467
J. Bruce Fields595947a2009-03-05 17:18:10 -0500468static int max_cb_time(void)
469{
J. Bruce Fieldscf07d2e2010-02-28 23:20:19 -0500470 return max(nfsd4_lease/10, (time_t)1) * HZ;
J. Bruce Fields595947a2009-03-05 17:18:10 -0500471}
472
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400473/* Reference counting, callback cleanup, etc., all look racy as heck.
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500474 * And why is cl_cb_set an atomic? */
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400475
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500476int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *cb)
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400477{
Chuck Leverae5c7942006-08-22 20:06:21 -0400478 struct rpc_timeout timeparms = {
J. Bruce Fields595947a2009-03-05 17:18:10 -0500479 .to_initval = max_cb_time(),
480 .to_retries = 0,
Chuck Leverae5c7942006-08-22 20:06:21 -0400481 };
Chuck Leverae5c7942006-08-22 20:06:21 -0400482 struct rpc_create_args args = {
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300483 .protocol = XPRT_TRANSPORT_TCP,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -0400484 .address = (struct sockaddr *) &cb->cb_addr,
485 .addrsize = cb->cb_addrlen,
Chuck Leverae5c7942006-08-22 20:06:21 -0400486 .timeout = &timeparms,
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400487 .program = &cb_program,
Benny Halevyd5b337b2008-09-28 09:21:26 +0300488 .prognumber = cb->cb_prog,
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400489 .version = 0,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500490 .authflavor = clp->cl_flavor,
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400491 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500492 .client_name = clp->cl_principal,
Chuck Leverae5c7942006-08-22 20:06:21 -0400493 };
J. Bruce Fields63c86712007-10-25 19:00:26 -0400494 struct rpc_clnt *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
J. Bruce Fields418cd202009-02-22 15:52:13 -0800496 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800497 return -EINVAL;
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300498 if (cb->cb_minorversion) {
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500499 args.bc_xprt = cb->cb_xprt;
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300500 args.protocol = XPRT_TRANSPORT_BC_TCP;
501 }
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 }
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500509 nfsd4_set_callback_client(clp, client);
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800510 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 Fields2bf23872010-03-08 12:37:27 -0500527 atomic_set(&clp->cl_cb_set, 1);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500528}
529
530static const struct rpc_call_ops nfsd4_cb_probe_ops = {
531 .rpc_call_done = nfsd4_cb_probe_done,
532};
533
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400534static struct rpc_cred *callback_cred;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800535
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400536int set_callback_cred(void)
537{
J. Bruce Fields8d75da8a2010-03-03 16:13:29 -0500538 if (callback_cred)
539 return 0;
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400540 callback_cred = rpc_lookup_machine_cred();
541 if (!callback_cred)
542 return -ENOMEM;
543 return 0;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800544}
545
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400546
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500547void do_probe_callback(struct nfs4_client *clp)
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800548{
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 Fields2bf23872010-03-08 12:37:27 -0500556 status = rpc_call_async(clp->cl_cb_client, &msg,
Chuck Lever58255a42010-02-24 13:48:06 -0800557 RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500558 &nfsd4_cb_probe_ops, (void *)clp);
J. Bruce Fields9045b4b2010-02-21 17:53:04 -0800559 if (status)
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800560 warn_no_callback_path(clp, status);
J. Bruce Fields63c86712007-10-25 19:00:26 -0400561}
562
563/*
564 * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
565 */
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500566void nfsd4_probe_callback(struct nfs4_client *clp, struct nfs4_cb_conn *cb)
J. Bruce Fields63c86712007-10-25 19:00:26 -0400567{
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800568 int status;
J. Bruce Fields63c86712007-10-25 19:00:26 -0400569
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500570 BUG_ON(atomic_read(&clp->cl_cb_set));
J. Bruce Fields63c86712007-10-25 19:00:26 -0400571
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500572 status = setup_callback_client(clp, cb);
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800573 if (status) {
574 warn_no_callback_path(clp, status);
575 return;
576 }
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500577 do_probe_callback(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300580/*
581 * There's currently a single callback channel slot.
582 * If the slot is available, then mark it busy. Otherwise, set the
583 * thread for sleeping on the callback RPC wait queue.
584 */
585static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
586 struct rpc_task *task)
587{
588 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
589 u32 *ptr = (u32 *)clp->cl_sessionid.data;
590 int status = 0;
591
592 dprintk("%s: %u:%u:%u:%u\n", __func__,
593 ptr[0], ptr[1], ptr[2], ptr[3]);
594
595 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
596 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
597 dprintk("%s slot is busy\n", __func__);
598 status = -EAGAIN;
599 goto out;
600 }
601
602 /*
603 * We'll need the clp during XDR encoding and decoding,
604 * and the sequence during decoding to verify the reply
605 */
606 args->args_seq.cbs_clp = clp;
607 task->tk_msg.rpc_resp = &args->args_seq;
608
609out:
610 dprintk("%s status=%d\n", __func__, status);
611 return status;
612}
613
614/*
615 * TODO: cb_sequence should support referring call lists, cachethis, multiple
616 * slots, and mark callback channel down on communication errors.
617 */
618static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
619{
620 struct nfs4_delegation *dp = calldata;
621 struct nfs4_client *clp = dp->dl_client;
622 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
623 u32 minorversion = clp->cl_cb_conn.cb_minorversion;
624 int status = 0;
625
626 args->args_seq.cbs_minorversion = minorversion;
627 if (minorversion) {
628 status = nfsd41_cb_setup_sequence(clp, task);
629 if (status) {
630 if (status != -EAGAIN) {
631 /* terminate rpc task */
632 task->tk_status = status;
633 task->tk_action = NULL;
634 }
635 return;
636 }
637 }
638 rpc_call_start(task);
639}
640
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300641static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
642{
643 struct nfs4_delegation *dp = calldata;
644 struct nfs4_client *clp = dp->dl_client;
645
646 dprintk("%s: minorversion=%d\n", __func__,
647 clp->cl_cb_conn.cb_minorversion);
648
649 if (clp->cl_cb_conn.cb_minorversion) {
650 /* No need for lock, access serialized in nfsd4_cb_prepare */
651 ++clp->cl_cb_seq_nr;
652 clear_bit(0, &clp->cl_cb_slot_busy);
653 rpc_wake_up_next(&clp->cl_cb_waitq);
654 dprintk("%s: freed slot, new seqid=%d\n", __func__,
655 clp->cl_cb_seq_nr);
656
657 /* We're done looking into the sequence information */
658 task->tk_msg.rpc_resp = NULL;
659 }
660}
661
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500662
J. Bruce Fields63e48632009-05-01 22:36:55 -0400663static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
664{
665 struct nfs4_delegation *dp = calldata;
666 struct nfs4_client *clp = dp->dl_client;
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500667 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400668
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300669 nfsd4_cb_done(task, calldata);
670
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500671 if (current_rpc_client == NULL) {
672 /* We're shutting down; give up. */
673 /* XXX: err, or is it ok just to fall through
674 * and rpc_restart_call? */
675 return;
676 }
677
J. Bruce Fields63e48632009-05-01 22:36:55 -0400678 switch (task->tk_status) {
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400679 case 0:
680 return;
681 case -EBADHANDLE:
682 case -NFS4ERR_BAD_STATEID:
683 /* Race: client probably got cb_recall
684 * before open reply granting delegation */
685 break;
686 default:
J. Bruce Fields63e48632009-05-01 22:36:55 -0400687 /* Network partition? */
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500688 atomic_set(&clp->cl_cb_set, 0);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400689 warn_no_callback_path(clp, task->tk_status);
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500690 if (current_rpc_client != task->tk_client) {
691 /* queue a callback on the new connection: */
J. Bruce Fieldscba9ba42010-06-01 11:21:40 -0400692 atomic_inc(&dp->dl_count);
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500693 nfsd4_cb_recall(dp);
694 return;
695 }
J. Bruce Fields63e48632009-05-01 22:36:55 -0400696 }
697 if (dp->dl_retries--) {
698 rpc_delay(task, 2*HZ);
699 task->tk_status = 0;
700 rpc_restart_call(task);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300701 return;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400702 } else {
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500703 atomic_set(&clp->cl_cb_set, 0);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400704 warn_no_callback_path(clp, task->tk_status);
705 }
706}
707
708static void nfsd4_cb_recall_release(void *calldata)
709{
710 struct nfs4_delegation *dp = calldata;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400711
712 nfs4_put_delegation(dp);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400713}
714
715static const struct rpc_call_ops nfsd4_cb_recall_ops = {
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300716 .rpc_call_prepare = nfsd4_cb_prepare,
J. Bruce Fields63e48632009-05-01 22:36:55 -0400717 .rpc_call_done = nfsd4_cb_recall_done,
718 .rpc_release = nfsd4_cb_recall_release,
719};
720
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500721static struct workqueue_struct *callback_wq;
722
723int nfsd4_create_callback_queue(void)
724{
725 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
726 if (!callback_wq)
727 return -ENOMEM;
728 return 0;
729}
730
731void nfsd4_destroy_callback_queue(void)
732{
733 destroy_workqueue(callback_wq);
734}
735
Benny Halevyab707e152010-05-12 00:14:06 +0300736/* must be called under the state lock */
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500737void nfsd4_set_callback_client(struct nfs4_client *clp, struct rpc_clnt *new)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500738{
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500739 struct rpc_clnt *old = clp->cl_cb_client;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500740
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500741 clp->cl_cb_client = new;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500742 /*
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500743 * After this, any work that saw the old value of cl_cb_client will
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500744 * be gone:
745 */
746 flush_workqueue(callback_wq);
747 /* So we can safely shut it down: */
748 if (old)
749 rpc_shutdown_client(old);
750}
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * called with dp->dl_count inc'ed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 */
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500755static void _nfsd4_cb_recall(struct nfs4_delegation *dp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
757 struct nfs4_client *clp = dp->dl_client;
J. Bruce Fields2bf23872010-03-08 12:37:27 -0500758 struct rpc_clnt *clnt = clp->cl_cb_client;
J. Bruce Fields227f98d92010-02-18 08:27:24 -0800759 struct nfs4_rpc_args *args = &dp->dl_recall.cb_args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct rpc_message msg = {
761 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400762 .rpc_cred = callback_cred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
J. Bruce Fieldsac94bf52010-05-31 19:06:39 -0400765 if (clnt == NULL) {
766 nfs4_put_delegation(dp);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500767 return; /* Client is shutting down; give up. */
J. Bruce Fieldsac94bf52010-05-31 19:06:39 -0400768 }
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500769
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300770 args->args_op = dp;
771 msg.rpc_argp = args;
J. Bruce Fields3aea09d2009-05-01 20:11:12 -0400772 dp->dl_retries = 1;
J. Bruce Fields378b7d32010-05-25 11:57:56 -0400773 rpc_call_async(clnt, &msg, RPC_TASK_SOFT, &nfsd4_cb_recall_ops, dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500775
776void nfsd4_do_callback_rpc(struct work_struct *w)
777{
778 /* XXX: for now, just send off delegation recall. */
779 /* In future, generalize to handle any sort of callback. */
780 struct nfsd4_callback *c = container_of(w, struct nfsd4_callback, cb_work);
781 struct nfs4_delegation *dp = container_of(c, struct nfs4_delegation, dl_recall);
782
783 _nfsd4_cb_recall(dp);
784}
785
786
787void nfsd4_cb_recall(struct nfs4_delegation *dp)
788{
789 queue_work(callback_wq, &dp->dl_recall.cb_work);
790}