xref: /linux/include/linux/sunrpc/svc.h (revision 893a68af6a2e06fb6cb5373d9370197405552f65)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * linux/include/linux/sunrpc/svc.h
4  *
5  * RPC server declarations.
6  *
7  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 
11 #ifndef SUNRPC_SVC_H
12 #define SUNRPC_SVC_H
13 
14 #include <linux/in.h>
15 #include <linux/in6.h>
16 #include <linux/sunrpc/types.h>
17 #include <linux/sunrpc/xdr.h>
18 #include <linux/sunrpc/auth.h>
19 #include <linux/sunrpc/svcauth.h>
20 #include <linux/lwq.h>
21 #include <linux/wait.h>
22 #include <linux/mm.h>
23 #include <linux/folio_batch.h>
24 #include <linux/kthread.h>
25 
26 /*
27  *
28  * RPC service thread pool.
29  *
30  * Pool of threads and temporary sockets.  Generally there is only
31  * a single one of these per RPC service, but on NUMA machines those
32  * services that can benefit from it (i.e. nfs but not lockd) will
33  * have one pool per NUMA node.  This optimisation reduces cross-
34  * node traffic on multi-node NUMA NFS servers.
35  */
36 struct svc_pool {
37 	unsigned int		sp_id;		/* pool id; also node id on NUMA */
38 	unsigned int		sp_nrthreads;	/* # of threads currently running in pool */
39 	unsigned int		sp_nrthrmin;	/* Min number of threads to run per pool */
40 	unsigned int		sp_nrthrmax;	/* Max requested number of threads in pool */
41 	struct lwq		sp_xprts;	/* pending transports */
42 	struct list_head	sp_all_threads;	/* all server threads */
43 	struct llist_head	sp_idle_threads; /* idle server threads */
44 
45 	/* statistics on pool operation */
46 	struct percpu_counter	sp_messages_arrived;
47 	struct percpu_counter	sp_sockets_queued;
48 	struct percpu_counter	sp_threads_woken;
49 
50 	unsigned long		sp_flags;
51 } ____cacheline_aligned_in_smp;
52 
53 /* bits for sp_flags */
54 enum {
55 	SP_TASK_PENDING,	/* still work to do even if no xprt is queued */
56 	SP_NEED_VICTIM,		/* One thread needs to agree to exit */
57 	SP_VICTIM_REMAINS,	/* One thread needs to actually exit */
58 	SP_TASK_STARTING,	/* Task has started but not added to idle yet */
59 };
60 
61 
62 /*
63  * RPC service.
64  *
65  * An RPC service is a ``daemon,'' possibly multithreaded, which
66  * receives and processes incoming RPC messages.
67  * It has one or more transport sockets associated with it, and maintains
68  * a list of idle threads waiting for input.
69  *
70  * We currently do not support more than one RPC program per daemon.
71  */
72 struct svc_serv {
73 	struct svc_program *	sv_programs;	/* RPC programs */
74 	struct svc_stat *	sv_stats;	/* RPC statistics */
75 	spinlock_t		sv_lock;
76 	unsigned int		sv_nprogs;	/* Number of sv_programs */
77 	unsigned int		sv_nrthreads;	/* # of running server threads */
78 	unsigned int		sv_max_payload;	/* datagram payload size */
79 	unsigned int		sv_max_mesg;	/* max_payload + 1 page for overheads */
80 	unsigned int		sv_xdrsize;	/* XDR buffer size */
81 	atomic_t		sv_rpcb_failures; /* unanswered rpcbind calls */
82 	struct list_head	sv_permsocks;	/* all permanent sockets */
83 	struct list_head	sv_tempsocks;	/* all temporary sockets */
84 	int			sv_tmpcnt;	/* count of temporary "valid" sockets */
85 	struct timer_list	sv_temptimer;	/* timer for aging temporary sockets */
86 
87 	char *			sv_name;	/* service name */
88 
89 	bool			sv_is_pooled;	/* is this a pooled service? */
90 	/* Caller registers with rpcbind itself. Set before svc_bind(). */
91 	bool			sv_no_rpcbind;
92 	struct svc_pool *	sv_pools;	/* array of thread pools */
93 	int			(*sv_threadfn)(void *data);
94 
95 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
96 	struct lwq		sv_cb_list;	/* queue for callback requests
97 						 * that arrive over the same
98 						 * connection */
99 	bool			sv_bc_enabled;	/* service uses backchannel */
100 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
101 };
102 
103 /* This is used by pool_stats to find and lock an svc */
104 struct svc_info {
105 	struct svc_serv		*serv;
106 	struct mutex		*mutex;
107 };
108 
109 void svc_destroy(struct svc_serv **svcp);
110 
111 /*
112  * Maximum payload size supported by a kernel RPC server.
113  * This is use to determine the max number of pages nfsd is
114  * willing to return in a single READ operation.
115  *
116  * These happen to all be powers of 2, which is not strictly
117  * necessary but helps enforce the real limitation, which is
118  * that they should be multiples of PAGE_SIZE.
119  *
120  * For UDP transports, a block plus NFS,RPC, and UDP headers
121  * has to fit into the IP datagram limit of 64K.  The largest
122  * feasible number for all known page sizes is probably 48K,
123  * but we choose 32K here.  This is the same as the historical
124  * Linux limit; someone who cares more about NFS/UDP performance
125  * can test a larger number.
126  *
127  * For non-UDP transports we have more freedom.  A size of 4MB is
128  * chosen to accommodate clients that support larger I/O sizes.
129  */
130 enum {
131 	RPCSVC_MAXPAYLOAD	= 4 * 1024 * 1024,
132 	RPCSVC_MAXPAYLOAD_TCP	= RPCSVC_MAXPAYLOAD,
133 	RPCSVC_MAXPAYLOAD_UDP	= 32 * 1024,
134 };
135 
136 extern u32 svc_max_payload(const struct svc_rqst *rqstp);
137 
138 /*
139  * RPC Call and Reply messages each have their own page array.
140  * rq_pages holds the incoming Call message; rq_respages holds
141  * the outgoing Reply message. Both arrays are sized to
142  * svc_serv_maxpages() entries and are allocated dynamically.
143  *
144  * Pages are sent using ->sendmsg with MSG_SPLICE_PAGES so each
145  * server thread needs to allocate more to replace those used in
146  * sending.
147  *
148  * rq_pages request page contract:
149  *
150  * Transport receive paths that move request data pages out of
151  * rq_pages -- TCP multi-fragment reassembly (svc_tcp_save_pages)
152  * and RDMA Read I/O (svc_rdma_clear_rqst_pages) -- NULL those
153  * entries to prevent svc_rqst_release_pages() from freeing pages
154  * still in transport use, and set rq_pages_nfree to the count.
155  * svc_alloc_arg() refills only that many rq_pages entries.
156  *
157  * For rq_respages, svc_rqst_release_pages() NULLs entries in
158  * [rq_respages, rq_next_page) after each RPC. svc_alloc_arg()
159  * refills only that range.
160  *
161  * xdr_buf holds responses; the structure fits NFS read responses
162  * (header, data pages, optional tail) and enables sharing of
163  * client-side routines.
164  *
165  * The xdr_buf.head kvec always points to the first page in the
166  * rq_*pages list. The xdr_buf.pages pointer points to the second
167  * page on that list. xdr_buf.tail points to the end of the first
168  * page. This assumes that the non-page part of an rpc reply will
169  * fit in a page - NFSd ensures this. lockd also has no trouble.
170  */
171 
172 /**
173  * svc_serv_maxpages - maximum count of pages needed for one RPC message
174  * @serv: RPC service context
175  *
176  * Returns a count of pages or vectors that can hold the maximum
177  * size RPC message for @serv.
178  *
179  * Each page array can hold at most one payload plus two
180  * overhead pages (one for the RPC header, one for tail data).
181  * nfsd_splice_actor() might need an extra page when a READ
182  * payload is not page-aligned.
183  */
184 static inline unsigned long svc_serv_maxpages(const struct svc_serv *serv)
185 {
186 	return DIV_ROUND_UP(serv->sv_max_mesg, PAGE_SIZE) + 2 + 1;
187 }
188 
189 /*
190  * The context of a single thread, including the request currently being
191  * processed.
192  *
193  * RPC programs are free to use rq_private to stash thread-local information.
194  * The sunrpc layer will not access it.
195  */
196 struct svc_rqst {
197 	struct list_head	rq_all;		/* all threads list */
198 	struct llist_node	rq_idle;	/* On the idle list */
199 	struct rcu_head		rq_rcu_head;	/* for RCU deferred kfree */
200 	struct svc_xprt *	rq_xprt;	/* transport ptr */
201 
202 	struct sockaddr_storage	rq_addr;	/* peer address */
203 	size_t			rq_addrlen;
204 	struct sockaddr_storage	rq_daddr;	/* dest addr of request
205 						 *  - reply from here */
206 	size_t			rq_daddrlen;
207 
208 	struct svc_serv *	rq_server;	/* RPC service definition */
209 	struct svc_pool *	rq_pool;	/* thread pool */
210 	const struct svc_procedure *rq_procinfo;/* procedure info */
211 	struct auth_ops *	rq_authop;	/* authentication flavour */
212 	struct svc_cred		rq_cred;	/* auth info */
213 	void *			rq_xprt_ctxt;	/* transport specific context ptr */
214 	struct svc_deferred_req*rq_deferred;	/* deferred request we are replaying */
215 
216 	struct xdr_buf		rq_arg;
217 	struct xdr_stream	rq_arg_stream;
218 	struct xdr_stream	rq_res_stream;
219 	struct folio		*rq_scratch_folio;
220 	struct xdr_buf		rq_res;
221 	unsigned long		rq_maxpages;	/* entries per page array */
222 	unsigned long		rq_pages_nfree;	/* rq_pages entries NULLed by transport */
223 	struct page *		*rq_pages;	/* Call buffer pages */
224 	struct page *		*rq_respages;	/* Reply buffer pages */
225 	struct page *		*rq_next_page; /* next reply page to use */
226 	struct page *		*rq_page_end;  /* one past the last reply page */
227 
228 	struct folio_batch	rq_fbatch;
229 	struct bio_vec		*rq_bvec;
230 
231 	__be32			rq_xid;		/* transmission id */
232 	u32			rq_prog;	/* program number */
233 	u32			rq_vers;	/* program version */
234 	u32			rq_proc;	/* procedure number */
235 	u32			rq_prot;	/* IP protocol */
236 	unsigned long		rq_flags;	/* flags field */
237 	ktime_t			rq_qtime;	/* enqueue time */
238 
239 	void *			rq_argp;	/* decoded arguments */
240 	void *			rq_resp;	/* xdr'd results */
241 	__be32			*rq_accept_statp;
242 	void *			rq_auth_data;	/* flavor-specific data */
243 	__be32			rq_auth_stat;	/* authentication status */
244 	int			rq_auth_slack;	/* extra space xdr code
245 						 * should leave in head
246 						 * for krb5i, krb5p.
247 						 */
248 	int			rq_reserved;	/* space on socket outq
249 						 * reserved for this request
250 						 */
251 	ktime_t			rq_stime;	/* start time */
252 
253 	struct cache_req	rq_chandle;	/* handle passed to caches for
254 						 * request delaying
255 						 */
256 	/* Catering to nfsd */
257 	struct auth_domain *	rq_client;	/* RPC peer info */
258 	struct auth_domain *	rq_gssclient;	/* "gss/"-style peer info */
259 	struct task_struct	*rq_task;	/* service thread */
260 	struct net		*rq_bc_net;	/* pointer to backchannel's
261 						 * net namespace
262 						 */
263 
264 	int			rq_err;		/* Thread sets this to inidicate
265 						 * initialisation success.
266 						 */
267 
268 	unsigned long		bc_to_initval;
269 	unsigned int		bc_to_retries;
270 	unsigned int		rq_status_counter; /* RPC processing counter */
271 	void			*rq_private;	/* For use by the service thread */
272 };
273 
274 /* bits for rq_flags */
275 enum {
276 	RQ_SECURE,		/* secure port */
277 	RQ_LOCAL,		/* local request */
278 	RQ_USEDEFERRAL,		/* use deferral */
279 	RQ_DROPME,		/* drop current reply */
280 	RQ_VICTIM,		/* Have agreed to shut down */
281 	RQ_DATA,		/* request has data */
282 };
283 
284 #define SVC_NET(rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)
285 
286 /*
287  * Rigorous type checking on sockaddr type conversions
288  */
289 static inline struct sockaddr_in *svc_addr_in(const struct svc_rqst *rqst)
290 {
291 	return (struct sockaddr_in *) &rqst->rq_addr;
292 }
293 
294 static inline struct sockaddr_in6 *svc_addr_in6(const struct svc_rqst *rqst)
295 {
296 	return (struct sockaddr_in6 *) &rqst->rq_addr;
297 }
298 
299 static inline struct sockaddr *svc_addr(const struct svc_rqst *rqst)
300 {
301 	return (struct sockaddr *) &rqst->rq_addr;
302 }
303 
304 static inline struct sockaddr_in *svc_daddr_in(const struct svc_rqst *rqst)
305 {
306 	return (struct sockaddr_in *) &rqst->rq_daddr;
307 }
308 
309 static inline struct sockaddr_in6 *svc_daddr_in6(const struct svc_rqst *rqst)
310 {
311 	return (struct sockaddr_in6 *) &rqst->rq_daddr;
312 }
313 
314 static inline struct sockaddr *svc_daddr(const struct svc_rqst *rqst)
315 {
316 	return (struct sockaddr *) &rqst->rq_daddr;
317 }
318 
319 /**
320  * svc_thread_should_stop - check if this thread should stop
321  * @rqstp: the thread that might need to stop
322  *
323  * To stop an svc thread, the pool flags SP_NEED_VICTIM and SP_VICTIM_REMAINS
324  * are set.  The first thread which sees SP_NEED_VICTIM clears it, becoming
325  * the victim using this function.  It should then promptly call
326  * svc_exit_thread() to complete the process, clearing SP_VICTIM_REMAINS
327  * so the task waiting for a thread to exit can wake and continue.
328  *
329  * Return values:
330  *   %true: caller should invoke svc_exit_thread()
331  *   %false: caller should do nothing
332  */
333 static inline bool svc_thread_should_stop(struct svc_rqst *rqstp)
334 {
335 	if (test_and_clear_bit(SP_NEED_VICTIM, &rqstp->rq_pool->sp_flags))
336 		set_bit(RQ_VICTIM, &rqstp->rq_flags);
337 
338 	return test_bit(RQ_VICTIM, &rqstp->rq_flags);
339 }
340 
341 /**
342  * svc_thread_init_status - report whether thread has initialised successfully
343  * @rqstp: the thread in question
344  * @err: errno code
345  *
346  * After performing any initialisation that could fail, and before starting
347  * normal work, each sunrpc svc_thread must call svc_thread_init_status()
348  * with an appropriate error, or zero.
349  *
350  * If zero is passed, the thread is ready and must continue until
351  * svc_thread_should_stop() returns true.  If a non-zero error is passed
352  * the call will not return - the thread will exit.
353  */
354 static inline void svc_thread_init_status(struct svc_rqst *rqstp, int err)
355 {
356 	store_release_wake_up(&rqstp->rq_err, err);
357 	if (err)
358 		kthread_exit(1);
359 }
360 
361 struct svc_deferred_req {
362 	u32			prot;	/* protocol (UDP or TCP) */
363 	struct svc_xprt		*xprt;
364 	struct sockaddr_storage	addr;	/* where reply must go */
365 	size_t			addrlen;
366 	struct sockaddr_storage	daddr;	/* where reply must come from */
367 	size_t			daddrlen;
368 	void			*xprt_ctxt;
369 	struct cache_deferred_req handle;
370 	int			argslen;
371 	__be32			args[];
372 };
373 
374 struct svc_process_info {
375 	union {
376 		int  (*dispatch)(struct svc_rqst *rqstp);
377 		struct {
378 			unsigned int lovers;
379 			unsigned int hivers;
380 		} mismatch;
381 	};
382 };
383 
384 /*
385  * RPC program - an array of these can use the same transport endpoint
386  */
387 struct svc_program {
388 	u32			pg_prog;	/* program number */
389 	unsigned int		pg_lovers;	/* lowest version */
390 	unsigned int		pg_hivers;	/* highest version */
391 	unsigned int		pg_nvers;	/* number of versions */
392 	const struct svc_version **pg_vers;	/* version array */
393 	char *			pg_name;	/* service name */
394 	char *			pg_class;	/* class name: services sharing authentication */
395 	enum svc_auth_status	(*pg_authenticate)(struct svc_rqst *rqstp);
396 	__be32			(*pg_init_request)(struct svc_rqst *,
397 						   const struct svc_program *,
398 						   struct svc_process_info *);
399 	int			(*pg_rpcbind_set)(struct net *net,
400 						  const struct svc_program *,
401 						  u32 version, int family,
402 						  unsigned short proto,
403 						  unsigned short port);
404 };
405 
406 /*
407  * RPC program version
408  */
409 struct svc_version {
410 	u32			vs_vers;	/* version number */
411 	u32			vs_nproc;	/* number of procedures */
412 	const struct svc_procedure *vs_proc;	/* per-procedure info */
413 	u32			vs_xdrsize;	/* xdrsize needed for this version */
414 
415 	/* Don't register with rpcbind */
416 	bool			vs_hidden;
417 
418 	/* Don't care if the rpcbind registration fails */
419 	bool			vs_rpcb_optnl;
420 
421 	/* Need xprt with congestion control */
422 	bool			vs_need_cong_ctrl;
423 
424 	/* Dispatch function */
425 	int			(*vs_dispatch)(struct svc_rqst *rqstp);
426 };
427 
428 /*
429  * RPC procedure info
430  */
431 struct svc_procedure {
432 	/* process the request: */
433 	__be32			(*pc_func)(struct svc_rqst *);
434 	/* XDR decode args: */
435 	bool			(*pc_decode)(struct svc_rqst *rqstp,
436 					     struct xdr_stream *xdr);
437 	/* XDR encode result: */
438 	bool			(*pc_encode)(struct svc_rqst *rqstp,
439 					     struct xdr_stream *xdr);
440 	/* XDR free result: */
441 	void			(*pc_release)(struct svc_rqst *);
442 	unsigned int		pc_argsize;	/* argument struct size */
443 	unsigned int		pc_argzero;	/* how much of argument to clear */
444 	unsigned int		pc_ressize;	/* result struct size */
445 	unsigned int		pc_cachetype;	/* cache info (NFS) */
446 	unsigned int		pc_xdrressize;	/* maximum size of XDR reply */
447 	const char *		pc_name;	/* for display */
448 };
449 
450 /*
451  * Function prototypes.
452  */
453 int sunrpc_set_pool_mode(const char *val);
454 int sunrpc_get_pool_mode(char *val, size_t size);
455 void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net);
456 int svc_bind(struct svc_serv *serv, struct net *net);
457 unsigned int svc_rpcb_failure_count(struct svc_serv *serv);
458 struct svc_serv *svc_create(struct svc_program *, unsigned int,
459 			    int (*threadfn)(void *data));
460 bool		   svc_rqst_replace_page(struct svc_rqst *rqstp,
461 					 struct page *page);
462 void		   svc_rqst_release_pages(struct svc_rqst *rqstp);
463 int		   svc_new_thread(struct svc_serv *serv, struct svc_pool *pool);
464 void		   svc_exit_thread(struct svc_rqst *);
465 struct svc_serv *  svc_create_pooled(struct svc_program *prog,
466 				     unsigned int nprog,
467 				     struct svc_stat *stats,
468 				     unsigned int bufsize,
469 				     int (*threadfn)(void *data));
470 int		   svc_set_pool_threads(struct svc_serv *serv, struct svc_pool *pool,
471 					unsigned int min_threads, unsigned int max_threads);
472 int		   svc_set_num_threads(struct svc_serv *serv, unsigned int min_threads,
473 				       unsigned int nrservs);
474 unsigned int	   svc_serv_maxthreads(const struct svc_serv *serv);
475 int		   svc_pool_stats_open(struct svc_info *si, struct file *file);
476 void		   svc_process(struct svc_rqst *rqstp);
477 void		   svc_process_bc(struct rpc_rqst *req, struct svc_rqst *rqstp);
478 int		   svc_register(struct svc_serv *serv, struct net *net,
479 				const int family, const unsigned short proto,
480 				const unsigned short port);
481 
482 void		   svc_wake_up(struct svc_serv *);
483 void		   svc_reserve(struct svc_rqst *rqstp, int space);
484 void		   svc_pool_wake_idle_thread(struct svc_pool *pool);
485 struct svc_pool   *svc_pool_for_cpu(struct svc_serv *serv);
486 unsigned int	   svc_serv_nrpools(const struct svc_serv *serv);
487 char *		   svc_print_addr(struct svc_rqst *, char *, size_t);
488 const char *	   svc_proc_name(const struct svc_rqst *rqstp);
489 int		   svc_encode_result_payload(struct svc_rqst *rqstp,
490 					     unsigned int offset,
491 					     unsigned int length);
492 char		  *svc_fill_symlink_pathname(struct svc_rqst *rqstp,
493 					     struct kvec *first, void *p,
494 					     size_t total);
495 __be32		   svc_generic_init_request(struct svc_rqst *rqstp,
496 					    const struct svc_program *progp,
497 					    struct svc_process_info *procinfo);
498 int		   svc_generic_rpcbind_set(struct net *net,
499 					   const struct svc_program *progp,
500 					   u32 version, int family,
501 					   unsigned short proto,
502 					   unsigned short port);
503 
504 #define	RPC_MAX_ADDRBUFLEN	(63U)
505 
506 /**
507  * svc_rqst_page_release - release a page associated with an RPC transaction
508  * @rqstp: RPC transaction context
509  * @page: page to release
510  *
511  * Released pages are batched and freed together, reducing
512  * allocator pressure under heavy RPC workloads.
513  */
514 static inline void svc_rqst_page_release(struct svc_rqst *rqstp,
515 					 struct page *page)
516 {
517 	if (!folio_batch_add(&rqstp->rq_fbatch, page_folio(page)))
518 		__folio_batch_release(&rqstp->rq_fbatch);
519 }
520 
521 /*
522  * When we want to reduce the size of the reserved space in the response
523  * buffer, we need to take into account the size of any checksum data that
524  * may be at the end of the packet. This is difficult to determine exactly
525  * for all cases without actually generating the checksum, so we just use a
526  * static value.
527  */
528 static inline void svc_reserve_auth(struct svc_rqst *rqstp, int space)
529 {
530 	svc_reserve(rqstp, space + rqstp->rq_auth_slack);
531 }
532 
533 /**
534  * svcxdr_init_decode - Prepare an xdr_stream for Call decoding
535  * @rqstp: controlling server RPC transaction context
536  *
537  * The stream records @rqstp, so a codec running on it reaches the
538  * transaction with svcxdr_rqst().
539  */
540 static inline void svcxdr_init_decode(struct svc_rqst *rqstp)
541 {
542 	struct xdr_stream *xdr = &rqstp->rq_arg_stream;
543 	struct xdr_buf *buf = &rqstp->rq_arg;
544 	struct kvec *argv = buf->head;
545 
546 	WARN_ON(buf->len != buf->head->iov_len + buf->page_len + buf->tail->iov_len);
547 	buf->len = buf->head->iov_len + buf->page_len + buf->tail->iov_len;
548 
549 	xdr_init_decode(xdr, buf, argv->iov_base, NULL);
550 	xdr_set_scratch_folio(xdr, rqstp->rq_scratch_folio);
551 	xdr->xdrgen_ctx = rqstp;
552 }
553 
554 /**
555  * svcxdr_init_encode - Prepare an xdr_stream for svc Reply encoding
556  * @rqstp: controlling server RPC transaction context
557  *
558  * The stream records @rqstp, so a codec running on it reaches the
559  * transaction with svcxdr_rqst().
560  */
561 static inline void svcxdr_init_encode(struct svc_rqst *rqstp)
562 {
563 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
564 	struct xdr_buf *buf = &rqstp->rq_res;
565 	struct kvec *resv = buf->head;
566 
567 	xdr_reset_scratch_buffer(xdr);
568 
569 	xdr->buf = buf;
570 	xdr->iov = resv;
571 	xdr->p   = resv->iov_base + resv->iov_len;
572 	xdr->end = resv->iov_base + PAGE_SIZE;
573 	buf->len = resv->iov_len;
574 	xdr->page_ptr = buf->pages - 1;
575 	buf->buflen = PAGE_SIZE * (rqstp->rq_page_end - buf->pages);
576 	xdr->rqst = NULL;
577 	xdr->xdrgen_ctx = rqstp;
578 }
579 
580 /**
581  * svcxdr_rqst - Retrieve the transaction bound to an xdr_stream
582  * @xdr: stream to query
583  *
584  * Return: the controlling svc_rqst when @xdr was initialized by
585  * svcxdr_init_decode() or svcxdr_init_encode(), otherwise NULL.
586  */
587 static inline struct svc_rqst *svcxdr_rqst(struct xdr_stream *xdr)
588 {
589 	return xdr->xdrgen_ctx;
590 }
591 
592 /**
593  * svcxdr_encode_opaque_pages - Insert pages into an xdr_stream
594  * @xdr: xdr_stream to be updated
595  * @pages: array of pages to insert
596  * @base: starting offset of first data byte in @pages
597  * @len: number of data bytes in @pages to insert
598  *
599  * After the @pages are added, the tail iovec is instantiated pointing
600  * to end of the head buffer, and the stream is set up to encode
601  * subsequent items into the tail.
602  */
603 static inline void svcxdr_encode_opaque_pages(struct svc_rqst *rqstp,
604 					      struct xdr_stream *xdr,
605 					      struct page **pages,
606 					      unsigned int base,
607 					      unsigned int len)
608 {
609 	xdr_write_pages(xdr, pages, base, len);
610 	xdr->page_ptr = rqstp->rq_next_page - 1;
611 }
612 
613 /**
614  * svcxdr_encode_opaque_payload - Encode a page-resident opaque data item
615  * @xdr: xdr_stream to be updated
616  * @len: number of octets of content in the data item
617  *
618  * Context: Process context. @xdr must have been initialized by
619  *	    svcxdr_init_encode() and still be positioned in the reply
620  *	    head.
621  *
622  * Return:
623  *   %true: Success
624  *   %false: The reply head cannot hold the length prefix and XDR
625  *   padding, or the transport could not accommodate the result payload
626  */
627 static inline bool svcxdr_encode_opaque_payload(struct xdr_stream *xdr, u32 len)
628 {
629 	struct svc_rqst *rqstp = svcxdr_rqst(xdr);
630 	struct xdr_buf *buf = xdr->buf;
631 
632 	/*
633 	 * The length prefix and any pad word must land in the reply
634 	 * head. A prefix that spills into the first Reply page lands
635 	 * on the payload itself, and xdr_write_pages() BUGs when the
636 	 * pad word does not fit.
637 	 */
638 	if ((xdr->end - xdr->p) * XDR_UNIT < XDR_UNIT + xdr_pad_size(len))
639 		return false;
640 	if (xdr_stream_encode_u32(xdr, len) < 0)
641 		return false;
642 	svcxdr_encode_opaque_pages(rqstp, xdr, buf->pages, buf->page_base, len);
643 	if (svc_encode_result_payload(rqstp, buf->head->iov_len, len) < 0)
644 		return false;
645 	return true;
646 }
647 
648 /**
649  * svcxdr_decode_opaque_payload - Decode a page-resident opaque data item
650  * @xdr: xdr_stream to be decoded
651  * @payload: on success, describes the octets of the data item's content
652  * @maxlen: largest data item length the caller will accept, or zero for
653  *	    no limit
654  *
655  * A bulk payload such as the content of an NFS WRITE request resides in
656  * the pages of the Receive buffer. Rather than copy it, set @payload to
657  * describe the item's content in place.
658  *
659  * Context: Process context. @xdr must have been initialized by
660  *	    svcxdr_init_decode().
661  *
662  * Return:
663  *   %true: @payload describes the item in place and @xdr has advanced
664  *	    past it
665  *   %false: a bounds error occurred, or the length prefix exceeds
666  *	     @maxlen; @payload is undefined
667  */
668 static inline bool svcxdr_decode_opaque_payload(struct xdr_stream *xdr,
669 						struct xdr_buf *payload,
670 						u32 maxlen)
671 {
672 	u32 len;
673 
674 	if (xdr_stream_decode_u32(xdr, &len) < 0)
675 		return false;
676 	if (maxlen && len > maxlen)
677 		return false;
678 	return xdr_stream_subsegment(xdr, payload, len);
679 }
680 
681 /**
682  * svcxdr_set_auth_slack -
683  * @rqstp: RPC transaction
684  * @slack: buffer space to reserve for the transaction's security flavor
685  *
686  * Set the request's slack space requirement, and set aside that much
687  * space in the rqstp's rq_res.head for use when the auth wraps the Reply.
688  */
689 static inline void svcxdr_set_auth_slack(struct svc_rqst *rqstp, int slack)
690 {
691 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
692 	struct xdr_buf *buf = &rqstp->rq_res;
693 	struct kvec *resv = buf->head;
694 
695 	rqstp->rq_auth_slack = slack;
696 
697 	xdr->end -= XDR_QUADLEN(slack);
698 	buf->buflen -= rqstp->rq_auth_slack;
699 
700 	WARN_ON(xdr->iov != resv);
701 	WARN_ON(xdr->p > xdr->end);
702 }
703 
704 /**
705  * svcxdr_set_accept_stat - Reserve space for the accept_stat field
706  * @rqstp: RPC transaction context
707  *
708  * Return values:
709  *   %true: Success
710  *   %false: No response buffer space was available
711  */
712 static inline bool svcxdr_set_accept_stat(struct svc_rqst *rqstp)
713 {
714 	struct xdr_stream *xdr = &rqstp->rq_res_stream;
715 
716 	rqstp->rq_accept_statp = xdr_reserve_space(xdr, XDR_UNIT);
717 	if (unlikely(!rqstp->rq_accept_statp))
718 		return false;
719 	*rqstp->rq_accept_statp = rpc_success;
720 	return true;
721 }
722 
723 #endif /* SUNRPC_SVC_H */
724