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