2 * Copyright (c) 2001 The Regents of the University of Michigan.
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
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/xprt.h>
36 #include <linux/sunrpc/svc_xprt.h>
37 #include <linux/slab.h>
43 #define NFSDDBG_FACILITY NFSDDBG_PROC
45 static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
47 #define NFSPROC4_CB_NULL 0
48 #define NFSPROC4_CB_COMPOUND 1
50 /* Index of predefined Linux callback client operations */
53 NFSPROC4_CLNT_CB_NULL = 0,
54 NFSPROC4_CLNT_CB_RECALL,
55 NFSPROC4_CLNT_CB_SEQUENCE,
58 #define to_delegation(cb) \
59 container_of(cb, struct nfs4_delegation, dl_recall)
61 struct nfs4_cb_compound_hdr {
63 u32 ident; /* minorversion 0 only */
72 * Handle decode buffer overflows out-of-line.
74 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
76 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
77 "Remaining buffer length is %tu words.\n",
78 func, xdr->end - xdr->p);
81 static __be32 *xdr_encode_empty_array(__be32 *p)
88 * Encode/decode NFSv4 CB basic data types
90 * Basic NFSv4 callback data types are defined in section 15 of RFC
91 * 3530: "Network File System (NFS) version 4 Protocol" and section
92 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
99 * enum nfs_cb_opnum4 {
107 OP_CB_LAYOUTRECALL = 5,
109 OP_CB_PUSH_DELEG = 7,
110 OP_CB_RECALL_ANY = 8,
111 OP_CB_RECALLABLE_OBJ_AVAIL = 9,
112 OP_CB_RECALL_SLOT = 10,
114 OP_CB_WANTS_CANCELLED = 12,
115 OP_CB_NOTIFY_LOCK = 13,
116 OP_CB_NOTIFY_DEVICEID = 14,
117 OP_CB_ILLEGAL = 10044
120 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
124 p = xdr_reserve_space(xdr, 4);
125 *p = cpu_to_be32(op);
131 * typedef opaque nfs_fh4<NFS4_FHSIZE>;
133 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
135 u32 length = fh->fh_size;
138 BUG_ON(length > NFS4_FHSIZE);
139 p = xdr_reserve_space(xdr, 4 + length);
140 xdr_encode_opaque(p, &fh->fh_base, length);
151 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
155 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
156 *p++ = cpu_to_be32(sid->si_generation);
157 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
163 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
165 static void encode_sessionid4(struct xdr_stream *xdr,
166 const struct nfsd4_session *session)
170 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
171 xdr_encode_opaque_fixed(p, session->se_sessionid.data,
172 NFS4_MAX_SESSIONID_LEN);
178 static const struct {
181 } nfs_cb_errtbl[] = {
183 { NFS4ERR_PERM, -EPERM },
184 { NFS4ERR_NOENT, -ENOENT },
185 { NFS4ERR_IO, -EIO },
186 { NFS4ERR_NXIO, -ENXIO },
187 { NFS4ERR_ACCESS, -EACCES },
188 { NFS4ERR_EXIST, -EEXIST },
189 { NFS4ERR_XDEV, -EXDEV },
190 { NFS4ERR_NOTDIR, -ENOTDIR },
191 { NFS4ERR_ISDIR, -EISDIR },
192 { NFS4ERR_INVAL, -EINVAL },
193 { NFS4ERR_FBIG, -EFBIG },
194 { NFS4ERR_NOSPC, -ENOSPC },
195 { NFS4ERR_ROFS, -EROFS },
196 { NFS4ERR_MLINK, -EMLINK },
197 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
198 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
199 { NFS4ERR_DQUOT, -EDQUOT },
200 { NFS4ERR_STALE, -ESTALE },
201 { NFS4ERR_BADHANDLE, -EBADHANDLE },
202 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
203 { NFS4ERR_NOTSUPP, -ENOTSUPP },
204 { NFS4ERR_TOOSMALL, -ETOOSMALL },
205 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
206 { NFS4ERR_BADTYPE, -EBADTYPE },
207 { NFS4ERR_LOCKED, -EAGAIN },
208 { NFS4ERR_RESOURCE, -EREMOTEIO },
209 { NFS4ERR_SYMLINK, -ELOOP },
210 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
211 { NFS4ERR_DEADLOCK, -EDEADLK },
216 * If we cannot translate the error, the recovery routines should
219 * Note: remaining NFSv4 error codes have values > 10000, so should
220 * not conflict with native Linux error codes.
222 static int nfs_cb_stat_to_errno(int status)
226 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
227 if (nfs_cb_errtbl[i].stat == status)
228 return nfs_cb_errtbl[i].errno;
231 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
235 static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
236 enum nfsstat4 *status)
241 p = xdr_inline_decode(xdr, 4 + 4);
242 if (unlikely(p == NULL))
244 op = be32_to_cpup(p++);
245 if (unlikely(op != expected))
247 *status = be32_to_cpup(p);
250 print_overflow_msg(__func__, xdr);
253 dprintk("NFSD: Callback server returned operation %d but "
254 "we issued a request for %d\n", op, expected);
261 * struct CB_COMPOUND4args {
263 * uint32_t minorversion;
264 * uint32_t callback_ident;
265 * nfs_cb_argop4 argarray<>;
268 static void encode_cb_compound4args(struct xdr_stream *xdr,
269 struct nfs4_cb_compound_hdr *hdr)
273 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
274 p = xdr_encode_empty_array(p); /* empty tag */
275 *p++ = cpu_to_be32(hdr->minorversion);
276 *p++ = cpu_to_be32(hdr->ident);
279 *p = cpu_to_be32(hdr->nops); /* argarray element count */
283 * Update argarray element count
285 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
287 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
288 *hdr->nops_p = cpu_to_be32(hdr->nops);
294 * struct CB_COMPOUND4res {
297 * nfs_cb_resop4 resarray<>;
300 static int decode_cb_compound4res(struct xdr_stream *xdr,
301 struct nfs4_cb_compound_hdr *hdr)
306 p = xdr_inline_decode(xdr, 4 + 4);
307 if (unlikely(p == NULL))
309 hdr->status = be32_to_cpup(p++);
311 length = be32_to_cpup(p++);
312 p = xdr_inline_decode(xdr, length + 4);
313 if (unlikely(p == NULL))
315 hdr->nops = be32_to_cpup(p);
318 print_overflow_msg(__func__, xdr);
325 * struct CB_RECALL4args {
331 static void encode_cb_recall4args(struct xdr_stream *xdr,
332 const struct nfs4_delegation *dp,
333 struct nfs4_cb_compound_hdr *hdr)
337 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
338 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
340 p = xdr_reserve_space(xdr, 4);
341 *p++ = xdr_zero; /* truncate */
343 encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
351 * struct CB_SEQUENCE4args {
352 * sessionid4 csa_sessionid;
353 * sequenceid4 csa_sequenceid;
354 * slotid4 csa_slotid;
355 * slotid4 csa_highest_slotid;
356 * bool csa_cachethis;
357 * referring_call_list4 csa_referring_call_lists<>;
360 static void encode_cb_sequence4args(struct xdr_stream *xdr,
361 const struct nfsd4_callback *cb,
362 struct nfs4_cb_compound_hdr *hdr)
364 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
367 if (hdr->minorversion == 0)
370 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
371 encode_sessionid4(xdr, session);
373 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
374 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
375 *p++ = xdr_zero; /* csa_slotid */
376 *p++ = xdr_zero; /* csa_highest_slotid */
377 *p++ = xdr_zero; /* csa_cachethis */
378 xdr_encode_empty_array(p); /* csa_referring_call_lists */
386 * struct CB_SEQUENCE4resok {
387 * sessionid4 csr_sessionid;
388 * sequenceid4 csr_sequenceid;
389 * slotid4 csr_slotid;
390 * slotid4 csr_highest_slotid;
391 * slotid4 csr_target_highest_slotid;
394 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
396 * CB_SEQUENCE4resok csr_resok4;
401 * Our current back channel implmentation supports a single backchannel
402 * with a single slot.
404 static int decode_cb_sequence4resok(struct xdr_stream *xdr,
405 struct nfsd4_callback *cb)
407 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
408 struct nfs4_sessionid id;
413 status = -ESERVERFAULT;
416 * If the server returns different values for sessionID, slotID or
417 * sequence number, the server is looney tunes.
419 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
420 if (unlikely(p == NULL))
422 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
423 if (memcmp(id.data, session->se_sessionid.data,
424 NFS4_MAX_SESSIONID_LEN) != 0) {
425 dprintk("NFS: %s Invalid session id\n", __func__);
428 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
430 dummy = be32_to_cpup(p++);
431 if (dummy != session->se_cb_seq_nr) {
432 dprintk("NFS: %s Invalid sequence number\n", __func__);
436 dummy = be32_to_cpup(p++);
438 dprintk("NFS: %s Invalid slotid\n", __func__);
443 * FIXME: process highest slotid and target highest slotid
448 nfsd4_mark_cb_fault(cb->cb_clp, status);
451 print_overflow_msg(__func__, xdr);
455 static int decode_cb_sequence4res(struct xdr_stream *xdr,
456 struct nfsd4_callback *cb)
458 enum nfsstat4 nfserr;
461 if (cb->cb_minorversion == 0)
464 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
465 if (unlikely(status))
467 if (unlikely(nfserr != NFS4_OK))
469 status = decode_cb_sequence4resok(xdr, cb);
473 return nfs_cb_stat_to_errno(nfserr);
477 * NFSv4.0 and NFSv4.1 XDR encode functions
479 * NFSv4.0 callback argument types are defined in section 15 of RFC
480 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
481 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
486 * NB: Without this zero space reservation, callbacks over krb5p fail
488 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
491 xdr_reserve_space(xdr, 0);
495 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
497 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
498 const struct nfsd4_callback *cb)
500 const struct nfs4_delegation *dp = to_delegation(cb);
501 struct nfs4_cb_compound_hdr hdr = {
502 .ident = cb->cb_clp->cl_cb_ident,
503 .minorversion = cb->cb_minorversion,
506 encode_cb_compound4args(xdr, &hdr);
507 encode_cb_sequence4args(xdr, cb, &hdr);
508 encode_cb_recall4args(xdr, dp, &hdr);
509 encode_cb_nops(&hdr);
514 * NFSv4.0 and NFSv4.1 XDR decode functions
516 * NFSv4.0 callback result types are defined in section 15 of RFC
517 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
518 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
522 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
529 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
531 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
532 struct xdr_stream *xdr,
533 struct nfsd4_callback *cb)
535 struct nfs4_cb_compound_hdr hdr;
536 enum nfsstat4 nfserr;
539 status = decode_cb_compound4res(xdr, &hdr);
540 if (unlikely(status))
544 status = decode_cb_sequence4res(xdr, cb);
545 if (unlikely(status))
549 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
550 if (unlikely(status))
552 if (unlikely(nfserr != NFS4_OK))
553 status = nfs_cb_stat_to_errno(nfserr);
559 * RPC procedure tables
561 #define PROC(proc, call, argtype, restype) \
562 [NFSPROC4_CLNT_##proc] = { \
563 .p_proc = NFSPROC4_CB_##call, \
564 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
565 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
566 .p_arglen = NFS4_enc_##argtype##_sz, \
567 .p_replen = NFS4_dec_##restype##_sz, \
568 .p_statidx = NFSPROC4_CB_##call, \
572 static struct rpc_procinfo nfs4_cb_procedures[] = {
573 PROC(CB_NULL, NULL, cb_null, cb_null),
574 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
577 static struct rpc_version nfs_cb_version4 = {
579 * Note on the callback rpc program version number: despite language in rfc
580 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
581 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
582 * in practice that appears to be what implementations use. The section
583 * 18.36.3 language is expected to be fixed in an erratum.
586 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
587 .procs = nfs4_cb_procedures
590 static const struct rpc_version *nfs_cb_version[] = {
594 static const struct rpc_program cb_program;
596 static struct rpc_stat cb_stats = {
597 .program = &cb_program
600 #define NFS4_CALLBACK 0x40000000
601 static const struct rpc_program cb_program = {
603 .number = NFS4_CALLBACK,
604 .nrvers = ARRAY_SIZE(nfs_cb_version),
605 .version = nfs_cb_version,
607 .pipe_dir_name = "nfsd4_cb",
610 static int max_cb_time(struct net *net)
612 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
613 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
616 static struct rpc_cred *callback_cred;
618 int set_callback_cred(void)
622 callback_cred = rpc_lookup_machine_cred("nfs");
628 static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
630 if (clp->cl_minorversion == 0) {
631 return get_rpccred(callback_cred);
633 struct rpc_auth *auth = client->cl_auth;
634 struct auth_cred acred = {};
636 acred.uid = ses->se_cb_sec.uid;
637 acred.gid = ses->se_cb_sec.gid;
638 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
642 static struct rpc_clnt *create_backchannel_client(struct rpc_create_args *args)
644 struct rpc_xprt *xprt;
646 if (args->protocol != XPRT_TRANSPORT_BC_TCP)
647 return rpc_create(args);
649 xprt = args->bc_xprt->xpt_bc_xprt;
652 return rpc_create_xprt(args, xprt);
655 return rpc_create(args);
658 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
660 int maxtime = max_cb_time(clp->net);
661 struct rpc_timeout timeparms = {
662 .to_initval = maxtime,
664 .to_maxval = maxtime,
666 struct rpc_create_args args = {
668 .address = (struct sockaddr *) &conn->cb_addr,
669 .addrsize = conn->cb_addrlen,
670 .saddress = (struct sockaddr *) &conn->cb_saddr,
671 .timeout = &timeparms,
672 .program = &cb_program,
674 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
676 struct rpc_clnt *client;
677 struct rpc_cred *cred;
679 if (clp->cl_minorversion == 0) {
680 if (!clp->cl_cred.cr_principal &&
681 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
683 args.client_name = clp->cl_cred.cr_principal;
684 args.prognumber = conn->cb_prog;
685 args.protocol = XPRT_TRANSPORT_TCP;
686 args.authflavor = clp->cl_cred.cr_flavor;
687 clp->cl_cb_ident = conn->cb_ident;
691 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
692 clp->cl_cb_session = ses;
693 args.bc_xprt = conn->cb_xprt;
694 args.prognumber = clp->cl_cb_session->se_cb_prog;
695 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
697 args.authflavor = ses->se_cb_sec.flavor;
699 /* Create RPC client */
700 client = create_backchannel_client(&args);
701 if (IS_ERR(client)) {
702 dprintk("NFSD: couldn't create callback client: %ld\n",
704 return PTR_ERR(client);
706 cred = get_backchannel_cred(clp, client, ses);
708 rpc_shutdown_client(client);
709 return PTR_ERR(cred);
711 clp->cl_cb_client = client;
712 clp->cl_cb_cred = cred;
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);
739 nfsd4_mark_cb_down(clp, task->tk_status);
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;
763 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
764 cb->cb_msg.rpc_argp = NULL;
765 cb->cb_msg.rpc_resp = NULL;
767 cb->cb_ops = &nfsd4_cb_probe_ops;
773 * Poke the callback thread to process any updates to the callback
774 * parameters, and send a null probe.
776 void nfsd4_probe_callback(struct nfs4_client *clp)
778 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
779 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
780 do_probe_callback(clp);
783 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
785 nfsd4_probe_callback(clp);
786 flush_workqueue(callback_wq);
789 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
791 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
792 spin_lock(&clp->cl_lock);
793 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
794 spin_unlock(&clp->cl_lock);
798 * There's currently a single callback channel slot.
799 * If the slot is available, then mark it busy. Otherwise, set the
800 * thread for sleeping on the callback RPC wait queue.
802 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
804 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
805 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
806 dprintk("%s slot is busy\n", __func__);
813 * TODO: cb_sequence should support referring call lists, cachethis, multiple
814 * slots, and mark callback channel down on communication errors.
816 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
818 struct nfsd4_callback *cb = calldata;
819 struct nfs4_client *clp = cb->cb_clp;
820 u32 minorversion = clp->cl_minorversion;
822 cb->cb_minorversion = minorversion;
824 if (!nfsd41_cb_get_slot(clp, task))
827 spin_lock(&clp->cl_lock);
828 if (list_empty(&cb->cb_per_client)) {
829 /* This is the first call, not a restart */
831 list_add(&cb->cb_per_client, &clp->cl_callbacks);
833 spin_unlock(&clp->cl_lock);
834 rpc_call_start(task);
837 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
839 struct nfsd4_callback *cb = calldata;
840 struct nfs4_client *clp = cb->cb_clp;
842 dprintk("%s: minorversion=%d\n", __func__,
843 clp->cl_minorversion);
845 if (clp->cl_minorversion) {
846 /* No need for lock, access serialized in nfsd4_cb_prepare */
847 ++clp->cl_cb_session->se_cb_seq_nr;
848 clear_bit(0, &clp->cl_cb_slot_busy);
849 rpc_wake_up_next(&clp->cl_cb_waitq);
850 dprintk("%s: freed slot, new seqid=%d\n", __func__,
851 clp->cl_cb_session->se_cb_seq_nr);
855 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
857 struct nfsd4_callback *cb = calldata;
858 struct nfs4_delegation *dp = to_delegation(cb);
859 struct nfs4_client *clp = cb->cb_clp;
860 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
862 nfsd4_cb_done(task, calldata);
864 if (current_rpc_client != task->tk_client) {
865 /* We're shutting down or changing cl_cb_client; leave
866 * it to nfsd4_process_cb_update to restart the call if
873 switch (task->tk_status) {
877 case -NFS4ERR_BAD_STATEID:
878 /* Race: client probably got cb_recall
879 * before open reply granting delegation */
880 if (dp->dl_retries--) {
881 rpc_delay(task, 2*HZ);
883 rpc_restart_call_prepare(task);
887 /* Network partition? */
888 nfsd4_mark_cb_down(clp, task->tk_status);
893 static void nfsd4_cb_recall_release(void *calldata)
895 struct nfsd4_callback *cb = calldata;
896 struct nfs4_client *clp = cb->cb_clp;
899 struct nfs4_delegation *dp = to_delegation(cb);
901 spin_lock(&clp->cl_lock);
902 list_del(&cb->cb_per_client);
903 spin_unlock(&clp->cl_lock);
904 nfs4_put_stid(&dp->dl_stid);
908 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
909 .rpc_call_prepare = nfsd4_cb_prepare,
910 .rpc_call_done = nfsd4_cb_recall_done,
911 .rpc_release = nfsd4_cb_recall_release,
914 int nfsd4_create_callback_queue(void)
916 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
922 void nfsd4_destroy_callback_queue(void)
924 destroy_workqueue(callback_wq);
927 /* must be called under the state lock */
928 void nfsd4_shutdown_callback(struct nfs4_client *clp)
930 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
932 * Note this won't actually result in a null callback;
933 * instead, nfsd4_run_cb_null() will detect the killed
934 * client, destroy the rpc client, and stop:
936 do_probe_callback(clp);
937 flush_workqueue(callback_wq);
940 static void nfsd4_release_cb(struct nfsd4_callback *cb)
942 if (cb->cb_ops->rpc_release)
943 cb->cb_ops->rpc_release(cb);
946 /* requires cl_lock: */
947 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
949 struct nfsd4_session *s;
950 struct nfsd4_conn *c;
952 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
953 list_for_each_entry(c, &s->se_conns, cn_persession) {
954 if (c->cn_flags & NFS4_CDFC4_BACK)
961 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
963 struct nfs4_cb_conn conn;
964 struct nfs4_client *clp = cb->cb_clp;
965 struct nfsd4_session *ses = NULL;
966 struct nfsd4_conn *c;
970 * This is either an update, or the client dying; in either case,
971 * kill the old client:
973 if (clp->cl_cb_client) {
974 rpc_shutdown_client(clp->cl_cb_client);
975 clp->cl_cb_client = NULL;
976 put_rpccred(clp->cl_cb_cred);
977 clp->cl_cb_cred = NULL;
979 if (clp->cl_cb_conn.cb_xprt) {
980 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
981 clp->cl_cb_conn.cb_xprt = NULL;
983 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
985 spin_lock(&clp->cl_lock);
987 * Only serialized callback code is allowed to clear these
988 * flags; main nfsd code can only set them:
990 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
991 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
992 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
993 c = __nfsd4_find_backchannel(clp);
995 svc_xprt_get(c->cn_xprt);
996 conn.cb_xprt = c->cn_xprt;
999 spin_unlock(&clp->cl_lock);
1001 err = setup_callback_client(clp, &conn, ses);
1003 nfsd4_mark_cb_down(clp, err);
1006 /* Yay, the callback channel's back! Restart any callbacks: */
1007 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
1012 nfsd4_run_callback_rpc(struct nfsd4_callback *cb)
1014 struct nfs4_client *clp = cb->cb_clp;
1015 struct rpc_clnt *clnt;
1017 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
1018 nfsd4_process_cb_update(cb);
1020 clnt = clp->cl_cb_client;
1022 /* Callback channel broken, or client killed; give up: */
1023 nfsd4_release_cb(cb);
1026 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
1027 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1032 nfsd4_run_cb_null(struct work_struct *w)
1034 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1036 nfsd4_run_callback_rpc(cb);
1040 nfsd4_run_cb_recall(struct work_struct *w)
1042 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1045 nfsd4_prepare_cb_recall(to_delegation(cb));
1046 nfsd4_run_callback_rpc(cb);
1049 void nfsd4_cb_recall(struct nfs4_delegation *dp)
1051 struct nfsd4_callback *cb = &dp->dl_recall;
1052 struct nfs4_client *clp = dp->dl_stid.sc_client;
1056 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
1057 cb->cb_msg.rpc_argp = cb;
1058 cb->cb_msg.rpc_resp = cb;
1060 cb->cb_ops = &nfsd4_cb_recall_ops;
1062 INIT_LIST_HEAD(&cb->cb_per_client);
1065 run_nfsd4_cb(&dp->dl_recall);