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