Linux 6.9-rc5
[sfrench/cifs-2.6.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/nfs4.h>
35 #include <linux/sunrpc/clnt.h>
36 #include <linux/sunrpc/xprt.h>
37 #include <linux/sunrpc/svc_xprt.h>
38 #include <linux/slab.h>
39 #include "nfsd.h"
40 #include "state.h"
41 #include "netns.h"
42 #include "trace.h"
43 #include "xdr4cb.h"
44 #include "xdr4.h"
45
46 #define NFSDDBG_FACILITY                NFSDDBG_PROC
47
48 static void nfsd4_mark_cb_fault(struct nfs4_client *clp);
49
50 #define NFSPROC4_CB_NULL 0
51 #define NFSPROC4_CB_COMPOUND 1
52
53 /* Index of predefined Linux callback client operations */
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 static __be32 *xdr_encode_empty_array(__be32 *p)
66 {
67         *p++ = xdr_zero;
68         return p;
69 }
70
71 /*
72  * Encode/decode NFSv4 CB basic data types
73  *
74  * Basic NFSv4 callback data types are defined in section 15 of RFC
75  * 3530: "Network File System (NFS) version 4 Protocol" and section
76  * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
77  * 1 Protocol"
78  */
79
80 static void encode_uint32(struct xdr_stream *xdr, u32 n)
81 {
82         WARN_ON_ONCE(xdr_stream_encode_u32(xdr, n) < 0);
83 }
84
85 static void encode_bitmap4(struct xdr_stream *xdr, const __u32 *bitmap,
86                            size_t len)
87 {
88         xdr_stream_encode_uint32_array(xdr, bitmap, len);
89 }
90
91 static int decode_cb_fattr4(struct xdr_stream *xdr, uint32_t *bitmap,
92                                 struct nfs4_cb_fattr *fattr)
93 {
94         fattr->ncf_cb_change = 0;
95         fattr->ncf_cb_fsize = 0;
96         if (bitmap[0] & FATTR4_WORD0_CHANGE)
97                 if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_change) < 0)
98                         return -NFSERR_BAD_XDR;
99         if (bitmap[0] & FATTR4_WORD0_SIZE)
100                 if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_fsize) < 0)
101                         return -NFSERR_BAD_XDR;
102         return 0;
103 }
104
105 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
106 {
107         __be32 *p;
108
109         p = xdr_reserve_space(xdr, 4);
110         *p = cpu_to_be32(op);
111 }
112
113 /*
114  * nfs_fh4
115  *
116  *      typedef opaque nfs_fh4<NFS4_FHSIZE>;
117  */
118 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
119 {
120         u32 length = fh->fh_size;
121         __be32 *p;
122
123         BUG_ON(length > NFS4_FHSIZE);
124         p = xdr_reserve_space(xdr, 4 + length);
125         xdr_encode_opaque(p, &fh->fh_raw, length);
126 }
127
128 /*
129  * stateid4
130  *
131  *      struct stateid4 {
132  *              uint32_t        seqid;
133  *              opaque          other[12];
134  *      };
135  */
136 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
137 {
138         __be32 *p;
139
140         p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
141         *p++ = cpu_to_be32(sid->si_generation);
142         xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
143 }
144
145 /*
146  * sessionid4
147  *
148  *      typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
149  */
150 static void encode_sessionid4(struct xdr_stream *xdr,
151                               const struct nfsd4_session *session)
152 {
153         __be32 *p;
154
155         p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
156         xdr_encode_opaque_fixed(p, session->se_sessionid.data,
157                                         NFS4_MAX_SESSIONID_LEN);
158 }
159
160 /*
161  * nfsstat4
162  */
163 static const struct {
164         int stat;
165         int errno;
166 } nfs_cb_errtbl[] = {
167         { NFS4_OK,              0               },
168         { NFS4ERR_PERM,         -EPERM          },
169         { NFS4ERR_NOENT,        -ENOENT         },
170         { NFS4ERR_IO,           -EIO            },
171         { NFS4ERR_NXIO,         -ENXIO          },
172         { NFS4ERR_ACCESS,       -EACCES         },
173         { NFS4ERR_EXIST,        -EEXIST         },
174         { NFS4ERR_XDEV,         -EXDEV          },
175         { NFS4ERR_NOTDIR,       -ENOTDIR        },
176         { NFS4ERR_ISDIR,        -EISDIR         },
177         { NFS4ERR_INVAL,        -EINVAL         },
178         { NFS4ERR_FBIG,         -EFBIG          },
179         { NFS4ERR_NOSPC,        -ENOSPC         },
180         { NFS4ERR_ROFS,         -EROFS          },
181         { NFS4ERR_MLINK,        -EMLINK         },
182         { NFS4ERR_NAMETOOLONG,  -ENAMETOOLONG   },
183         { NFS4ERR_NOTEMPTY,     -ENOTEMPTY      },
184         { NFS4ERR_DQUOT,        -EDQUOT         },
185         { NFS4ERR_STALE,        -ESTALE         },
186         { NFS4ERR_BADHANDLE,    -EBADHANDLE     },
187         { NFS4ERR_BAD_COOKIE,   -EBADCOOKIE     },
188         { NFS4ERR_NOTSUPP,      -ENOTSUPP       },
189         { NFS4ERR_TOOSMALL,     -ETOOSMALL      },
190         { NFS4ERR_SERVERFAULT,  -ESERVERFAULT   },
191         { NFS4ERR_BADTYPE,      -EBADTYPE       },
192         { NFS4ERR_LOCKED,       -EAGAIN         },
193         { NFS4ERR_RESOURCE,     -EREMOTEIO      },
194         { NFS4ERR_SYMLINK,      -ELOOP          },
195         { NFS4ERR_OP_ILLEGAL,   -EOPNOTSUPP     },
196         { NFS4ERR_DEADLOCK,     -EDEADLK        },
197         { -1,                   -EIO            }
198 };
199
200 /*
201  * If we cannot translate the error, the recovery routines should
202  * handle it.
203  *
204  * Note: remaining NFSv4 error codes have values > 10000, so should
205  * not conflict with native Linux error codes.
206  */
207 static int nfs_cb_stat_to_errno(int status)
208 {
209         int i;
210
211         for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
212                 if (nfs_cb_errtbl[i].stat == status)
213                         return nfs_cb_errtbl[i].errno;
214         }
215
216         dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
217         return -status;
218 }
219
220 static int decode_cb_op_status(struct xdr_stream *xdr,
221                                enum nfs_cb_opnum4 expected, int *status)
222 {
223         __be32 *p;
224         u32 op;
225
226         p = xdr_inline_decode(xdr, 4 + 4);
227         if (unlikely(p == NULL))
228                 goto out_overflow;
229         op = be32_to_cpup(p++);
230         if (unlikely(op != expected))
231                 goto out_unexpected;
232         *status = nfs_cb_stat_to_errno(be32_to_cpup(p));
233         return 0;
234 out_overflow:
235         return -EIO;
236 out_unexpected:
237         dprintk("NFSD: Callback server returned operation %d but "
238                 "we issued a request for %d\n", op, expected);
239         return -EIO;
240 }
241
242 /*
243  * CB_COMPOUND4args
244  *
245  *      struct CB_COMPOUND4args {
246  *              utf8str_cs      tag;
247  *              uint32_t        minorversion;
248  *              uint32_t        callback_ident;
249  *              nfs_cb_argop4   argarray<>;
250  *      };
251 */
252 static void encode_cb_compound4args(struct xdr_stream *xdr,
253                                     struct nfs4_cb_compound_hdr *hdr)
254 {
255         __be32 * p;
256
257         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
258         p = xdr_encode_empty_array(p);          /* empty tag */
259         *p++ = cpu_to_be32(hdr->minorversion);
260         *p++ = cpu_to_be32(hdr->ident);
261
262         hdr->nops_p = p;
263         *p = cpu_to_be32(hdr->nops);            /* argarray element count */
264 }
265
266 /*
267  * Update argarray element count
268  */
269 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
270 {
271         BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
272         *hdr->nops_p = cpu_to_be32(hdr->nops);
273 }
274
275 /*
276  * CB_COMPOUND4res
277  *
278  *      struct CB_COMPOUND4res {
279  *              nfsstat4        status;
280  *              utf8str_cs      tag;
281  *              nfs_cb_resop4   resarray<>;
282  *      };
283  */
284 static int decode_cb_compound4res(struct xdr_stream *xdr,
285                                   struct nfs4_cb_compound_hdr *hdr)
286 {
287         u32 length;
288         __be32 *p;
289
290         p = xdr_inline_decode(xdr, 4 + 4);
291         if (unlikely(p == NULL))
292                 goto out_overflow;
293         hdr->status = be32_to_cpup(p++);
294         /* Ignore the tag */
295         length = be32_to_cpup(p++);
296         p = xdr_inline_decode(xdr, length + 4);
297         if (unlikely(p == NULL))
298                 goto out_overflow;
299         p += XDR_QUADLEN(length);
300         hdr->nops = be32_to_cpup(p);
301         return 0;
302 out_overflow:
303         return -EIO;
304 }
305
306 /*
307  * CB_RECALL4args
308  *
309  *      struct CB_RECALL4args {
310  *              stateid4        stateid;
311  *              bool            truncate;
312  *              nfs_fh4         fh;
313  *      };
314  */
315 static void encode_cb_recall4args(struct xdr_stream *xdr,
316                                   const struct nfs4_delegation *dp,
317                                   struct nfs4_cb_compound_hdr *hdr)
318 {
319         __be32 *p;
320
321         encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
322         encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
323
324         p = xdr_reserve_space(xdr, 4);
325         *p++ = xdr_zero;                        /* truncate */
326
327         encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
328
329         hdr->nops++;
330 }
331
332 /*
333  * CB_RECALLANY4args
334  *
335  *      struct CB_RECALLANY4args {
336  *              uint32_t        craa_objects_to_keep;
337  *              bitmap4         craa_type_mask;
338  *      };
339  */
340 static void
341 encode_cb_recallany4args(struct xdr_stream *xdr,
342         struct nfs4_cb_compound_hdr *hdr, struct nfsd4_cb_recall_any *ra)
343 {
344         encode_nfs_cb_opnum4(xdr, OP_CB_RECALL_ANY);
345         encode_uint32(xdr, ra->ra_keep);
346         encode_bitmap4(xdr, ra->ra_bmval, ARRAY_SIZE(ra->ra_bmval));
347         hdr->nops++;
348 }
349
350 /*
351  * CB_GETATTR4args
352  *      struct CB_GETATTR4args {
353  *         nfs_fh4 fh;
354  *         bitmap4 attr_request;
355  *      };
356  *
357  * The size and change attributes are the only one
358  * guaranteed to be serviced by the client.
359  */
360 static void
361 encode_cb_getattr4args(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr,
362                         struct nfs4_cb_fattr *fattr)
363 {
364         struct nfs4_delegation *dp =
365                 container_of(fattr, struct nfs4_delegation, dl_cb_fattr);
366         struct knfsd_fh *fh = &dp->dl_stid.sc_file->fi_fhandle;
367
368         encode_nfs_cb_opnum4(xdr, OP_CB_GETATTR);
369         encode_nfs_fh4(xdr, fh);
370         encode_bitmap4(xdr, fattr->ncf_cb_bmap, ARRAY_SIZE(fattr->ncf_cb_bmap));
371         hdr->nops++;
372 }
373
374 /*
375  * CB_SEQUENCE4args
376  *
377  *      struct CB_SEQUENCE4args {
378  *              sessionid4              csa_sessionid;
379  *              sequenceid4             csa_sequenceid;
380  *              slotid4                 csa_slotid;
381  *              slotid4                 csa_highest_slotid;
382  *              bool                    csa_cachethis;
383  *              referring_call_list4    csa_referring_call_lists<>;
384  *      };
385  */
386 static void encode_cb_sequence4args(struct xdr_stream *xdr,
387                                     const struct nfsd4_callback *cb,
388                                     struct nfs4_cb_compound_hdr *hdr)
389 {
390         struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
391         __be32 *p;
392
393         if (hdr->minorversion == 0)
394                 return;
395
396         encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
397         encode_sessionid4(xdr, session);
398
399         p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
400         *p++ = cpu_to_be32(session->se_cb_seq_nr);      /* csa_sequenceid */
401         *p++ = xdr_zero;                        /* csa_slotid */
402         *p++ = xdr_zero;                        /* csa_highest_slotid */
403         *p++ = xdr_zero;                        /* csa_cachethis */
404         xdr_encode_empty_array(p);              /* csa_referring_call_lists */
405
406         hdr->nops++;
407 }
408
409 /*
410  * CB_SEQUENCE4resok
411  *
412  *      struct CB_SEQUENCE4resok {
413  *              sessionid4      csr_sessionid;
414  *              sequenceid4     csr_sequenceid;
415  *              slotid4         csr_slotid;
416  *              slotid4         csr_highest_slotid;
417  *              slotid4         csr_target_highest_slotid;
418  *      };
419  *
420  *      union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
421  *      case NFS4_OK:
422  *              CB_SEQUENCE4resok       csr_resok4;
423  *      default:
424  *              void;
425  *      };
426  *
427  * Our current back channel implmentation supports a single backchannel
428  * with a single slot.
429  */
430 static int decode_cb_sequence4resok(struct xdr_stream *xdr,
431                                     struct nfsd4_callback *cb)
432 {
433         struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
434         int status = -ESERVERFAULT;
435         __be32 *p;
436         u32 dummy;
437
438         /*
439          * If the server returns different values for sessionID, slotID or
440          * sequence number, the server is looney tunes.
441          */
442         p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
443         if (unlikely(p == NULL))
444                 goto out_overflow;
445
446         if (memcmp(p, session->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
447                 dprintk("NFS: %s Invalid session id\n", __func__);
448                 goto out;
449         }
450         p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
451
452         dummy = be32_to_cpup(p++);
453         if (dummy != session->se_cb_seq_nr) {
454                 dprintk("NFS: %s Invalid sequence number\n", __func__);
455                 goto out;
456         }
457
458         dummy = be32_to_cpup(p++);
459         if (dummy != 0) {
460                 dprintk("NFS: %s Invalid slotid\n", __func__);
461                 goto out;
462         }
463
464         /*
465          * FIXME: process highest slotid and target highest slotid
466          */
467         status = 0;
468 out:
469         cb->cb_seq_status = status;
470         return status;
471 out_overflow:
472         status = -EIO;
473         goto out;
474 }
475
476 static int decode_cb_sequence4res(struct xdr_stream *xdr,
477                                   struct nfsd4_callback *cb)
478 {
479         int status;
480
481         if (cb->cb_clp->cl_minorversion == 0)
482                 return 0;
483
484         status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &cb->cb_seq_status);
485         if (unlikely(status || cb->cb_seq_status))
486                 return status;
487
488         return decode_cb_sequence4resok(xdr, cb);
489 }
490
491 /*
492  * NFSv4.0 and NFSv4.1 XDR encode functions
493  *
494  * NFSv4.0 callback argument types are defined in section 15 of RFC
495  * 3530: "Network File System (NFS) version 4 Protocol" and section 20
496  * of RFC 5661:  "Network File System (NFS) Version 4 Minor Version 1
497  * Protocol".
498  */
499
500 /*
501  * NB: Without this zero space reservation, callbacks over krb5p fail
502  */
503 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
504                                  const void *__unused)
505 {
506         xdr_reserve_space(xdr, 0);
507 }
508
509 /*
510  * 20.1.  Operation 3: CB_GETATTR - Get Attributes
511  */
512 static void nfs4_xdr_enc_cb_getattr(struct rpc_rqst *req,
513                 struct xdr_stream *xdr, const void *data)
514 {
515         const struct nfsd4_callback *cb = data;
516         struct nfs4_cb_fattr *ncf =
517                 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
518         struct nfs4_cb_compound_hdr hdr = {
519                 .ident = cb->cb_clp->cl_cb_ident,
520                 .minorversion = cb->cb_clp->cl_minorversion,
521         };
522
523         encode_cb_compound4args(xdr, &hdr);
524         encode_cb_sequence4args(xdr, cb, &hdr);
525         encode_cb_getattr4args(xdr, &hdr, ncf);
526         encode_cb_nops(&hdr);
527 }
528
529 /*
530  * 20.2. Operation 4: CB_RECALL - Recall a Delegation
531  */
532 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
533                                    const void *data)
534 {
535         const struct nfsd4_callback *cb = data;
536         const struct nfs4_delegation *dp = cb_to_delegation(cb);
537         struct nfs4_cb_compound_hdr hdr = {
538                 .ident = cb->cb_clp->cl_cb_ident,
539                 .minorversion = cb->cb_clp->cl_minorversion,
540         };
541
542         encode_cb_compound4args(xdr, &hdr);
543         encode_cb_sequence4args(xdr, cb, &hdr);
544         encode_cb_recall4args(xdr, dp, &hdr);
545         encode_cb_nops(&hdr);
546 }
547
548 /*
549  * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects
550  */
551 static void
552 nfs4_xdr_enc_cb_recall_any(struct rpc_rqst *req,
553                 struct xdr_stream *xdr, const void *data)
554 {
555         const struct nfsd4_callback *cb = data;
556         struct nfsd4_cb_recall_any *ra;
557         struct nfs4_cb_compound_hdr hdr = {
558                 .ident = cb->cb_clp->cl_cb_ident,
559                 .minorversion = cb->cb_clp->cl_minorversion,
560         };
561
562         ra = container_of(cb, struct nfsd4_cb_recall_any, ra_cb);
563         encode_cb_compound4args(xdr, &hdr);
564         encode_cb_sequence4args(xdr, cb, &hdr);
565         encode_cb_recallany4args(xdr, &hdr, ra);
566         encode_cb_nops(&hdr);
567 }
568
569 /*
570  * NFSv4.0 and NFSv4.1 XDR decode functions
571  *
572  * NFSv4.0 callback result types are defined in section 15 of RFC
573  * 3530: "Network File System (NFS) version 4 Protocol" and section 20
574  * of RFC 5661:  "Network File System (NFS) Version 4 Minor Version 1
575  * Protocol".
576  */
577
578 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
579                                 void *__unused)
580 {
581         return 0;
582 }
583
584 /*
585  * 20.1.  Operation 3: CB_GETATTR - Get Attributes
586  */
587 static int nfs4_xdr_dec_cb_getattr(struct rpc_rqst *rqstp,
588                                   struct xdr_stream *xdr,
589                                   void *data)
590 {
591         struct nfsd4_callback *cb = data;
592         struct nfs4_cb_compound_hdr hdr;
593         int status;
594         u32 bitmap[3] = {0};
595         u32 attrlen;
596         struct nfs4_cb_fattr *ncf =
597                 container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
598
599         status = decode_cb_compound4res(xdr, &hdr);
600         if (unlikely(status))
601                 return status;
602
603         status = decode_cb_sequence4res(xdr, cb);
604         if (unlikely(status || cb->cb_seq_status))
605                 return status;
606
607         status = decode_cb_op_status(xdr, OP_CB_GETATTR, &cb->cb_status);
608         if (status)
609                 return status;
610         if (xdr_stream_decode_uint32_array(xdr, bitmap, 3) < 0)
611                 return -NFSERR_BAD_XDR;
612         if (xdr_stream_decode_u32(xdr, &attrlen) < 0)
613                 return -NFSERR_BAD_XDR;
614         if (attrlen > (sizeof(ncf->ncf_cb_change) + sizeof(ncf->ncf_cb_fsize)))
615                 return -NFSERR_BAD_XDR;
616         status = decode_cb_fattr4(xdr, bitmap, ncf);
617         return status;
618 }
619
620 /*
621  * 20.2. Operation 4: CB_RECALL - Recall a Delegation
622  */
623 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
624                                   struct xdr_stream *xdr,
625                                   void *data)
626 {
627         struct nfsd4_callback *cb = data;
628         struct nfs4_cb_compound_hdr hdr;
629         int status;
630
631         status = decode_cb_compound4res(xdr, &hdr);
632         if (unlikely(status))
633                 return status;
634
635         status = decode_cb_sequence4res(xdr, cb);
636         if (unlikely(status || cb->cb_seq_status))
637                 return status;
638
639         return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
640 }
641
642 /*
643  * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects
644  */
645 static int
646 nfs4_xdr_dec_cb_recall_any(struct rpc_rqst *rqstp,
647                                   struct xdr_stream *xdr,
648                                   void *data)
649 {
650         struct nfsd4_callback *cb = data;
651         struct nfs4_cb_compound_hdr hdr;
652         int status;
653
654         status = decode_cb_compound4res(xdr, &hdr);
655         if (unlikely(status))
656                 return status;
657         status = decode_cb_sequence4res(xdr, cb);
658         if (unlikely(status || cb->cb_seq_status))
659                 return status;
660         status =  decode_cb_op_status(xdr, OP_CB_RECALL_ANY, &cb->cb_status);
661         return status;
662 }
663
664 #ifdef CONFIG_NFSD_PNFS
665 /*
666  * CB_LAYOUTRECALL4args
667  *
668  *      struct layoutrecall_file4 {
669  *              nfs_fh4         lor_fh;
670  *              offset4         lor_offset;
671  *              length4         lor_length;
672  *              stateid4        lor_stateid;
673  *      };
674  *
675  *      union layoutrecall4 switch(layoutrecall_type4 lor_recalltype) {
676  *      case LAYOUTRECALL4_FILE:
677  *              layoutrecall_file4 lor_layout;
678  *      case LAYOUTRECALL4_FSID:
679  *              fsid4              lor_fsid;
680  *      case LAYOUTRECALL4_ALL:
681  *              void;
682  *      };
683  *
684  *      struct CB_LAYOUTRECALL4args {
685  *              layouttype4             clora_type;
686  *              layoutiomode4           clora_iomode;
687  *              bool                    clora_changed;
688  *              layoutrecall4           clora_recall;
689  *      };
690  */
691 static void encode_cb_layout4args(struct xdr_stream *xdr,
692                                   const struct nfs4_layout_stateid *ls,
693                                   struct nfs4_cb_compound_hdr *hdr)
694 {
695         __be32 *p;
696
697         BUG_ON(hdr->minorversion == 0);
698
699         p = xdr_reserve_space(xdr, 5 * 4);
700         *p++ = cpu_to_be32(OP_CB_LAYOUTRECALL);
701         *p++ = cpu_to_be32(ls->ls_layout_type);
702         *p++ = cpu_to_be32(IOMODE_ANY);
703         *p++ = cpu_to_be32(1);
704         *p = cpu_to_be32(RETURN_FILE);
705
706         encode_nfs_fh4(xdr, &ls->ls_stid.sc_file->fi_fhandle);
707
708         p = xdr_reserve_space(xdr, 2 * 8);
709         p = xdr_encode_hyper(p, 0);
710         xdr_encode_hyper(p, NFS4_MAX_UINT64);
711
712         encode_stateid4(xdr, &ls->ls_recall_sid);
713
714         hdr->nops++;
715 }
716
717 static void nfs4_xdr_enc_cb_layout(struct rpc_rqst *req,
718                                    struct xdr_stream *xdr,
719                                    const void *data)
720 {
721         const struct nfsd4_callback *cb = data;
722         const struct nfs4_layout_stateid *ls =
723                 container_of(cb, struct nfs4_layout_stateid, ls_recall);
724         struct nfs4_cb_compound_hdr hdr = {
725                 .ident = 0,
726                 .minorversion = cb->cb_clp->cl_minorversion,
727         };
728
729         encode_cb_compound4args(xdr, &hdr);
730         encode_cb_sequence4args(xdr, cb, &hdr);
731         encode_cb_layout4args(xdr, ls, &hdr);
732         encode_cb_nops(&hdr);
733 }
734
735 static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
736                                   struct xdr_stream *xdr,
737                                   void *data)
738 {
739         struct nfsd4_callback *cb = data;
740         struct nfs4_cb_compound_hdr hdr;
741         int status;
742
743         status = decode_cb_compound4res(xdr, &hdr);
744         if (unlikely(status))
745                 return status;
746
747         status = decode_cb_sequence4res(xdr, cb);
748         if (unlikely(status || cb->cb_seq_status))
749                 return status;
750
751         return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
752 }
753 #endif /* CONFIG_NFSD_PNFS */
754
755 static void encode_stateowner(struct xdr_stream *xdr, struct nfs4_stateowner *so)
756 {
757         __be32  *p;
758
759         p = xdr_reserve_space(xdr, 8 + 4 + so->so_owner.len);
760         p = xdr_encode_opaque_fixed(p, &so->so_client->cl_clientid, 8);
761         xdr_encode_opaque(p, so->so_owner.data, so->so_owner.len);
762 }
763
764 static void nfs4_xdr_enc_cb_notify_lock(struct rpc_rqst *req,
765                                         struct xdr_stream *xdr,
766                                         const void *data)
767 {
768         const struct nfsd4_callback *cb = data;
769         const struct nfsd4_blocked_lock *nbl =
770                 container_of(cb, struct nfsd4_blocked_lock, nbl_cb);
771         struct nfs4_lockowner *lo = (struct nfs4_lockowner *)nbl->nbl_lock.c.flc_owner;
772         struct nfs4_cb_compound_hdr hdr = {
773                 .ident = 0,
774                 .minorversion = cb->cb_clp->cl_minorversion,
775         };
776
777         __be32 *p;
778
779         BUG_ON(hdr.minorversion == 0);
780
781         encode_cb_compound4args(xdr, &hdr);
782         encode_cb_sequence4args(xdr, cb, &hdr);
783
784         p = xdr_reserve_space(xdr, 4);
785         *p = cpu_to_be32(OP_CB_NOTIFY_LOCK);
786         encode_nfs_fh4(xdr, &nbl->nbl_fh);
787         encode_stateowner(xdr, &lo->lo_owner);
788         hdr.nops++;
789
790         encode_cb_nops(&hdr);
791 }
792
793 static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
794                                         struct xdr_stream *xdr,
795                                         void *data)
796 {
797         struct nfsd4_callback *cb = data;
798         struct nfs4_cb_compound_hdr hdr;
799         int status;
800
801         status = decode_cb_compound4res(xdr, &hdr);
802         if (unlikely(status))
803                 return status;
804
805         status = decode_cb_sequence4res(xdr, cb);
806         if (unlikely(status || cb->cb_seq_status))
807                 return status;
808
809         return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
810 }
811
812 /*
813  * struct write_response4 {
814  *      stateid4        wr_callback_id<1>;
815  *      length4         wr_count;
816  *      stable_how4     wr_committed;
817  *      verifier4       wr_writeverf;
818  * };
819  * union offload_info4 switch (nfsstat4 coa_status) {
820  *      case NFS4_OK:
821  *              write_response4 coa_resok4;
822  *      default:
823  *              length4         coa_bytes_copied;
824  * };
825  * struct CB_OFFLOAD4args {
826  *      nfs_fh4         coa_fh;
827  *      stateid4        coa_stateid;
828  *      offload_info4   coa_offload_info;
829  * };
830  */
831 static void encode_offload_info4(struct xdr_stream *xdr,
832                                  const struct nfsd4_cb_offload *cbo)
833 {
834         __be32 *p;
835
836         p = xdr_reserve_space(xdr, 4);
837         *p = cbo->co_nfserr;
838         switch (cbo->co_nfserr) {
839         case nfs_ok:
840                 p = xdr_reserve_space(xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE);
841                 p = xdr_encode_empty_array(p);
842                 p = xdr_encode_hyper(p, cbo->co_res.wr_bytes_written);
843                 *p++ = cpu_to_be32(cbo->co_res.wr_stable_how);
844                 p = xdr_encode_opaque_fixed(p, cbo->co_res.wr_verifier.data,
845                                             NFS4_VERIFIER_SIZE);
846                 break;
847         default:
848                 p = xdr_reserve_space(xdr, 8);
849                 /* We always return success if bytes were written */
850                 p = xdr_encode_hyper(p, 0);
851         }
852 }
853
854 static void encode_cb_offload4args(struct xdr_stream *xdr,
855                                    const struct nfsd4_cb_offload *cbo,
856                                    struct nfs4_cb_compound_hdr *hdr)
857 {
858         __be32 *p;
859
860         p = xdr_reserve_space(xdr, 4);
861         *p = cpu_to_be32(OP_CB_OFFLOAD);
862         encode_nfs_fh4(xdr, &cbo->co_fh);
863         encode_stateid4(xdr, &cbo->co_res.cb_stateid);
864         encode_offload_info4(xdr, cbo);
865
866         hdr->nops++;
867 }
868
869 static void nfs4_xdr_enc_cb_offload(struct rpc_rqst *req,
870                                     struct xdr_stream *xdr,
871                                     const void *data)
872 {
873         const struct nfsd4_callback *cb = data;
874         const struct nfsd4_cb_offload *cbo =
875                 container_of(cb, struct nfsd4_cb_offload, co_cb);
876         struct nfs4_cb_compound_hdr hdr = {
877                 .ident = 0,
878                 .minorversion = cb->cb_clp->cl_minorversion,
879         };
880
881         encode_cb_compound4args(xdr, &hdr);
882         encode_cb_sequence4args(xdr, cb, &hdr);
883         encode_cb_offload4args(xdr, cbo, &hdr);
884         encode_cb_nops(&hdr);
885 }
886
887 static int nfs4_xdr_dec_cb_offload(struct rpc_rqst *rqstp,
888                                    struct xdr_stream *xdr,
889                                    void *data)
890 {
891         struct nfsd4_callback *cb = data;
892         struct nfs4_cb_compound_hdr hdr;
893         int status;
894
895         status = decode_cb_compound4res(xdr, &hdr);
896         if (unlikely(status))
897                 return status;
898
899         status = decode_cb_sequence4res(xdr, cb);
900         if (unlikely(status || cb->cb_seq_status))
901                 return status;
902
903         return decode_cb_op_status(xdr, OP_CB_OFFLOAD, &cb->cb_status);
904 }
905 /*
906  * RPC procedure tables
907  */
908 #define PROC(proc, call, argtype, restype)                              \
909 [NFSPROC4_CLNT_##proc] = {                                              \
910         .p_proc    = NFSPROC4_CB_##call,                                \
911         .p_encode  = nfs4_xdr_enc_##argtype,            \
912         .p_decode  = nfs4_xdr_dec_##restype,                            \
913         .p_arglen  = NFS4_enc_##argtype##_sz,                           \
914         .p_replen  = NFS4_dec_##restype##_sz,                           \
915         .p_statidx = NFSPROC4_CB_##call,                                \
916         .p_name    = #proc,                                             \
917 }
918
919 static const struct rpc_procinfo nfs4_cb_procedures[] = {
920         PROC(CB_NULL,   NULL,           cb_null,        cb_null),
921         PROC(CB_RECALL, COMPOUND,       cb_recall,      cb_recall),
922 #ifdef CONFIG_NFSD_PNFS
923         PROC(CB_LAYOUT, COMPOUND,       cb_layout,      cb_layout),
924 #endif
925         PROC(CB_NOTIFY_LOCK,    COMPOUND,       cb_notify_lock, cb_notify_lock),
926         PROC(CB_OFFLOAD,        COMPOUND,       cb_offload,     cb_offload),
927         PROC(CB_RECALL_ANY,     COMPOUND,       cb_recall_any,  cb_recall_any),
928         PROC(CB_GETATTR,        COMPOUND,       cb_getattr,     cb_getattr),
929 };
930
931 static unsigned int nfs4_cb_counts[ARRAY_SIZE(nfs4_cb_procedures)];
932 static const struct rpc_version nfs_cb_version4 = {
933 /*
934  * Note on the callback rpc program version number: despite language in rfc
935  * 5661 section 18.36.3 requiring servers to use 4 in this field, the
936  * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
937  * in practice that appears to be what implementations use.  The section
938  * 18.36.3 language is expected to be fixed in an erratum.
939  */
940         .number                 = 1,
941         .nrprocs                = ARRAY_SIZE(nfs4_cb_procedures),
942         .procs                  = nfs4_cb_procedures,
943         .counts                 = nfs4_cb_counts,
944 };
945
946 static const struct rpc_version *nfs_cb_version[2] = {
947         [1] = &nfs_cb_version4,
948 };
949
950 static const struct rpc_program cb_program;
951
952 static struct rpc_stat cb_stats = {
953         .program                = &cb_program
954 };
955
956 #define NFS4_CALLBACK 0x40000000
957 static const struct rpc_program cb_program = {
958         .name                   = "nfs4_cb",
959         .number                 = NFS4_CALLBACK,
960         .nrvers                 = ARRAY_SIZE(nfs_cb_version),
961         .version                = nfs_cb_version,
962         .stats                  = &cb_stats,
963         .pipe_dir_name          = "nfsd4_cb",
964 };
965
966 static int max_cb_time(struct net *net)
967 {
968         struct nfsd_net *nn = net_generic(net, nfsd_net_id);
969
970         /*
971          * nfsd4_lease is set to at most one hour in __nfsd4_write_time,
972          * so we can use 32-bit math on it. Warn if that assumption
973          * ever stops being true.
974          */
975         if (WARN_ON_ONCE(nn->nfsd4_lease > 3600))
976                 return 360 * HZ;
977
978         return max(((u32)nn->nfsd4_lease)/10, 1u) * HZ;
979 }
980
981 static struct workqueue_struct *callback_wq;
982
983 static bool nfsd4_queue_cb(struct nfsd4_callback *cb)
984 {
985         trace_nfsd_cb_queue(cb->cb_clp, cb);
986         return queue_delayed_work(callback_wq, &cb->cb_work, 0);
987 }
988
989 static void nfsd4_queue_cb_delayed(struct nfsd4_callback *cb,
990                                    unsigned long msecs)
991 {
992         trace_nfsd_cb_queue(cb->cb_clp, cb);
993         queue_delayed_work(callback_wq, &cb->cb_work,
994                            msecs_to_jiffies(msecs));
995 }
996
997 static void nfsd41_cb_inflight_begin(struct nfs4_client *clp)
998 {
999         atomic_inc(&clp->cl_cb_inflight);
1000 }
1001
1002 static void nfsd41_cb_inflight_end(struct nfs4_client *clp)
1003 {
1004
1005         if (atomic_dec_and_test(&clp->cl_cb_inflight))
1006                 wake_up_var(&clp->cl_cb_inflight);
1007 }
1008
1009 static void nfsd41_cb_inflight_wait_complete(struct nfs4_client *clp)
1010 {
1011         wait_var_event(&clp->cl_cb_inflight,
1012                         !atomic_read(&clp->cl_cb_inflight));
1013 }
1014
1015 static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
1016 {
1017         if (clp->cl_minorversion == 0) {
1018                 client->cl_principal = clp->cl_cred.cr_targ_princ ?
1019                         clp->cl_cred.cr_targ_princ : "nfs";
1020
1021                 return get_cred(rpc_machine_cred());
1022         } else {
1023                 struct cred *kcred;
1024
1025                 kcred = prepare_kernel_cred(&init_task);
1026                 if (!kcred)
1027                         return NULL;
1028
1029                 kcred->fsuid = ses->se_cb_sec.uid;
1030                 kcred->fsgid = ses->se_cb_sec.gid;
1031                 return kcred;
1032         }
1033 }
1034
1035 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
1036 {
1037         int maxtime = max_cb_time(clp->net);
1038         struct rpc_timeout      timeparms = {
1039                 .to_initval     = maxtime,
1040                 .to_retries     = 0,
1041                 .to_maxval      = maxtime,
1042         };
1043         struct rpc_create_args args = {
1044                 .net            = clp->net,
1045                 .address        = (struct sockaddr *) &conn->cb_addr,
1046                 .addrsize       = conn->cb_addrlen,
1047                 .saddress       = (struct sockaddr *) &conn->cb_saddr,
1048                 .timeout        = &timeparms,
1049                 .program        = &cb_program,
1050                 .version        = 1,
1051                 .flags          = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
1052                 .cred           = current_cred(),
1053         };
1054         struct rpc_clnt *client;
1055         const struct cred *cred;
1056
1057         if (clp->cl_minorversion == 0) {
1058                 if (!clp->cl_cred.cr_principal &&
1059                     (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5)) {
1060                         trace_nfsd_cb_setup_err(clp, -EINVAL);
1061                         return -EINVAL;
1062                 }
1063                 args.client_name = clp->cl_cred.cr_principal;
1064                 args.prognumber = conn->cb_prog;
1065                 args.protocol = XPRT_TRANSPORT_TCP;
1066                 args.authflavor = clp->cl_cred.cr_flavor;
1067                 clp->cl_cb_ident = conn->cb_ident;
1068         } else {
1069                 if (!conn->cb_xprt)
1070                         return -EINVAL;
1071                 clp->cl_cb_session = ses;
1072                 args.bc_xprt = conn->cb_xprt;
1073                 args.prognumber = clp->cl_cb_session->se_cb_prog;
1074                 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
1075                                 XPRT_TRANSPORT_BC;
1076                 args.authflavor = ses->se_cb_sec.flavor;
1077         }
1078         /* Create RPC client */
1079         client = rpc_create(&args);
1080         if (IS_ERR(client)) {
1081                 trace_nfsd_cb_setup_err(clp, PTR_ERR(client));
1082                 return PTR_ERR(client);
1083         }
1084         cred = get_backchannel_cred(clp, client, ses);
1085         if (!cred) {
1086                 trace_nfsd_cb_setup_err(clp, -ENOMEM);
1087                 rpc_shutdown_client(client);
1088                 return -ENOMEM;
1089         }
1090
1091         if (clp->cl_minorversion != 0)
1092                 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
1093         clp->cl_cb_client = client;
1094         clp->cl_cb_cred = cred;
1095         rcu_read_lock();
1096         trace_nfsd_cb_setup(clp, rpc_peeraddr2str(client, RPC_DISPLAY_NETID),
1097                             args.authflavor);
1098         rcu_read_unlock();
1099         return 0;
1100 }
1101
1102 static void nfsd4_mark_cb_state(struct nfs4_client *clp, int newstate)
1103 {
1104         if (clp->cl_cb_state != newstate) {
1105                 clp->cl_cb_state = newstate;
1106                 trace_nfsd_cb_new_state(clp);
1107         }
1108 }
1109
1110 static void nfsd4_mark_cb_down(struct nfs4_client *clp)
1111 {
1112         if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
1113                 return;
1114         nfsd4_mark_cb_state(clp, NFSD4_CB_DOWN);
1115 }
1116
1117 static void nfsd4_mark_cb_fault(struct nfs4_client *clp)
1118 {
1119         if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
1120                 return;
1121         nfsd4_mark_cb_state(clp, NFSD4_CB_FAULT);
1122 }
1123
1124 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
1125 {
1126         struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
1127
1128         if (task->tk_status)
1129                 nfsd4_mark_cb_down(clp);
1130         else
1131                 nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
1132 }
1133
1134 static void nfsd4_cb_probe_release(void *calldata)
1135 {
1136         struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
1137
1138         nfsd41_cb_inflight_end(clp);
1139
1140 }
1141
1142 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
1143         /* XXX: release method to ensure we set the cb channel down if
1144          * necessary on early failure? */
1145         .rpc_call_done = nfsd4_cb_probe_done,
1146         .rpc_release = nfsd4_cb_probe_release,
1147 };
1148
1149 /*
1150  * Poke the callback thread to process any updates to the callback
1151  * parameters, and send a null probe.
1152  */
1153 void nfsd4_probe_callback(struct nfs4_client *clp)
1154 {
1155         trace_nfsd_cb_probe(clp);
1156         nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
1157         set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
1158         nfsd4_run_cb(&clp->cl_cb_null);
1159 }
1160
1161 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
1162 {
1163         nfsd4_probe_callback(clp);
1164         flush_workqueue(callback_wq);
1165 }
1166
1167 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
1168 {
1169         nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
1170         spin_lock(&clp->cl_lock);
1171         memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
1172         spin_unlock(&clp->cl_lock);
1173 }
1174
1175 /*
1176  * There's currently a single callback channel slot.
1177  * If the slot is available, then mark it busy.  Otherwise, set the
1178  * thread for sleeping on the callback RPC wait queue.
1179  */
1180 static bool nfsd41_cb_get_slot(struct nfsd4_callback *cb, struct rpc_task *task)
1181 {
1182         struct nfs4_client *clp = cb->cb_clp;
1183
1184         if (!cb->cb_holds_slot &&
1185             test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
1186                 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
1187                 /* Race breaker */
1188                 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
1189                         dprintk("%s slot is busy\n", __func__);
1190                         return false;
1191                 }
1192                 rpc_wake_up_queued_task(&clp->cl_cb_waitq, task);
1193         }
1194         cb->cb_holds_slot = true;
1195         return true;
1196 }
1197
1198 static void nfsd41_cb_release_slot(struct nfsd4_callback *cb)
1199 {
1200         struct nfs4_client *clp = cb->cb_clp;
1201
1202         if (cb->cb_holds_slot) {
1203                 cb->cb_holds_slot = false;
1204                 clear_bit(0, &clp->cl_cb_slot_busy);
1205                 rpc_wake_up_next(&clp->cl_cb_waitq);
1206         }
1207 }
1208
1209 static void nfsd41_destroy_cb(struct nfsd4_callback *cb)
1210 {
1211         struct nfs4_client *clp = cb->cb_clp;
1212
1213         trace_nfsd_cb_destroy(clp, cb);
1214         nfsd41_cb_release_slot(cb);
1215         if (cb->cb_ops && cb->cb_ops->release)
1216                 cb->cb_ops->release(cb);
1217         nfsd41_cb_inflight_end(clp);
1218 }
1219
1220 /*
1221  * TODO: cb_sequence should support referring call lists, cachethis, multiple
1222  * slots, and mark callback channel down on communication errors.
1223  */
1224 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
1225 {
1226         struct nfsd4_callback *cb = calldata;
1227         struct nfs4_client *clp = cb->cb_clp;
1228         u32 minorversion = clp->cl_minorversion;
1229
1230         /*
1231          * cb_seq_status is only set in decode_cb_sequence4res,
1232          * and so will remain 1 if an rpc level failure occurs.
1233          */
1234         cb->cb_seq_status = 1;
1235         cb->cb_status = 0;
1236         if (minorversion && !nfsd41_cb_get_slot(cb, task))
1237                 return;
1238         rpc_call_start(task);
1239 }
1240
1241 static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
1242 {
1243         struct nfs4_client *clp = cb->cb_clp;
1244         struct nfsd4_session *session = clp->cl_cb_session;
1245         bool ret = true;
1246
1247         if (!clp->cl_minorversion) {
1248                 /*
1249                  * If the backchannel connection was shut down while this
1250                  * task was queued, we need to resubmit it after setting up
1251                  * a new backchannel connection.
1252                  *
1253                  * Note that if we lost our callback connection permanently
1254                  * the submission code will error out, so we don't need to
1255                  * handle that case here.
1256                  */
1257                 if (RPC_SIGNALLED(task))
1258                         goto need_restart;
1259
1260                 return true;
1261         }
1262
1263         if (!cb->cb_holds_slot)
1264                 goto need_restart;
1265
1266         /* This is the operation status code for CB_SEQUENCE */
1267         trace_nfsd_cb_seq_status(task, cb);
1268         switch (cb->cb_seq_status) {
1269         case 0:
1270                 /*
1271                  * No need for lock, access serialized in nfsd4_cb_prepare
1272                  *
1273                  * RFC5661 20.9.3
1274                  * If CB_SEQUENCE returns an error, then the state of the slot
1275                  * (sequence ID, cached reply) MUST NOT change.
1276                  */
1277                 ++session->se_cb_seq_nr;
1278                 break;
1279         case -ESERVERFAULT:
1280                 ++session->se_cb_seq_nr;
1281                 nfsd4_mark_cb_fault(cb->cb_clp);
1282                 ret = false;
1283                 break;
1284         case 1:
1285                 /*
1286                  * cb_seq_status remains 1 if an RPC Reply was never
1287                  * received. NFSD can't know if the client processed
1288                  * the CB_SEQUENCE operation. Ask the client to send a
1289                  * DESTROY_SESSION to recover.
1290                  */
1291                 fallthrough;
1292         case -NFS4ERR_BADSESSION:
1293                 nfsd4_mark_cb_fault(cb->cb_clp);
1294                 ret = false;
1295                 goto need_restart;
1296         case -NFS4ERR_DELAY:
1297                 cb->cb_seq_status = 1;
1298                 if (!rpc_restart_call(task))
1299                         goto out;
1300
1301                 rpc_delay(task, 2 * HZ);
1302                 return false;
1303         case -NFS4ERR_BADSLOT:
1304                 goto retry_nowait;
1305         case -NFS4ERR_SEQ_MISORDERED:
1306                 if (session->se_cb_seq_nr != 1) {
1307                         session->se_cb_seq_nr = 1;
1308                         goto retry_nowait;
1309                 }
1310                 break;
1311         default:
1312                 nfsd4_mark_cb_fault(cb->cb_clp);
1313         }
1314         nfsd41_cb_release_slot(cb);
1315
1316         trace_nfsd_cb_free_slot(task, cb);
1317
1318         if (RPC_SIGNALLED(task))
1319                 goto need_restart;
1320 out:
1321         return ret;
1322 retry_nowait:
1323         if (rpc_restart_call_prepare(task))
1324                 ret = false;
1325         goto out;
1326 need_restart:
1327         if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
1328                 trace_nfsd_cb_restart(clp, cb);
1329                 task->tk_status = 0;
1330                 cb->cb_need_restart = true;
1331         }
1332         return false;
1333 }
1334
1335 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
1336 {
1337         struct nfsd4_callback *cb = calldata;
1338         struct nfs4_client *clp = cb->cb_clp;
1339
1340         if (!nfsd4_cb_sequence_done(task, cb))
1341                 return;
1342
1343         if (cb->cb_status) {
1344                 WARN_ON_ONCE(task->tk_status);
1345                 task->tk_status = cb->cb_status;
1346         }
1347
1348         switch (cb->cb_ops->done(cb, task)) {
1349         case 0:
1350                 task->tk_status = 0;
1351                 rpc_restart_call_prepare(task);
1352                 return;
1353         case 1:
1354                 switch (task->tk_status) {
1355                 case -EIO:
1356                 case -ETIMEDOUT:
1357                 case -EACCES:
1358                         nfsd4_mark_cb_down(clp);
1359                 }
1360                 break;
1361         default:
1362                 BUG();
1363         }
1364 }
1365
1366 static void nfsd4_cb_release(void *calldata)
1367 {
1368         struct nfsd4_callback *cb = calldata;
1369
1370         if (cb->cb_need_restart)
1371                 nfsd4_queue_cb(cb);
1372         else
1373                 nfsd41_destroy_cb(cb);
1374
1375 }
1376
1377 static const struct rpc_call_ops nfsd4_cb_ops = {
1378         .rpc_call_prepare = nfsd4_cb_prepare,
1379         .rpc_call_done = nfsd4_cb_done,
1380         .rpc_release = nfsd4_cb_release,
1381 };
1382
1383 int nfsd4_create_callback_queue(void)
1384 {
1385         callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
1386         if (!callback_wq)
1387                 return -ENOMEM;
1388         return 0;
1389 }
1390
1391 void nfsd4_destroy_callback_queue(void)
1392 {
1393         destroy_workqueue(callback_wq);
1394 }
1395
1396 /* must be called under the state lock */
1397 void nfsd4_shutdown_callback(struct nfs4_client *clp)
1398 {
1399         if (clp->cl_cb_state != NFSD4_CB_UNKNOWN)
1400                 trace_nfsd_cb_shutdown(clp);
1401
1402         set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
1403         /*
1404          * Note this won't actually result in a null callback;
1405          * instead, nfsd4_run_cb_null() will detect the killed
1406          * client, destroy the rpc client, and stop:
1407          */
1408         nfsd4_run_cb(&clp->cl_cb_null);
1409         flush_workqueue(callback_wq);
1410         nfsd41_cb_inflight_wait_complete(clp);
1411 }
1412
1413 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
1414 {
1415         struct nfsd4_session *s;
1416         struct nfsd4_conn *c;
1417
1418         lockdep_assert_held(&clp->cl_lock);
1419
1420         list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
1421                 list_for_each_entry(c, &s->se_conns, cn_persession) {
1422                         if (c->cn_flags & NFS4_CDFC4_BACK)
1423                                 return c;
1424                 }
1425         }
1426         return NULL;
1427 }
1428
1429 /*
1430  * Note there isn't a lot of locking in this code; instead we depend on
1431  * the fact that it is run from the callback_wq, which won't run two
1432  * work items at once.  So, for example, callback_wq handles all access
1433  * of cl_cb_client and all calls to rpc_create or rpc_shutdown_client.
1434  */
1435 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
1436 {
1437         struct nfs4_cb_conn conn;
1438         struct nfs4_client *clp = cb->cb_clp;
1439         struct nfsd4_session *ses = NULL;
1440         struct nfsd4_conn *c;
1441         int err;
1442
1443         trace_nfsd_cb_bc_update(clp, cb);
1444
1445         /*
1446          * This is either an update, or the client dying; in either case,
1447          * kill the old client:
1448          */
1449         if (clp->cl_cb_client) {
1450                 trace_nfsd_cb_bc_shutdown(clp, cb);
1451                 rpc_shutdown_client(clp->cl_cb_client);
1452                 clp->cl_cb_client = NULL;
1453                 put_cred(clp->cl_cb_cred);
1454                 clp->cl_cb_cred = NULL;
1455         }
1456         if (clp->cl_cb_conn.cb_xprt) {
1457                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1458                 clp->cl_cb_conn.cb_xprt = NULL;
1459         }
1460         if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
1461                 return;
1462
1463         spin_lock(&clp->cl_lock);
1464         /*
1465          * Only serialized callback code is allowed to clear these
1466          * flags; main nfsd code can only set them:
1467          */
1468         WARN_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
1469         clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
1470
1471         memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
1472         c = __nfsd4_find_backchannel(clp);
1473         if (c) {
1474                 svc_xprt_get(c->cn_xprt);
1475                 conn.cb_xprt = c->cn_xprt;
1476                 ses = c->cn_session;
1477         }
1478         spin_unlock(&clp->cl_lock);
1479
1480         err = setup_callback_client(clp, &conn, ses);
1481         if (err) {
1482                 nfsd4_mark_cb_down(clp);
1483                 if (c)
1484                         svc_xprt_put(c->cn_xprt);
1485                 return;
1486         }
1487 }
1488
1489 static void
1490 nfsd4_run_cb_work(struct work_struct *work)
1491 {
1492         struct nfsd4_callback *cb =
1493                 container_of(work, struct nfsd4_callback, cb_work.work);
1494         struct nfs4_client *clp = cb->cb_clp;
1495         struct rpc_clnt *clnt;
1496         int flags;
1497
1498         trace_nfsd_cb_start(clp);
1499
1500         if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
1501                 nfsd4_process_cb_update(cb);
1502
1503         clnt = clp->cl_cb_client;
1504         if (!clnt) {
1505                 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
1506                         nfsd41_destroy_cb(cb);
1507                 else {
1508                         /*
1509                          * XXX: Ideally, we could wait for the client to
1510                          *      reconnect, but I haven't figured out how
1511                          *      to do that yet.
1512                          */
1513                         nfsd4_queue_cb_delayed(cb, 25);
1514                 }
1515                 return;
1516         }
1517
1518         /*
1519          * Don't send probe messages for 4.1 or later.
1520          */
1521         if (!cb->cb_ops && clp->cl_minorversion) {
1522                 nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
1523                 nfsd41_destroy_cb(cb);
1524                 return;
1525         }
1526
1527         if (cb->cb_need_restart) {
1528                 cb->cb_need_restart = false;
1529         } else {
1530                 if (cb->cb_ops && cb->cb_ops->prepare)
1531                         cb->cb_ops->prepare(cb);
1532         }
1533         cb->cb_msg.rpc_cred = clp->cl_cb_cred;
1534         flags = clp->cl_minorversion ? RPC_TASK_NOCONNECT : RPC_TASK_SOFTCONN;
1535         rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | flags,
1536                         cb->cb_ops ? &nfsd4_cb_ops : &nfsd4_cb_probe_ops, cb);
1537 }
1538
1539 void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
1540                 const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op)
1541 {
1542         cb->cb_clp = clp;
1543         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
1544         cb->cb_msg.rpc_argp = cb;
1545         cb->cb_msg.rpc_resp = cb;
1546         cb->cb_ops = ops;
1547         INIT_DELAYED_WORK(&cb->cb_work, nfsd4_run_cb_work);
1548         cb->cb_status = 0;
1549         cb->cb_need_restart = false;
1550         cb->cb_holds_slot = false;
1551 }
1552
1553 /**
1554  * nfsd4_run_cb - queue up a callback job to run
1555  * @cb: callback to queue
1556  *
1557  * Kick off a callback to do its thing. Returns false if it was already
1558  * on a queue, true otherwise.
1559  */
1560 bool nfsd4_run_cb(struct nfsd4_callback *cb)
1561 {
1562         struct nfs4_client *clp = cb->cb_clp;
1563         bool queued;
1564
1565         nfsd41_cb_inflight_begin(clp);
1566         queued = nfsd4_queue_cb(cb);
1567         if (!queued)
1568                 nfsd41_cb_inflight_end(clp);
1569         return queued;
1570 }