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 */
52 #define to_delegation(cb) \
53 container_of(cb, struct nfs4_delegation, dl_recall)
55 struct nfs4_cb_compound_hdr {
57 u32 ident; /* minorversion 0 only */
66 * Handle decode buffer overflows out-of-line.
68 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
70 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
71 "Remaining buffer length is %tu words.\n",
72 func, xdr->end - xdr->p);
75 static __be32 *xdr_encode_empty_array(__be32 *p)
82 * Encode/decode NFSv4 CB basic data types
84 * Basic NFSv4 callback data types are defined in section 15 of RFC
85 * 3530: "Network File System (NFS) version 4 Protocol" and section
86 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
93 * enum nfs_cb_opnum4 {
101 OP_CB_LAYOUTRECALL = 5,
103 OP_CB_PUSH_DELEG = 7,
104 OP_CB_RECALL_ANY = 8,
105 OP_CB_RECALLABLE_OBJ_AVAIL = 9,
106 OP_CB_RECALL_SLOT = 10,
108 OP_CB_WANTS_CANCELLED = 12,
109 OP_CB_NOTIFY_LOCK = 13,
110 OP_CB_NOTIFY_DEVICEID = 14,
111 OP_CB_ILLEGAL = 10044
114 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
118 p = xdr_reserve_space(xdr, 4);
119 *p = cpu_to_be32(op);
125 * typedef opaque nfs_fh4<NFS4_FHSIZE>;
127 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
129 u32 length = fh->fh_size;
132 BUG_ON(length > NFS4_FHSIZE);
133 p = xdr_reserve_space(xdr, 4 + length);
134 xdr_encode_opaque(p, &fh->fh_base, length);
145 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
149 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
150 *p++ = cpu_to_be32(sid->si_generation);
151 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
157 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
159 static void encode_sessionid4(struct xdr_stream *xdr,
160 const struct nfsd4_session *session)
164 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
165 xdr_encode_opaque_fixed(p, session->se_sessionid.data,
166 NFS4_MAX_SESSIONID_LEN);
172 static const struct {
175 } nfs_cb_errtbl[] = {
177 { NFS4ERR_PERM, -EPERM },
178 { NFS4ERR_NOENT, -ENOENT },
179 { NFS4ERR_IO, -EIO },
180 { NFS4ERR_NXIO, -ENXIO },
181 { NFS4ERR_ACCESS, -EACCES },
182 { NFS4ERR_EXIST, -EEXIST },
183 { NFS4ERR_XDEV, -EXDEV },
184 { NFS4ERR_NOTDIR, -ENOTDIR },
185 { NFS4ERR_ISDIR, -EISDIR },
186 { NFS4ERR_INVAL, -EINVAL },
187 { NFS4ERR_FBIG, -EFBIG },
188 { NFS4ERR_NOSPC, -ENOSPC },
189 { NFS4ERR_ROFS, -EROFS },
190 { NFS4ERR_MLINK, -EMLINK },
191 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
192 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
193 { NFS4ERR_DQUOT, -EDQUOT },
194 { NFS4ERR_STALE, -ESTALE },
195 { NFS4ERR_BADHANDLE, -EBADHANDLE },
196 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
197 { NFS4ERR_NOTSUPP, -ENOTSUPP },
198 { NFS4ERR_TOOSMALL, -ETOOSMALL },
199 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
200 { NFS4ERR_BADTYPE, -EBADTYPE },
201 { NFS4ERR_LOCKED, -EAGAIN },
202 { NFS4ERR_RESOURCE, -EREMOTEIO },
203 { NFS4ERR_SYMLINK, -ELOOP },
204 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
205 { NFS4ERR_DEADLOCK, -EDEADLK },
210 * If we cannot translate the error, the recovery routines should
213 * Note: remaining NFSv4 error codes have values > 10000, so should
214 * not conflict with native Linux error codes.
216 static int nfs_cb_stat_to_errno(int status)
220 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
221 if (nfs_cb_errtbl[i].stat == status)
222 return nfs_cb_errtbl[i].errno;
225 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
229 static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
230 enum nfsstat4 *status)
235 p = xdr_inline_decode(xdr, 4 + 4);
236 if (unlikely(p == NULL))
238 op = be32_to_cpup(p++);
239 if (unlikely(op != expected))
241 *status = be32_to_cpup(p);
244 print_overflow_msg(__func__, xdr);
247 dprintk("NFSD: Callback server returned operation %d but "
248 "we issued a request for %d\n", op, expected);
255 * struct CB_COMPOUND4args {
257 * uint32_t minorversion;
258 * uint32_t callback_ident;
259 * nfs_cb_argop4 argarray<>;
262 static void encode_cb_compound4args(struct xdr_stream *xdr,
263 struct nfs4_cb_compound_hdr *hdr)
267 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
268 p = xdr_encode_empty_array(p); /* empty tag */
269 *p++ = cpu_to_be32(hdr->minorversion);
270 *p++ = cpu_to_be32(hdr->ident);
273 *p = cpu_to_be32(hdr->nops); /* argarray element count */
277 * Update argarray element count
279 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
281 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
282 *hdr->nops_p = cpu_to_be32(hdr->nops);
288 * struct CB_COMPOUND4res {
291 * nfs_cb_resop4 resarray<>;
294 static int decode_cb_compound4res(struct xdr_stream *xdr,
295 struct nfs4_cb_compound_hdr *hdr)
300 p = xdr_inline_decode(xdr, 4 + 4);
301 if (unlikely(p == NULL))
303 hdr->status = be32_to_cpup(p++);
305 length = be32_to_cpup(p++);
306 p = xdr_inline_decode(xdr, length + 4);
307 if (unlikely(p == NULL))
309 hdr->nops = be32_to_cpup(p);
312 print_overflow_msg(__func__, xdr);
319 * struct CB_RECALL4args {
325 static void encode_cb_recall4args(struct xdr_stream *xdr,
326 const struct nfs4_delegation *dp,
327 struct nfs4_cb_compound_hdr *hdr)
331 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
332 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
334 p = xdr_reserve_space(xdr, 4);
335 *p++ = xdr_zero; /* truncate */
337 encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
345 * struct CB_SEQUENCE4args {
346 * sessionid4 csa_sessionid;
347 * sequenceid4 csa_sequenceid;
348 * slotid4 csa_slotid;
349 * slotid4 csa_highest_slotid;
350 * bool csa_cachethis;
351 * referring_call_list4 csa_referring_call_lists<>;
354 static void encode_cb_sequence4args(struct xdr_stream *xdr,
355 const struct nfsd4_callback *cb,
356 struct nfs4_cb_compound_hdr *hdr)
358 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
361 if (hdr->minorversion == 0)
364 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
365 encode_sessionid4(xdr, session);
367 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
368 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
369 *p++ = xdr_zero; /* csa_slotid */
370 *p++ = xdr_zero; /* csa_highest_slotid */
371 *p++ = xdr_zero; /* csa_cachethis */
372 xdr_encode_empty_array(p); /* csa_referring_call_lists */
380 * struct CB_SEQUENCE4resok {
381 * sessionid4 csr_sessionid;
382 * sequenceid4 csr_sequenceid;
383 * slotid4 csr_slotid;
384 * slotid4 csr_highest_slotid;
385 * slotid4 csr_target_highest_slotid;
388 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
390 * CB_SEQUENCE4resok csr_resok4;
395 * Our current back channel implmentation supports a single backchannel
396 * with a single slot.
398 static int decode_cb_sequence4resok(struct xdr_stream *xdr,
399 struct nfsd4_callback *cb)
401 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
402 struct nfs4_sessionid id;
407 status = -ESERVERFAULT;
410 * If the server returns different values for sessionID, slotID or
411 * sequence number, the server is looney tunes.
413 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
414 if (unlikely(p == NULL))
416 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
417 if (memcmp(id.data, session->se_sessionid.data,
418 NFS4_MAX_SESSIONID_LEN) != 0) {
419 dprintk("NFS: %s Invalid session id\n", __func__);
422 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
424 dummy = be32_to_cpup(p++);
425 if (dummy != session->se_cb_seq_nr) {
426 dprintk("NFS: %s Invalid sequence number\n", __func__);
430 dummy = be32_to_cpup(p++);
432 dprintk("NFS: %s Invalid slotid\n", __func__);
437 * FIXME: process highest slotid and target highest slotid
442 nfsd4_mark_cb_fault(cb->cb_clp, status);
445 print_overflow_msg(__func__, xdr);
449 static int decode_cb_sequence4res(struct xdr_stream *xdr,
450 struct nfsd4_callback *cb)
452 enum nfsstat4 nfserr;
455 if (cb->cb_minorversion == 0)
458 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
459 if (unlikely(status))
461 if (unlikely(nfserr != NFS4_OK))
463 status = decode_cb_sequence4resok(xdr, cb);
467 return nfs_cb_stat_to_errno(nfserr);
471 * NFSv4.0 and NFSv4.1 XDR encode functions
473 * NFSv4.0 callback argument types are defined in section 15 of RFC
474 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
475 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
480 * NB: Without this zero space reservation, callbacks over krb5p fail
482 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
485 xdr_reserve_space(xdr, 0);
489 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
491 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
492 const struct nfsd4_callback *cb)
494 const struct nfs4_delegation *dp = to_delegation(cb);
495 struct nfs4_cb_compound_hdr hdr = {
496 .ident = cb->cb_clp->cl_cb_ident,
497 .minorversion = cb->cb_minorversion,
500 encode_cb_compound4args(xdr, &hdr);
501 encode_cb_sequence4args(xdr, cb, &hdr);
502 encode_cb_recall4args(xdr, dp, &hdr);
503 encode_cb_nops(&hdr);
508 * NFSv4.0 and NFSv4.1 XDR decode functions
510 * NFSv4.0 callback result types are defined in section 15 of RFC
511 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
512 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
516 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
523 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
525 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
526 struct xdr_stream *xdr,
527 struct nfsd4_callback *cb)
529 struct nfs4_cb_compound_hdr hdr;
530 enum nfsstat4 nfserr;
533 status = decode_cb_compound4res(xdr, &hdr);
534 if (unlikely(status))
538 status = decode_cb_sequence4res(xdr, cb);
539 if (unlikely(status))
543 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
544 if (unlikely(status))
546 if (unlikely(nfserr != NFS4_OK))
547 status = nfs_cb_stat_to_errno(nfserr);
553 * RPC procedure tables
555 #define PROC(proc, call, argtype, restype) \
556 [NFSPROC4_CLNT_##proc] = { \
557 .p_proc = NFSPROC4_CB_##call, \
558 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
559 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
560 .p_arglen = NFS4_enc_##argtype##_sz, \
561 .p_replen = NFS4_dec_##restype##_sz, \
562 .p_statidx = NFSPROC4_CB_##call, \
566 static struct rpc_procinfo nfs4_cb_procedures[] = {
567 PROC(CB_NULL, NULL, cb_null, cb_null),
568 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
571 static struct rpc_version nfs_cb_version4 = {
573 * Note on the callback rpc program version number: despite language in rfc
574 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
575 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
576 * in practice that appears to be what implementations use. The section
577 * 18.36.3 language is expected to be fixed in an erratum.
580 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
581 .procs = nfs4_cb_procedures
584 static const struct rpc_version *nfs_cb_version[] = {
588 static const struct rpc_program cb_program;
590 static struct rpc_stat cb_stats = {
591 .program = &cb_program
594 #define NFS4_CALLBACK 0x40000000
595 static const struct rpc_program cb_program = {
597 .number = NFS4_CALLBACK,
598 .nrvers = ARRAY_SIZE(nfs_cb_version),
599 .version = nfs_cb_version,
601 .pipe_dir_name = "nfsd4_cb",
604 static int max_cb_time(struct net *net)
606 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
607 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
610 static struct rpc_cred *callback_cred;
612 int set_callback_cred(void)
616 callback_cred = rpc_lookup_machine_cred("nfs");
622 static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
624 if (clp->cl_minorversion == 0) {
625 return get_rpccred(callback_cred);
627 struct rpc_auth *auth = client->cl_auth;
628 struct auth_cred acred = {};
630 acred.uid = ses->se_cb_sec.uid;
631 acred.gid = ses->se_cb_sec.gid;
632 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
636 static struct rpc_clnt *create_backchannel_client(struct rpc_create_args *args)
638 struct rpc_xprt *xprt;
640 if (args->protocol != XPRT_TRANSPORT_BC_TCP)
641 return rpc_create(args);
643 xprt = args->bc_xprt->xpt_bc_xprt;
646 return rpc_create_xprt(args, xprt);
649 return rpc_create(args);
652 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
654 int maxtime = max_cb_time(clp->net);
655 struct rpc_timeout timeparms = {
656 .to_initval = maxtime,
658 .to_maxval = maxtime,
660 struct rpc_create_args args = {
662 .address = (struct sockaddr *) &conn->cb_addr,
663 .addrsize = conn->cb_addrlen,
664 .saddress = (struct sockaddr *) &conn->cb_saddr,
665 .timeout = &timeparms,
666 .program = &cb_program,
668 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
670 struct rpc_clnt *client;
671 struct rpc_cred *cred;
673 if (clp->cl_minorversion == 0) {
674 if (!clp->cl_cred.cr_principal &&
675 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
677 args.client_name = clp->cl_cred.cr_principal;
678 args.prognumber = conn->cb_prog;
679 args.protocol = XPRT_TRANSPORT_TCP;
680 args.authflavor = clp->cl_cred.cr_flavor;
681 clp->cl_cb_ident = conn->cb_ident;
685 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
686 clp->cl_cb_session = ses;
687 args.bc_xprt = conn->cb_xprt;
688 args.prognumber = clp->cl_cb_session->se_cb_prog;
689 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
691 args.authflavor = ses->se_cb_sec.flavor;
693 /* Create RPC client */
694 client = create_backchannel_client(&args);
695 if (IS_ERR(client)) {
696 dprintk("NFSD: couldn't create callback client: %ld\n",
698 return PTR_ERR(client);
700 cred = get_backchannel_cred(clp, client, ses);
702 rpc_shutdown_client(client);
703 return PTR_ERR(cred);
705 clp->cl_cb_client = client;
706 clp->cl_cb_cred = cred;
710 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
712 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
713 (int)clp->cl_name.len, clp->cl_name.data, reason);
716 static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
718 clp->cl_cb_state = NFSD4_CB_DOWN;
719 warn_no_callback_path(clp, reason);
722 static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
724 clp->cl_cb_state = NFSD4_CB_FAULT;
725 warn_no_callback_path(clp, reason);
728 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
730 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
733 nfsd4_mark_cb_down(clp, task->tk_status);
735 clp->cl_cb_state = NFSD4_CB_UP;
738 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
739 /* XXX: release method to ensure we set the cb channel down if
740 * necessary on early failure? */
741 .rpc_call_done = nfsd4_cb_probe_done,
744 static struct workqueue_struct *callback_wq;
746 static void do_probe_callback(struct nfs4_client *clp)
748 return nfsd4_cb(&clp->cl_cb_null, clp, NFSPROC4_CLNT_CB_NULL);
752 * Poke the callback thread to process any updates to the callback
753 * parameters, and send a null probe.
755 void nfsd4_probe_callback(struct nfs4_client *clp)
757 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
758 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
759 do_probe_callback(clp);
762 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
764 nfsd4_probe_callback(clp);
765 flush_workqueue(callback_wq);
768 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
770 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
771 spin_lock(&clp->cl_lock);
772 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
773 spin_unlock(&clp->cl_lock);
777 * There's currently a single callback channel slot.
778 * If the slot is available, then mark it busy. Otherwise, set the
779 * thread for sleeping on the callback RPC wait queue.
781 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
783 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
784 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
785 dprintk("%s slot is busy\n", __func__);
792 * TODO: cb_sequence should support referring call lists, cachethis, multiple
793 * slots, and mark callback channel down on communication errors.
795 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
797 struct nfsd4_callback *cb = calldata;
798 struct nfs4_client *clp = cb->cb_clp;
799 u32 minorversion = clp->cl_minorversion;
801 cb->cb_minorversion = minorversion;
803 if (!nfsd41_cb_get_slot(clp, task))
806 spin_lock(&clp->cl_lock);
807 if (list_empty(&cb->cb_per_client)) {
808 /* This is the first call, not a restart */
810 list_add(&cb->cb_per_client, &clp->cl_callbacks);
812 spin_unlock(&clp->cl_lock);
813 rpc_call_start(task);
816 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
818 struct nfsd4_callback *cb = calldata;
819 struct nfs4_client *clp = cb->cb_clp;
821 dprintk("%s: minorversion=%d\n", __func__,
822 clp->cl_minorversion);
824 if (clp->cl_minorversion) {
825 /* No need for lock, access serialized in nfsd4_cb_prepare */
826 ++clp->cl_cb_session->se_cb_seq_nr;
827 clear_bit(0, &clp->cl_cb_slot_busy);
828 rpc_wake_up_next(&clp->cl_cb_waitq);
829 dprintk("%s: freed slot, new seqid=%d\n", __func__,
830 clp->cl_cb_session->se_cb_seq_nr);
834 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
836 struct nfsd4_callback *cb = calldata;
837 struct nfs4_delegation *dp = to_delegation(cb);
838 struct nfs4_client *clp = cb->cb_clp;
839 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
841 nfsd4_cb_done(task, calldata);
843 if (current_rpc_client != task->tk_client) {
844 /* We're shutting down or changing cl_cb_client; leave
845 * it to nfsd4_process_cb_update to restart the call if
852 switch (task->tk_status) {
856 case -NFS4ERR_BAD_STATEID:
857 /* Race: client probably got cb_recall
858 * before open reply granting delegation */
859 if (dp->dl_retries--) {
860 rpc_delay(task, 2*HZ);
862 rpc_restart_call_prepare(task);
866 /* Network partition? */
867 nfsd4_mark_cb_down(clp, task->tk_status);
872 static void nfsd4_cb_recall_release(void *calldata)
874 struct nfsd4_callback *cb = calldata;
875 struct nfs4_client *clp = cb->cb_clp;
878 struct nfs4_delegation *dp = to_delegation(cb);
880 spin_lock(&clp->cl_lock);
881 list_del(&cb->cb_per_client);
882 spin_unlock(&clp->cl_lock);
883 nfs4_put_stid(&dp->dl_stid);
887 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
888 .rpc_call_prepare = nfsd4_cb_prepare,
889 .rpc_call_done = nfsd4_cb_recall_done,
890 .rpc_release = nfsd4_cb_recall_release,
893 int nfsd4_create_callback_queue(void)
895 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
901 void nfsd4_destroy_callback_queue(void)
903 destroy_workqueue(callback_wq);
906 /* must be called under the state lock */
907 void nfsd4_shutdown_callback(struct nfs4_client *clp)
909 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
911 * Note this won't actually result in a null callback;
912 * instead, nfsd4_run_cb_null() will detect the killed
913 * client, destroy the rpc client, and stop:
915 do_probe_callback(clp);
916 flush_workqueue(callback_wq);
919 static void nfsd4_release_cb(struct nfsd4_callback *cb)
921 if (cb->cb_ops->rpc_release)
922 cb->cb_ops->rpc_release(cb);
925 /* requires cl_lock: */
926 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
928 struct nfsd4_session *s;
929 struct nfsd4_conn *c;
931 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
932 list_for_each_entry(c, &s->se_conns, cn_persession) {
933 if (c->cn_flags & NFS4_CDFC4_BACK)
940 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
942 struct nfs4_cb_conn conn;
943 struct nfs4_client *clp = cb->cb_clp;
944 struct nfsd4_session *ses = NULL;
945 struct nfsd4_conn *c;
949 * This is either an update, or the client dying; in either case,
950 * kill the old client:
952 if (clp->cl_cb_client) {
953 rpc_shutdown_client(clp->cl_cb_client);
954 clp->cl_cb_client = NULL;
955 put_rpccred(clp->cl_cb_cred);
956 clp->cl_cb_cred = NULL;
958 if (clp->cl_cb_conn.cb_xprt) {
959 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
960 clp->cl_cb_conn.cb_xprt = NULL;
962 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
964 spin_lock(&clp->cl_lock);
966 * Only serialized callback code is allowed to clear these
967 * flags; main nfsd code can only set them:
969 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
970 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
971 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
972 c = __nfsd4_find_backchannel(clp);
974 svc_xprt_get(c->cn_xprt);
975 conn.cb_xprt = c->cn_xprt;
978 spin_unlock(&clp->cl_lock);
980 err = setup_callback_client(clp, &conn, ses);
982 nfsd4_mark_cb_down(clp, err);
985 /* Yay, the callback channel's back! Restart any callbacks: */
986 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
987 queue_work(callback_wq, &cb->cb_work);
991 nfsd4_run_callback_rpc(struct nfsd4_callback *cb)
993 struct nfs4_client *clp = cb->cb_clp;
994 struct rpc_clnt *clnt;
996 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
997 nfsd4_process_cb_update(cb);
999 clnt = clp->cl_cb_client;
1001 /* Callback channel broken, or client killed; give up: */
1002 nfsd4_release_cb(cb);
1005 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
1006 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1011 nfsd4_run_cb_null(struct work_struct *w)
1013 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1015 nfsd4_run_callback_rpc(cb);
1019 nfsd4_run_cb_recall(struct work_struct *w)
1021 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1024 nfsd4_prepare_cb_recall(to_delegation(cb));
1025 nfsd4_run_callback_rpc(cb);
1028 void nfsd4_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
1029 enum nfsd4_cb_op op)
1032 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
1033 cb->cb_msg.rpc_argp = cb;
1034 cb->cb_msg.rpc_resp = cb;
1035 if (op == NFSPROC4_CLNT_CB_NULL)
1036 cb->cb_ops = &nfsd4_cb_probe_ops;
1038 cb->cb_ops = &nfsd4_cb_recall_ops;
1039 INIT_LIST_HEAD(&cb->cb_per_client);
1042 queue_work(callback_wq, &cb->cb_work);