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