2 * linux/fs/nfsd/nfs4callback.c
4 * Copyright (c) 2001 The Regents of the University of Michigan.
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/module.h>
37 #include <linux/list.h>
38 #include <linux/inet.h>
39 #include <linux/errno.h>
40 #include <linux/delay.h>
41 #include <linux/sched.h>
42 #include <linux/kthread.h>
43 #include <linux/sunrpc/xdr.h>
44 #include <linux/sunrpc/svc.h>
45 #include <linux/sunrpc/clnt.h>
46 #include <linux/sunrpc/svcsock.h>
47 #include <linux/nfsd/nfsd.h>
48 #include <linux/nfsd/state.h>
49 #include <linux/sunrpc/sched.h>
50 #include <linux/nfs4.h>
51 #include <linux/sunrpc/xprtsock.h>
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define NFSPROC4_CB_NULL 0
56 #define NFSPROC4_CB_COMPOUND 1
57 #define NFS4_STATEID_SIZE 16
59 /* Index of predefined Linux callback client operations */
62 NFSPROC4_CLNT_CB_NULL
= 0,
63 NFSPROC4_CLNT_CB_RECALL
,
64 NFSPROC4_CLNT_CB_SEQUENCE
,
72 #define NFS4_MAXTAGLEN 20
74 #define NFS4_enc_cb_null_sz 0
75 #define NFS4_dec_cb_null_sz 0
76 #define cb_compound_enc_hdr_sz 4
77 #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
78 #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
79 #define cb_sequence_enc_sz (sessionid_sz + 4 + \
80 1 /* no referring calls list yet */)
81 #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
85 #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
86 #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
87 #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
88 cb_sequence_enc_sz + \
89 1 + enc_stateid_sz + \
92 #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
93 cb_sequence_dec_sz + \
96 struct nfs4_rpc_args
{
98 struct nfsd4_cb_sequence args_seq
;
102 * Generic encode routines from fs/nfs/nfs4xdr.c
104 static inline __be32
*
105 xdr_writemem(__be32
*p
, const void *ptr
, int nbytes
)
107 int tmp
= XDR_QUADLEN(nbytes
);
111 memcpy(p
, ptr
, nbytes
);
115 #define WRITE32(n) *p++ = htonl(n)
116 #define WRITEMEM(ptr,nbytes) do { \
117 p = xdr_writemem(p, ptr, nbytes); \
119 #define RESERVE_SPACE(nbytes) do { \
120 p = xdr_reserve_space(xdr, nbytes); \
121 if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
126 * Generic decode routines from fs/nfs/nfs4xdr.c
128 #define DECODE_TAIL \
133 dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
137 #define READ32(x) (x) = ntohl(*p++)
138 #define READ64(x) do { \
139 (x) = (u64)ntohl(*p++) << 32; \
140 (x) |= ntohl(*p++); \
142 #define READTIME(x) do { \
144 (x.tv_sec) = ntohl(*p++); \
145 (x.tv_nsec) = ntohl(*p++); \
147 #define READ_BUF(nbytes) do { \
148 p = xdr_inline_decode(xdr, nbytes); \
150 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
151 __func__, __LINE__); \
156 struct nfs4_cb_compound_hdr
{
158 u32 ident
; /* minorversion 0 only */
171 } nfs_cb_errtbl
[] = {
173 { NFS4ERR_PERM
, EPERM
},
174 { NFS4ERR_NOENT
, ENOENT
},
176 { NFS4ERR_NXIO
, ENXIO
},
177 { NFS4ERR_ACCESS
, EACCES
},
178 { NFS4ERR_EXIST
, EEXIST
},
179 { NFS4ERR_XDEV
, EXDEV
},
180 { NFS4ERR_NOTDIR
, ENOTDIR
},
181 { NFS4ERR_ISDIR
, EISDIR
},
182 { NFS4ERR_INVAL
, EINVAL
},
183 { NFS4ERR_FBIG
, EFBIG
},
184 { NFS4ERR_NOSPC
, ENOSPC
},
185 { NFS4ERR_ROFS
, EROFS
},
186 { NFS4ERR_MLINK
, EMLINK
},
187 { NFS4ERR_NAMETOOLONG
, ENAMETOOLONG
},
188 { NFS4ERR_NOTEMPTY
, ENOTEMPTY
},
189 { NFS4ERR_DQUOT
, EDQUOT
},
190 { NFS4ERR_STALE
, ESTALE
},
191 { NFS4ERR_BADHANDLE
, EBADHANDLE
},
192 { NFS4ERR_BAD_COOKIE
, EBADCOOKIE
},
193 { NFS4ERR_NOTSUPP
, ENOTSUPP
},
194 { NFS4ERR_TOOSMALL
, ETOOSMALL
},
195 { NFS4ERR_SERVERFAULT
, ESERVERFAULT
},
196 { NFS4ERR_BADTYPE
, EBADTYPE
},
197 { NFS4ERR_LOCKED
, EAGAIN
},
198 { NFS4ERR_RESOURCE
, EREMOTEIO
},
199 { NFS4ERR_SYMLINK
, ELOOP
},
200 { NFS4ERR_OP_ILLEGAL
, EOPNOTSUPP
},
201 { NFS4ERR_DEADLOCK
, EDEADLK
},
206 nfs_cb_stat_to_errno(int stat
)
209 for (i
= 0; nfs_cb_errtbl
[i
].stat
!= -1; i
++) {
210 if (nfs_cb_errtbl
[i
].stat
== stat
)
211 return nfs_cb_errtbl
[i
].errno
;
213 /* If we cannot translate the error, the recovery routines should
215 * Note: remaining NFSv4 error codes have values > 10000, so should
216 * not conflict with native Linux error codes.
226 encode_cb_compound_hdr(struct xdr_stream
*xdr
, struct nfs4_cb_compound_hdr
*hdr
)
231 WRITE32(0); /* tag length is always 0 */
232 WRITE32(hdr
->minorversion
);
238 static void encode_cb_nops(struct nfs4_cb_compound_hdr
*hdr
)
240 *hdr
->nops_p
= htonl(hdr
->nops
);
244 encode_cb_recall(struct xdr_stream
*xdr
, struct nfs4_delegation
*dp
,
245 struct nfs4_cb_compound_hdr
*hdr
)
248 int len
= dp
->dl_fh
.fh_size
;
250 RESERVE_SPACE(12+sizeof(dp
->dl_stateid
) + len
);
251 WRITE32(OP_CB_RECALL
);
252 WRITE32(dp
->dl_stateid
.si_generation
);
253 WRITEMEM(&dp
->dl_stateid
.si_opaque
, sizeof(stateid_opaque_t
));
254 WRITE32(0); /* truncate optimization not implemented */
256 WRITEMEM(&dp
->dl_fh
.fh_base
, len
);
261 encode_cb_sequence(struct xdr_stream
*xdr
, struct nfsd4_cb_sequence
*args
,
262 struct nfs4_cb_compound_hdr
*hdr
)
266 if (hdr
->minorversion
== 0)
269 RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN
+ 20);
271 WRITE32(OP_CB_SEQUENCE
);
272 WRITEMEM(args
->cbs_clp
->cl_sessionid
.data
, NFS4_MAX_SESSIONID_LEN
);
273 WRITE32(args
->cbs_clp
->cl_cb_seq_nr
);
274 WRITE32(0); /* slotid, always 0 */
275 WRITE32(0); /* highest slotid always 0 */
276 WRITE32(0); /* cachethis always 0 */
277 WRITE32(0); /* FIXME: support referring_call_lists */
282 nfs4_xdr_enc_cb_null(struct rpc_rqst
*req
, __be32
*p
)
284 struct xdr_stream xdrs
, *xdr
= &xdrs
;
286 xdr_init_encode(&xdrs
, &req
->rq_snd_buf
, p
);
292 nfs4_xdr_enc_cb_recall(struct rpc_rqst
*req
, __be32
*p
,
293 struct nfs4_rpc_args
*rpc_args
)
295 struct xdr_stream xdr
;
296 struct nfs4_delegation
*args
= rpc_args
->args_op
;
297 struct nfs4_cb_compound_hdr hdr
= {
298 .ident
= args
->dl_ident
,
299 .minorversion
= rpc_args
->args_seq
.cbs_minorversion
,
302 xdr_init_encode(&xdr
, &req
->rq_snd_buf
, p
);
303 encode_cb_compound_hdr(&xdr
, &hdr
);
304 encode_cb_sequence(&xdr
, &rpc_args
->args_seq
, &hdr
);
305 encode_cb_recall(&xdr
, args
, &hdr
);
306 encode_cb_nops(&hdr
);
312 decode_cb_compound_hdr(struct xdr_stream
*xdr
, struct nfs4_cb_compound_hdr
*hdr
){
318 READ_BUF(hdr
->taglen
+ 4);
319 hdr
->tag
= (char *)p
;
320 p
+= XDR_QUADLEN(hdr
->taglen
);
326 decode_cb_op_hdr(struct xdr_stream
*xdr
, enum nfs_opnum4 expected
)
334 if (op
!= expected
) {
335 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
336 " operation %d but we issued a request for %d\n",
341 if (nfserr
!= NFS_OK
)
342 return -nfs_cb_stat_to_errno(nfserr
);
347 * Our current back channel implmentation supports a single backchannel
348 * with a single slot.
351 decode_cb_sequence(struct xdr_stream
*xdr
, struct nfsd4_cb_sequence
*res
,
352 struct rpc_rqst
*rqstp
)
354 struct nfs4_sessionid id
;
359 if (res
->cbs_minorversion
== 0)
362 status
= decode_cb_op_hdr(xdr
, OP_CB_SEQUENCE
);
367 * If the server returns different values for sessionID, slotID or
368 * sequence number, the server is looney tunes.
370 status
= -ESERVERFAULT
;
372 READ_BUF(NFS4_MAX_SESSIONID_LEN
+ 16);
373 memcpy(id
.data
, p
, NFS4_MAX_SESSIONID_LEN
);
374 p
+= XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN
);
375 if (memcmp(id
.data
, res
->cbs_clp
->cl_sessionid
.data
,
376 NFS4_MAX_SESSIONID_LEN
)) {
377 dprintk("%s Invalid session id\n", __func__
);
381 if (dummy
!= res
->cbs_clp
->cl_cb_seq_nr
) {
382 dprintk("%s Invalid sequence number\n", __func__
);
385 READ32(dummy
); /* slotid must be 0 */
387 dprintk("%s Invalid slotid\n", __func__
);
390 /* FIXME: process highest slotid and target highest slotid */
398 nfs4_xdr_dec_cb_null(struct rpc_rqst
*req
, __be32
*p
)
404 nfs4_xdr_dec_cb_recall(struct rpc_rqst
*rqstp
, __be32
*p
,
405 struct nfsd4_cb_sequence
*seq
)
407 struct xdr_stream xdr
;
408 struct nfs4_cb_compound_hdr hdr
;
411 xdr_init_decode(&xdr
, &rqstp
->rq_rcv_buf
, p
);
412 status
= decode_cb_compound_hdr(&xdr
, &hdr
);
416 status
= decode_cb_sequence(&xdr
, seq
, rqstp
);
420 status
= decode_cb_op_hdr(&xdr
, OP_CB_RECALL
);
426 * RPC procedure tables
428 #define PROC(proc, call, argtype, restype) \
429 [NFSPROC4_CLNT_##proc] = { \
430 .p_proc = NFSPROC4_CB_##call, \
431 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
432 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
433 .p_arglen = NFS4_##argtype##_sz, \
434 .p_replen = NFS4_##restype##_sz, \
435 .p_statidx = NFSPROC4_CB_##call, \
439 static struct rpc_procinfo nfs4_cb_procedures
[] = {
440 PROC(CB_NULL
, NULL
, enc_cb_null
, dec_cb_null
),
441 PROC(CB_RECALL
, COMPOUND
, enc_cb_recall
, dec_cb_recall
),
444 static struct rpc_version nfs_cb_version4
= {
446 .nrprocs
= ARRAY_SIZE(nfs4_cb_procedures
),
447 .procs
= nfs4_cb_procedures
450 static struct rpc_version
* nfs_cb_version
[] = {
455 static struct rpc_program cb_program
;
457 static struct rpc_stat cb_stats
= {
458 .program
= &cb_program
461 #define NFS4_CALLBACK 0x40000000
462 static struct rpc_program cb_program
= {
464 .number
= NFS4_CALLBACK
,
465 .nrvers
= ARRAY_SIZE(nfs_cb_version
),
466 .version
= nfs_cb_version
,
468 .pipe_dir_name
= "/nfsd4_cb",
471 static int max_cb_time(void)
473 return max(NFSD_LEASE_TIME
/10, (time_t)1) * HZ
;
476 /* Reference counting, callback cleanup, etc., all look racy as heck.
477 * And why is cb_set an atomic? */
479 int setup_callback_client(struct nfs4_client
*clp
)
481 struct nfs4_cb_conn
*cb
= &clp
->cl_cb_conn
;
482 struct rpc_timeout timeparms
= {
483 .to_initval
= max_cb_time(),
486 struct rpc_create_args args
= {
487 .protocol
= XPRT_TRANSPORT_TCP
,
488 .address
= (struct sockaddr
*) &cb
->cb_addr
,
489 .addrsize
= cb
->cb_addrlen
,
490 .timeout
= &timeparms
,
491 .program
= &cb_program
,
492 .prognumber
= cb
->cb_prog
,
493 .version
= nfs_cb_version
[1]->number
,
494 .authflavor
= clp
->cl_flavor
,
495 .flags
= (RPC_CLNT_CREATE_NOPING
| RPC_CLNT_CREATE_QUIET
),
496 .client_name
= clp
->cl_principal
,
498 struct rpc_clnt
*client
;
500 if (!clp
->cl_principal
&& (clp
->cl_flavor
>= RPC_AUTH_GSS_KRB5
))
502 if (cb
->cb_minorversion
) {
503 args
.bc_xprt
= clp
->cl_cb_xprt
;
504 args
.protocol
= XPRT_TRANSPORT_BC_TCP
;
506 /* Create RPC client */
507 client
= rpc_create(&args
);
508 if (IS_ERR(client
)) {
509 dprintk("NFSD: couldn't create callback client: %ld\n",
511 return PTR_ERR(client
);
513 cb
->cb_client
= client
;
518 static void warn_no_callback_path(struct nfs4_client
*clp
, int reason
)
520 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
521 (int)clp
->cl_name
.len
, clp
->cl_name
.data
, reason
);
524 static void nfsd4_cb_probe_done(struct rpc_task
*task
, void *calldata
)
526 struct nfs4_client
*clp
= calldata
;
529 warn_no_callback_path(clp
, task
->tk_status
);
531 atomic_set(&clp
->cl_cb_conn
.cb_set
, 1);
532 put_nfs4_client(clp
);
535 static const struct rpc_call_ops nfsd4_cb_probe_ops
= {
536 .rpc_call_done
= nfsd4_cb_probe_done
,
539 static struct rpc_cred
*callback_cred
;
541 int set_callback_cred(void)
543 callback_cred
= rpc_lookup_machine_cred();
550 void do_probe_callback(struct nfs4_client
*clp
)
552 struct nfs4_cb_conn
*cb
= &clp
->cl_cb_conn
;
553 struct rpc_message msg
= {
554 .rpc_proc
= &nfs4_cb_procedures
[NFSPROC4_CLNT_CB_NULL
],
556 .rpc_cred
= callback_cred
560 status
= rpc_call_async(cb
->cb_client
, &msg
, RPC_TASK_SOFT
,
561 &nfsd4_cb_probe_ops
, (void *)clp
);
563 warn_no_callback_path(clp
, status
);
564 put_nfs4_client(clp
);
569 * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
572 nfsd4_probe_callback(struct nfs4_client
*clp
)
576 BUG_ON(atomic_read(&clp
->cl_cb_conn
.cb_set
));
578 status
= setup_callback_client(clp
);
580 warn_no_callback_path(clp
, status
);
584 /* the task holds a reference to the nfs4_client struct */
585 atomic_inc(&clp
->cl_count
);
587 do_probe_callback(clp
);
591 * There's currently a single callback channel slot.
592 * If the slot is available, then mark it busy. Otherwise, set the
593 * thread for sleeping on the callback RPC wait queue.
595 static int nfsd41_cb_setup_sequence(struct nfs4_client
*clp
,
596 struct rpc_task
*task
)
598 struct nfs4_rpc_args
*args
= task
->tk_msg
.rpc_argp
;
599 u32
*ptr
= (u32
*)clp
->cl_sessionid
.data
;
602 dprintk("%s: %u:%u:%u:%u\n", __func__
,
603 ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
605 if (test_and_set_bit(0, &clp
->cl_cb_slot_busy
) != 0) {
606 rpc_sleep_on(&clp
->cl_cb_waitq
, task
, NULL
);
607 dprintk("%s slot is busy\n", __func__
);
613 * We'll need the clp during XDR encoding and decoding,
614 * and the sequence during decoding to verify the reply
616 args
->args_seq
.cbs_clp
= clp
;
617 task
->tk_msg
.rpc_resp
= &args
->args_seq
;
620 dprintk("%s status=%d\n", __func__
, status
);
625 * TODO: cb_sequence should support referring call lists, cachethis, multiple
626 * slots, and mark callback channel down on communication errors.
628 static void nfsd4_cb_prepare(struct rpc_task
*task
, void *calldata
)
630 struct nfs4_delegation
*dp
= calldata
;
631 struct nfs4_client
*clp
= dp
->dl_client
;
632 struct nfs4_rpc_args
*args
= task
->tk_msg
.rpc_argp
;
633 u32 minorversion
= clp
->cl_cb_conn
.cb_minorversion
;
636 args
->args_seq
.cbs_minorversion
= minorversion
;
638 status
= nfsd41_cb_setup_sequence(clp
, task
);
640 if (status
!= -EAGAIN
) {
641 /* terminate rpc task */
642 task
->tk_status
= status
;
643 task
->tk_action
= NULL
;
648 rpc_call_start(task
);
651 static void nfsd4_cb_done(struct rpc_task
*task
, void *calldata
)
653 struct nfs4_delegation
*dp
= calldata
;
654 struct nfs4_client
*clp
= dp
->dl_client
;
656 dprintk("%s: minorversion=%d\n", __func__
,
657 clp
->cl_cb_conn
.cb_minorversion
);
659 if (clp
->cl_cb_conn
.cb_minorversion
) {
660 /* No need for lock, access serialized in nfsd4_cb_prepare */
662 clear_bit(0, &clp
->cl_cb_slot_busy
);
663 rpc_wake_up_next(&clp
->cl_cb_waitq
);
664 dprintk("%s: freed slot, new seqid=%d\n", __func__
,
667 /* We're done looking into the sequence information */
668 task
->tk_msg
.rpc_resp
= NULL
;
672 static void nfsd4_cb_recall_done(struct rpc_task
*task
, void *calldata
)
674 struct nfs4_delegation
*dp
= calldata
;
675 struct nfs4_client
*clp
= dp
->dl_client
;
677 nfsd4_cb_done(task
, calldata
);
679 switch (task
->tk_status
) {
681 /* Network partition? */
682 atomic_set(&clp
->cl_cb_conn
.cb_set
, 0);
683 warn_no_callback_path(clp
, task
->tk_status
);
685 case -NFS4ERR_BAD_STATEID
:
686 /* Race: client probably got cb_recall
687 * before open reply granting delegation */
690 /* success, or error we can't handle */
693 if (dp
->dl_retries
--) {
694 rpc_delay(task
, 2*HZ
);
696 rpc_restart_call(task
);
699 atomic_set(&clp
->cl_cb_conn
.cb_set
, 0);
700 warn_no_callback_path(clp
, task
->tk_status
);
703 kfree(task
->tk_msg
.rpc_argp
);
706 static void nfsd4_cb_recall_release(void *calldata
)
708 struct nfs4_delegation
*dp
= calldata
;
709 struct nfs4_client
*clp
= dp
->dl_client
;
711 nfs4_put_delegation(dp
);
712 put_nfs4_client(clp
);
715 static const struct rpc_call_ops nfsd4_cb_recall_ops
= {
716 .rpc_call_prepare
= nfsd4_cb_prepare
,
717 .rpc_call_done
= nfsd4_cb_recall_done
,
718 .rpc_release
= nfsd4_cb_recall_release
,
722 * called with dp->dl_count inc'ed.
725 nfsd4_cb_recall(struct nfs4_delegation
*dp
)
727 struct nfs4_client
*clp
= dp
->dl_client
;
728 struct rpc_clnt
*clnt
= clp
->cl_cb_conn
.cb_client
;
729 struct nfs4_rpc_args
*args
;
730 struct rpc_message msg
= {
731 .rpc_proc
= &nfs4_cb_procedures
[NFSPROC4_CLNT_CB_RECALL
],
732 .rpc_cred
= callback_cred
734 int status
= -ENOMEM
;
736 args
= kzalloc(sizeof(*args
), GFP_KERNEL
);
742 status
= rpc_call_async(clnt
, &msg
, RPC_TASK_SOFT
,
743 &nfsd4_cb_recall_ops
, dp
);
747 put_nfs4_client(clp
);
748 nfs4_put_delegation(dp
);