762d7231e574b737d34682aa3257ad4692519164
[platform/kernel/linux-starfive.git] / include / linux / sunrpc / svc.h
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/wait.h>
21 #include <linux/mm.h>
22 #include <linux/pagevec.h>
23
24 /*
25  *
26  * RPC service thread pool.
27  *
28  * Pool of threads and temporary sockets.  Generally there is only
29  * a single one of these per RPC service, but on NUMA machines those
30  * services that can benefit from it (i.e. nfs but not lockd) will
31  * have one pool per NUMA node.  This optimisation reduces cross-
32  * node traffic on multi-node NUMA NFS servers.
33  */
34 struct svc_pool {
35         unsigned int            sp_id;          /* pool id; also node id on NUMA */
36         spinlock_t              sp_lock;        /* protects all fields */
37         struct list_head        sp_sockets;     /* pending sockets */
38         unsigned int            sp_nrthreads;   /* # of threads in pool */
39         struct list_head        sp_all_threads; /* all server threads */
40
41         /* statistics on pool operation */
42         struct percpu_counter   sp_sockets_queued;
43         struct percpu_counter   sp_threads_woken;
44         struct percpu_counter   sp_threads_timedout;
45
46 #define SP_TASK_PENDING         (0)             /* still work to do even if no
47                                                  * xprt is queued. */
48 #define SP_CONGESTED            (1)
49         unsigned long           sp_flags;
50 } ____cacheline_aligned_in_smp;
51
52 /*
53  * RPC service.
54  *
55  * An RPC service is a ``daemon,'' possibly multithreaded, which
56  * receives and processes incoming RPC messages.
57  * It has one or more transport sockets associated with it, and maintains
58  * a list of idle threads waiting for input.
59  *
60  * We currently do not support more than one RPC program per daemon.
61  */
62 struct svc_serv {
63         struct svc_program *    sv_program;     /* RPC program */
64         struct svc_stat *       sv_stats;       /* RPC statistics */
65         spinlock_t              sv_lock;
66         struct kref             sv_refcnt;
67         unsigned int            sv_nrthreads;   /* # of server threads */
68         unsigned int            sv_maxconn;     /* max connections allowed or
69                                                  * '0' causing max to be based
70                                                  * on number of threads. */
71
72         unsigned int            sv_max_payload; /* datagram payload size */
73         unsigned int            sv_max_mesg;    /* max_payload + 1 page for overheads */
74         unsigned int            sv_xdrsize;     /* XDR buffer size */
75         struct list_head        sv_permsocks;   /* all permanent sockets */
76         struct list_head        sv_tempsocks;   /* all temporary sockets */
77         int                     sv_tmpcnt;      /* count of temporary sockets */
78         struct timer_list       sv_temptimer;   /* timer for aging temporary sockets */
79
80         char *                  sv_name;        /* service name */
81
82         unsigned int            sv_nrpools;     /* number of thread pools */
83         struct svc_pool *       sv_pools;       /* array of thread pools */
84         int                     (*sv_threadfn)(void *data);
85
86 #if defined(CONFIG_SUNRPC_BACKCHANNEL)
87         struct list_head        sv_cb_list;     /* queue for callback requests
88                                                  * that arrive over the same
89                                                  * connection */
90         spinlock_t              sv_cb_lock;     /* protects the svc_cb_list */
91         wait_queue_head_t       sv_cb_waitq;    /* sleep here if there are no
92                                                  * entries in the svc_cb_list */
93         bool                    sv_bc_enabled;  /* service uses backchannel */
94 #endif /* CONFIG_SUNRPC_BACKCHANNEL */
95 };
96
97 /**
98  * svc_get() - increment reference count on a SUNRPC serv
99  * @serv:  the svc_serv to have count incremented
100  *
101  * Returns: the svc_serv that was passed in.
102  */
103 static inline struct svc_serv *svc_get(struct svc_serv *serv)
104 {
105         kref_get(&serv->sv_refcnt);
106         return serv;
107 }
108
109 void svc_destroy(struct kref *);
110
111 /**
112  * svc_put - decrement reference count on a SUNRPC serv
113  * @serv:  the svc_serv to have count decremented
114  *
115  * When the reference count reaches zero, svc_destroy()
116  * is called to clean up and free the serv.
117  */
118 static inline void svc_put(struct svc_serv *serv)
119 {
120         kref_put(&serv->sv_refcnt, svc_destroy);
121 }
122
123 /**
124  * svc_put_not_last - decrement non-final reference count on SUNRPC serv
125  * @serv:  the svc_serv to have count decremented
126  *
127  * Returns: %true is refcount was decremented.
128  *
129  * If the refcount is 1, it is not decremented and instead failure is reported.
130  */
131 static inline bool svc_put_not_last(struct svc_serv *serv)
132 {
133         return refcount_dec_not_one(&serv->sv_refcnt.refcount);
134 }
135
136 /*
137  * Maximum payload size supported by a kernel RPC server.
138  * This is use to determine the max number of pages nfsd is
139  * willing to return in a single READ operation.
140  *
141  * These happen to all be powers of 2, which is not strictly
142  * necessary but helps enforce the real limitation, which is
143  * that they should be multiples of PAGE_SIZE.
144  *
145  * For UDP transports, a block plus NFS,RPC, and UDP headers
146  * has to fit into the IP datagram limit of 64K.  The largest
147  * feasible number for all known page sizes is probably 48K,
148  * but we choose 32K here.  This is the same as the historical
149  * Linux limit; someone who cares more about NFS/UDP performance
150  * can test a larger number.
151  *
152  * For TCP transports we have more freedom.  A size of 1MB is
153  * chosen to match the client limit.  Other OSes are known to
154  * have larger limits, but those numbers are probably beyond
155  * the point of diminishing returns.
156  */
157 #define RPCSVC_MAXPAYLOAD       (1*1024*1024u)
158 #define RPCSVC_MAXPAYLOAD_TCP   RPCSVC_MAXPAYLOAD
159 #define RPCSVC_MAXPAYLOAD_UDP   (32*1024u)
160
161 extern u32 svc_max_payload(const struct svc_rqst *rqstp);
162
163 /*
164  * RPC Requsts and replies are stored in one or more pages.
165  * We maintain an array of pages for each server thread.
166  * Requests are copied into these pages as they arrive.  Remaining
167  * pages are available to write the reply into.
168  *
169  * Pages are sent using ->sendpage so each server thread needs to
170  * allocate more to replace those used in sending.  To help keep track
171  * of these pages we have a receive list where all pages initialy live,
172  * and a send list where pages are moved to when there are to be part
173  * of a reply.
174  *
175  * We use xdr_buf for holding responses as it fits well with NFS
176  * read responses (that have a header, and some data pages, and possibly
177  * a tail) and means we can share some client side routines.
178  *
179  * The xdr_buf.head kvec always points to the first page in the rq_*pages
180  * list.  The xdr_buf.pages pointer points to the second page on that
181  * list.  xdr_buf.tail points to the end of the first page.
182  * This assumes that the non-page part of an rpc reply will fit
183  * in a page - NFSd ensures this.  lockd also has no trouble.
184  *
185  * Each request/reply pair can have at most one "payload", plus two pages,
186  * one for the request, and one for the reply.
187  * We using ->sendfile to return read data, we might need one extra page
188  * if the request is not page-aligned.  So add another '1'.
189  */
190 #define RPCSVC_MAXPAGES         ((RPCSVC_MAXPAYLOAD+PAGE_SIZE-1)/PAGE_SIZE \
191                                 + 2 + 1)
192
193 /*
194  * The context of a single thread, including the request currently being
195  * processed.
196  */
197 struct svc_rqst {
198         struct list_head        rq_all;         /* all threads 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 page             *rq_scratch_page;
220         struct xdr_buf          rq_res;
221         struct page             *rq_pages[RPCSVC_MAXPAGES + 1];
222         struct page *           *rq_respages;   /* points into rq_pages */
223         struct page *           *rq_next_page; /* next reply page to use */
224         struct page *           *rq_page_end;  /* one past the last page */
225
226         struct pagevec          rq_pvec;
227         struct kvec             rq_vec[RPCSVC_MAXPAGES]; /* generally useful.. */
228         struct bio_vec          rq_bvec[RPCSVC_MAXPAGES];
229
230         __be32                  rq_xid;         /* transmission id */
231         u32                     rq_prog;        /* program number */
232         u32                     rq_vers;        /* program version */
233         u32                     rq_proc;        /* procedure number */
234         u32                     rq_prot;        /* IP protocol */
235         int                     rq_cachetype;   /* catering to nfsd */
236 #define RQ_SECURE       (0)                     /* secure port */
237 #define RQ_LOCAL        (1)                     /* local request */
238 #define RQ_USEDEFERRAL  (2)                     /* use deferral */
239 #define RQ_DROPME       (3)                     /* drop current reply */
240 #define RQ_SPLICE_OK    (4)                     /* turned off in gss privacy
241                                                  * to prevent encrypting page
242                                                  * cache pages */
243 #define RQ_VICTIM       (5)                     /* about to be shut down */
244 #define RQ_BUSY         (6)                     /* request is busy */
245 #define RQ_DATA         (7)                     /* request has data */
246         unsigned long           rq_flags;       /* flags field */
247         ktime_t                 rq_qtime;       /* enqueue time */
248
249         void *                  rq_argp;        /* decoded arguments */
250         void *                  rq_resp;        /* xdr'd results */
251         __be32                  *rq_accept_statp;
252         void *                  rq_auth_data;   /* flavor-specific data */
253         __be32                  rq_auth_stat;   /* authentication status */
254         int                     rq_auth_slack;  /* extra space xdr code
255                                                  * should leave in head
256                                                  * for krb5i, krb5p.
257                                                  */
258         int                     rq_reserved;    /* space on socket outq
259                                                  * reserved for this request
260                                                  */
261         ktime_t                 rq_stime;       /* start time */
262
263         struct cache_req        rq_chandle;     /* handle passed to caches for 
264                                                  * request delaying 
265                                                  */
266         /* Catering to nfsd */
267         struct auth_domain *    rq_client;      /* RPC peer info */
268         struct auth_domain *    rq_gssclient;   /* "gss/"-style peer info */
269         struct svc_cacherep *   rq_cacherep;    /* cache info */
270         struct task_struct      *rq_task;       /* service thread */
271         struct net              *rq_bc_net;     /* pointer to backchannel's
272                                                  * net namespace
273                                                  */
274         void **                 rq_lease_breaker; /* The v4 client breaking a lease */
275 };
276
277 #define SVC_NET(rqst) (rqst->rq_xprt ? rqst->rq_xprt->xpt_net : rqst->rq_bc_net)
278
279 /*
280  * Rigorous type checking on sockaddr type conversions
281  */
282 static inline struct sockaddr_in *svc_addr_in(const struct svc_rqst *rqst)
283 {
284         return (struct sockaddr_in *) &rqst->rq_addr;
285 }
286
287 static inline struct sockaddr_in6 *svc_addr_in6(const struct svc_rqst *rqst)
288 {
289         return (struct sockaddr_in6 *) &rqst->rq_addr;
290 }
291
292 static inline struct sockaddr *svc_addr(const struct svc_rqst *rqst)
293 {
294         return (struct sockaddr *) &rqst->rq_addr;
295 }
296
297 static inline struct sockaddr_in *svc_daddr_in(const struct svc_rqst *rqst)
298 {
299         return (struct sockaddr_in *) &rqst->rq_daddr;
300 }
301
302 static inline struct sockaddr_in6 *svc_daddr_in6(const struct svc_rqst *rqst)
303 {
304         return (struct sockaddr_in6 *) &rqst->rq_daddr;
305 }
306
307 static inline struct sockaddr *svc_daddr(const struct svc_rqst *rqst)
308 {
309         return (struct sockaddr *) &rqst->rq_daddr;
310 }
311
312 struct svc_deferred_req {
313         u32                     prot;   /* protocol (UDP or TCP) */
314         struct svc_xprt         *xprt;
315         struct sockaddr_storage addr;   /* where reply must go */
316         size_t                  addrlen;
317         struct sockaddr_storage daddr;  /* where reply must come from */
318         size_t                  daddrlen;
319         void                    *xprt_ctxt;
320         struct cache_deferred_req handle;
321         int                     argslen;
322         __be32                  args[];
323 };
324
325 struct svc_process_info {
326         union {
327                 int  (*dispatch)(struct svc_rqst *rqstp);
328                 struct {
329                         unsigned int lovers;
330                         unsigned int hivers;
331                 } mismatch;
332         };
333 };
334
335 /*
336  * List of RPC programs on the same transport endpoint
337  */
338 struct svc_program {
339         struct svc_program *    pg_next;        /* other programs (same xprt) */
340         u32                     pg_prog;        /* program number */
341         unsigned int            pg_lovers;      /* lowest version */
342         unsigned int            pg_hivers;      /* highest version */
343         unsigned int            pg_nvers;       /* number of versions */
344         const struct svc_version **pg_vers;     /* version array */
345         char *                  pg_name;        /* service name */
346         char *                  pg_class;       /* class name: services sharing authentication */
347         struct svc_stat *       pg_stats;       /* rpc statistics */
348         int                     (*pg_authenticate)(struct svc_rqst *);
349         __be32                  (*pg_init_request)(struct svc_rqst *,
350                                                    const struct svc_program *,
351                                                    struct svc_process_info *);
352         int                     (*pg_rpcbind_set)(struct net *net,
353                                                   const struct svc_program *,
354                                                   u32 version, int family,
355                                                   unsigned short proto,
356                                                   unsigned short port);
357 };
358
359 /*
360  * RPC program version
361  */
362 struct svc_version {
363         u32                     vs_vers;        /* version number */
364         u32                     vs_nproc;       /* number of procedures */
365         const struct svc_procedure *vs_proc;    /* per-procedure info */
366         unsigned long __percpu  *vs_count;      /* call counts */
367         u32                     vs_xdrsize;     /* xdrsize needed for this version */
368
369         /* Don't register with rpcbind */
370         bool                    vs_hidden;
371
372         /* Don't care if the rpcbind registration fails */
373         bool                    vs_rpcb_optnl;
374
375         /* Need xprt with congestion control */
376         bool                    vs_need_cong_ctrl;
377
378         /* Dispatch function */
379         int                     (*vs_dispatch)(struct svc_rqst *rqstp);
380 };
381
382 /*
383  * RPC procedure info
384  */
385 struct svc_procedure {
386         /* process the request: */
387         __be32                  (*pc_func)(struct svc_rqst *);
388         /* XDR decode args: */
389         bool                    (*pc_decode)(struct svc_rqst *rqstp,
390                                              struct xdr_stream *xdr);
391         /* XDR encode result: */
392         bool                    (*pc_encode)(struct svc_rqst *rqstp,
393                                              struct xdr_stream *xdr);
394         /* XDR free result: */
395         void                    (*pc_release)(struct svc_rqst *);
396         unsigned int            pc_argsize;     /* argument struct size */
397         unsigned int            pc_argzero;     /* how much of argument to clear */
398         unsigned int            pc_ressize;     /* result struct size */
399         unsigned int            pc_cachetype;   /* cache info (NFS) */
400         unsigned int            pc_xdrressize;  /* maximum size of XDR reply */
401         const char *            pc_name;        /* for display */
402 };
403
404 /*
405  * Function prototypes.
406  */
407 int svc_rpcb_setup(struct svc_serv *serv, struct net *net);
408 void svc_rpcb_cleanup(struct svc_serv *serv, struct net *net);
409 int svc_bind(struct svc_serv *serv, struct net *net);
410 struct svc_serv *svc_create(struct svc_program *, unsigned int,
411                             int (*threadfn)(void *data));
412 struct svc_rqst *svc_rqst_alloc(struct svc_serv *serv,
413                                         struct svc_pool *pool, int node);
414 bool               svc_rqst_replace_page(struct svc_rqst *rqstp,
415                                          struct page *page);
416 void               svc_rqst_release_pages(struct svc_rqst *rqstp);
417 void               svc_rqst_free(struct svc_rqst *);
418 void               svc_exit_thread(struct svc_rqst *);
419 struct svc_serv *  svc_create_pooled(struct svc_program *, unsigned int,
420                                      int (*threadfn)(void *data));
421 int                svc_set_num_threads(struct svc_serv *, struct svc_pool *, int);
422 int                svc_pool_stats_open(struct svc_serv *serv, struct file *file);
423 void               svc_process(struct svc_rqst *rqstp);
424 int                bc_svc_process(struct svc_serv *, struct rpc_rqst *,
425                         struct svc_rqst *);
426 int                svc_register(const struct svc_serv *, struct net *, const int,
427                                 const unsigned short, const unsigned short);
428
429 void               svc_wake_up(struct svc_serv *);
430 void               svc_reserve(struct svc_rqst *rqstp, int space);
431 struct svc_pool   *svc_pool_for_cpu(struct svc_serv *serv);
432 char *             svc_print_addr(struct svc_rqst *, char *, size_t);
433 const char *       svc_proc_name(const struct svc_rqst *rqstp);
434 int                svc_encode_result_payload(struct svc_rqst *rqstp,
435                                              unsigned int offset,
436                                              unsigned int length);
437 unsigned int       svc_fill_write_vector(struct svc_rqst *rqstp,
438                                          struct xdr_buf *payload);
439 char              *svc_fill_symlink_pathname(struct svc_rqst *rqstp,
440                                              struct kvec *first, void *p,
441                                              size_t total);
442 __be32             svc_generic_init_request(struct svc_rqst *rqstp,
443                                             const struct svc_program *progp,
444                                             struct svc_process_info *procinfo);
445 int                svc_generic_rpcbind_set(struct net *net,
446                                            const struct svc_program *progp,
447                                            u32 version, int family,
448                                            unsigned short proto,
449                                            unsigned short port);
450 int                svc_rpcbind_set_version(struct net *net,
451                                            const struct svc_program *progp,
452                                            u32 version, int family,
453                                            unsigned short proto,
454                                            unsigned short port);
455
456 #define RPC_MAX_ADDRBUFLEN      (63U)
457
458 /*
459  * When we want to reduce the size of the reserved space in the response
460  * buffer, we need to take into account the size of any checksum data that
461  * may be at the end of the packet. This is difficult to determine exactly
462  * for all cases without actually generating the checksum, so we just use a
463  * static value.
464  */
465 static inline void svc_reserve_auth(struct svc_rqst *rqstp, int space)
466 {
467         svc_reserve(rqstp, space + rqstp->rq_auth_slack);
468 }
469
470 /**
471  * svcxdr_init_decode - Prepare an xdr_stream for Call decoding
472  * @rqstp: controlling server RPC transaction context
473  *
474  */
475 static inline void svcxdr_init_decode(struct svc_rqst *rqstp)
476 {
477         struct xdr_stream *xdr = &rqstp->rq_arg_stream;
478         struct xdr_buf *buf = &rqstp->rq_arg;
479         struct kvec *argv = buf->head;
480
481         WARN_ON(buf->len != buf->head->iov_len + buf->page_len + buf->tail->iov_len);
482         buf->len = buf->head->iov_len + buf->page_len + buf->tail->iov_len;
483
484         xdr_init_decode(xdr, buf, argv->iov_base, NULL);
485         xdr_set_scratch_page(xdr, rqstp->rq_scratch_page);
486 }
487
488 /**
489  * svcxdr_init_encode - Prepare an xdr_stream for svc Reply encoding
490  * @rqstp: controlling server RPC transaction context
491  *
492  */
493 static inline void svcxdr_init_encode(struct svc_rqst *rqstp)
494 {
495         struct xdr_stream *xdr = &rqstp->rq_res_stream;
496         struct xdr_buf *buf = &rqstp->rq_res;
497         struct kvec *resv = buf->head;
498
499         xdr_reset_scratch_buffer(xdr);
500
501         xdr->buf = buf;
502         xdr->iov = resv;
503         xdr->p   = resv->iov_base + resv->iov_len;
504         xdr->end = resv->iov_base + PAGE_SIZE;
505         buf->len = resv->iov_len;
506         xdr->page_ptr = buf->pages - 1;
507         buf->buflen = PAGE_SIZE * (rqstp->rq_page_end - buf->pages);
508         xdr->rqst = NULL;
509 }
510
511 /**
512  * svcxdr_set_auth_slack -
513  * @rqstp: RPC transaction
514  * @slack: buffer space to reserve for the transaction's security flavor
515  *
516  * Set the request's slack space requirement, and set aside that much
517  * space in the rqstp's rq_res.head for use when the auth wraps the Reply.
518  */
519 static inline void svcxdr_set_auth_slack(struct svc_rqst *rqstp, int slack)
520 {
521         struct xdr_stream *xdr = &rqstp->rq_res_stream;
522         struct xdr_buf *buf = &rqstp->rq_res;
523         struct kvec *resv = buf->head;
524
525         rqstp->rq_auth_slack = slack;
526
527         xdr->end -= XDR_QUADLEN(slack);
528         buf->buflen -= rqstp->rq_auth_slack;
529
530         WARN_ON(xdr->iov != resv);
531         WARN_ON(xdr->p > xdr->end);
532 }
533
534 /**
535  * svcxdr_set_accept_stat - Reserve space for the accept_stat field
536  * @rqstp: RPC transaction context
537  *
538  * Return values:
539  *   %true: Success
540  *   %false: No response buffer space was available
541  */
542 static inline bool svcxdr_set_accept_stat(struct svc_rqst *rqstp)
543 {
544         struct xdr_stream *xdr = &rqstp->rq_res_stream;
545
546         rqstp->rq_accept_statp = xdr_reserve_space(xdr, XDR_UNIT);
547         if (unlikely(!rqstp->rq_accept_statp))
548                 return false;
549         *rqstp->rq_accept_statp = rpc_success;
550         return true;
551 }
552
553 #endif /* SUNRPC_SVC_H */