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/svc_xprt.h>
36 #include <linux/slab.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 */
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)
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 + \
76 #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
77 cb_sequence_dec_sz + \
80 struct nfs4_cb_compound_hdr
{
82 u32 ident
; /* minorversion 0 only */
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
)
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
118 * enum nfs_cb_opnum4 {
126 OP_CB_LAYOUTRECALL
= 5,
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,
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
)
143 p
= xdr_reserve_space(xdr
, 4);
144 *p
= cpu_to_be32(op
);
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
;
157 BUG_ON(length
> NFS4_FHSIZE
);
158 p
= xdr_reserve_space(xdr
, 4 + length
);
159 xdr_encode_opaque(p
, &fh
->fh_base
, length
);
170 static void encode_stateid4(struct xdr_stream
*xdr
, const stateid_t
*sid
)
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
);
182 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
184 static void encode_sessionid4(struct xdr_stream
*xdr
,
185 const struct nfsd4_session
*session
)
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
);
197 static const struct {
200 } nfs_cb_errtbl
[] = {
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
},
235 * If we cannot translate the error, the recovery routines should
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
)
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
);
254 static int decode_cb_op_status(struct xdr_stream
*xdr
, enum nfs_opnum4 expected
,
255 enum nfsstat4
*status
)
260 p
= xdr_inline_decode(xdr
, 4 + 4);
261 if (unlikely(p
== NULL
))
263 op
= be32_to_cpup(p
++);
264 if (unlikely(op
!= expected
))
266 *status
= be32_to_cpup(p
);
269 print_overflow_msg(__func__
, xdr
);
272 dprintk("NFSD: Callback server returned operation %d but "
273 "we issued a request for %d\n", op
, expected
);
280 * struct CB_COMPOUND4args {
282 * uint32_t minorversion;
283 * uint32_t callback_ident;
284 * nfs_cb_argop4 argarray<>;
287 static void encode_cb_compound4args(struct xdr_stream
*xdr
,
288 struct nfs4_cb_compound_hdr
*hdr
)
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
);
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
);
313 * struct CB_COMPOUND4res {
316 * nfs_cb_resop4 resarray<>;
319 static int decode_cb_compound4res(struct xdr_stream
*xdr
,
320 struct nfs4_cb_compound_hdr
*hdr
)
325 p
= xdr_inline_decode(xdr
, 4 + 4);
326 if (unlikely(p
== NULL
))
328 hdr
->status
= be32_to_cpup(p
++);
330 length
= be32_to_cpup(p
++);
331 p
= xdr_inline_decode(xdr
, length
+ 4);
332 if (unlikely(p
== NULL
))
334 hdr
->nops
= be32_to_cpup(p
);
337 print_overflow_msg(__func__
, xdr
);
344 * struct CB_RECALL4args {
350 static void encode_cb_recall4args(struct xdr_stream
*xdr
,
351 const struct nfs4_delegation
*dp
,
352 struct nfs4_cb_compound_hdr
*hdr
)
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
);
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<>;
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
;
386 if (hdr
->minorversion
== 0)
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 */
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;
413 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
415 * CB_SEQUENCE4resok csr_resok4;
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
;
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
))
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__
);
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__
);
455 dummy
= be32_to_cpup(p
++);
457 dprintk("NFS: %s Invalid slotid\n", __func__
);
462 * FIXME: process highest slotid and target highest slotid
467 nfsd4_mark_cb_fault(cb
->cb_clp
, status
);
470 print_overflow_msg(__func__
, xdr
);
474 static int decode_cb_sequence4res(struct xdr_stream
*xdr
,
475 struct nfsd4_callback
*cb
)
477 enum nfsstat4 nfserr
;
480 if (cb
->cb_minorversion
== 0)
483 status
= decode_cb_op_status(xdr
, OP_CB_SEQUENCE
, &nfserr
);
484 if (unlikely(status
))
486 if (unlikely(nfserr
!= NFS4_OK
))
488 status
= decode_cb_sequence4resok(xdr
, cb
);
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
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
,
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
541 static int nfs4_xdr_dec_cb_null(struct rpc_rqst
*req
, struct xdr_stream
*xdr
,
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
;
558 status
= decode_cb_compound4res(xdr
, &hdr
);
559 if (unlikely(status
))
563 status
= decode_cb_sequence4res(xdr
, cb
);
564 if (unlikely(status
))
568 status
= decode_cb_op_status(xdr
, OP_CB_RECALL
, &nfserr
);
569 if (unlikely(status
))
571 if (unlikely(nfserr
!= NFS4_OK
))
572 status
= nfs_cb_stat_to_errno(nfserr
);
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, \
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.
605 .nrprocs
= ARRAY_SIZE(nfs4_cb_procedures
),
606 .procs
= nfs4_cb_procedures
609 static const struct rpc_version
*nfs_cb_version
[] = {
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
= {
622 .number
= NFS4_CALLBACK
,
623 .nrvers
= ARRAY_SIZE(nfs_cb_version
),
624 .version
= nfs_cb_version
,
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)
641 callback_cred
= rpc_lookup_machine_cred("nfs");
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
);
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
),
667 struct rpc_create_args args
= {
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
,
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
))
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
;
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",
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
;
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
;
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__
);
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
;
826 if (!nfsd41_cb_get_slot(clp
, task
))
829 spin_lock(&clp
->cl_lock
);
830 if (list_empty(&cb
->cb_per_client
)) {
831 /* This is the first call, not a restart */
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
880 switch (task
->tk_status
) {
885 case -NFS4ERR_BAD_STATEID
:
886 /* Race: client probably got cb_recall
887 * before open reply granting delegation */
890 /* Network partition? */
891 nfsd4_mark_cb_down(clp
, task
->tk_status
);
893 if (dp
->dl_retries
--) {
894 rpc_delay(task
, 2*HZ
);
896 rpc_restart_call_prepare(task
);
899 nfsd4_mark_cb_down(clp
, task
->tk_status
);
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
);
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");
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
)
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
;
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
))
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
);
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
);
1012 nfsd4_mark_cb_down(clp
, err
);
1015 /* Yay, the callback channel's back! Restart any callbacks: */
1016 list_for_each_entry(cb
, &clp
->cl_callbacks
, cb_per_client
)
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
;
1031 /* Callback channel broken, or client killed; give up: */
1032 nfsd4_release_cb(cb
);
1035 cb
->cb_msg
.rpc_cred
= clp
->cl_cb_cred
;
1036 rpc_call_async(clnt
, &cb
->cb_msg
, RPC_TASK_SOFT
| RPC_TASK_SOFTCONN
,
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
;
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
);
1062 run_nfsd4_cb(&dp
->dl_recall
);