NFSD: Update XDR encoders in NFSv4 callback client
[pandora-kernel.git] / fs / nfsd / nfs4callback.c
1 /*
2  *  Copyright (c) 2001 The Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  Kendrick Smith <kmsmith@umich.edu>
6  *  Andy Adamson <andros@umich.edu>
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
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.
20  *
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.
32  */
33
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/svc_xprt.h>
36 #include <linux/slab.h>
37 #include "nfsd.h"
38 #include "state.h"
39
40 #define NFSDDBG_FACILITY                NFSDDBG_PROC
41
42 #define NFSPROC4_CB_NULL 0
43 #define NFSPROC4_CB_COMPOUND 1
44
45 /* Index of predefined Linux callback client operations */
46
47 enum {
48         NFSPROC4_CLNT_CB_NULL = 0,
49         NFSPROC4_CLNT_CB_RECALL,
50         NFSPROC4_CLNT_CB_SEQUENCE,
51 };
52
53 #define NFS4_MAXTAGLEN          20
54
55 #define NFS4_enc_cb_null_sz             0
56 #define NFS4_dec_cb_null_sz             0
57 #define cb_compound_enc_hdr_sz          4
58 #define cb_compound_dec_hdr_sz          (3 + (NFS4_MAXTAGLEN >> 2))
59 #define sessionid_sz                    (NFS4_MAX_SESSIONID_LEN >> 2)
60 #define cb_sequence_enc_sz              (sessionid_sz + 4 +             \
61                                         1 /* no referring calls list yet */)
62 #define cb_sequence_dec_sz              (op_dec_sz + sessionid_sz + 4)
63
64 #define op_enc_sz                       1
65 #define op_dec_sz                       2
66 #define enc_nfs4_fh_sz                  (1 + (NFS4_FHSIZE >> 2))
67 #define enc_stateid_sz                  (NFS4_STATEID_SIZE >> 2)
68 #define NFS4_enc_cb_recall_sz           (cb_compound_enc_hdr_sz +       \
69                                         cb_sequence_enc_sz +            \
70                                         1 + enc_stateid_sz +            \
71                                         enc_nfs4_fh_sz)
72
73 #define NFS4_dec_cb_recall_sz           (cb_compound_dec_hdr_sz  +      \
74                                         cb_sequence_dec_sz +            \
75                                         op_dec_sz)
76
77 /*
78  * Generic decode routines from fs/nfs/nfs4xdr.c
79  */
80 #define DECODE_TAIL                             \
81         status = 0;                             \
82 out:                                            \
83         return status;                          \
84 xdr_error:                                      \
85         dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
86         status = -EIO;                          \
87         goto out
88
89 #define READ32(x)         (x) = ntohl(*p++)
90 #define READ64(x)         do {                  \
91         (x) = (u64)ntohl(*p++) << 32;           \
92         (x) |= ntohl(*p++);                     \
93 } while (0)
94 #define READTIME(x)       do {                  \
95         p++;                                    \
96         (x.tv_sec) = ntohl(*p++);               \
97         (x.tv_nsec) = ntohl(*p++);              \
98 } while (0)
99 #define READ_BUF(nbytes)  do { \
100         p = xdr_inline_decode(xdr, nbytes); \
101         if (!p) { \
102                 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
103                         __func__, __LINE__); \
104                 return -EIO; \
105         } \
106 } while (0)
107
108 struct nfs4_cb_compound_hdr {
109         /* args */
110         u32             ident;  /* minorversion 0 only */
111         u32             nops;
112         __be32          *nops_p;
113         u32             minorversion;
114         /* res */
115         int             status;
116 };
117
118 static struct {
119 int stat;
120 int errno;
121 } nfs_cb_errtbl[] = {
122         { NFS4_OK,              0               },
123         { NFS4ERR_PERM,         EPERM           },
124         { NFS4ERR_NOENT,        ENOENT          },
125         { NFS4ERR_IO,           EIO             },
126         { NFS4ERR_NXIO,         ENXIO           },
127         { NFS4ERR_ACCESS,       EACCES          },
128         { NFS4ERR_EXIST,        EEXIST          },
129         { NFS4ERR_XDEV,         EXDEV           },
130         { NFS4ERR_NOTDIR,       ENOTDIR         },
131         { NFS4ERR_ISDIR,        EISDIR          },
132         { NFS4ERR_INVAL,        EINVAL          },
133         { NFS4ERR_FBIG,         EFBIG           },
134         { NFS4ERR_NOSPC,        ENOSPC          },
135         { NFS4ERR_ROFS,         EROFS           },
136         { NFS4ERR_MLINK,        EMLINK          },
137         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
138         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
139         { NFS4ERR_DQUOT,        EDQUOT          },
140         { NFS4ERR_STALE,        ESTALE          },
141         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
142         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
143         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
144         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
145         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
146         { NFS4ERR_BADTYPE,      EBADTYPE        },
147         { NFS4ERR_LOCKED,       EAGAIN          },
148         { NFS4ERR_RESOURCE,     EREMOTEIO       },
149         { NFS4ERR_SYMLINK,      ELOOP           },
150         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
151         { NFS4ERR_DEADLOCK,     EDEADLK         },
152         { -1,                   EIO             }
153 };
154
155 static int
156 nfs_cb_stat_to_errno(int stat)
157 {
158         int i;
159         for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
160                 if (nfs_cb_errtbl[i].stat == stat)
161                         return nfs_cb_errtbl[i].errno;
162         }
163         /* If we cannot translate the error, the recovery routines should
164         * handle it.
165         * Note: remaining NFSv4 error codes have values > 10000, so should
166         * not conflict with native Linux error codes.
167         */
168         return stat;
169 }
170
171 static __be32 *xdr_encode_empty_array(__be32 *p)
172 {
173         *p++ = xdr_zero;
174         return p;
175 }
176
177 /*
178  * Encode/decode NFSv4 CB basic data types
179  *
180  * Basic NFSv4 callback data types are defined in section 15 of RFC
181  * 3530: "Network File System (NFS) version 4 Protocol" and section
182  * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
183  * 1 Protocol"
184  */
185
186 /*
187  *      nfs_cb_opnum4
188  *
189  *      enum nfs_cb_opnum4 {
190  *              OP_CB_GETATTR           = 3,
191  *                ...
192  *      };
193  */
194 enum nfs_cb_opnum4 {
195         OP_CB_GETATTR                   = 3,
196         OP_CB_RECALL                    = 4,
197         OP_CB_LAYOUTRECALL              = 5,
198         OP_CB_NOTIFY                    = 6,
199         OP_CB_PUSH_DELEG                = 7,
200         OP_CB_RECALL_ANY                = 8,
201         OP_CB_RECALLABLE_OBJ_AVAIL      = 9,
202         OP_CB_RECALL_SLOT               = 10,
203         OP_CB_SEQUENCE                  = 11,
204         OP_CB_WANTS_CANCELLED           = 12,
205         OP_CB_NOTIFY_LOCK               = 13,
206         OP_CB_NOTIFY_DEVICEID           = 14,
207         OP_CB_ILLEGAL                   = 10044
208 };
209
210 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
211 {
212         __be32 *p;
213
214         p = xdr_reserve_space(xdr, 4);
215         *p = cpu_to_be32(op);
216 }
217
218 /*
219  * nfs_fh4
220  *
221  *      typedef opaque nfs_fh4<NFS4_FHSIZE>;
222  */
223 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
224 {
225         u32 length = fh->fh_size;
226         __be32 *p;
227
228         BUG_ON(length > NFS4_FHSIZE);
229         p = xdr_reserve_space(xdr, 4 + length);
230         xdr_encode_opaque(p, &fh->fh_base, length);
231 }
232
233 /*
234  * stateid4
235  *
236  *      struct stateid4 {
237  *              uint32_t        seqid;
238  *              opaque          other[12];
239  *      };
240  */
241 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
242 {
243         __be32 *p;
244
245         p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
246         *p++ = cpu_to_be32(sid->si_generation);
247         xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
248 }
249
250 /*
251  * sessionid4
252  *
253  *      typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
254  */
255 static void encode_sessionid4(struct xdr_stream *xdr,
256                               const struct nfsd4_session *session)
257 {
258         __be32 *p;
259
260         p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
261         xdr_encode_opaque_fixed(p, session->se_sessionid.data,
262                                         NFS4_MAX_SESSIONID_LEN);
263 }
264
265 /*
266  * CB_COMPOUND4args
267  *
268  *      struct CB_COMPOUND4args {
269  *              utf8str_cs      tag;
270  *              uint32_t        minorversion;
271  *              uint32_t        callback_ident;
272  *              nfs_cb_argop4   argarray<>;
273  *      };
274 */
275 static void encode_cb_compound4args(struct xdr_stream *xdr,
276                                     struct nfs4_cb_compound_hdr *hdr)
277 {
278         __be32 * p;
279
280         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
281         p = xdr_encode_empty_array(p);          /* empty tag */
282         *p++ = cpu_to_be32(hdr->minorversion);
283         *p++ = cpu_to_be32(hdr->ident);
284
285         hdr->nops_p = p;
286         *p = cpu_to_be32(hdr->nops);            /* argarray element count */
287 }
288
289 /*
290  * Update argarray element count
291  */
292 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
293 {
294         BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
295         *hdr->nops_p = cpu_to_be32(hdr->nops);
296 }
297
298 /*
299  * CB_RECALL4args
300  *
301  *      struct CB_RECALL4args {
302  *              stateid4        stateid;
303  *              bool            truncate;
304  *              nfs_fh4         fh;
305  *      };
306  */
307 static void encode_cb_recall4args(struct xdr_stream *xdr,
308                                   const struct nfs4_delegation *dp,
309                                   struct nfs4_cb_compound_hdr *hdr)
310 {
311         __be32 *p;
312
313         encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
314         encode_stateid4(xdr, &dp->dl_stateid);
315
316         p = xdr_reserve_space(xdr, 4);
317         *p++ = xdr_zero;                        /* truncate */
318
319         encode_nfs_fh4(xdr, &dp->dl_fh);
320
321         hdr->nops++;
322 }
323
324 /*
325  * CB_SEQUENCE4args
326  *
327  *      struct CB_SEQUENCE4args {
328  *              sessionid4              csa_sessionid;
329  *              sequenceid4             csa_sequenceid;
330  *              slotid4                 csa_slotid;
331  *              slotid4                 csa_highest_slotid;
332  *              bool                    csa_cachethis;
333  *              referring_call_list4    csa_referring_call_lists<>;
334  *      };
335  */
336 static void encode_cb_sequence4args(struct xdr_stream *xdr,
337                                     const struct nfsd4_callback *cb,
338                                     struct nfs4_cb_compound_hdr *hdr)
339 {
340         struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
341         __be32 *p;
342
343         if (hdr->minorversion == 0)
344                 return;
345
346         encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
347         encode_sessionid4(xdr, session);
348
349         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
350         *p++ = cpu_to_be32(session->se_cb_seq_nr);      /* csa_sequenceid */
351         *p++ = xdr_zero;                        /* csa_slotid */
352         *p++ = xdr_zero;                        /* csa_highest_slotid */
353         *p++ = xdr_zero;                        /* csa_cachethis */
354         xdr_encode_empty_array(p);              /* csa_referring_call_lists */
355
356         hdr->nops++;
357 }
358
359 /*
360  * NFSv4.0 and NFSv4.1 XDR encode functions
361  *
362  * NFSv4.0 callback argument types are defined in section 15 of RFC
363  * 3530: "Network File System (NFS) version 4 Protocol" and section 20
364  * of RFC 5661:  "Network File System (NFS) Version 4 Minor Version 1
365  * Protocol".
366  */
367
368 /*
369  * NB: Without this zero space reservation, callbacks over krb5p fail
370  */
371 static int nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p, void *__unused)
372 {
373         struct xdr_stream xdrs, *xdr = &xdrs;
374
375         xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
376         xdr_reserve_space(xdr, 0);
377         return 0;
378 }
379
380 /*
381  * 20.2. Operation 4: CB_RECALL - Recall a Delegation
382  */
383 static int nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
384                                   const struct nfsd4_callback *cb)
385 {
386         struct xdr_stream xdr;
387         const struct nfs4_delegation *args = cb->cb_op;
388         struct nfs4_cb_compound_hdr hdr = {
389                 .ident = cb->cb_clp->cl_cb_ident,
390                 .minorversion = cb->cb_minorversion,
391         };
392
393         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
394         encode_cb_compound4args(&xdr, &hdr);
395         encode_cb_sequence4args(&xdr, cb, &hdr);
396         encode_cb_recall4args(&xdr, args, &hdr);
397         encode_cb_nops(&hdr);
398         return 0;
399 }
400
401
402 static int
403 decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
404         __be32 *p;
405         u32 taglen;
406
407         READ_BUF(8);
408         READ32(hdr->status);
409         /* We've got no use for the tag; ignore it: */
410         READ32(taglen);
411         READ_BUF(taglen + 4);
412         p += XDR_QUADLEN(taglen);
413         READ32(hdr->nops);
414         return 0;
415 }
416
417 static int
418 decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
419 {
420         __be32 *p;
421         u32 op;
422         int32_t nfserr;
423
424         READ_BUF(8);
425         READ32(op);
426         if (op != expected) {
427                 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
428                          " operation %d but we issued a request for %d\n",
429                          op, expected);
430                 return -EIO;
431         }
432         READ32(nfserr);
433         if (nfserr != NFS_OK)
434                 return -nfs_cb_stat_to_errno(nfserr);
435         return 0;
436 }
437
438 /*
439  * Our current back channel implmentation supports a single backchannel
440  * with a single slot.
441  */
442 static int
443 decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_callback *cb,
444                    struct rpc_rqst *rqstp)
445 {
446         struct nfsd4_session *ses = cb->cb_clp->cl_cb_session;
447         struct nfs4_sessionid id;
448         int status;
449         u32 dummy;
450         __be32 *p;
451
452         if (cb->cb_minorversion == 0)
453                 return 0;
454
455         status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
456         if (status)
457                 return status;
458
459         /*
460          * If the server returns different values for sessionID, slotID or
461          * sequence number, the server is looney tunes.
462          */
463         status = -ESERVERFAULT;
464
465         READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
466         memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
467         p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
468         if (memcmp(id.data, ses->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
469                 dprintk("%s Invalid session id\n", __func__);
470                 goto out;
471         }
472         READ32(dummy);
473         if (dummy != ses->se_cb_seq_nr) {
474                 dprintk("%s Invalid sequence number\n", __func__);
475                 goto out;
476         }
477         READ32(dummy);  /* slotid must be 0 */
478         if (dummy != 0) {
479                 dprintk("%s Invalid slotid\n", __func__);
480                 goto out;
481         }
482         /* FIXME: process highest slotid and target highest slotid */
483         status = 0;
484 out:
485         return status;
486 }
487
488
489 static int
490 nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
491 {
492         return 0;
493 }
494
495 static int
496 nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
497                 struct nfsd4_callback *cb)
498 {
499         struct xdr_stream xdr;
500         struct nfs4_cb_compound_hdr hdr;
501         int status;
502
503         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
504         status = decode_cb_compound_hdr(&xdr, &hdr);
505         if (status)
506                 goto out;
507         if (cb) {
508                 status = decode_cb_sequence(&xdr, cb, rqstp);
509                 if (status)
510                         goto out;
511         }
512         status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
513 out:
514         return status;
515 }
516
517 /*
518  * RPC procedure tables
519  */
520 #define PROC(proc, call, argtype, restype)                              \
521 [NFSPROC4_CLNT_##proc] = {                                              \
522         .p_proc   = NFSPROC4_CB_##call,                                 \
523         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,                    \
524         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,                    \
525         .p_arglen = NFS4_##argtype##_sz,                                \
526         .p_replen = NFS4_##restype##_sz,                                \
527         .p_statidx = NFSPROC4_CB_##call,                                \
528         .p_name   = #proc,                                              \
529 }
530
531 static struct rpc_procinfo     nfs4_cb_procedures[] = {
532     PROC(CB_NULL,      NULL,     enc_cb_null,     dec_cb_null),
533     PROC(CB_RECALL,    COMPOUND,   enc_cb_recall,      dec_cb_recall),
534 };
535
536 static struct rpc_version       nfs_cb_version4 = {
537 /*
538  * Note on the callback rpc program version number: despite language in rfc
539  * 5661 section 18.36.3 requiring servers to use 4 in this field, the
540  * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
541  * in practice that appears to be what implementations use.  The section
542  * 18.36.3 language is expected to be fixed in an erratum.
543  */
544         .number                 = 1,
545         .nrprocs                = ARRAY_SIZE(nfs4_cb_procedures),
546         .procs                  = nfs4_cb_procedures
547 };
548
549 static struct rpc_version *     nfs_cb_version[] = {
550         &nfs_cb_version4,
551 };
552
553 static struct rpc_program cb_program;
554
555 static struct rpc_stat cb_stats = {
556                 .program        = &cb_program
557 };
558
559 #define NFS4_CALLBACK 0x40000000
560 static struct rpc_program cb_program = {
561                 .name           = "nfs4_cb",
562                 .number         = NFS4_CALLBACK,
563                 .nrvers         = ARRAY_SIZE(nfs_cb_version),
564                 .version        = nfs_cb_version,
565                 .stats          = &cb_stats,
566                 .pipe_dir_name  = "/nfsd4_cb",
567 };
568
569 static int max_cb_time(void)
570 {
571         return max(nfsd4_lease/10, (time_t)1) * HZ;
572 }
573
574 /* Reference counting, callback cleanup, etc., all look racy as heck.
575  * And why is cl_cb_set an atomic? */
576
577 int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
578 {
579         struct rpc_timeout      timeparms = {
580                 .to_initval     = max_cb_time(),
581                 .to_retries     = 0,
582         };
583         struct rpc_create_args args = {
584                 .net            = &init_net,
585                 .address        = (struct sockaddr *) &conn->cb_addr,
586                 .addrsize       = conn->cb_addrlen,
587                 .timeout        = &timeparms,
588                 .program        = &cb_program,
589                 .version        = 0,
590                 .authflavor     = clp->cl_flavor,
591                 .flags          = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
592         };
593         struct rpc_clnt *client;
594
595         if (clp->cl_minorversion == 0) {
596                 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
597                         return -EINVAL;
598                 args.client_name = clp->cl_principal;
599                 args.prognumber = conn->cb_prog,
600                 args.protocol = XPRT_TRANSPORT_TCP;
601                 clp->cl_cb_ident = conn->cb_ident;
602         } else {
603                 args.bc_xprt = conn->cb_xprt;
604                 args.prognumber = clp->cl_cb_session->se_cb_prog;
605                 args.protocol = XPRT_TRANSPORT_BC_TCP;
606         }
607         /* Create RPC client */
608         client = rpc_create(&args);
609         if (IS_ERR(client)) {
610                 dprintk("NFSD: couldn't create callback client: %ld\n",
611                         PTR_ERR(client));
612                 return PTR_ERR(client);
613         }
614         clp->cl_cb_client = client;
615         return 0;
616
617 }
618
619 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
620 {
621         dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
622                 (int)clp->cl_name.len, clp->cl_name.data, reason);
623 }
624
625 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
626 {
627         struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
628
629         if (task->tk_status)
630                 warn_no_callback_path(clp, task->tk_status);
631         else
632                 atomic_set(&clp->cl_cb_set, 1);
633 }
634
635 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
636         /* XXX: release method to ensure we set the cb channel down if
637          * necessary on early failure? */
638         .rpc_call_done = nfsd4_cb_probe_done,
639 };
640
641 static struct rpc_cred *callback_cred;
642
643 int set_callback_cred(void)
644 {
645         if (callback_cred)
646                 return 0;
647         callback_cred = rpc_lookup_machine_cred();
648         if (!callback_cred)
649                 return -ENOMEM;
650         return 0;
651 }
652
653 static struct workqueue_struct *callback_wq;
654
655 static void do_probe_callback(struct nfs4_client *clp)
656 {
657         struct nfsd4_callback *cb = &clp->cl_cb_null;
658
659         cb->cb_op = NULL;
660         cb->cb_clp = clp;
661
662         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
663         cb->cb_msg.rpc_argp = NULL;
664         cb->cb_msg.rpc_resp = NULL;
665         cb->cb_msg.rpc_cred = callback_cred;
666
667         cb->cb_ops = &nfsd4_cb_probe_ops;
668
669         queue_work(callback_wq, &cb->cb_work);
670 }
671
672 /*
673  * Poke the callback thread to process any updates to the callback
674  * parameters, and send a null probe.
675  */
676 void nfsd4_probe_callback(struct nfs4_client *clp)
677 {
678         set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
679         do_probe_callback(clp);
680 }
681
682 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
683 {
684         BUG_ON(atomic_read(&clp->cl_cb_set));
685
686         spin_lock(&clp->cl_lock);
687         memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
688         spin_unlock(&clp->cl_lock);
689 }
690
691 /*
692  * There's currently a single callback channel slot.
693  * If the slot is available, then mark it busy.  Otherwise, set the
694  * thread for sleeping on the callback RPC wait queue.
695  */
696 static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
697                 struct rpc_task *task)
698 {
699         u32 *ptr = (u32 *)clp->cl_cb_session->se_sessionid.data;
700         int status = 0;
701
702         dprintk("%s: %u:%u:%u:%u\n", __func__,
703                 ptr[0], ptr[1], ptr[2], ptr[3]);
704
705         if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
706                 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
707                 dprintk("%s slot is busy\n", __func__);
708                 status = -EAGAIN;
709                 goto out;
710         }
711 out:
712         dprintk("%s status=%d\n", __func__, status);
713         return status;
714 }
715
716 /*
717  * TODO: cb_sequence should support referring call lists, cachethis, multiple
718  * slots, and mark callback channel down on communication errors.
719  */
720 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
721 {
722         struct nfsd4_callback *cb = calldata;
723         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
724         struct nfs4_client *clp = dp->dl_client;
725         u32 minorversion = clp->cl_minorversion;
726         int status = 0;
727
728         cb->cb_minorversion = minorversion;
729         if (minorversion) {
730                 status = nfsd41_cb_setup_sequence(clp, task);
731                 if (status) {
732                         if (status != -EAGAIN) {
733                                 /* terminate rpc task */
734                                 task->tk_status = status;
735                                 task->tk_action = NULL;
736                         }
737                         return;
738                 }
739         }
740         rpc_call_start(task);
741 }
742
743 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
744 {
745         struct nfsd4_callback *cb = calldata;
746         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
747         struct nfs4_client *clp = dp->dl_client;
748
749         dprintk("%s: minorversion=%d\n", __func__,
750                 clp->cl_minorversion);
751
752         if (clp->cl_minorversion) {
753                 /* No need for lock, access serialized in nfsd4_cb_prepare */
754                 ++clp->cl_cb_session->se_cb_seq_nr;
755                 clear_bit(0, &clp->cl_cb_slot_busy);
756                 rpc_wake_up_next(&clp->cl_cb_waitq);
757                 dprintk("%s: freed slot, new seqid=%d\n", __func__,
758                         clp->cl_cb_session->se_cb_seq_nr);
759
760                 /* We're done looking into the sequence information */
761                 task->tk_msg.rpc_resp = NULL;
762         }
763 }
764
765
766 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
767 {
768         struct nfsd4_callback *cb = calldata;
769         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
770         struct nfs4_client *clp = dp->dl_client;
771         struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
772
773         nfsd4_cb_done(task, calldata);
774
775         if (current_rpc_client == NULL) {
776                 /* We're shutting down; give up. */
777                 /* XXX: err, or is it ok just to fall through
778                  * and rpc_restart_call? */
779                 return;
780         }
781
782         switch (task->tk_status) {
783         case 0:
784                 return;
785         case -EBADHANDLE:
786         case -NFS4ERR_BAD_STATEID:
787                 /* Race: client probably got cb_recall
788                  * before open reply granting delegation */
789                 break;
790         default:
791                 /* Network partition? */
792                 atomic_set(&clp->cl_cb_set, 0);
793                 warn_no_callback_path(clp, task->tk_status);
794                 if (current_rpc_client != task->tk_client) {
795                         /* queue a callback on the new connection: */
796                         atomic_inc(&dp->dl_count);
797                         nfsd4_cb_recall(dp);
798                         return;
799                 }
800         }
801         if (dp->dl_retries--) {
802                 rpc_delay(task, 2*HZ);
803                 task->tk_status = 0;
804                 rpc_restart_call_prepare(task);
805                 return;
806         } else {
807                 atomic_set(&clp->cl_cb_set, 0);
808                 warn_no_callback_path(clp, task->tk_status);
809         }
810 }
811
812 static void nfsd4_cb_recall_release(void *calldata)
813 {
814         struct nfsd4_callback *cb = calldata;
815         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
816
817         nfs4_put_delegation(dp);
818 }
819
820 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
821         .rpc_call_prepare = nfsd4_cb_prepare,
822         .rpc_call_done = nfsd4_cb_recall_done,
823         .rpc_release = nfsd4_cb_recall_release,
824 };
825
826 int nfsd4_create_callback_queue(void)
827 {
828         callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
829         if (!callback_wq)
830                 return -ENOMEM;
831         return 0;
832 }
833
834 void nfsd4_destroy_callback_queue(void)
835 {
836         destroy_workqueue(callback_wq);
837 }
838
839 /* must be called under the state lock */
840 void nfsd4_shutdown_callback(struct nfs4_client *clp)
841 {
842         set_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags);
843         /*
844          * Note this won't actually result in a null callback;
845          * instead, nfsd4_do_callback_rpc() will detect the killed
846          * client, destroy the rpc client, and stop:
847          */
848         do_probe_callback(clp);
849         flush_workqueue(callback_wq);
850 }
851
852 void nfsd4_release_cb(struct nfsd4_callback *cb)
853 {
854         if (cb->cb_ops->rpc_release)
855                 cb->cb_ops->rpc_release(cb);
856 }
857
858 void nfsd4_process_cb_update(struct nfsd4_callback *cb)
859 {
860         struct nfs4_cb_conn conn;
861         struct nfs4_client *clp = cb->cb_clp;
862         int err;
863
864         /*
865          * This is either an update, or the client dying; in either case,
866          * kill the old client:
867          */
868         if (clp->cl_cb_client) {
869                 rpc_shutdown_client(clp->cl_cb_client);
870                 clp->cl_cb_client = NULL;
871         }
872         if (test_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags))
873                 return;
874         spin_lock(&clp->cl_lock);
875         /*
876          * Only serialized callback code is allowed to clear these
877          * flags; main nfsd code can only set them:
878          */
879         BUG_ON(!clp->cl_cb_flags);
880         clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
881         memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
882         spin_unlock(&clp->cl_lock);
883
884         err = setup_callback_client(clp, &conn);
885         if (err)
886                 warn_no_callback_path(clp, err);
887 }
888
889 void nfsd4_do_callback_rpc(struct work_struct *w)
890 {
891         struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
892         struct nfs4_client *clp = cb->cb_clp;
893         struct rpc_clnt *clnt;
894
895         if (clp->cl_cb_flags)
896                 nfsd4_process_cb_update(cb);
897
898         clnt = clp->cl_cb_client;
899         if (!clnt) {
900                 /* Callback channel broken, or client killed; give up: */
901                 nfsd4_release_cb(cb);
902                 return;
903         }
904         rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
905                         cb->cb_ops, cb);
906 }
907
908 void nfsd4_cb_recall(struct nfs4_delegation *dp)
909 {
910         struct nfsd4_callback *cb = &dp->dl_recall;
911
912         dp->dl_retries = 1;
913         cb->cb_op = dp;
914         cb->cb_clp = dp->dl_client;
915         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
916         cb->cb_msg.rpc_argp = cb;
917         cb->cb_msg.rpc_resp = cb;
918         cb->cb_msg.rpc_cred = callback_cred;
919
920         cb->cb_ops = &nfsd4_cb_recall_ops;
921         dp->dl_retries = 1;
922
923         queue_work(callback_wq, &dp->dl_recall.cb_work);
924 }