nfsd: introduce a generic nfsd4_cb
[firefly-linux-kernel-4.4.55.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/xprt.h>
36 #include <linux/sunrpc/svc_xprt.h>
37 #include <linux/slab.h>
38 #include "nfsd.h"
39 #include "state.h"
40 #include "netns.h"
41 #include "xdr4cb.h"
42
43 #define NFSDDBG_FACILITY                NFSDDBG_PROC
44
45 static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
46
47 #define NFSPROC4_CB_NULL 0
48 #define NFSPROC4_CB_COMPOUND 1
49
50 /* Index of predefined Linux callback client operations */
51
52 #define to_delegation(cb) \
53         container_of(cb, struct nfs4_delegation, dl_recall)
54
55 struct nfs4_cb_compound_hdr {
56         /* args */
57         u32             ident;  /* minorversion 0 only */
58         u32             nops;
59         __be32          *nops_p;
60         u32             minorversion;
61         /* res */
62         int             status;
63 };
64
65 /*
66  * Handle decode buffer overflows out-of-line.
67  */
68 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
69 {
70         dprintk("NFS: %s prematurely hit the end of our receive buffer. "
71                 "Remaining buffer length is %tu words.\n",
72                 func, xdr->end - xdr->p);
73 }
74
75 static __be32 *xdr_encode_empty_array(__be32 *p)
76 {
77         *p++ = xdr_zero;
78         return p;
79 }
80
81 /*
82  * Encode/decode NFSv4 CB basic data types
83  *
84  * Basic NFSv4 callback data types are defined in section 15 of RFC
85  * 3530: "Network File System (NFS) version 4 Protocol" and section
86  * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
87  * 1 Protocol"
88  */
89
90 /*
91  *      nfs_cb_opnum4
92  *
93  *      enum nfs_cb_opnum4 {
94  *              OP_CB_GETATTR           = 3,
95  *                ...
96  *      };
97  */
98 enum nfs_cb_opnum4 {
99         OP_CB_GETATTR                   = 3,
100         OP_CB_RECALL                    = 4,
101         OP_CB_LAYOUTRECALL              = 5,
102         OP_CB_NOTIFY                    = 6,
103         OP_CB_PUSH_DELEG                = 7,
104         OP_CB_RECALL_ANY                = 8,
105         OP_CB_RECALLABLE_OBJ_AVAIL      = 9,
106         OP_CB_RECALL_SLOT               = 10,
107         OP_CB_SEQUENCE                  = 11,
108         OP_CB_WANTS_CANCELLED           = 12,
109         OP_CB_NOTIFY_LOCK               = 13,
110         OP_CB_NOTIFY_DEVICEID           = 14,
111         OP_CB_ILLEGAL                   = 10044
112 };
113
114 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
115 {
116         __be32 *p;
117
118         p = xdr_reserve_space(xdr, 4);
119         *p = cpu_to_be32(op);
120 }
121
122 /*
123  * nfs_fh4
124  *
125  *      typedef opaque nfs_fh4<NFS4_FHSIZE>;
126  */
127 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
128 {
129         u32 length = fh->fh_size;
130         __be32 *p;
131
132         BUG_ON(length > NFS4_FHSIZE);
133         p = xdr_reserve_space(xdr, 4 + length);
134         xdr_encode_opaque(p, &fh->fh_base, length);
135 }
136
137 /*
138  * stateid4
139  *
140  *      struct stateid4 {
141  *              uint32_t        seqid;
142  *              opaque          other[12];
143  *      };
144  */
145 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
146 {
147         __be32 *p;
148
149         p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
150         *p++ = cpu_to_be32(sid->si_generation);
151         xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
152 }
153
154 /*
155  * sessionid4
156  *
157  *      typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
158  */
159 static void encode_sessionid4(struct xdr_stream *xdr,
160                               const struct nfsd4_session *session)
161 {
162         __be32 *p;
163
164         p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
165         xdr_encode_opaque_fixed(p, session->se_sessionid.data,
166                                         NFS4_MAX_SESSIONID_LEN);
167 }
168
169 /*
170  * nfsstat4
171  */
172 static const struct {
173         int stat;
174         int errno;
175 } nfs_cb_errtbl[] = {
176         { NFS4_OK,              0               },
177         { NFS4ERR_PERM,         -EPERM          },
178         { NFS4ERR_NOENT,        -ENOENT         },
179         { NFS4ERR_IO,           -EIO            },
180         { NFS4ERR_NXIO,         -ENXIO          },
181         { NFS4ERR_ACCESS,       -EACCES         },
182         { NFS4ERR_EXIST,        -EEXIST         },
183         { NFS4ERR_XDEV,         -EXDEV          },
184         { NFS4ERR_NOTDIR,       -ENOTDIR        },
185         { NFS4ERR_ISDIR,        -EISDIR         },
186         { NFS4ERR_INVAL,        -EINVAL         },
187         { NFS4ERR_FBIG,         -EFBIG          },
188         { NFS4ERR_NOSPC,        -ENOSPC         },
189         { NFS4ERR_ROFS,         -EROFS          },
190         { NFS4ERR_MLINK,        -EMLINK         },
191         { NFS4ERR_NAMETOOLONG,  -ENAMETOOLONG   },
192         { NFS4ERR_NOTEMPTY,     -ENOTEMPTY      },
193         { NFS4ERR_DQUOT,        -EDQUOT         },
194         { NFS4ERR_STALE,        -ESTALE         },
195         { NFS4ERR_BADHANDLE,    -EBADHANDLE     },
196         { NFS4ERR_BAD_COOKIE,   -EBADCOOKIE     },
197         { NFS4ERR_NOTSUPP,      -ENOTSUPP       },
198         { NFS4ERR_TOOSMALL,     -ETOOSMALL      },
199         { NFS4ERR_SERVERFAULT,  -ESERVERFAULT   },
200         { NFS4ERR_BADTYPE,      -EBADTYPE       },
201         { NFS4ERR_LOCKED,       -EAGAIN         },
202         { NFS4ERR_RESOURCE,     -EREMOTEIO      },
203         { NFS4ERR_SYMLINK,      -ELOOP          },
204         { NFS4ERR_OP_ILLEGAL,   -EOPNOTSUPP     },
205         { NFS4ERR_DEADLOCK,     -EDEADLK        },
206         { -1,                   -EIO            }
207 };
208
209 /*
210  * If we cannot translate the error, the recovery routines should
211  * handle it.
212  *
213  * Note: remaining NFSv4 error codes have values > 10000, so should
214  * not conflict with native Linux error codes.
215  */
216 static int nfs_cb_stat_to_errno(int status)
217 {
218         int i;
219
220         for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
221                 if (nfs_cb_errtbl[i].stat == status)
222                         return nfs_cb_errtbl[i].errno;
223         }
224
225         dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
226         return -status;
227 }
228
229 static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
230                                enum nfsstat4 *status)
231 {
232         __be32 *p;
233         u32 op;
234
235         p = xdr_inline_decode(xdr, 4 + 4);
236         if (unlikely(p == NULL))
237                 goto out_overflow;
238         op = be32_to_cpup(p++);
239         if (unlikely(op != expected))
240                 goto out_unexpected;
241         *status = be32_to_cpup(p);
242         return 0;
243 out_overflow:
244         print_overflow_msg(__func__, xdr);
245         return -EIO;
246 out_unexpected:
247         dprintk("NFSD: Callback server returned operation %d but "
248                 "we issued a request for %d\n", op, expected);
249         return -EIO;
250 }
251
252 /*
253  * CB_COMPOUND4args
254  *
255  *      struct CB_COMPOUND4args {
256  *              utf8str_cs      tag;
257  *              uint32_t        minorversion;
258  *              uint32_t        callback_ident;
259  *              nfs_cb_argop4   argarray<>;
260  *      };
261 */
262 static void encode_cb_compound4args(struct xdr_stream *xdr,
263                                     struct nfs4_cb_compound_hdr *hdr)
264 {
265         __be32 * p;
266
267         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
268         p = xdr_encode_empty_array(p);          /* empty tag */
269         *p++ = cpu_to_be32(hdr->minorversion);
270         *p++ = cpu_to_be32(hdr->ident);
271
272         hdr->nops_p = p;
273         *p = cpu_to_be32(hdr->nops);            /* argarray element count */
274 }
275
276 /*
277  * Update argarray element count
278  */
279 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
280 {
281         BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
282         *hdr->nops_p = cpu_to_be32(hdr->nops);
283 }
284
285 /*
286  * CB_COMPOUND4res
287  *
288  *      struct CB_COMPOUND4res {
289  *              nfsstat4        status;
290  *              utf8str_cs      tag;
291  *              nfs_cb_resop4   resarray<>;
292  *      };
293  */
294 static int decode_cb_compound4res(struct xdr_stream *xdr,
295                                   struct nfs4_cb_compound_hdr *hdr)
296 {
297         u32 length;
298         __be32 *p;
299
300         p = xdr_inline_decode(xdr, 4 + 4);
301         if (unlikely(p == NULL))
302                 goto out_overflow;
303         hdr->status = be32_to_cpup(p++);
304         /* Ignore the tag */
305         length = be32_to_cpup(p++);
306         p = xdr_inline_decode(xdr, length + 4);
307         if (unlikely(p == NULL))
308                 goto out_overflow;
309         hdr->nops = be32_to_cpup(p);
310         return 0;
311 out_overflow:
312         print_overflow_msg(__func__, xdr);
313         return -EIO;
314 }
315
316 /*
317  * CB_RECALL4args
318  *
319  *      struct CB_RECALL4args {
320  *              stateid4        stateid;
321  *              bool            truncate;
322  *              nfs_fh4         fh;
323  *      };
324  */
325 static void encode_cb_recall4args(struct xdr_stream *xdr,
326                                   const struct nfs4_delegation *dp,
327                                   struct nfs4_cb_compound_hdr *hdr)
328 {
329         __be32 *p;
330
331         encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
332         encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
333
334         p = xdr_reserve_space(xdr, 4);
335         *p++ = xdr_zero;                        /* truncate */
336
337         encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
338
339         hdr->nops++;
340 }
341
342 /*
343  * CB_SEQUENCE4args
344  *
345  *      struct CB_SEQUENCE4args {
346  *              sessionid4              csa_sessionid;
347  *              sequenceid4             csa_sequenceid;
348  *              slotid4                 csa_slotid;
349  *              slotid4                 csa_highest_slotid;
350  *              bool                    csa_cachethis;
351  *              referring_call_list4    csa_referring_call_lists<>;
352  *      };
353  */
354 static void encode_cb_sequence4args(struct xdr_stream *xdr,
355                                     const struct nfsd4_callback *cb,
356                                     struct nfs4_cb_compound_hdr *hdr)
357 {
358         struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
359         __be32 *p;
360
361         if (hdr->minorversion == 0)
362                 return;
363
364         encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
365         encode_sessionid4(xdr, session);
366
367         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
368         *p++ = cpu_to_be32(session->se_cb_seq_nr);      /* csa_sequenceid */
369         *p++ = xdr_zero;                        /* csa_slotid */
370         *p++ = xdr_zero;                        /* csa_highest_slotid */
371         *p++ = xdr_zero;                        /* csa_cachethis */
372         xdr_encode_empty_array(p);              /* csa_referring_call_lists */
373
374         hdr->nops++;
375 }
376
377 /*
378  * CB_SEQUENCE4resok
379  *
380  *      struct CB_SEQUENCE4resok {
381  *              sessionid4      csr_sessionid;
382  *              sequenceid4     csr_sequenceid;
383  *              slotid4         csr_slotid;
384  *              slotid4         csr_highest_slotid;
385  *              slotid4         csr_target_highest_slotid;
386  *      };
387  *
388  *      union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
389  *      case NFS4_OK:
390  *              CB_SEQUENCE4resok       csr_resok4;
391  *      default:
392  *              void;
393  *      };
394  *
395  * Our current back channel implmentation supports a single backchannel
396  * with a single slot.
397  */
398 static int decode_cb_sequence4resok(struct xdr_stream *xdr,
399                                     struct nfsd4_callback *cb)
400 {
401         struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
402         struct nfs4_sessionid id;
403         int status;
404         __be32 *p;
405         u32 dummy;
406
407         status = -ESERVERFAULT;
408
409         /*
410          * If the server returns different values for sessionID, slotID or
411          * sequence number, the server is looney tunes.
412          */
413         p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
414         if (unlikely(p == NULL))
415                 goto out_overflow;
416         memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
417         if (memcmp(id.data, session->se_sessionid.data,
418                                         NFS4_MAX_SESSIONID_LEN) != 0) {
419                 dprintk("NFS: %s Invalid session id\n", __func__);
420                 goto out;
421         }
422         p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
423
424         dummy = be32_to_cpup(p++);
425         if (dummy != session->se_cb_seq_nr) {
426                 dprintk("NFS: %s Invalid sequence number\n", __func__);
427                 goto out;
428         }
429
430         dummy = be32_to_cpup(p++);
431         if (dummy != 0) {
432                 dprintk("NFS: %s Invalid slotid\n", __func__);
433                 goto out;
434         }
435
436         /*
437          * FIXME: process highest slotid and target highest slotid
438          */
439         status = 0;
440 out:
441         if (status)
442                 nfsd4_mark_cb_fault(cb->cb_clp, status);
443         return status;
444 out_overflow:
445         print_overflow_msg(__func__, xdr);
446         return -EIO;
447 }
448
449 static int decode_cb_sequence4res(struct xdr_stream *xdr,
450                                   struct nfsd4_callback *cb)
451 {
452         enum nfsstat4 nfserr;
453         int status;
454
455         if (cb->cb_minorversion == 0)
456                 return 0;
457
458         status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
459         if (unlikely(status))
460                 goto out;
461         if (unlikely(nfserr != NFS4_OK))
462                 goto out_default;
463         status = decode_cb_sequence4resok(xdr, cb);
464 out:
465         return status;
466 out_default:
467         return nfs_cb_stat_to_errno(nfserr);
468 }
469
470 /*
471  * NFSv4.0 and NFSv4.1 XDR encode functions
472  *
473  * NFSv4.0 callback argument types are defined in section 15 of RFC
474  * 3530: "Network File System (NFS) version 4 Protocol" and section 20
475  * of RFC 5661:  "Network File System (NFS) Version 4 Minor Version 1
476  * Protocol".
477  */
478
479 /*
480  * NB: Without this zero space reservation, callbacks over krb5p fail
481  */
482 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
483                                  void *__unused)
484 {
485         xdr_reserve_space(xdr, 0);
486 }
487
488 /*
489  * 20.2. Operation 4: CB_RECALL - Recall a Delegation
490  */
491 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
492                                    const struct nfsd4_callback *cb)
493 {
494         const struct nfs4_delegation *dp = to_delegation(cb);
495         struct nfs4_cb_compound_hdr hdr = {
496                 .ident = cb->cb_clp->cl_cb_ident,
497                 .minorversion = cb->cb_minorversion,
498         };
499
500         encode_cb_compound4args(xdr, &hdr);
501         encode_cb_sequence4args(xdr, cb, &hdr);
502         encode_cb_recall4args(xdr, dp, &hdr);
503         encode_cb_nops(&hdr);
504 }
505
506
507 /*
508  * NFSv4.0 and NFSv4.1 XDR decode functions
509  *
510  * NFSv4.0 callback result types are defined in section 15 of RFC
511  * 3530: "Network File System (NFS) version 4 Protocol" and section 20
512  * of RFC 5661:  "Network File System (NFS) Version 4 Minor Version 1
513  * Protocol".
514  */
515
516 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
517                                 void *__unused)
518 {
519         return 0;
520 }
521
522 /*
523  * 20.2. Operation 4: CB_RECALL - Recall a Delegation
524  */
525 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
526                                   struct xdr_stream *xdr,
527                                   struct nfsd4_callback *cb)
528 {
529         struct nfs4_cb_compound_hdr hdr;
530         enum nfsstat4 nfserr;
531         int status;
532
533         status = decode_cb_compound4res(xdr, &hdr);
534         if (unlikely(status))
535                 goto out;
536
537         if (cb != NULL) {
538                 status = decode_cb_sequence4res(xdr, cb);
539                 if (unlikely(status))
540                         goto out;
541         }
542
543         status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
544         if (unlikely(status))
545                 goto out;
546         if (unlikely(nfserr != NFS4_OK))
547                 status = nfs_cb_stat_to_errno(nfserr);
548 out:
549         return status;
550 }
551
552 /*
553  * RPC procedure tables
554  */
555 #define PROC(proc, call, argtype, restype)                              \
556 [NFSPROC4_CLNT_##proc] = {                                              \
557         .p_proc    = NFSPROC4_CB_##call,                                \
558         .p_encode  = (kxdreproc_t)nfs4_xdr_enc_##argtype,               \
559         .p_decode  = (kxdrdproc_t)nfs4_xdr_dec_##restype,               \
560         .p_arglen  = NFS4_enc_##argtype##_sz,                           \
561         .p_replen  = NFS4_dec_##restype##_sz,                           \
562         .p_statidx = NFSPROC4_CB_##call,                                \
563         .p_name    = #proc,                                             \
564 }
565
566 static struct rpc_procinfo nfs4_cb_procedures[] = {
567         PROC(CB_NULL,   NULL,           cb_null,        cb_null),
568         PROC(CB_RECALL, COMPOUND,       cb_recall,      cb_recall),
569 };
570
571 static struct rpc_version nfs_cb_version4 = {
572 /*
573  * Note on the callback rpc program version number: despite language in rfc
574  * 5661 section 18.36.3 requiring servers to use 4 in this field, the
575  * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
576  * in practice that appears to be what implementations use.  The section
577  * 18.36.3 language is expected to be fixed in an erratum.
578  */
579         .number                 = 1,
580         .nrprocs                = ARRAY_SIZE(nfs4_cb_procedures),
581         .procs                  = nfs4_cb_procedures
582 };
583
584 static const struct rpc_version *nfs_cb_version[] = {
585         &nfs_cb_version4,
586 };
587
588 static const struct rpc_program cb_program;
589
590 static struct rpc_stat cb_stats = {
591         .program                = &cb_program
592 };
593
594 #define NFS4_CALLBACK 0x40000000
595 static const struct rpc_program cb_program = {
596         .name                   = "nfs4_cb",
597         .number                 = NFS4_CALLBACK,
598         .nrvers                 = ARRAY_SIZE(nfs_cb_version),
599         .version                = nfs_cb_version,
600         .stats                  = &cb_stats,
601         .pipe_dir_name          = "nfsd4_cb",
602 };
603
604 static int max_cb_time(struct net *net)
605 {
606         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
607         return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
608 }
609
610 static struct rpc_cred *callback_cred;
611
612 int set_callback_cred(void)
613 {
614         if (callback_cred)
615                 return 0;
616         callback_cred = rpc_lookup_machine_cred("nfs");
617         if (!callback_cred)
618                 return -ENOMEM;
619         return 0;
620 }
621
622 static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
623 {
624         if (clp->cl_minorversion == 0) {
625                 return get_rpccred(callback_cred);
626         } else {
627                 struct rpc_auth *auth = client->cl_auth;
628                 struct auth_cred acred = {};
629
630                 acred.uid = ses->se_cb_sec.uid;
631                 acred.gid = ses->se_cb_sec.gid;
632                 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
633         }
634 }
635
636 static struct rpc_clnt *create_backchannel_client(struct rpc_create_args *args)
637 {
638         struct rpc_xprt *xprt;
639
640         if (args->protocol != XPRT_TRANSPORT_BC_TCP)
641                 return rpc_create(args);
642
643         xprt = args->bc_xprt->xpt_bc_xprt;
644         if (xprt) {
645                 xprt_get(xprt);
646                 return rpc_create_xprt(args, xprt);
647         }
648
649         return rpc_create(args);
650 }
651
652 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
653 {
654         int maxtime = max_cb_time(clp->net);
655         struct rpc_timeout      timeparms = {
656                 .to_initval     = maxtime,
657                 .to_retries     = 0,
658                 .to_maxval      = maxtime,
659         };
660         struct rpc_create_args args = {
661                 .net            = clp->net,
662                 .address        = (struct sockaddr *) &conn->cb_addr,
663                 .addrsize       = conn->cb_addrlen,
664                 .saddress       = (struct sockaddr *) &conn->cb_saddr,
665                 .timeout        = &timeparms,
666                 .program        = &cb_program,
667                 .version        = 0,
668                 .flags          = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
669         };
670         struct rpc_clnt *client;
671         struct rpc_cred *cred;
672
673         if (clp->cl_minorversion == 0) {
674                 if (!clp->cl_cred.cr_principal &&
675                                 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
676                         return -EINVAL;
677                 args.client_name = clp->cl_cred.cr_principal;
678                 args.prognumber = conn->cb_prog;
679                 args.protocol = XPRT_TRANSPORT_TCP;
680                 args.authflavor = clp->cl_cred.cr_flavor;
681                 clp->cl_cb_ident = conn->cb_ident;
682         } else {
683                 if (!conn->cb_xprt)
684                         return -EINVAL;
685                 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
686                 clp->cl_cb_session = ses;
687                 args.bc_xprt = conn->cb_xprt;
688                 args.prognumber = clp->cl_cb_session->se_cb_prog;
689                 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
690                                 XPRT_TRANSPORT_BC;
691                 args.authflavor = ses->se_cb_sec.flavor;
692         }
693         /* Create RPC client */
694         client = create_backchannel_client(&args);
695         if (IS_ERR(client)) {
696                 dprintk("NFSD: couldn't create callback client: %ld\n",
697                         PTR_ERR(client));
698                 return PTR_ERR(client);
699         }
700         cred = get_backchannel_cred(clp, client, ses);
701         if (IS_ERR(cred)) {
702                 rpc_shutdown_client(client);
703                 return PTR_ERR(cred);
704         }
705         clp->cl_cb_client = client;
706         clp->cl_cb_cred = cred;
707         return 0;
708 }
709
710 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
711 {
712         dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
713                 (int)clp->cl_name.len, clp->cl_name.data, reason);
714 }
715
716 static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
717 {
718         clp->cl_cb_state = NFSD4_CB_DOWN;
719         warn_no_callback_path(clp, reason);
720 }
721
722 static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
723 {
724         clp->cl_cb_state = NFSD4_CB_FAULT;
725         warn_no_callback_path(clp, reason);
726 }
727
728 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
729 {
730         struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
731
732         if (task->tk_status)
733                 nfsd4_mark_cb_down(clp, task->tk_status);
734         else
735                 clp->cl_cb_state = NFSD4_CB_UP;
736 }
737
738 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
739         /* XXX: release method to ensure we set the cb channel down if
740          * necessary on early failure? */
741         .rpc_call_done = nfsd4_cb_probe_done,
742 };
743
744 static struct workqueue_struct *callback_wq;
745
746 static void do_probe_callback(struct nfs4_client *clp)
747 {
748         return nfsd4_cb(&clp->cl_cb_null, clp, NFSPROC4_CLNT_CB_NULL);
749 }
750
751 /*
752  * Poke the callback thread to process any updates to the callback
753  * parameters, and send a null probe.
754  */
755 void nfsd4_probe_callback(struct nfs4_client *clp)
756 {
757         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
758         set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
759         do_probe_callback(clp);
760 }
761
762 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
763 {
764         nfsd4_probe_callback(clp);
765         flush_workqueue(callback_wq);
766 }
767
768 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
769 {
770         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
771         spin_lock(&clp->cl_lock);
772         memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
773         spin_unlock(&clp->cl_lock);
774 }
775
776 /*
777  * There's currently a single callback channel slot.
778  * If the slot is available, then mark it busy.  Otherwise, set the
779  * thread for sleeping on the callback RPC wait queue.
780  */
781 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
782 {
783         if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
784                 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
785                 dprintk("%s slot is busy\n", __func__);
786                 return false;
787         }
788         return true;
789 }
790
791 /*
792  * TODO: cb_sequence should support referring call lists, cachethis, multiple
793  * slots, and mark callback channel down on communication errors.
794  */
795 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
796 {
797         struct nfsd4_callback *cb = calldata;
798         struct nfs4_client *clp = cb->cb_clp;
799         u32 minorversion = clp->cl_minorversion;
800
801         cb->cb_minorversion = minorversion;
802         if (minorversion) {
803                 if (!nfsd41_cb_get_slot(clp, task))
804                         return;
805         }
806         spin_lock(&clp->cl_lock);
807         if (list_empty(&cb->cb_per_client)) {
808                 /* This is the first call, not a restart */
809                 cb->cb_done = false;
810                 list_add(&cb->cb_per_client, &clp->cl_callbacks);
811         }
812         spin_unlock(&clp->cl_lock);
813         rpc_call_start(task);
814 }
815
816 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
817 {
818         struct nfsd4_callback *cb = calldata;
819         struct nfs4_client *clp = cb->cb_clp;
820
821         dprintk("%s: minorversion=%d\n", __func__,
822                 clp->cl_minorversion);
823
824         if (clp->cl_minorversion) {
825                 /* No need for lock, access serialized in nfsd4_cb_prepare */
826                 ++clp->cl_cb_session->se_cb_seq_nr;
827                 clear_bit(0, &clp->cl_cb_slot_busy);
828                 rpc_wake_up_next(&clp->cl_cb_waitq);
829                 dprintk("%s: freed slot, new seqid=%d\n", __func__,
830                         clp->cl_cb_session->se_cb_seq_nr);
831         }
832 }
833
834 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
835 {
836         struct nfsd4_callback *cb = calldata;
837         struct nfs4_delegation *dp = to_delegation(cb);
838         struct nfs4_client *clp = cb->cb_clp;
839         struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
840
841         nfsd4_cb_done(task, calldata);
842
843         if (current_rpc_client != task->tk_client) {
844                 /* We're shutting down or changing cl_cb_client; leave
845                  * it to nfsd4_process_cb_update to restart the call if
846                  * necessary. */
847                 return;
848         }
849
850         if (cb->cb_done)
851                 return;
852         switch (task->tk_status) {
853         case 0:
854                 break;
855         case -EBADHANDLE:
856         case -NFS4ERR_BAD_STATEID:
857                 /* Race: client probably got cb_recall
858                  * before open reply granting delegation */
859                 if (dp->dl_retries--) {
860                         rpc_delay(task, 2*HZ);
861                         task->tk_status = 0;
862                         rpc_restart_call_prepare(task);
863                         return;
864                 }
865         default:
866                 /* Network partition? */
867                 nfsd4_mark_cb_down(clp, task->tk_status);
868         }
869         cb->cb_done = true;
870 }
871
872 static void nfsd4_cb_recall_release(void *calldata)
873 {
874         struct nfsd4_callback *cb = calldata;
875         struct nfs4_client *clp = cb->cb_clp;
876
877         if (cb->cb_done) {
878                 struct nfs4_delegation *dp = to_delegation(cb);
879
880                 spin_lock(&clp->cl_lock);
881                 list_del(&cb->cb_per_client);
882                 spin_unlock(&clp->cl_lock);
883                 nfs4_put_stid(&dp->dl_stid);
884         }
885 }
886
887 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
888         .rpc_call_prepare = nfsd4_cb_prepare,
889         .rpc_call_done = nfsd4_cb_recall_done,
890         .rpc_release = nfsd4_cb_recall_release,
891 };
892
893 int nfsd4_create_callback_queue(void)
894 {
895         callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
896         if (!callback_wq)
897                 return -ENOMEM;
898         return 0;
899 }
900
901 void nfsd4_destroy_callback_queue(void)
902 {
903         destroy_workqueue(callback_wq);
904 }
905
906 /* must be called under the state lock */
907 void nfsd4_shutdown_callback(struct nfs4_client *clp)
908 {
909         set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
910         /*
911          * Note this won't actually result in a null callback;
912          * instead, nfsd4_run_cb_null() will detect the killed
913          * client, destroy the rpc client, and stop:
914          */
915         do_probe_callback(clp);
916         flush_workqueue(callback_wq);
917 }
918
919 static void nfsd4_release_cb(struct nfsd4_callback *cb)
920 {
921         if (cb->cb_ops->rpc_release)
922                 cb->cb_ops->rpc_release(cb);
923 }
924
925 /* requires cl_lock: */
926 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
927 {
928         struct nfsd4_session *s;
929         struct nfsd4_conn *c;
930
931         list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
932                 list_for_each_entry(c, &s->se_conns, cn_persession) {
933                         if (c->cn_flags & NFS4_CDFC4_BACK)
934                                 return c;
935                 }
936         }
937         return NULL;
938 }
939
940 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
941 {
942         struct nfs4_cb_conn conn;
943         struct nfs4_client *clp = cb->cb_clp;
944         struct nfsd4_session *ses = NULL;
945         struct nfsd4_conn *c;
946         int err;
947
948         /*
949          * This is either an update, or the client dying; in either case,
950          * kill the old client:
951          */
952         if (clp->cl_cb_client) {
953                 rpc_shutdown_client(clp->cl_cb_client);
954                 clp->cl_cb_client = NULL;
955                 put_rpccred(clp->cl_cb_cred);
956                 clp->cl_cb_cred = NULL;
957         }
958         if (clp->cl_cb_conn.cb_xprt) {
959                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
960                 clp->cl_cb_conn.cb_xprt = NULL;
961         }
962         if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
963                 return;
964         spin_lock(&clp->cl_lock);
965         /*
966          * Only serialized callback code is allowed to clear these
967          * flags; main nfsd code can only set them:
968          */
969         BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
970         clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
971         memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
972         c = __nfsd4_find_backchannel(clp);
973         if (c) {
974                 svc_xprt_get(c->cn_xprt);
975                 conn.cb_xprt = c->cn_xprt;
976                 ses = c->cn_session;
977         }
978         spin_unlock(&clp->cl_lock);
979
980         err = setup_callback_client(clp, &conn, ses);
981         if (err) {
982                 nfsd4_mark_cb_down(clp, err);
983                 return;
984         }
985         /* Yay, the callback channel's back! Restart any callbacks: */
986         list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
987                 queue_work(callback_wq, &cb->cb_work);
988 }
989
990 static void
991 nfsd4_run_callback_rpc(struct nfsd4_callback *cb)
992 {
993         struct nfs4_client *clp = cb->cb_clp;
994         struct rpc_clnt *clnt;
995
996         if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
997                 nfsd4_process_cb_update(cb);
998
999         clnt = clp->cl_cb_client;
1000         if (!clnt) {
1001                 /* Callback channel broken, or client killed; give up: */
1002                 nfsd4_release_cb(cb);
1003                 return;
1004         }
1005         cb->cb_msg.rpc_cred = clp->cl_cb_cred;
1006         rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1007                         cb->cb_ops, cb);
1008 }
1009
1010 void
1011 nfsd4_run_cb_null(struct work_struct *w)
1012 {
1013         struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1014                                                         cb_work);
1015         nfsd4_run_callback_rpc(cb);
1016 }
1017
1018 void
1019 nfsd4_run_cb_recall(struct work_struct *w)
1020 {
1021         struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1022                                                         cb_work);
1023
1024         nfsd4_prepare_cb_recall(to_delegation(cb));
1025         nfsd4_run_callback_rpc(cb);
1026 }
1027
1028 void nfsd4_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
1029                 enum nfsd4_cb_op op)
1030 {
1031         cb->cb_clp = clp;
1032         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
1033         cb->cb_msg.rpc_argp = cb;
1034         cb->cb_msg.rpc_resp = cb;
1035         if (op == NFSPROC4_CLNT_CB_NULL)
1036                 cb->cb_ops = &nfsd4_cb_probe_ops;
1037         else
1038                 cb->cb_ops = &nfsd4_cb_recall_ops;
1039         INIT_LIST_HEAD(&cb->cb_per_client);
1040         cb->cb_done = true;
1041
1042         queue_work(callback_wq, &cb->cb_work);
1043 }