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