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