bnx2x: fix compilation without CONFIG_BNX2X_SRIOV
[linux-2.6/btrfs-unstable.git] / fs / nfsd / nfs4callback.c
blob99bc85ff02179f3e28ed36f56872e1e48d190e21
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <andros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/svc_xprt.h>
36 #include <linux/slab.h>
37 #include "nfsd.h"
38 #include "state.h"
39 #include "netns.h"
41 #define NFSDDBG_FACILITY NFSDDBG_PROC
43 static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
45 #define NFSPROC4_CB_NULL 0
46 #define NFSPROC4_CB_COMPOUND 1
48 /* Index of predefined Linux callback client operations */
50 enum {
51 NFSPROC4_CLNT_CB_NULL = 0,
52 NFSPROC4_CLNT_CB_RECALL,
53 NFSPROC4_CLNT_CB_SEQUENCE,
56 #define NFS4_MAXTAGLEN 20
58 #define NFS4_enc_cb_null_sz 0
59 #define NFS4_dec_cb_null_sz 0
60 #define cb_compound_enc_hdr_sz 4
61 #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
62 #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
63 #define cb_sequence_enc_sz (sessionid_sz + 4 + \
64 1 /* no referring calls list yet */)
65 #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
67 #define op_enc_sz 1
68 #define op_dec_sz 2
69 #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
70 #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
71 #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
72 cb_sequence_enc_sz + \
73 1 + enc_stateid_sz + \
74 enc_nfs4_fh_sz)
76 #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
77 cb_sequence_dec_sz + \
78 op_dec_sz)
80 struct nfs4_cb_compound_hdr {
81 /* args */
82 u32 ident; /* minorversion 0 only */
83 u32 nops;
84 __be32 *nops_p;
85 u32 minorversion;
86 /* res */
87 int status;
91 * Handle decode buffer overflows out-of-line.
93 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
95 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
96 "Remaining buffer length is %tu words.\n",
97 func, xdr->end - xdr->p);
100 static __be32 *xdr_encode_empty_array(__be32 *p)
102 *p++ = xdr_zero;
103 return p;
107 * Encode/decode NFSv4 CB basic data types
109 * Basic NFSv4 callback data types are defined in section 15 of RFC
110 * 3530: "Network File System (NFS) version 4 Protocol" and section
111 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
112 * 1 Protocol"
116 * nfs_cb_opnum4
118 * enum nfs_cb_opnum4 {
119 * OP_CB_GETATTR = 3,
120 * ...
121 * };
123 enum nfs_cb_opnum4 {
124 OP_CB_GETATTR = 3,
125 OP_CB_RECALL = 4,
126 OP_CB_LAYOUTRECALL = 5,
127 OP_CB_NOTIFY = 6,
128 OP_CB_PUSH_DELEG = 7,
129 OP_CB_RECALL_ANY = 8,
130 OP_CB_RECALLABLE_OBJ_AVAIL = 9,
131 OP_CB_RECALL_SLOT = 10,
132 OP_CB_SEQUENCE = 11,
133 OP_CB_WANTS_CANCELLED = 12,
134 OP_CB_NOTIFY_LOCK = 13,
135 OP_CB_NOTIFY_DEVICEID = 14,
136 OP_CB_ILLEGAL = 10044
139 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
141 __be32 *p;
143 p = xdr_reserve_space(xdr, 4);
144 *p = cpu_to_be32(op);
148 * nfs_fh4
150 * typedef opaque nfs_fh4<NFS4_FHSIZE>;
152 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
154 u32 length = fh->fh_size;
155 __be32 *p;
157 BUG_ON(length > NFS4_FHSIZE);
158 p = xdr_reserve_space(xdr, 4 + length);
159 xdr_encode_opaque(p, &fh->fh_base, length);
163 * stateid4
165 * struct stateid4 {
166 * uint32_t seqid;
167 * opaque other[12];
168 * };
170 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
172 __be32 *p;
174 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
175 *p++ = cpu_to_be32(sid->si_generation);
176 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
180 * sessionid4
182 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
184 static void encode_sessionid4(struct xdr_stream *xdr,
185 const struct nfsd4_session *session)
187 __be32 *p;
189 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
190 xdr_encode_opaque_fixed(p, session->se_sessionid.data,
191 NFS4_MAX_SESSIONID_LEN);
195 * nfsstat4
197 static const struct {
198 int stat;
199 int errno;
200 } nfs_cb_errtbl[] = {
201 { NFS4_OK, 0 },
202 { NFS4ERR_PERM, -EPERM },
203 { NFS4ERR_NOENT, -ENOENT },
204 { NFS4ERR_IO, -EIO },
205 { NFS4ERR_NXIO, -ENXIO },
206 { NFS4ERR_ACCESS, -EACCES },
207 { NFS4ERR_EXIST, -EEXIST },
208 { NFS4ERR_XDEV, -EXDEV },
209 { NFS4ERR_NOTDIR, -ENOTDIR },
210 { NFS4ERR_ISDIR, -EISDIR },
211 { NFS4ERR_INVAL, -EINVAL },
212 { NFS4ERR_FBIG, -EFBIG },
213 { NFS4ERR_NOSPC, -ENOSPC },
214 { NFS4ERR_ROFS, -EROFS },
215 { NFS4ERR_MLINK, -EMLINK },
216 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
217 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
218 { NFS4ERR_DQUOT, -EDQUOT },
219 { NFS4ERR_STALE, -ESTALE },
220 { NFS4ERR_BADHANDLE, -EBADHANDLE },
221 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
222 { NFS4ERR_NOTSUPP, -ENOTSUPP },
223 { NFS4ERR_TOOSMALL, -ETOOSMALL },
224 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
225 { NFS4ERR_BADTYPE, -EBADTYPE },
226 { NFS4ERR_LOCKED, -EAGAIN },
227 { NFS4ERR_RESOURCE, -EREMOTEIO },
228 { NFS4ERR_SYMLINK, -ELOOP },
229 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
230 { NFS4ERR_DEADLOCK, -EDEADLK },
231 { -1, -EIO }
235 * If we cannot translate the error, the recovery routines should
236 * handle it.
238 * Note: remaining NFSv4 error codes have values > 10000, so should
239 * not conflict with native Linux error codes.
241 static int nfs_cb_stat_to_errno(int status)
243 int i;
245 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
246 if (nfs_cb_errtbl[i].stat == status)
247 return nfs_cb_errtbl[i].errno;
250 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
251 return -status;
254 static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
255 enum nfsstat4 *status)
257 __be32 *p;
258 u32 op;
260 p = xdr_inline_decode(xdr, 4 + 4);
261 if (unlikely(p == NULL))
262 goto out_overflow;
263 op = be32_to_cpup(p++);
264 if (unlikely(op != expected))
265 goto out_unexpected;
266 *status = be32_to_cpup(p);
267 return 0;
268 out_overflow:
269 print_overflow_msg(__func__, xdr);
270 return -EIO;
271 out_unexpected:
272 dprintk("NFSD: Callback server returned operation %d but "
273 "we issued a request for %d\n", op, expected);
274 return -EIO;
278 * CB_COMPOUND4args
280 * struct CB_COMPOUND4args {
281 * utf8str_cs tag;
282 * uint32_t minorversion;
283 * uint32_t callback_ident;
284 * nfs_cb_argop4 argarray<>;
285 * };
287 static void encode_cb_compound4args(struct xdr_stream *xdr,
288 struct nfs4_cb_compound_hdr *hdr)
290 __be32 * p;
292 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
293 p = xdr_encode_empty_array(p); /* empty tag */
294 *p++ = cpu_to_be32(hdr->minorversion);
295 *p++ = cpu_to_be32(hdr->ident);
297 hdr->nops_p = p;
298 *p = cpu_to_be32(hdr->nops); /* argarray element count */
302 * Update argarray element count
304 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
306 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
307 *hdr->nops_p = cpu_to_be32(hdr->nops);
311 * CB_COMPOUND4res
313 * struct CB_COMPOUND4res {
314 * nfsstat4 status;
315 * utf8str_cs tag;
316 * nfs_cb_resop4 resarray<>;
317 * };
319 static int decode_cb_compound4res(struct xdr_stream *xdr,
320 struct nfs4_cb_compound_hdr *hdr)
322 u32 length;
323 __be32 *p;
325 p = xdr_inline_decode(xdr, 4 + 4);
326 if (unlikely(p == NULL))
327 goto out_overflow;
328 hdr->status = be32_to_cpup(p++);
329 /* Ignore the tag */
330 length = be32_to_cpup(p++);
331 p = xdr_inline_decode(xdr, length + 4);
332 if (unlikely(p == NULL))
333 goto out_overflow;
334 hdr->nops = be32_to_cpup(p);
335 return 0;
336 out_overflow:
337 print_overflow_msg(__func__, xdr);
338 return -EIO;
342 * CB_RECALL4args
344 * struct CB_RECALL4args {
345 * stateid4 stateid;
346 * bool truncate;
347 * nfs_fh4 fh;
348 * };
350 static void encode_cb_recall4args(struct xdr_stream *xdr,
351 const struct nfs4_delegation *dp,
352 struct nfs4_cb_compound_hdr *hdr)
354 __be32 *p;
356 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
357 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
359 p = xdr_reserve_space(xdr, 4);
360 *p++ = xdr_zero; /* truncate */
362 encode_nfs_fh4(xdr, &dp->dl_fh);
364 hdr->nops++;
368 * CB_SEQUENCE4args
370 * struct CB_SEQUENCE4args {
371 * sessionid4 csa_sessionid;
372 * sequenceid4 csa_sequenceid;
373 * slotid4 csa_slotid;
374 * slotid4 csa_highest_slotid;
375 * bool csa_cachethis;
376 * referring_call_list4 csa_referring_call_lists<>;
377 * };
379 static void encode_cb_sequence4args(struct xdr_stream *xdr,
380 const struct nfsd4_callback *cb,
381 struct nfs4_cb_compound_hdr *hdr)
383 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
384 __be32 *p;
386 if (hdr->minorversion == 0)
387 return;
389 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
390 encode_sessionid4(xdr, session);
392 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
393 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
394 *p++ = xdr_zero; /* csa_slotid */
395 *p++ = xdr_zero; /* csa_highest_slotid */
396 *p++ = xdr_zero; /* csa_cachethis */
397 xdr_encode_empty_array(p); /* csa_referring_call_lists */
399 hdr->nops++;
403 * CB_SEQUENCE4resok
405 * struct CB_SEQUENCE4resok {
406 * sessionid4 csr_sessionid;
407 * sequenceid4 csr_sequenceid;
408 * slotid4 csr_slotid;
409 * slotid4 csr_highest_slotid;
410 * slotid4 csr_target_highest_slotid;
411 * };
413 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
414 * case NFS4_OK:
415 * CB_SEQUENCE4resok csr_resok4;
416 * default:
417 * void;
418 * };
420 * Our current back channel implmentation supports a single backchannel
421 * with a single slot.
423 static int decode_cb_sequence4resok(struct xdr_stream *xdr,
424 struct nfsd4_callback *cb)
426 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
427 struct nfs4_sessionid id;
428 int status;
429 __be32 *p;
430 u32 dummy;
432 status = -ESERVERFAULT;
435 * If the server returns different values for sessionID, slotID or
436 * sequence number, the server is looney tunes.
438 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
439 if (unlikely(p == NULL))
440 goto out_overflow;
441 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
442 if (memcmp(id.data, session->se_sessionid.data,
443 NFS4_MAX_SESSIONID_LEN) != 0) {
444 dprintk("NFS: %s Invalid session id\n", __func__);
445 goto out;
447 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
449 dummy = be32_to_cpup(p++);
450 if (dummy != session->se_cb_seq_nr) {
451 dprintk("NFS: %s Invalid sequence number\n", __func__);
452 goto out;
455 dummy = be32_to_cpup(p++);
456 if (dummy != 0) {
457 dprintk("NFS: %s Invalid slotid\n", __func__);
458 goto out;
462 * FIXME: process highest slotid and target highest slotid
464 status = 0;
465 out:
466 if (status)
467 nfsd4_mark_cb_fault(cb->cb_clp, status);
468 return status;
469 out_overflow:
470 print_overflow_msg(__func__, xdr);
471 return -EIO;
474 static int decode_cb_sequence4res(struct xdr_stream *xdr,
475 struct nfsd4_callback *cb)
477 enum nfsstat4 nfserr;
478 int status;
480 if (cb->cb_minorversion == 0)
481 return 0;
483 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
484 if (unlikely(status))
485 goto out;
486 if (unlikely(nfserr != NFS4_OK))
487 goto out_default;
488 status = decode_cb_sequence4resok(xdr, cb);
489 out:
490 return status;
491 out_default:
492 return nfs_cb_stat_to_errno(nfserr);
496 * NFSv4.0 and NFSv4.1 XDR encode functions
498 * NFSv4.0 callback argument types are defined in section 15 of RFC
499 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
500 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
501 * Protocol".
505 * NB: Without this zero space reservation, callbacks over krb5p fail
507 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
508 void *__unused)
510 xdr_reserve_space(xdr, 0);
514 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
516 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
517 const struct nfsd4_callback *cb)
519 const struct nfs4_delegation *args = cb->cb_op;
520 struct nfs4_cb_compound_hdr hdr = {
521 .ident = cb->cb_clp->cl_cb_ident,
522 .minorversion = cb->cb_minorversion,
525 encode_cb_compound4args(xdr, &hdr);
526 encode_cb_sequence4args(xdr, cb, &hdr);
527 encode_cb_recall4args(xdr, args, &hdr);
528 encode_cb_nops(&hdr);
533 * NFSv4.0 and NFSv4.1 XDR decode functions
535 * NFSv4.0 callback result types are defined in section 15 of RFC
536 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
537 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
538 * Protocol".
541 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
542 void *__unused)
544 return 0;
548 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
550 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
551 struct xdr_stream *xdr,
552 struct nfsd4_callback *cb)
554 struct nfs4_cb_compound_hdr hdr;
555 enum nfsstat4 nfserr;
556 int status;
558 status = decode_cb_compound4res(xdr, &hdr);
559 if (unlikely(status))
560 goto out;
562 if (cb != NULL) {
563 status = decode_cb_sequence4res(xdr, cb);
564 if (unlikely(status))
565 goto out;
568 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
569 if (unlikely(status))
570 goto out;
571 if (unlikely(nfserr != NFS4_OK))
572 status = nfs_cb_stat_to_errno(nfserr);
573 out:
574 return status;
578 * RPC procedure tables
580 #define PROC(proc, call, argtype, restype) \
581 [NFSPROC4_CLNT_##proc] = { \
582 .p_proc = NFSPROC4_CB_##call, \
583 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
584 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
585 .p_arglen = NFS4_enc_##argtype##_sz, \
586 .p_replen = NFS4_dec_##restype##_sz, \
587 .p_statidx = NFSPROC4_CB_##call, \
588 .p_name = #proc, \
591 static struct rpc_procinfo nfs4_cb_procedures[] = {
592 PROC(CB_NULL, NULL, cb_null, cb_null),
593 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
596 static struct rpc_version nfs_cb_version4 = {
598 * Note on the callback rpc program version number: despite language in rfc
599 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
600 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
601 * in practice that appears to be what implementations use. The section
602 * 18.36.3 language is expected to be fixed in an erratum.
604 .number = 1,
605 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
606 .procs = nfs4_cb_procedures
609 static const struct rpc_version *nfs_cb_version[] = {
610 &nfs_cb_version4,
613 static const struct rpc_program cb_program;
615 static struct rpc_stat cb_stats = {
616 .program = &cb_program
619 #define NFS4_CALLBACK 0x40000000
620 static const struct rpc_program cb_program = {
621 .name = "nfs4_cb",
622 .number = NFS4_CALLBACK,
623 .nrvers = ARRAY_SIZE(nfs_cb_version),
624 .version = nfs_cb_version,
625 .stats = &cb_stats,
626 .pipe_dir_name = "nfsd4_cb",
629 static int max_cb_time(struct net *net)
631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
632 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
635 static struct rpc_cred *callback_cred;
637 int set_callback_cred(void)
639 if (callback_cred)
640 return 0;
641 callback_cred = rpc_lookup_machine_cred("nfs");
642 if (!callback_cred)
643 return -ENOMEM;
644 return 0;
647 static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
649 if (clp->cl_minorversion == 0) {
650 return get_rpccred(callback_cred);
651 } else {
652 struct rpc_auth *auth = client->cl_auth;
653 struct auth_cred acred = {};
655 acred.uid = ses->se_cb_sec.uid;
656 acred.gid = ses->se_cb_sec.gid;
657 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
661 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
663 struct rpc_timeout timeparms = {
664 .to_initval = max_cb_time(clp->net),
665 .to_retries = 0,
667 struct rpc_create_args args = {
668 .net = clp->net,
669 .address = (struct sockaddr *) &conn->cb_addr,
670 .addrsize = conn->cb_addrlen,
671 .saddress = (struct sockaddr *) &conn->cb_saddr,
672 .timeout = &timeparms,
673 .program = &cb_program,
674 .version = 0,
675 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
677 struct rpc_clnt *client;
678 struct rpc_cred *cred;
680 if (clp->cl_minorversion == 0) {
681 if (!clp->cl_cred.cr_principal &&
682 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
683 return -EINVAL;
684 args.client_name = clp->cl_cred.cr_principal;
685 args.prognumber = conn->cb_prog,
686 args.protocol = XPRT_TRANSPORT_TCP;
687 args.authflavor = clp->cl_cred.cr_flavor;
688 clp->cl_cb_ident = conn->cb_ident;
689 } else {
690 if (!conn->cb_xprt)
691 return -EINVAL;
692 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
693 clp->cl_cb_session = ses;
694 args.bc_xprt = conn->cb_xprt;
695 args.prognumber = clp->cl_cb_session->se_cb_prog;
696 args.protocol = XPRT_TRANSPORT_BC_TCP;
697 args.authflavor = ses->se_cb_sec.flavor;
699 /* Create RPC client */
700 client = rpc_create(&args);
701 if (IS_ERR(client)) {
702 dprintk("NFSD: couldn't create callback client: %ld\n",
703 PTR_ERR(client));
704 return PTR_ERR(client);
706 cred = get_backchannel_cred(clp, client, ses);
707 if (IS_ERR(cred)) {
708 rpc_shutdown_client(client);
709 return PTR_ERR(cred);
711 clp->cl_cb_client = client;
712 clp->cl_cb_cred = cred;
713 return 0;
716 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
718 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
719 (int)clp->cl_name.len, clp->cl_name.data, reason);
722 static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
724 clp->cl_cb_state = NFSD4_CB_DOWN;
725 warn_no_callback_path(clp, reason);
728 static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
730 clp->cl_cb_state = NFSD4_CB_FAULT;
731 warn_no_callback_path(clp, reason);
734 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
736 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
738 if (task->tk_status)
739 nfsd4_mark_cb_down(clp, task->tk_status);
740 else
741 clp->cl_cb_state = NFSD4_CB_UP;
744 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
745 /* XXX: release method to ensure we set the cb channel down if
746 * necessary on early failure? */
747 .rpc_call_done = nfsd4_cb_probe_done,
750 static struct workqueue_struct *callback_wq;
752 static void run_nfsd4_cb(struct nfsd4_callback *cb)
754 queue_work(callback_wq, &cb->cb_work);
757 static void do_probe_callback(struct nfs4_client *clp)
759 struct nfsd4_callback *cb = &clp->cl_cb_null;
761 cb->cb_op = NULL;
762 cb->cb_clp = clp;
764 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
765 cb->cb_msg.rpc_argp = NULL;
766 cb->cb_msg.rpc_resp = NULL;
768 cb->cb_ops = &nfsd4_cb_probe_ops;
770 run_nfsd4_cb(cb);
774 * Poke the callback thread to process any updates to the callback
775 * parameters, and send a null probe.
777 void nfsd4_probe_callback(struct nfs4_client *clp)
779 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
780 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
781 do_probe_callback(clp);
784 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
786 nfsd4_probe_callback(clp);
787 flush_workqueue(callback_wq);
790 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
792 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
793 spin_lock(&clp->cl_lock);
794 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
795 spin_unlock(&clp->cl_lock);
799 * There's currently a single callback channel slot.
800 * If the slot is available, then mark it busy. Otherwise, set the
801 * thread for sleeping on the callback RPC wait queue.
803 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
805 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
806 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
807 dprintk("%s slot is busy\n", __func__);
808 return false;
810 return true;
814 * TODO: cb_sequence should support referring call lists, cachethis, multiple
815 * slots, and mark callback channel down on communication errors.
817 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
819 struct nfsd4_callback *cb = calldata;
820 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
821 struct nfs4_client *clp = dp->dl_stid.sc_client;
822 u32 minorversion = clp->cl_minorversion;
824 cb->cb_minorversion = minorversion;
825 if (minorversion) {
826 if (!nfsd41_cb_get_slot(clp, task))
827 return;
829 spin_lock(&clp->cl_lock);
830 if (list_empty(&cb->cb_per_client)) {
831 /* This is the first call, not a restart */
832 cb->cb_done = false;
833 list_add(&cb->cb_per_client, &clp->cl_callbacks);
835 spin_unlock(&clp->cl_lock);
836 rpc_call_start(task);
839 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
841 struct nfsd4_callback *cb = calldata;
842 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
843 struct nfs4_client *clp = dp->dl_stid.sc_client;
845 dprintk("%s: minorversion=%d\n", __func__,
846 clp->cl_minorversion);
848 if (clp->cl_minorversion) {
849 /* No need for lock, access serialized in nfsd4_cb_prepare */
850 ++clp->cl_cb_session->se_cb_seq_nr;
851 clear_bit(0, &clp->cl_cb_slot_busy);
852 rpc_wake_up_next(&clp->cl_cb_waitq);
853 dprintk("%s: freed slot, new seqid=%d\n", __func__,
854 clp->cl_cb_session->se_cb_seq_nr);
856 /* We're done looking into the sequence information */
857 task->tk_msg.rpc_resp = NULL;
862 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
864 struct nfsd4_callback *cb = calldata;
865 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
866 struct nfs4_client *clp = dp->dl_stid.sc_client;
867 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
869 nfsd4_cb_done(task, calldata);
871 if (current_rpc_client != task->tk_client) {
872 /* We're shutting down or changing cl_cb_client; leave
873 * it to nfsd4_process_cb_update to restart the call if
874 * necessary. */
875 return;
878 if (cb->cb_done)
879 return;
880 switch (task->tk_status) {
881 case 0:
882 cb->cb_done = true;
883 return;
884 case -EBADHANDLE:
885 case -NFS4ERR_BAD_STATEID:
886 /* Race: client probably got cb_recall
887 * before open reply granting delegation */
888 break;
889 default:
890 /* Network partition? */
891 nfsd4_mark_cb_down(clp, task->tk_status);
893 if (dp->dl_retries--) {
894 rpc_delay(task, 2*HZ);
895 task->tk_status = 0;
896 rpc_restart_call_prepare(task);
897 return;
899 nfsd4_mark_cb_down(clp, task->tk_status);
900 cb->cb_done = true;
903 static void nfsd4_cb_recall_release(void *calldata)
905 struct nfsd4_callback *cb = calldata;
906 struct nfs4_client *clp = cb->cb_clp;
907 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
909 if (cb->cb_done) {
910 spin_lock(&clp->cl_lock);
911 list_del(&cb->cb_per_client);
912 spin_unlock(&clp->cl_lock);
913 nfs4_put_delegation(dp);
917 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
918 .rpc_call_prepare = nfsd4_cb_prepare,
919 .rpc_call_done = nfsd4_cb_recall_done,
920 .rpc_release = nfsd4_cb_recall_release,
923 int nfsd4_create_callback_queue(void)
925 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
926 if (!callback_wq)
927 return -ENOMEM;
928 return 0;
931 void nfsd4_destroy_callback_queue(void)
933 destroy_workqueue(callback_wq);
936 /* must be called under the state lock */
937 void nfsd4_shutdown_callback(struct nfs4_client *clp)
939 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
941 * Note this won't actually result in a null callback;
942 * instead, nfsd4_do_callback_rpc() will detect the killed
943 * client, destroy the rpc client, and stop:
945 do_probe_callback(clp);
946 flush_workqueue(callback_wq);
949 static void nfsd4_release_cb(struct nfsd4_callback *cb)
951 if (cb->cb_ops->rpc_release)
952 cb->cb_ops->rpc_release(cb);
955 /* requires cl_lock: */
956 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
958 struct nfsd4_session *s;
959 struct nfsd4_conn *c;
961 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
962 list_for_each_entry(c, &s->se_conns, cn_persession) {
963 if (c->cn_flags & NFS4_CDFC4_BACK)
964 return c;
967 return NULL;
970 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
972 struct nfs4_cb_conn conn;
973 struct nfs4_client *clp = cb->cb_clp;
974 struct nfsd4_session *ses = NULL;
975 struct nfsd4_conn *c;
976 int err;
979 * This is either an update, or the client dying; in either case,
980 * kill the old client:
982 if (clp->cl_cb_client) {
983 rpc_shutdown_client(clp->cl_cb_client);
984 clp->cl_cb_client = NULL;
985 put_rpccred(clp->cl_cb_cred);
986 clp->cl_cb_cred = NULL;
988 if (clp->cl_cb_conn.cb_xprt) {
989 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
990 clp->cl_cb_conn.cb_xprt = NULL;
992 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
993 return;
994 spin_lock(&clp->cl_lock);
996 * Only serialized callback code is allowed to clear these
997 * flags; main nfsd code can only set them:
999 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
1000 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
1001 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
1002 c = __nfsd4_find_backchannel(clp);
1003 if (c) {
1004 svc_xprt_get(c->cn_xprt);
1005 conn.cb_xprt = c->cn_xprt;
1006 ses = c->cn_session;
1008 spin_unlock(&clp->cl_lock);
1010 err = setup_callback_client(clp, &conn, ses);
1011 if (err) {
1012 nfsd4_mark_cb_down(clp, err);
1013 return;
1015 /* Yay, the callback channel's back! Restart any callbacks: */
1016 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
1017 run_nfsd4_cb(cb);
1020 static void nfsd4_do_callback_rpc(struct work_struct *w)
1022 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
1023 struct nfs4_client *clp = cb->cb_clp;
1024 struct rpc_clnt *clnt;
1026 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
1027 nfsd4_process_cb_update(cb);
1029 clnt = clp->cl_cb_client;
1030 if (!clnt) {
1031 /* Callback channel broken, or client killed; give up: */
1032 nfsd4_release_cb(cb);
1033 return;
1035 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
1036 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1037 cb->cb_ops, cb);
1040 void nfsd4_init_callback(struct nfsd4_callback *cb)
1042 INIT_WORK(&cb->cb_work, nfsd4_do_callback_rpc);
1045 void nfsd4_cb_recall(struct nfs4_delegation *dp)
1047 struct nfsd4_callback *cb = &dp->dl_recall;
1048 struct nfs4_client *clp = dp->dl_stid.sc_client;
1050 dp->dl_retries = 1;
1051 cb->cb_op = dp;
1052 cb->cb_clp = clp;
1053 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
1054 cb->cb_msg.rpc_argp = cb;
1055 cb->cb_msg.rpc_resp = cb;
1057 cb->cb_ops = &nfsd4_cb_recall_ops;
1059 INIT_LIST_HEAD(&cb->cb_per_client);
1060 cb->cb_done = true;
1062 run_nfsd4_cb(&dp->dl_recall);