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>
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define NFSPROC4_CB_NULL 0
55 #define NFSPROC4_CB_COMPOUND 1
56 #define NFS4_STATEID_SIZE 16
58 /* Index of predefined Linux callback client operations */
61 NFSPROC4_CLNT_CB_NULL = 0,
62 NFSPROC4_CLNT_CB_RECALL,
63 NFSPROC4_CLNT_CB_SEQUENCE,
71 #define NFS4_MAXTAGLEN 20
73 #define NFS4_enc_cb_null_sz 0
74 #define NFS4_dec_cb_null_sz 0
75 #define cb_compound_enc_hdr_sz 4
76 #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
77 #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
78 #define cb_sequence_enc_sz (sessionid_sz + 4 + \
79 1 /* no referring calls list yet */)
80 #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
84 #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
85 #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
86 #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
87 cb_sequence_enc_sz + \
88 1 + enc_stateid_sz + \
91 #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
92 cb_sequence_dec_sz + \
95 struct nfs4_rpc_args {
97 struct nfsd4_cb_sequence args_seq;
101 * Generic encode routines from fs/nfs/nfs4xdr.c
103 static inline __be32 *
104 xdr_writemem(__be32 *p, const void *ptr, int nbytes)
106 int tmp = XDR_QUADLEN(nbytes);
110 memcpy(p, ptr, nbytes);
114 #define WRITE32(n) *p++ = htonl(n)
115 #define WRITEMEM(ptr,nbytes) do { \
116 p = xdr_writemem(p, ptr, nbytes); \
118 #define RESERVE_SPACE(nbytes) do { \
119 p = xdr_reserve_space(xdr, nbytes); \
120 if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
125 * Generic decode routines from fs/nfs/nfs4xdr.c
127 #define DECODE_TAIL \
132 dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
136 #define READ32(x) (x) = ntohl(*p++)
137 #define READ64(x) do { \
138 (x) = (u64)ntohl(*p++) << 32; \
139 (x) |= ntohl(*p++); \
141 #define READTIME(x) do { \
143 (x.tv_sec) = ntohl(*p++); \
144 (x.tv_nsec) = ntohl(*p++); \
146 #define READ_BUF(nbytes) do { \
147 p = xdr_inline_decode(xdr, nbytes); \
149 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
150 __func__, __LINE__); \
155 struct nfs4_cb_compound_hdr {
157 u32 ident; /* minorversion 0 only */
170 } nfs_cb_errtbl[] = {
172 { NFS4ERR_PERM, EPERM },
173 { NFS4ERR_NOENT, ENOENT },
175 { NFS4ERR_NXIO, ENXIO },
176 { NFS4ERR_ACCESS, EACCES },
177 { NFS4ERR_EXIST, EEXIST },
178 { NFS4ERR_XDEV, EXDEV },
179 { NFS4ERR_NOTDIR, ENOTDIR },
180 { NFS4ERR_ISDIR, EISDIR },
181 { NFS4ERR_INVAL, EINVAL },
182 { NFS4ERR_FBIG, EFBIG },
183 { NFS4ERR_NOSPC, ENOSPC },
184 { NFS4ERR_ROFS, EROFS },
185 { NFS4ERR_MLINK, EMLINK },
186 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
187 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
188 { NFS4ERR_DQUOT, EDQUOT },
189 { NFS4ERR_STALE, ESTALE },
190 { NFS4ERR_BADHANDLE, EBADHANDLE },
191 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
192 { NFS4ERR_NOTSUPP, ENOTSUPP },
193 { NFS4ERR_TOOSMALL, ETOOSMALL },
194 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
195 { NFS4ERR_BADTYPE, EBADTYPE },
196 { NFS4ERR_LOCKED, EAGAIN },
197 { NFS4ERR_RESOURCE, EREMOTEIO },
198 { NFS4ERR_SYMLINK, ELOOP },
199 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
200 { NFS4ERR_DEADLOCK, EDEADLK },
205 nfs_cb_stat_to_errno(int stat)
208 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
209 if (nfs_cb_errtbl[i].stat == stat)
210 return nfs_cb_errtbl[i].errno;
212 /* If we cannot translate the error, the recovery routines should
214 * Note: remaining NFSv4 error codes have values > 10000, so should
215 * not conflict with native Linux error codes.
225 encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
230 WRITE32(0); /* tag length is always 0 */
231 WRITE32(hdr->minorversion);
237 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
239 *hdr->nops_p = htonl(hdr->nops);
243 encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
244 struct nfs4_cb_compound_hdr *hdr)
247 int len = dp->dl_fh.fh_size;
249 RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
250 WRITE32(OP_CB_RECALL);
251 WRITE32(dp->dl_stateid.si_generation);
252 WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
253 WRITE32(0); /* truncate optimization not implemented */
255 WRITEMEM(&dp->dl_fh.fh_base, len);
260 encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *args,
261 struct nfs4_cb_compound_hdr *hdr)
265 if (hdr->minorversion == 0)
268 RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
270 WRITE32(OP_CB_SEQUENCE);
271 WRITEMEM(args->cbs_clp->cl_sessionid.data, NFS4_MAX_SESSIONID_LEN);
272 WRITE32(args->cbs_clp->cl_cb_seq_nr);
273 WRITE32(0); /* slotid, always 0 */
274 WRITE32(0); /* highest slotid always 0 */
275 WRITE32(0); /* cachethis always 0 */
276 WRITE32(0); /* FIXME: support referring_call_lists */
281 nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
283 struct xdr_stream xdrs, *xdr = &xdrs;
285 xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
291 nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
292 struct nfs4_rpc_args *rpc_args)
294 struct xdr_stream xdr;
295 struct nfs4_delegation *args = rpc_args->args_op;
296 struct nfs4_cb_compound_hdr hdr = {
297 .ident = args->dl_ident,
298 .minorversion = rpc_args->args_seq.cbs_minorversion,
301 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
302 encode_cb_compound_hdr(&xdr, &hdr);
303 encode_cb_sequence(&xdr, &rpc_args->args_seq, &hdr);
304 encode_cb_recall(&xdr, args, &hdr);
305 encode_cb_nops(&hdr);
311 decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
317 READ_BUF(hdr->taglen + 4);
318 hdr->tag = (char *)p;
319 p += XDR_QUADLEN(hdr->taglen);
325 decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
333 if (op != expected) {
334 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
335 " operation %d but we issued a request for %d\n",
340 if (nfserr != NFS_OK)
341 return -nfs_cb_stat_to_errno(nfserr);
346 * Our current back channel implmentation supports a single backchannel
347 * with a single slot.
350 decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *res,
351 struct rpc_rqst *rqstp)
353 struct nfs4_sessionid id;
358 if (res->cbs_minorversion == 0)
361 status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
366 * If the server returns different values for sessionID, slotID or
367 * sequence number, the server is looney tunes.
369 status = -ESERVERFAULT;
371 READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
372 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
373 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
374 if (memcmp(id.data, res->cbs_clp->cl_sessionid.data,
375 NFS4_MAX_SESSIONID_LEN)) {
376 dprintk("%s Invalid session id\n", __func__);
380 if (dummy != res->cbs_clp->cl_cb_seq_nr) {
381 dprintk("%s Invalid sequence number\n", __func__);
384 READ32(dummy); /* slotid must be 0 */
386 dprintk("%s Invalid slotid\n", __func__);
389 /* FIXME: process highest slotid and target highest slotid */
397 nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
403 nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
404 struct nfsd4_cb_sequence *seq)
406 struct xdr_stream xdr;
407 struct nfs4_cb_compound_hdr hdr;
410 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
411 status = decode_cb_compound_hdr(&xdr, &hdr);
415 status = decode_cb_sequence(&xdr, seq, rqstp);
419 status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
425 * RPC procedure tables
427 #define PROC(proc, call, argtype, restype) \
428 [NFSPROC4_CLNT_##proc] = { \
429 .p_proc = NFSPROC4_CB_##call, \
430 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
431 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
432 .p_arglen = NFS4_##argtype##_sz, \
433 .p_replen = NFS4_##restype##_sz, \
434 .p_statidx = NFSPROC4_CB_##call, \
438 static struct rpc_procinfo nfs4_cb_procedures[] = {
439 PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
440 PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
443 static struct rpc_version nfs_cb_version4 = {
445 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
446 .procs = nfs4_cb_procedures
449 static struct rpc_version * nfs_cb_version[] = {
454 static struct rpc_program cb_program;
456 static struct rpc_stat cb_stats = {
457 .program = &cb_program
460 #define NFS4_CALLBACK 0x40000000
461 static struct rpc_program cb_program = {
463 .number = NFS4_CALLBACK,
464 .nrvers = ARRAY_SIZE(nfs_cb_version),
465 .version = nfs_cb_version,
467 .pipe_dir_name = "/nfsd4_cb",
470 static int max_cb_time(void)
472 return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
475 /* Reference counting, callback cleanup, etc., all look racy as heck.
476 * And why is cb_set an atomic? */
478 int setup_callback_client(struct nfs4_client *clp)
480 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
481 struct rpc_timeout timeparms = {
482 .to_initval = max_cb_time(),
485 struct rpc_create_args args = {
486 .protocol = IPPROTO_TCP,
487 .address = (struct sockaddr *) &cb->cb_addr,
488 .addrsize = cb->cb_addrlen,
489 .timeout = &timeparms,
490 .program = &cb_program,
491 .prognumber = cb->cb_prog,
492 .version = nfs_cb_version[1]->number,
493 .authflavor = clp->cl_flavor,
494 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
495 .client_name = clp->cl_principal,
497 struct rpc_clnt *client;
499 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
502 /* Create RPC client */
503 client = rpc_create(&args);
504 if (IS_ERR(client)) {
505 dprintk("NFSD: couldn't create callback client: %ld\n",
507 return PTR_ERR(client);
509 cb->cb_client = client;
514 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
516 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
517 (int)clp->cl_name.len, clp->cl_name.data, reason);
520 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
522 struct nfs4_client *clp = calldata;
525 warn_no_callback_path(clp, task->tk_status);
527 atomic_set(&clp->cl_cb_conn.cb_set, 1);
528 put_nfs4_client(clp);
531 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
532 .rpc_call_done = nfsd4_cb_probe_done,
535 static struct rpc_cred *callback_cred;
537 int set_callback_cred(void)
539 callback_cred = rpc_lookup_machine_cred();
546 void do_probe_callback(struct nfs4_client *clp)
548 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
549 struct rpc_message msg = {
550 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
552 .rpc_cred = callback_cred
556 status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_SOFT,
557 &nfsd4_cb_probe_ops, (void *)clp);
559 warn_no_callback_path(clp, status);
560 put_nfs4_client(clp);
565 * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
568 nfsd4_probe_callback(struct nfs4_client *clp)
572 BUG_ON(atomic_read(&clp->cl_cb_conn.cb_set));
574 status = setup_callback_client(clp);
576 warn_no_callback_path(clp, status);
580 /* the task holds a reference to the nfs4_client struct */
581 atomic_inc(&clp->cl_count);
583 do_probe_callback(clp);
587 * There's currently a single callback channel slot.
588 * If the slot is available, then mark it busy. Otherwise, set the
589 * thread for sleeping on the callback RPC wait queue.
591 static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
592 struct rpc_task *task)
594 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
595 u32 *ptr = (u32 *)clp->cl_sessionid.data;
598 dprintk("%s: %u:%u:%u:%u\n", __func__,
599 ptr[0], ptr[1], ptr[2], ptr[3]);
601 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
602 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
603 dprintk("%s slot is busy\n", __func__);
609 * We'll need the clp during XDR encoding and decoding,
610 * and the sequence during decoding to verify the reply
612 args->args_seq.cbs_clp = clp;
613 task->tk_msg.rpc_resp = &args->args_seq;
616 dprintk("%s status=%d\n", __func__, status);
621 * TODO: cb_sequence should support referring call lists, cachethis, multiple
622 * slots, and mark callback channel down on communication errors.
624 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
626 struct nfs4_delegation *dp = calldata;
627 struct nfs4_client *clp = dp->dl_client;
628 struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
629 u32 minorversion = clp->cl_cb_conn.cb_minorversion;
632 args->args_seq.cbs_minorversion = minorversion;
634 status = nfsd41_cb_setup_sequence(clp, task);
636 if (status != -EAGAIN) {
637 /* terminate rpc task */
638 task->tk_status = status;
639 task->tk_action = NULL;
644 rpc_call_start(task);
647 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
649 struct nfs4_delegation *dp = calldata;
650 struct nfs4_client *clp = dp->dl_client;
652 dprintk("%s: minorversion=%d\n", __func__,
653 clp->cl_cb_conn.cb_minorversion);
655 if (clp->cl_cb_conn.cb_minorversion) {
656 /* No need for lock, access serialized in nfsd4_cb_prepare */
658 clear_bit(0, &clp->cl_cb_slot_busy);
659 rpc_wake_up_next(&clp->cl_cb_waitq);
660 dprintk("%s: freed slot, new seqid=%d\n", __func__,
663 /* We're done looking into the sequence information */
664 task->tk_msg.rpc_resp = NULL;
668 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
670 struct nfs4_delegation *dp = calldata;
671 struct nfs4_client *clp = dp->dl_client;
673 nfsd4_cb_done(task, calldata);
675 switch (task->tk_status) {
677 /* Network partition? */
678 atomic_set(&clp->cl_cb_conn.cb_set, 0);
679 warn_no_callback_path(clp, task->tk_status);
681 case -NFS4ERR_BAD_STATEID:
682 /* Race: client probably got cb_recall
683 * before open reply granting delegation */
686 /* success, or error we can't handle */
689 if (dp->dl_retries--) {
690 rpc_delay(task, 2*HZ);
692 rpc_restart_call(task);
695 atomic_set(&clp->cl_cb_conn.cb_set, 0);
696 warn_no_callback_path(clp, task->tk_status);
699 kfree(task->tk_msg.rpc_argp);
702 static void nfsd4_cb_recall_release(void *calldata)
704 struct nfs4_delegation *dp = calldata;
705 struct nfs4_client *clp = dp->dl_client;
707 nfs4_put_delegation(dp);
708 put_nfs4_client(clp);
711 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
712 .rpc_call_prepare = nfsd4_cb_prepare,
713 .rpc_call_done = nfsd4_cb_recall_done,
714 .rpc_release = nfsd4_cb_recall_release,
718 * called with dp->dl_count inc'ed.
721 nfsd4_cb_recall(struct nfs4_delegation *dp)
723 struct nfs4_client *clp = dp->dl_client;
724 struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
725 struct nfs4_rpc_args *args;
726 struct rpc_message msg = {
727 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
728 .rpc_cred = callback_cred
730 int status = -ENOMEM;
732 args = kzalloc(sizeof(*args), GFP_KERNEL);
738 status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
739 &nfsd4_cb_recall_ops, dp);
743 put_nfs4_client(clp);
744 nfs4_put_delegation(dp);