Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/net/sunrpc/svcsock.c | |
3 | * | |
4 | * These are the RPC server socket internals. | |
5 | * | |
6 | * The server scheduling algorithm does not always distribute the load | |
7 | * evenly when servicing a single client. May need to modify the | |
f6150c3c | 8 | * svc_xprt_enqueue procedure... |
1da177e4 LT |
9 | * |
10 | * TCP support is largely untested and may be a little slow. The problem | |
11 | * is that we currently do two separate recvfrom's, one for the 4-byte | |
12 | * record length, and the second for the actual record. This could possibly | |
13 | * be improved by always reading a minimum size of around 100 bytes and | |
14 | * tucking any superfluous bytes away in a temporary store. Still, that | |
15 | * leaves write requests out in the rain. An alternative may be to peek at | |
16 | * the first skb in the queue, and if it matches the next TCP sequence | |
17 | * number, to extract the record marker. Yuck. | |
18 | * | |
19 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> | |
20 | */ | |
21 | ||
172589cc | 22 | #include <linux/kernel.h> |
1da177e4 LT |
23 | #include <linux/sched.h> |
24 | #include <linux/errno.h> | |
25 | #include <linux/fcntl.h> | |
26 | #include <linux/net.h> | |
27 | #include <linux/in.h> | |
28 | #include <linux/inet.h> | |
29 | #include <linux/udp.h> | |
91483c4b | 30 | #include <linux/tcp.h> |
1da177e4 LT |
31 | #include <linux/unistd.h> |
32 | #include <linux/slab.h> | |
33 | #include <linux/netdevice.h> | |
34 | #include <linux/skbuff.h> | |
b41b66d6 | 35 | #include <linux/file.h> |
7dfb7103 | 36 | #include <linux/freezer.h> |
1da177e4 LT |
37 | #include <net/sock.h> |
38 | #include <net/checksum.h> | |
39 | #include <net/ip.h> | |
b92503b2 | 40 | #include <net/ipv6.h> |
c752f073 | 41 | #include <net/tcp_states.h> |
1da177e4 LT |
42 | #include <asm/uaccess.h> |
43 | #include <asm/ioctls.h> | |
44 | ||
45 | #include <linux/sunrpc/types.h> | |
ad06e4bd | 46 | #include <linux/sunrpc/clnt.h> |
1da177e4 LT |
47 | #include <linux/sunrpc/xdr.h> |
48 | #include <linux/sunrpc/svcsock.h> | |
49 | #include <linux/sunrpc/stats.h> | |
50 | ||
51 | /* SMP locking strategy: | |
52 | * | |
3262c816 GB |
53 | * svc_pool->sp_lock protects most of the fields of that pool. |
54 | * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. | |
55 | * when both need to be taken (rare), svc_serv->sv_lock is first. | |
56 | * BKL protects svc_serv->sv_nrthread. | |
7ac1bea5 N |
57 | * svc_sock->sk_lock protects the svc_sock->sk_deferred list |
58 | * and the ->sk_info_authunix cache. | |
02fc6c36 TT |
59 | * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being |
60 | * enqueued multiply. | |
1da177e4 LT |
61 | * |
62 | * Some flags can be set to certain values at any time | |
63 | * providing that certain rules are followed: | |
64 | * | |
02fc6c36 | 65 | * XPT_CONN, XPT_DATA, can be set or cleared at any time. |
f6150c3c | 66 | * after a set, svc_xprt_enqueue must be called. |
1da177e4 LT |
67 | * after a clear, the socket must be read/accepted |
68 | * if this succeeds, it must be set again. | |
02fc6c36 TT |
69 | * XPT_CLOSE can set at any time. It is never cleared. |
70 | * xpt_ref contains a bias of '1' until XPT_DEAD is set. | |
e1b3157f | 71 | * so when xprt_ref hits zero, we know the transport is dead |
aaf68cfb | 72 | * and no-one is using it. |
02fc6c36 | 73 | * XPT_DEAD can only be set while XPT_BUSY is held which ensures |
aaf68cfb | 74 | * no other thread will be using the socket or will try to |
02fc6c36 | 75 | * set XPT_DEAD. |
1da177e4 LT |
76 | * |
77 | */ | |
78 | ||
360d8738 | 79 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
1da177e4 LT |
80 | |
81 | ||
82 | static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, | |
6b174337 | 83 | int *errp, int flags); |
7a182083 | 84 | static void svc_delete_xprt(struct svc_xprt *xprt); |
1da177e4 LT |
85 | static void svc_udp_data_ready(struct sock *, int); |
86 | static int svc_udp_recvfrom(struct svc_rqst *); | |
87 | static int svc_udp_sendto(struct svc_rqst *); | |
7a182083 | 88 | static void svc_close_xprt(struct svc_xprt *xprt); |
755cceab TT |
89 | static void svc_sock_detach(struct svc_xprt *); |
90 | static void svc_sock_free(struct svc_xprt *); | |
1da177e4 LT |
91 | |
92 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk); | |
93 | static int svc_deferred_recv(struct svc_rqst *rqstp); | |
94 | static struct cache_deferred_req *svc_defer(struct cache_req *req); | |
b700cbb1 TT |
95 | static struct svc_xprt *svc_create_socket(struct svc_serv *, int, |
96 | struct sockaddr *, int, int); | |
1da177e4 | 97 | |
36bdfc8b GB |
98 | /* apparently the "standard" is that clients close |
99 | * idle connections after 5 minutes, servers after | |
100 | * 6 minutes | |
101 | * http://www.connectathon.org/talks96/nfstcp.pdf | |
102 | */ | |
103 | static int svc_conn_age_period = 6*60; | |
104 | ||
ed07536e PZ |
105 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
106 | static struct lock_class_key svc_key[2]; | |
107 | static struct lock_class_key svc_slock_key[2]; | |
108 | ||
109 | static inline void svc_reclassify_socket(struct socket *sock) | |
110 | { | |
111 | struct sock *sk = sock->sk; | |
02b3d346 | 112 | BUG_ON(sock_owned_by_user(sk)); |
ed07536e PZ |
113 | switch (sk->sk_family) { |
114 | case AF_INET: | |
115 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", | |
116 | &svc_slock_key[0], "sk_lock-AF_INET-NFSD", &svc_key[0]); | |
117 | break; | |
118 | ||
119 | case AF_INET6: | |
120 | sock_lock_init_class_and_name(sk, "slock-AF_INET6-NFSD", | |
121 | &svc_slock_key[1], "sk_lock-AF_INET6-NFSD", &svc_key[1]); | |
122 | break; | |
123 | ||
124 | default: | |
125 | BUG(); | |
126 | } | |
127 | } | |
128 | #else | |
129 | static inline void svc_reclassify_socket(struct socket *sock) | |
130 | { | |
131 | } | |
132 | #endif | |
133 | ||
ad06e4bd CL |
134 | static char *__svc_print_addr(struct sockaddr *addr, char *buf, size_t len) |
135 | { | |
136 | switch (addr->sa_family) { | |
137 | case AF_INET: | |
138 | snprintf(buf, len, "%u.%u.%u.%u, port=%u", | |
139 | NIPQUAD(((struct sockaddr_in *) addr)->sin_addr), | |
582ee43d | 140 | ntohs(((struct sockaddr_in *) addr)->sin_port)); |
ad06e4bd | 141 | break; |
5a05ed73 | 142 | |
ad06e4bd CL |
143 | case AF_INET6: |
144 | snprintf(buf, len, "%x:%x:%x:%x:%x:%x:%x:%x, port=%u", | |
145 | NIP6(((struct sockaddr_in6 *) addr)->sin6_addr), | |
582ee43d | 146 | ntohs(((struct sockaddr_in6 *) addr)->sin6_port)); |
ad06e4bd | 147 | break; |
5a05ed73 | 148 | |
ad06e4bd CL |
149 | default: |
150 | snprintf(buf, len, "unknown address type: %d", addr->sa_family); | |
151 | break; | |
152 | } | |
153 | return buf; | |
154 | } | |
155 | ||
156 | /** | |
157 | * svc_print_addr - Format rq_addr field for printing | |
158 | * @rqstp: svc_rqst struct containing address to print | |
159 | * @buf: target buffer for formatted address | |
160 | * @len: length of target buffer | |
161 | * | |
162 | */ | |
163 | char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len) | |
164 | { | |
27459f09 | 165 | return __svc_print_addr(svc_addr(rqstp), buf, len); |
ad06e4bd CL |
166 | } |
167 | EXPORT_SYMBOL_GPL(svc_print_addr); | |
168 | ||
1da177e4 | 169 | /* |
3262c816 | 170 | * Queue up an idle server thread. Must have pool->sp_lock held. |
1da177e4 | 171 | * Note: this is really a stack rather than a queue, so that we only |
3262c816 | 172 | * use as many different threads as we need, and the rest don't pollute |
1da177e4 LT |
173 | * the cache. |
174 | */ | |
175 | static inline void | |
3262c816 | 176 | svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp) |
1da177e4 | 177 | { |
3262c816 | 178 | list_add(&rqstp->rq_list, &pool->sp_threads); |
1da177e4 LT |
179 | } |
180 | ||
181 | /* | |
3262c816 | 182 | * Dequeue an nfsd thread. Must have pool->sp_lock held. |
1da177e4 LT |
183 | */ |
184 | static inline void | |
3262c816 | 185 | svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp) |
1da177e4 LT |
186 | { |
187 | list_del(&rqstp->rq_list); | |
188 | } | |
189 | ||
190 | /* | |
191 | * Release an skbuff after use | |
192 | */ | |
5148bf4e | 193 | static void svc_release_skb(struct svc_rqst *rqstp) |
1da177e4 | 194 | { |
5148bf4e | 195 | struct sk_buff *skb = rqstp->rq_xprt_ctxt; |
1da177e4 LT |
196 | struct svc_deferred_req *dr = rqstp->rq_deferred; |
197 | ||
198 | if (skb) { | |
5148bf4e | 199 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 LT |
200 | |
201 | dprintk("svc: service %p, releasing skb %p\n", rqstp, skb); | |
202 | skb_free_datagram(rqstp->rq_sock->sk_sk, skb); | |
203 | } | |
204 | if (dr) { | |
205 | rqstp->rq_deferred = NULL; | |
206 | kfree(dr); | |
207 | } | |
208 | } | |
209 | ||
1da177e4 LT |
210 | /* |
211 | * Queue up a socket with data pending. If there are idle nfsd | |
212 | * processes, wake 'em up. | |
213 | * | |
214 | */ | |
f6150c3c | 215 | void svc_xprt_enqueue(struct svc_xprt *xprt) |
1da177e4 | 216 | { |
f6150c3c | 217 | struct svc_serv *serv = xprt->xpt_server; |
bfd24160 | 218 | struct svc_pool *pool; |
1da177e4 | 219 | struct svc_rqst *rqstp; |
bfd24160 | 220 | int cpu; |
1da177e4 | 221 | |
f6150c3c | 222 | if (!(xprt->xpt_flags & |
02fc6c36 | 223 | ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED)))) |
1da177e4 | 224 | return; |
f6150c3c | 225 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
1da177e4 LT |
226 | return; |
227 | ||
bfd24160 | 228 | cpu = get_cpu(); |
f6150c3c | 229 | pool = svc_pool_for_cpu(xprt->xpt_server, cpu); |
bfd24160 GB |
230 | put_cpu(); |
231 | ||
3262c816 | 232 | spin_lock_bh(&pool->sp_lock); |
1da177e4 | 233 | |
3262c816 GB |
234 | if (!list_empty(&pool->sp_threads) && |
235 | !list_empty(&pool->sp_sockets)) | |
1da177e4 | 236 | printk(KERN_ERR |
f6150c3c TT |
237 | "svc_xprt_enqueue: " |
238 | "threads and transports both waiting??\n"); | |
1da177e4 | 239 | |
f6150c3c | 240 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) { |
1da177e4 | 241 | /* Don't enqueue dead sockets */ |
f6150c3c | 242 | dprintk("svc: transport %p is dead, not enqueued\n", xprt); |
1da177e4 LT |
243 | goto out_unlock; |
244 | } | |
245 | ||
c081a0c7 GB |
246 | /* Mark socket as busy. It will remain in this state until the |
247 | * server has processed all pending data and put the socket back | |
02fc6c36 | 248 | * on the idle list. We update XPT_BUSY atomically because |
c081a0c7 GB |
249 | * it also guards against trying to enqueue the svc_sock twice. |
250 | */ | |
f6150c3c | 251 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) { |
c081a0c7 | 252 | /* Don't enqueue socket while already enqueued */ |
f6150c3c | 253 | dprintk("svc: transport %p busy, not enqueued\n", xprt); |
1da177e4 LT |
254 | goto out_unlock; |
255 | } | |
f6150c3c TT |
256 | BUG_ON(xprt->xpt_pool != NULL); |
257 | xprt->xpt_pool = pool; | |
1da177e4 | 258 | |
323bee32 | 259 | /* Handle pending connection */ |
f6150c3c | 260 | if (test_bit(XPT_CONN, &xprt->xpt_flags)) |
323bee32 TT |
261 | goto process; |
262 | ||
263 | /* Handle close in-progress */ | |
f6150c3c | 264 | if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) |
323bee32 TT |
265 | goto process; |
266 | ||
267 | /* Check if we have space to reply to a request */ | |
f6150c3c | 268 | if (!xprt->xpt_ops->xpo_has_wspace(xprt)) { |
1da177e4 | 269 | /* Don't enqueue while not enough space for reply */ |
f6150c3c TT |
270 | dprintk("svc: no write space, transport %p not enqueued\n", |
271 | xprt); | |
272 | xprt->xpt_pool = NULL; | |
273 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1da177e4 LT |
274 | goto out_unlock; |
275 | } | |
1da177e4 | 276 | |
323bee32 | 277 | process: |
3262c816 GB |
278 | if (!list_empty(&pool->sp_threads)) { |
279 | rqstp = list_entry(pool->sp_threads.next, | |
1da177e4 LT |
280 | struct svc_rqst, |
281 | rq_list); | |
f6150c3c TT |
282 | dprintk("svc: transport %p served by daemon %p\n", |
283 | xprt, rqstp); | |
3262c816 | 284 | svc_thread_dequeue(pool, rqstp); |
f6150c3c | 285 | if (rqstp->rq_xprt) |
cca5172a | 286 | printk(KERN_ERR |
f6150c3c TT |
287 | "svc_xprt_enqueue: server %p, rq_xprt=%p!\n", |
288 | rqstp, rqstp->rq_xprt); | |
289 | rqstp->rq_xprt = xprt; | |
290 | svc_xprt_get(xprt); | |
c6b0a9f8 | 291 | rqstp->rq_reserved = serv->sv_max_mesg; |
f6150c3c TT |
292 | atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved); |
293 | BUG_ON(xprt->xpt_pool != pool); | |
1da177e4 LT |
294 | wake_up(&rqstp->rq_wait); |
295 | } else { | |
f6150c3c TT |
296 | dprintk("svc: transport %p put into queue\n", xprt); |
297 | list_add_tail(&xprt->xpt_ready, &pool->sp_sockets); | |
298 | BUG_ON(xprt->xpt_pool != pool); | |
1da177e4 LT |
299 | } |
300 | ||
301 | out_unlock: | |
3262c816 | 302 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 | 303 | } |
f6150c3c | 304 | EXPORT_SYMBOL_GPL(svc_xprt_enqueue); |
1da177e4 LT |
305 | |
306 | /* | |
3262c816 | 307 | * Dequeue the first socket. Must be called with the pool->sp_lock held. |
1da177e4 LT |
308 | */ |
309 | static inline struct svc_sock * | |
3262c816 | 310 | svc_sock_dequeue(struct svc_pool *pool) |
1da177e4 LT |
311 | { |
312 | struct svc_sock *svsk; | |
313 | ||
3262c816 | 314 | if (list_empty(&pool->sp_sockets)) |
1da177e4 LT |
315 | return NULL; |
316 | ||
3262c816 | 317 | svsk = list_entry(pool->sp_sockets.next, |
7a182083 TT |
318 | struct svc_sock, sk_xprt.xpt_ready); |
319 | list_del_init(&svsk->sk_xprt.xpt_ready); | |
1da177e4 LT |
320 | |
321 | dprintk("svc: socket %p dequeued, inuse=%d\n", | |
e1b3157f | 322 | svsk->sk_sk, atomic_read(&svsk->sk_xprt.xpt_ref.refcount)); |
1da177e4 LT |
323 | |
324 | return svsk; | |
325 | } | |
326 | ||
327 | /* | |
a6046f71 TT |
328 | * svc_xprt_received conditionally queues the transport for processing |
329 | * by another thread. The caller must hold the XPT_BUSY bit and must | |
330 | * not thereafter touch transport data. | |
331 | * | |
332 | * Note: XPT_DATA only gets cleared when a read-attempt finds no (or | |
333 | * insufficient) data. | |
1da177e4 | 334 | */ |
a6046f71 | 335 | void svc_xprt_received(struct svc_xprt *xprt) |
1da177e4 | 336 | { |
a6046f71 TT |
337 | BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags)); |
338 | xprt->xpt_pool = NULL; | |
339 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
340 | svc_xprt_enqueue(xprt); | |
1da177e4 | 341 | } |
a6046f71 | 342 | EXPORT_SYMBOL_GPL(svc_xprt_received); |
1da177e4 LT |
343 | |
344 | /** | |
345 | * svc_reserve - change the space reserved for the reply to a request. | |
346 | * @rqstp: The request in question | |
347 | * @space: new max space to reserve | |
348 | * | |
349 | * Each request reserves some space on the output queue of the socket | |
350 | * to make sure the reply fits. This function reduces that reserved | |
351 | * space to be the amount of space used already, plus @space. | |
352 | * | |
353 | */ | |
354 | void svc_reserve(struct svc_rqst *rqstp, int space) | |
355 | { | |
356 | space += rqstp->rq_res.head[0].iov_len; | |
357 | ||
358 | if (space < rqstp->rq_reserved) { | |
f6150c3c TT |
359 | struct svc_xprt *xprt = rqstp->rq_xprt; |
360 | atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved); | |
1da177e4 | 361 | rqstp->rq_reserved = space; |
1da177e4 | 362 | |
f6150c3c | 363 | svc_xprt_enqueue(xprt); |
1da177e4 LT |
364 | } |
365 | } | |
366 | ||
1da177e4 LT |
367 | static void |
368 | svc_sock_release(struct svc_rqst *rqstp) | |
369 | { | |
370 | struct svc_sock *svsk = rqstp->rq_sock; | |
371 | ||
5148bf4e | 372 | rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp); |
1da177e4 | 373 | |
44524359 | 374 | svc_free_res_pages(rqstp); |
1da177e4 LT |
375 | rqstp->rq_res.page_len = 0; |
376 | rqstp->rq_res.page_base = 0; | |
377 | ||
378 | ||
379 | /* Reset response buffer and release | |
380 | * the reservation. | |
381 | * But first, check that enough space was reserved | |
382 | * for the reply, otherwise we have a bug! | |
383 | */ | |
384 | if ((rqstp->rq_res.len) > rqstp->rq_reserved) | |
385 | printk(KERN_ERR "RPC request reserved %d but used %d\n", | |
386 | rqstp->rq_reserved, | |
387 | rqstp->rq_res.len); | |
388 | ||
389 | rqstp->rq_res.head[0].iov_len = 0; | |
390 | svc_reserve(rqstp, 0); | |
391 | rqstp->rq_sock = NULL; | |
392 | ||
e1b3157f | 393 | svc_xprt_put(&svsk->sk_xprt); |
1da177e4 LT |
394 | } |
395 | ||
396 | /* | |
397 | * External function to wake up a server waiting for data | |
3262c816 GB |
398 | * This really only makes sense for services like lockd |
399 | * which have exactly one thread anyway. | |
1da177e4 LT |
400 | */ |
401 | void | |
402 | svc_wake_up(struct svc_serv *serv) | |
403 | { | |
404 | struct svc_rqst *rqstp; | |
3262c816 GB |
405 | unsigned int i; |
406 | struct svc_pool *pool; | |
407 | ||
408 | for (i = 0; i < serv->sv_nrpools; i++) { | |
409 | pool = &serv->sv_pools[i]; | |
410 | ||
411 | spin_lock_bh(&pool->sp_lock); | |
412 | if (!list_empty(&pool->sp_threads)) { | |
413 | rqstp = list_entry(pool->sp_threads.next, | |
414 | struct svc_rqst, | |
415 | rq_list); | |
416 | dprintk("svc: daemon %p woken up.\n", rqstp); | |
417 | /* | |
418 | svc_thread_dequeue(pool, rqstp); | |
419 | rqstp->rq_sock = NULL; | |
420 | */ | |
421 | wake_up(&rqstp->rq_wait); | |
422 | } | |
423 | spin_unlock_bh(&pool->sp_lock); | |
1da177e4 | 424 | } |
1da177e4 LT |
425 | } |
426 | ||
b92503b2 CL |
427 | union svc_pktinfo_u { |
428 | struct in_pktinfo pkti; | |
b92503b2 | 429 | struct in6_pktinfo pkti6; |
b92503b2 | 430 | }; |
bc375ea7 DM |
431 | #define SVC_PKTINFO_SPACE \ |
432 | CMSG_SPACE(sizeof(union svc_pktinfo_u)) | |
b92503b2 CL |
433 | |
434 | static void svc_set_cmsg_data(struct svc_rqst *rqstp, struct cmsghdr *cmh) | |
435 | { | |
436 | switch (rqstp->rq_sock->sk_sk->sk_family) { | |
437 | case AF_INET: { | |
438 | struct in_pktinfo *pki = CMSG_DATA(cmh); | |
439 | ||
440 | cmh->cmsg_level = SOL_IP; | |
441 | cmh->cmsg_type = IP_PKTINFO; | |
442 | pki->ipi_ifindex = 0; | |
443 | pki->ipi_spec_dst.s_addr = rqstp->rq_daddr.addr.s_addr; | |
444 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); | |
445 | } | |
446 | break; | |
5a05ed73 | 447 | |
b92503b2 CL |
448 | case AF_INET6: { |
449 | struct in6_pktinfo *pki = CMSG_DATA(cmh); | |
450 | ||
451 | cmh->cmsg_level = SOL_IPV6; | |
452 | cmh->cmsg_type = IPV6_PKTINFO; | |
453 | pki->ipi6_ifindex = 0; | |
454 | ipv6_addr_copy(&pki->ipi6_addr, | |
455 | &rqstp->rq_daddr.addr6); | |
456 | cmh->cmsg_len = CMSG_LEN(sizeof(*pki)); | |
457 | } | |
458 | break; | |
b92503b2 CL |
459 | } |
460 | return; | |
461 | } | |
462 | ||
1da177e4 LT |
463 | /* |
464 | * Generic sendto routine | |
465 | */ | |
466 | static int | |
467 | svc_sendto(struct svc_rqst *rqstp, struct xdr_buf *xdr) | |
468 | { | |
469 | struct svc_sock *svsk = rqstp->rq_sock; | |
470 | struct socket *sock = svsk->sk_sock; | |
471 | int slen; | |
bc375ea7 DM |
472 | union { |
473 | struct cmsghdr hdr; | |
474 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
475 | } buffer; | |
476 | struct cmsghdr *cmh = &buffer.hdr; | |
1da177e4 LT |
477 | int len = 0; |
478 | int result; | |
479 | int size; | |
480 | struct page **ppage = xdr->pages; | |
481 | size_t base = xdr->page_base; | |
482 | unsigned int pglen = xdr->page_len; | |
483 | unsigned int flags = MSG_MORE; | |
ad06e4bd | 484 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 LT |
485 | |
486 | slen = xdr->len; | |
487 | ||
488 | if (rqstp->rq_prot == IPPROTO_UDP) { | |
b92503b2 CL |
489 | struct msghdr msg = { |
490 | .msg_name = &rqstp->rq_addr, | |
491 | .msg_namelen = rqstp->rq_addrlen, | |
492 | .msg_control = cmh, | |
493 | .msg_controllen = sizeof(buffer), | |
494 | .msg_flags = MSG_MORE, | |
495 | }; | |
496 | ||
497 | svc_set_cmsg_data(rqstp, cmh); | |
1da177e4 LT |
498 | |
499 | if (sock_sendmsg(sock, &msg, 0) < 0) | |
500 | goto out; | |
501 | } | |
502 | ||
503 | /* send head */ | |
504 | if (slen == xdr->head[0].iov_len) | |
505 | flags = 0; | |
44524359 N |
506 | len = kernel_sendpage(sock, rqstp->rq_respages[0], 0, |
507 | xdr->head[0].iov_len, flags); | |
1da177e4 LT |
508 | if (len != xdr->head[0].iov_len) |
509 | goto out; | |
510 | slen -= xdr->head[0].iov_len; | |
511 | if (slen == 0) | |
512 | goto out; | |
513 | ||
514 | /* send page data */ | |
515 | size = PAGE_SIZE - base < pglen ? PAGE_SIZE - base : pglen; | |
516 | while (pglen > 0) { | |
517 | if (slen == size) | |
518 | flags = 0; | |
e6242e92 | 519 | result = kernel_sendpage(sock, *ppage, base, size, flags); |
1da177e4 LT |
520 | if (result > 0) |
521 | len += result; | |
522 | if (result != size) | |
523 | goto out; | |
524 | slen -= size; | |
525 | pglen -= size; | |
526 | size = PAGE_SIZE < pglen ? PAGE_SIZE : pglen; | |
527 | base = 0; | |
528 | ppage++; | |
529 | } | |
530 | /* send tail */ | |
531 | if (xdr->tail[0].iov_len) { | |
44524359 N |
532 | result = kernel_sendpage(sock, rqstp->rq_respages[0], |
533 | ((unsigned long)xdr->tail[0].iov_base) | |
cca5172a | 534 | & (PAGE_SIZE-1), |
1da177e4 LT |
535 | xdr->tail[0].iov_len, 0); |
536 | ||
537 | if (result > 0) | |
538 | len += result; | |
539 | } | |
540 | out: | |
ad06e4bd CL |
541 | dprintk("svc: socket %p sendto([%p %Zu... ], %d) = %d (addr %s)\n", |
542 | rqstp->rq_sock, xdr->head[0].iov_base, xdr->head[0].iov_len, | |
543 | xdr->len, len, svc_print_addr(rqstp, buf, sizeof(buf))); | |
1da177e4 LT |
544 | |
545 | return len; | |
546 | } | |
547 | ||
80212d59 N |
548 | /* |
549 | * Report socket names for nfsdfs | |
550 | */ | |
551 | static int one_sock_name(char *buf, struct svc_sock *svsk) | |
552 | { | |
553 | int len; | |
554 | ||
555 | switch(svsk->sk_sk->sk_family) { | |
556 | case AF_INET: | |
557 | len = sprintf(buf, "ipv4 %s %u.%u.%u.%u %d\n", | |
558 | svsk->sk_sk->sk_protocol==IPPROTO_UDP? | |
559 | "udp" : "tcp", | |
560 | NIPQUAD(inet_sk(svsk->sk_sk)->rcv_saddr), | |
561 | inet_sk(svsk->sk_sk)->num); | |
562 | break; | |
563 | default: | |
564 | len = sprintf(buf, "*unknown-%d*\n", | |
565 | svsk->sk_sk->sk_family); | |
566 | } | |
567 | return len; | |
568 | } | |
569 | ||
570 | int | |
b41b66d6 | 571 | svc_sock_names(char *buf, struct svc_serv *serv, char *toclose) |
80212d59 | 572 | { |
b41b66d6 | 573 | struct svc_sock *svsk, *closesk = NULL; |
80212d59 N |
574 | int len = 0; |
575 | ||
576 | if (!serv) | |
577 | return 0; | |
aaf68cfb | 578 | spin_lock_bh(&serv->sv_lock); |
7a182083 | 579 | list_for_each_entry(svsk, &serv->sv_permsocks, sk_xprt.xpt_list) { |
80212d59 | 580 | int onelen = one_sock_name(buf+len, svsk); |
b41b66d6 N |
581 | if (toclose && strcmp(toclose, buf+len) == 0) |
582 | closesk = svsk; | |
583 | else | |
584 | len += onelen; | |
80212d59 | 585 | } |
aaf68cfb | 586 | spin_unlock_bh(&serv->sv_lock); |
b41b66d6 | 587 | if (closesk) |
5680c446 N |
588 | /* Should unregister with portmap, but you cannot |
589 | * unregister just one protocol... | |
590 | */ | |
7a182083 | 591 | svc_close_xprt(&closesk->sk_xprt); |
37a03472 N |
592 | else if (toclose) |
593 | return -ENOENT; | |
80212d59 N |
594 | return len; |
595 | } | |
596 | EXPORT_SYMBOL(svc_sock_names); | |
597 | ||
1da177e4 LT |
598 | /* |
599 | * Check input queue length | |
600 | */ | |
601 | static int | |
602 | svc_recv_available(struct svc_sock *svsk) | |
603 | { | |
1da177e4 LT |
604 | struct socket *sock = svsk->sk_sock; |
605 | int avail, err; | |
606 | ||
e6242e92 | 607 | err = kernel_sock_ioctl(sock, TIOCINQ, (unsigned long) &avail); |
1da177e4 LT |
608 | |
609 | return (err >= 0)? avail : err; | |
610 | } | |
611 | ||
612 | /* | |
613 | * Generic recvfrom routine. | |
614 | */ | |
615 | static int | |
616 | svc_recvfrom(struct svc_rqst *rqstp, struct kvec *iov, int nr, int buflen) | |
617 | { | |
067d7817 | 618 | struct svc_sock *svsk = rqstp->rq_sock; |
1ba95105 CL |
619 | struct msghdr msg = { |
620 | .msg_flags = MSG_DONTWAIT, | |
621 | }; | |
a9747692 | 622 | struct sockaddr *sin; |
1ba95105 | 623 | int len; |
1da177e4 | 624 | |
1ba95105 CL |
625 | len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen, |
626 | msg.msg_flags); | |
1da177e4 LT |
627 | |
628 | /* sock_recvmsg doesn't fill in the name/namelen, so we must.. | |
1da177e4 | 629 | */ |
067d7817 CL |
630 | memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen); |
631 | rqstp->rq_addrlen = svsk->sk_remotelen; | |
1da177e4 | 632 | |
a9747692 FM |
633 | /* Destination address in request is needed for binding the |
634 | * source address in RPC callbacks later. | |
635 | */ | |
636 | sin = (struct sockaddr *)&svsk->sk_local; | |
637 | switch (sin->sa_family) { | |
638 | case AF_INET: | |
639 | rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr; | |
640 | break; | |
641 | case AF_INET6: | |
642 | rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr; | |
643 | break; | |
644 | } | |
645 | ||
1da177e4 | 646 | dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n", |
1ba95105 | 647 | svsk, iov[0].iov_base, iov[0].iov_len, len); |
1da177e4 LT |
648 | |
649 | return len; | |
650 | } | |
651 | ||
652 | /* | |
653 | * Set socket snd and rcv buffer lengths | |
654 | */ | |
655 | static inline void | |
656 | svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv) | |
657 | { | |
658 | #if 0 | |
659 | mm_segment_t oldfs; | |
660 | oldfs = get_fs(); set_fs(KERNEL_DS); | |
661 | sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, | |
662 | (char*)&snd, sizeof(snd)); | |
663 | sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, | |
664 | (char*)&rcv, sizeof(rcv)); | |
665 | #else | |
666 | /* sock_setsockopt limits use to sysctl_?mem_max, | |
667 | * which isn't acceptable. Until that is made conditional | |
668 | * on not having CAP_SYS_RESOURCE or similar, we go direct... | |
669 | * DaveM said I could! | |
670 | */ | |
671 | lock_sock(sock->sk); | |
672 | sock->sk->sk_sndbuf = snd * 2; | |
673 | sock->sk->sk_rcvbuf = rcv * 2; | |
674 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK; | |
675 | release_sock(sock->sk); | |
676 | #endif | |
677 | } | |
678 | /* | |
679 | * INET callback when data has been received on the socket. | |
680 | */ | |
681 | static void | |
682 | svc_udp_data_ready(struct sock *sk, int count) | |
683 | { | |
939bb7ef | 684 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 | 685 | |
939bb7ef NB |
686 | if (svsk) { |
687 | dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n", | |
02fc6c36 TT |
688 | svsk, sk, count, |
689 | test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); | |
690 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
f6150c3c | 691 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 692 | } |
1da177e4 LT |
693 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
694 | wake_up_interruptible(sk->sk_sleep); | |
695 | } | |
696 | ||
697 | /* | |
698 | * INET callback when space is newly available on the socket. | |
699 | */ | |
700 | static void | |
701 | svc_write_space(struct sock *sk) | |
702 | { | |
703 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); | |
704 | ||
705 | if (svsk) { | |
706 | dprintk("svc: socket %p(inet %p), write_space busy=%d\n", | |
02fc6c36 | 707 | svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
f6150c3c | 708 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 LT |
709 | } |
710 | ||
711 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) { | |
939bb7ef | 712 | dprintk("RPC svc_write_space: someone sleeping on %p\n", |
1da177e4 LT |
713 | svsk); |
714 | wake_up_interruptible(sk->sk_sleep); | |
715 | } | |
716 | } | |
717 | ||
7a37f578 N |
718 | static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp, |
719 | struct cmsghdr *cmh) | |
95756482 CL |
720 | { |
721 | switch (rqstp->rq_sock->sk_sk->sk_family) { | |
722 | case AF_INET: { | |
7a37f578 N |
723 | struct in_pktinfo *pki = CMSG_DATA(cmh); |
724 | rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr; | |
95756482 | 725 | break; |
7a37f578 | 726 | } |
95756482 | 727 | case AF_INET6: { |
7a37f578 N |
728 | struct in6_pktinfo *pki = CMSG_DATA(cmh); |
729 | ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr); | |
95756482 | 730 | break; |
7a37f578 | 731 | } |
95756482 | 732 | } |
95756482 CL |
733 | } |
734 | ||
1da177e4 LT |
735 | /* |
736 | * Receive a datagram from a UDP socket. | |
737 | */ | |
1da177e4 LT |
738 | static int |
739 | svc_udp_recvfrom(struct svc_rqst *rqstp) | |
740 | { | |
741 | struct svc_sock *svsk = rqstp->rq_sock; | |
bb5cf160 | 742 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 | 743 | struct sk_buff *skb; |
bc375ea7 DM |
744 | union { |
745 | struct cmsghdr hdr; | |
746 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; | |
747 | } buffer; | |
748 | struct cmsghdr *cmh = &buffer.hdr; | |
1da177e4 | 749 | int err, len; |
7a37f578 N |
750 | struct msghdr msg = { |
751 | .msg_name = svc_addr(rqstp), | |
752 | .msg_control = cmh, | |
753 | .msg_controllen = sizeof(buffer), | |
754 | .msg_flags = MSG_DONTWAIT, | |
755 | }; | |
1da177e4 | 756 | |
02fc6c36 | 757 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
1da177e4 LT |
758 | /* udp sockets need large rcvbuf as all pending |
759 | * requests are still in that buffer. sndbuf must | |
760 | * also be large enough that there is enough space | |
3262c816 GB |
761 | * for one reply per thread. We count all threads |
762 | * rather than threads in a particular pool, which | |
763 | * provides an upper bound on the number of threads | |
764 | * which will access the socket. | |
1da177e4 LT |
765 | */ |
766 | svc_sock_setbufsize(svsk->sk_sock, | |
c6b0a9f8 N |
767 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
768 | (serv->sv_nrthreads+3) * serv->sv_max_mesg); | |
1da177e4 LT |
769 | |
770 | if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) { | |
a6046f71 | 771 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
772 | return svc_deferred_recv(rqstp); |
773 | } | |
774 | ||
02fc6c36 | 775 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
05ed690e N |
776 | skb = NULL; |
777 | err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, | |
778 | 0, 0, MSG_PEEK | MSG_DONTWAIT); | |
779 | if (err >= 0) | |
780 | skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err); | |
781 | ||
782 | if (skb == NULL) { | |
783 | if (err != -EAGAIN) { | |
784 | /* possibly an icmp error */ | |
785 | dprintk("svc: recvfrom returned error %d\n", -err); | |
02fc6c36 | 786 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 787 | } |
a6046f71 | 788 | svc_xprt_received(&svsk->sk_xprt); |
05ed690e | 789 | return -EAGAIN; |
1da177e4 | 790 | } |
7a37f578 | 791 | rqstp->rq_addrlen = sizeof(rqstp->rq_addr); |
b7aa0bf7 ED |
792 | if (skb->tstamp.tv64 == 0) { |
793 | skb->tstamp = ktime_get_real(); | |
cca5172a | 794 | /* Don't enable netstamp, sunrpc doesn't |
1da177e4 LT |
795 | need that much accuracy */ |
796 | } | |
b7aa0bf7 | 797 | svsk->sk_sk->sk_stamp = skb->tstamp; |
02fc6c36 | 798 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* there may be more data... */ |
1da177e4 LT |
799 | |
800 | /* | |
801 | * Maybe more packets - kick another thread ASAP. | |
802 | */ | |
a6046f71 | 803 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
804 | |
805 | len = skb->len - sizeof(struct udphdr); | |
806 | rqstp->rq_arg.len = len; | |
807 | ||
95756482 | 808 | rqstp->rq_prot = IPPROTO_UDP; |
27459f09 | 809 | |
7a37f578 N |
810 | if (cmh->cmsg_level != IPPROTO_IP || |
811 | cmh->cmsg_type != IP_PKTINFO) { | |
812 | if (net_ratelimit()) | |
813 | printk("rpcsvc: received unknown control message:" | |
814 | "%d/%d\n", | |
815 | cmh->cmsg_level, cmh->cmsg_type); | |
816 | skb_free_datagram(svsk->sk_sk, skb); | |
817 | return 0; | |
818 | } | |
819 | svc_udp_get_dest_address(rqstp, cmh); | |
1da177e4 LT |
820 | |
821 | if (skb_is_nonlinear(skb)) { | |
822 | /* we have to copy */ | |
823 | local_bh_disable(); | |
824 | if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { | |
825 | local_bh_enable(); | |
826 | /* checksum error */ | |
827 | skb_free_datagram(svsk->sk_sk, skb); | |
828 | return 0; | |
829 | } | |
830 | local_bh_enable(); | |
cca5172a | 831 | skb_free_datagram(svsk->sk_sk, skb); |
1da177e4 LT |
832 | } else { |
833 | /* we can use it in-place */ | |
834 | rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr); | |
835 | rqstp->rq_arg.head[0].iov_len = len; | |
fb286bb2 HX |
836 | if (skb_checksum_complete(skb)) { |
837 | skb_free_datagram(svsk->sk_sk, skb); | |
838 | return 0; | |
1da177e4 | 839 | } |
5148bf4e | 840 | rqstp->rq_xprt_ctxt = skb; |
1da177e4 LT |
841 | } |
842 | ||
843 | rqstp->rq_arg.page_base = 0; | |
844 | if (len <= rqstp->rq_arg.head[0].iov_len) { | |
845 | rqstp->rq_arg.head[0].iov_len = len; | |
846 | rqstp->rq_arg.page_len = 0; | |
44524359 | 847 | rqstp->rq_respages = rqstp->rq_pages+1; |
1da177e4 LT |
848 | } else { |
849 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; | |
44524359 | 850 | rqstp->rq_respages = rqstp->rq_pages + 1 + |
172589cc | 851 | DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); |
1da177e4 LT |
852 | } |
853 | ||
854 | if (serv->sv_stats) | |
855 | serv->sv_stats->netudpcnt++; | |
856 | ||
857 | return len; | |
858 | } | |
859 | ||
860 | static int | |
861 | svc_udp_sendto(struct svc_rqst *rqstp) | |
862 | { | |
863 | int error; | |
864 | ||
865 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
866 | if (error == -ECONNREFUSED) | |
867 | /* ICMP error on earlier request. */ | |
868 | error = svc_sendto(rqstp, &rqstp->rq_res); | |
869 | ||
870 | return error; | |
871 | } | |
872 | ||
e831fe65 TT |
873 | static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp) |
874 | { | |
875 | } | |
876 | ||
323bee32 TT |
877 | static int svc_udp_has_wspace(struct svc_xprt *xprt) |
878 | { | |
879 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 880 | struct svc_serv *serv = xprt->xpt_server; |
323bee32 TT |
881 | unsigned long required; |
882 | ||
883 | /* | |
884 | * Set the SOCK_NOSPACE flag before checking the available | |
885 | * sock space. | |
886 | */ | |
887 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
7a90e8cc | 888 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
323bee32 TT |
889 | if (required*2 > sock_wspace(svsk->sk_sk)) |
890 | return 0; | |
891 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
892 | return 1; | |
893 | } | |
894 | ||
38a417cc TT |
895 | static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) |
896 | { | |
897 | BUG(); | |
898 | return NULL; | |
899 | } | |
900 | ||
b700cbb1 TT |
901 | static struct svc_xprt *svc_udp_create(struct svc_serv *serv, |
902 | struct sockaddr *sa, int salen, | |
903 | int flags) | |
904 | { | |
905 | return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags); | |
906 | } | |
907 | ||
360d8738 | 908 | static struct svc_xprt_ops svc_udp_ops = { |
b700cbb1 | 909 | .xpo_create = svc_udp_create, |
5d137990 TT |
910 | .xpo_recvfrom = svc_udp_recvfrom, |
911 | .xpo_sendto = svc_udp_sendto, | |
5148bf4e | 912 | .xpo_release_rqst = svc_release_skb, |
755cceab TT |
913 | .xpo_detach = svc_sock_detach, |
914 | .xpo_free = svc_sock_free, | |
e831fe65 | 915 | .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr, |
323bee32 | 916 | .xpo_has_wspace = svc_udp_has_wspace, |
38a417cc | 917 | .xpo_accept = svc_udp_accept, |
360d8738 TT |
918 | }; |
919 | ||
920 | static struct svc_xprt_class svc_udp_class = { | |
921 | .xcl_name = "udp", | |
b700cbb1 | 922 | .xcl_owner = THIS_MODULE, |
360d8738 | 923 | .xcl_ops = &svc_udp_ops, |
49023155 | 924 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, |
360d8738 TT |
925 | }; |
926 | ||
bb5cf160 | 927 | static void svc_udp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 | 928 | { |
7a37f578 N |
929 | int one = 1; |
930 | mm_segment_t oldfs; | |
931 | ||
bb5cf160 | 932 | svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv); |
1da177e4 LT |
933 | svsk->sk_sk->sk_data_ready = svc_udp_data_ready; |
934 | svsk->sk_sk->sk_write_space = svc_write_space; | |
1da177e4 LT |
935 | |
936 | /* initialise setting must have enough space to | |
cca5172a | 937 | * receive and respond to one request. |
1da177e4 LT |
938 | * svc_udp_recvfrom will re-adjust if necessary |
939 | */ | |
940 | svc_sock_setbufsize(svsk->sk_sock, | |
bb5cf160 TT |
941 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
942 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); | |
1da177e4 | 943 | |
02fc6c36 TT |
944 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */ |
945 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); | |
7a37f578 N |
946 | |
947 | oldfs = get_fs(); | |
948 | set_fs(KERNEL_DS); | |
949 | /* make sure we get destination address info */ | |
950 | svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO, | |
951 | (char __user *)&one, sizeof(one)); | |
952 | set_fs(oldfs); | |
1da177e4 LT |
953 | } |
954 | ||
955 | /* | |
956 | * A data_ready event on a listening socket means there's a connection | |
957 | * pending. Do not use state_change as a substitute for it. | |
958 | */ | |
959 | static void | |
960 | svc_tcp_listen_data_ready(struct sock *sk, int count_unused) | |
961 | { | |
939bb7ef | 962 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
963 | |
964 | dprintk("svc: socket %p TCP (listen) state change %d\n", | |
939bb7ef | 965 | sk, sk->sk_state); |
1da177e4 | 966 | |
939bb7ef NB |
967 | /* |
968 | * This callback may called twice when a new connection | |
969 | * is established as a child socket inherits everything | |
970 | * from a parent LISTEN socket. | |
971 | * 1) data_ready method of the parent socket will be called | |
972 | * when one of child sockets become ESTABLISHED. | |
973 | * 2) data_ready method of the child socket may be called | |
974 | * when it receives data before the socket is accepted. | |
975 | * In case of 2, we should ignore it silently. | |
976 | */ | |
977 | if (sk->sk_state == TCP_LISTEN) { | |
978 | if (svsk) { | |
02fc6c36 | 979 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 980 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef NB |
981 | } else |
982 | printk("svc: socket %p: no user data\n", sk); | |
1da177e4 | 983 | } |
939bb7ef | 984 | |
1da177e4 LT |
985 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
986 | wake_up_interruptible_all(sk->sk_sleep); | |
987 | } | |
988 | ||
989 | /* | |
990 | * A state change on a connected socket means it's dying or dead. | |
991 | */ | |
992 | static void | |
993 | svc_tcp_state_change(struct sock *sk) | |
994 | { | |
939bb7ef | 995 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
996 | |
997 | dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", | |
939bb7ef | 998 | sk, sk->sk_state, sk->sk_user_data); |
1da177e4 | 999 | |
939bb7ef | 1000 | if (!svsk) |
1da177e4 | 1001 | printk("svc: socket %p: no user data\n", sk); |
939bb7ef | 1002 | else { |
02fc6c36 | 1003 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 1004 | svc_xprt_enqueue(&svsk->sk_xprt); |
1da177e4 | 1005 | } |
1da177e4 LT |
1006 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
1007 | wake_up_interruptible_all(sk->sk_sleep); | |
1008 | } | |
1009 | ||
1010 | static void | |
1011 | svc_tcp_data_ready(struct sock *sk, int count) | |
1012 | { | |
939bb7ef | 1013 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
1da177e4 LT |
1014 | |
1015 | dprintk("svc: socket %p TCP data ready (svsk %p)\n", | |
939bb7ef NB |
1016 | sk, sk->sk_user_data); |
1017 | if (svsk) { | |
02fc6c36 | 1018 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 1019 | svc_xprt_enqueue(&svsk->sk_xprt); |
939bb7ef | 1020 | } |
1da177e4 LT |
1021 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
1022 | wake_up_interruptible(sk->sk_sleep); | |
1023 | } | |
1024 | ||
bcdb81ae CL |
1025 | static inline int svc_port_is_privileged(struct sockaddr *sin) |
1026 | { | |
1027 | switch (sin->sa_family) { | |
1028 | case AF_INET: | |
1029 | return ntohs(((struct sockaddr_in *)sin)->sin_port) | |
1030 | < PROT_SOCK; | |
bcdb81ae CL |
1031 | case AF_INET6: |
1032 | return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) | |
1033 | < PROT_SOCK; | |
bcdb81ae CL |
1034 | default: |
1035 | return 0; | |
1036 | } | |
1037 | } | |
1038 | ||
1da177e4 LT |
1039 | /* |
1040 | * Accept a TCP connection | |
1041 | */ | |
38a417cc | 1042 | static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) |
1da177e4 | 1043 | { |
38a417cc | 1044 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
cdd88b9f AM |
1045 | struct sockaddr_storage addr; |
1046 | struct sockaddr *sin = (struct sockaddr *) &addr; | |
bb5cf160 | 1047 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 LT |
1048 | struct socket *sock = svsk->sk_sock; |
1049 | struct socket *newsock; | |
1da177e4 LT |
1050 | struct svc_sock *newsvsk; |
1051 | int err, slen; | |
ad06e4bd | 1052 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 LT |
1053 | |
1054 | dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); | |
1055 | if (!sock) | |
38a417cc | 1056 | return NULL; |
1da177e4 | 1057 | |
02fc6c36 | 1058 | clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
e6242e92 SS |
1059 | err = kernel_accept(sock, &newsock, O_NONBLOCK); |
1060 | if (err < 0) { | |
1da177e4 LT |
1061 | if (err == -ENOMEM) |
1062 | printk(KERN_WARNING "%s: no more sockets!\n", | |
1063 | serv->sv_name); | |
e6242e92 | 1064 | else if (err != -EAGAIN && net_ratelimit()) |
1da177e4 LT |
1065 | printk(KERN_WARNING "%s: accept failed (err %d)!\n", |
1066 | serv->sv_name, -err); | |
38a417cc | 1067 | return NULL; |
1da177e4 | 1068 | } |
02fc6c36 | 1069 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1070 | |
cdd88b9f | 1071 | err = kernel_getpeername(newsock, sin, &slen); |
1da177e4 LT |
1072 | if (err < 0) { |
1073 | if (net_ratelimit()) | |
1074 | printk(KERN_WARNING "%s: peername failed (err %d)!\n", | |
1075 | serv->sv_name, -err); | |
1076 | goto failed; /* aborted connection or whatever */ | |
1077 | } | |
1078 | ||
1079 | /* Ideally, we would want to reject connections from unauthorized | |
ad06e4bd CL |
1080 | * hosts here, but when we get encryption, the IP of the host won't |
1081 | * tell us anything. For now just warn about unpriv connections. | |
1da177e4 | 1082 | */ |
cdd88b9f | 1083 | if (!svc_port_is_privileged(sin)) { |
1da177e4 | 1084 | dprintk(KERN_WARNING |
ad06e4bd | 1085 | "%s: connect from unprivileged port: %s\n", |
cca5172a | 1086 | serv->sv_name, |
cdd88b9f | 1087 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 | 1088 | } |
ad06e4bd | 1089 | dprintk("%s: connect from %s\n", serv->sv_name, |
cdd88b9f | 1090 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
1091 | |
1092 | /* make sure that a write doesn't block forever when | |
1093 | * low on memory | |
1094 | */ | |
1095 | newsock->sk->sk_sndtimeo = HZ*30; | |
1096 | ||
6b174337 CL |
1097 | if (!(newsvsk = svc_setup_socket(serv, newsock, &err, |
1098 | (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)))) | |
1da177e4 | 1099 | goto failed; |
cdd88b9f | 1100 | memcpy(&newsvsk->sk_remote, sin, slen); |
067d7817 | 1101 | newsvsk->sk_remotelen = slen; |
a9747692 FM |
1102 | err = kernel_getsockname(newsock, sin, &slen); |
1103 | if (unlikely(err < 0)) { | |
1104 | dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); | |
1105 | slen = offsetof(struct sockaddr, sa_data); | |
1106 | } | |
1107 | memcpy(&newsvsk->sk_local, sin, slen); | |
067d7817 | 1108 | |
f9f3cc4f TT |
1109 | if (serv->sv_stats) |
1110 | serv->sv_stats->nettcpconn++; | |
1111 | ||
1112 | return &newsvsk->sk_xprt; | |
1113 | ||
1114 | failed: | |
1115 | sock_release(newsock); | |
1116 | return NULL; | |
1117 | } | |
1118 | ||
1da177e4 LT |
1119 | /* |
1120 | * Receive data from a TCP socket. | |
1121 | */ | |
1122 | static int | |
1123 | svc_tcp_recvfrom(struct svc_rqst *rqstp) | |
1124 | { | |
1125 | struct svc_sock *svsk = rqstp->rq_sock; | |
bb5cf160 | 1126 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
1da177e4 | 1127 | int len; |
3cc03b16 | 1128 | struct kvec *vec; |
1da177e4 LT |
1129 | int pnum, vlen; |
1130 | ||
1131 | dprintk("svc: tcp_recv %p data %d conn %d close %d\n", | |
02fc6c36 TT |
1132 | svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), |
1133 | test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), | |
1134 | test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); | |
1da177e4 LT |
1135 | |
1136 | if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) { | |
a6046f71 | 1137 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1138 | return svc_deferred_recv(rqstp); |
1139 | } | |
1140 | ||
02fc6c36 | 1141 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
1da177e4 LT |
1142 | /* sndbuf needs to have room for one request |
1143 | * per thread, otherwise we can stall even when the | |
1144 | * network isn't a bottleneck. | |
3262c816 GB |
1145 | * |
1146 | * We count all threads rather than threads in a | |
1147 | * particular pool, which provides an upper bound | |
1148 | * on the number of threads which will access the socket. | |
1149 | * | |
1da177e4 | 1150 | * rcvbuf just needs to be able to hold a few requests. |
cca5172a | 1151 | * Normally they will be removed from the queue |
1da177e4 LT |
1152 | * as soon a a complete request arrives. |
1153 | */ | |
1154 | svc_sock_setbufsize(svsk->sk_sock, | |
c6b0a9f8 N |
1155 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
1156 | 3 * serv->sv_max_mesg); | |
1da177e4 | 1157 | |
02fc6c36 | 1158 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1159 | |
1160 | /* Receive data. If we haven't got the record length yet, get | |
1161 | * the next four bytes. Otherwise try to gobble up as much as | |
1162 | * possible up to the complete record length. | |
1163 | */ | |
1164 | if (svsk->sk_tcplen < 4) { | |
1165 | unsigned long want = 4 - svsk->sk_tcplen; | |
1166 | struct kvec iov; | |
1167 | ||
1168 | iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; | |
1169 | iov.iov_len = want; | |
1170 | if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0) | |
1171 | goto error; | |
1172 | svsk->sk_tcplen += len; | |
1173 | ||
1174 | if (len < want) { | |
1175 | dprintk("svc: short recvfrom while reading record length (%d of %lu)\n", | |
cca5172a | 1176 | len, want); |
a6046f71 | 1177 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1178 | return -EAGAIN; /* record header not complete */ |
1179 | } | |
1180 | ||
1181 | svsk->sk_reclen = ntohl(svsk->sk_reclen); | |
1182 | if (!(svsk->sk_reclen & 0x80000000)) { | |
1183 | /* FIXME: technically, a record can be fragmented, | |
1184 | * and non-terminal fragments will not have the top | |
1185 | * bit set in the fragment length header. | |
1186 | * But apparently no known nfs clients send fragmented | |
1187 | * records. */ | |
34e9a63b N |
1188 | if (net_ratelimit()) |
1189 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" | |
1190 | " (non-terminal)\n", | |
1191 | (unsigned long) svsk->sk_reclen); | |
1da177e4 LT |
1192 | goto err_delete; |
1193 | } | |
1194 | svsk->sk_reclen &= 0x7fffffff; | |
1195 | dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen); | |
c6b0a9f8 | 1196 | if (svsk->sk_reclen > serv->sv_max_mesg) { |
34e9a63b N |
1197 | if (net_ratelimit()) |
1198 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" | |
1199 | " (large)\n", | |
1200 | (unsigned long) svsk->sk_reclen); | |
1da177e4 LT |
1201 | goto err_delete; |
1202 | } | |
1203 | } | |
1204 | ||
1205 | /* Check whether enough data is available */ | |
1206 | len = svc_recv_available(svsk); | |
1207 | if (len < 0) | |
1208 | goto error; | |
1209 | ||
1210 | if (len < svsk->sk_reclen) { | |
1211 | dprintk("svc: incomplete TCP record (%d of %d)\n", | |
1212 | len, svsk->sk_reclen); | |
a6046f71 | 1213 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1214 | return -EAGAIN; /* record not complete */ |
1215 | } | |
1216 | len = svsk->sk_reclen; | |
02fc6c36 | 1217 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1218 | |
3cc03b16 | 1219 | vec = rqstp->rq_vec; |
1da177e4 LT |
1220 | vec[0] = rqstp->rq_arg.head[0]; |
1221 | vlen = PAGE_SIZE; | |
1222 | pnum = 1; | |
1223 | while (vlen < len) { | |
44524359 | 1224 | vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]); |
1da177e4 LT |
1225 | vec[pnum].iov_len = PAGE_SIZE; |
1226 | pnum++; | |
1227 | vlen += PAGE_SIZE; | |
1228 | } | |
44524359 | 1229 | rqstp->rq_respages = &rqstp->rq_pages[pnum]; |
1da177e4 LT |
1230 | |
1231 | /* Now receive data */ | |
1232 | len = svc_recvfrom(rqstp, vec, pnum, len); | |
1233 | if (len < 0) | |
1234 | goto error; | |
1235 | ||
1236 | dprintk("svc: TCP complete record (%d bytes)\n", len); | |
1237 | rqstp->rq_arg.len = len; | |
1238 | rqstp->rq_arg.page_base = 0; | |
1239 | if (len <= rqstp->rq_arg.head[0].iov_len) { | |
1240 | rqstp->rq_arg.head[0].iov_len = len; | |
1241 | rqstp->rq_arg.page_len = 0; | |
1242 | } else { | |
1243 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; | |
1244 | } | |
1245 | ||
5148bf4e | 1246 | rqstp->rq_xprt_ctxt = NULL; |
1da177e4 LT |
1247 | rqstp->rq_prot = IPPROTO_TCP; |
1248 | ||
1249 | /* Reset TCP read info */ | |
1250 | svsk->sk_reclen = 0; | |
1251 | svsk->sk_tcplen = 0; | |
1252 | ||
a6046f71 | 1253 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1254 | if (serv->sv_stats) |
1255 | serv->sv_stats->nettcpcnt++; | |
1256 | ||
1257 | return len; | |
1258 | ||
1259 | err_delete: | |
02fc6c36 | 1260 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1261 | return -EAGAIN; |
1262 | ||
1263 | error: | |
1264 | if (len == -EAGAIN) { | |
1265 | dprintk("RPC: TCP recvfrom got EAGAIN\n"); | |
a6046f71 | 1266 | svc_xprt_received(&svsk->sk_xprt); |
1da177e4 LT |
1267 | } else { |
1268 | printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", | |
bb5cf160 | 1269 | svsk->sk_xprt.xpt_server->sv_name, -len); |
93fbf1a5 | 1270 | goto err_delete; |
1da177e4 LT |
1271 | } |
1272 | ||
1273 | return len; | |
1274 | } | |
1275 | ||
1276 | /* | |
1277 | * Send out data on TCP socket. | |
1278 | */ | |
1279 | static int | |
1280 | svc_tcp_sendto(struct svc_rqst *rqstp) | |
1281 | { | |
1282 | struct xdr_buf *xbufp = &rqstp->rq_res; | |
1283 | int sent; | |
d8ed029d | 1284 | __be32 reclen; |
1da177e4 LT |
1285 | |
1286 | /* Set up the first element of the reply kvec. | |
1287 | * Any other kvecs that may be in use have been taken | |
1288 | * care of by the server implementation itself. | |
1289 | */ | |
1290 | reclen = htonl(0x80000000|((xbufp->len ) - 4)); | |
1291 | memcpy(xbufp->head[0].iov_base, &reclen, 4); | |
1292 | ||
02fc6c36 | 1293 | if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags)) |
1da177e4 LT |
1294 | return -ENOTCONN; |
1295 | ||
1296 | sent = svc_sendto(rqstp, &rqstp->rq_res); | |
1297 | if (sent != xbufp->len) { | |
1298 | printk(KERN_NOTICE "rpc-srv/tcp: %s: %s %d when sending %d bytes - shutting down socket\n", | |
bb5cf160 | 1299 | rqstp->rq_sock->sk_xprt.xpt_server->sv_name, |
1da177e4 LT |
1300 | (sent<0)?"got error":"sent only", |
1301 | sent, xbufp->len); | |
02fc6c36 | 1302 | set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags); |
f6150c3c | 1303 | svc_xprt_enqueue(rqstp->rq_xprt); |
1da177e4 LT |
1304 | sent = -EAGAIN; |
1305 | } | |
1306 | return sent; | |
1307 | } | |
1308 | ||
e831fe65 TT |
1309 | /* |
1310 | * Setup response header. TCP has a 4B record length field. | |
1311 | */ | |
1312 | static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp) | |
1313 | { | |
1314 | struct kvec *resv = &rqstp->rq_res.head[0]; | |
1315 | ||
1316 | /* tcp needs a space for the record length... */ | |
1317 | svc_putnl(resv, 0); | |
1318 | } | |
1319 | ||
323bee32 TT |
1320 | static int svc_tcp_has_wspace(struct svc_xprt *xprt) |
1321 | { | |
1322 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
bb5cf160 | 1323 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
323bee32 TT |
1324 | int required; |
1325 | int wspace; | |
1326 | ||
1327 | /* | |
1328 | * Set the SOCK_NOSPACE flag before checking the available | |
1329 | * sock space. | |
1330 | */ | |
1331 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
7a90e8cc | 1332 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
323bee32 TT |
1333 | wspace = sk_stream_wspace(svsk->sk_sk); |
1334 | ||
1335 | if (wspace < sk_stream_min_wspace(svsk->sk_sk)) | |
1336 | return 0; | |
1337 | if (required * 2 > wspace) | |
1338 | return 0; | |
1339 | ||
1340 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); | |
1341 | return 1; | |
1342 | } | |
1343 | ||
b700cbb1 TT |
1344 | static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, |
1345 | struct sockaddr *sa, int salen, | |
1346 | int flags) | |
1347 | { | |
1348 | return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags); | |
1349 | } | |
1350 | ||
360d8738 | 1351 | static struct svc_xprt_ops svc_tcp_ops = { |
b700cbb1 | 1352 | .xpo_create = svc_tcp_create, |
5d137990 TT |
1353 | .xpo_recvfrom = svc_tcp_recvfrom, |
1354 | .xpo_sendto = svc_tcp_sendto, | |
5148bf4e | 1355 | .xpo_release_rqst = svc_release_skb, |
755cceab TT |
1356 | .xpo_detach = svc_sock_detach, |
1357 | .xpo_free = svc_sock_free, | |
e831fe65 | 1358 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
323bee32 | 1359 | .xpo_has_wspace = svc_tcp_has_wspace, |
38a417cc | 1360 | .xpo_accept = svc_tcp_accept, |
360d8738 TT |
1361 | }; |
1362 | ||
1363 | static struct svc_xprt_class svc_tcp_class = { | |
1364 | .xcl_name = "tcp", | |
b700cbb1 | 1365 | .xcl_owner = THIS_MODULE, |
360d8738 | 1366 | .xcl_ops = &svc_tcp_ops, |
49023155 | 1367 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
360d8738 TT |
1368 | }; |
1369 | ||
1370 | void svc_init_xprt_sock(void) | |
1371 | { | |
1372 | svc_reg_xprt_class(&svc_tcp_class); | |
1373 | svc_reg_xprt_class(&svc_udp_class); | |
1374 | } | |
1375 | ||
1376 | void svc_cleanup_xprt_sock(void) | |
1377 | { | |
1378 | svc_unreg_xprt_class(&svc_tcp_class); | |
1379 | svc_unreg_xprt_class(&svc_udp_class); | |
1380 | } | |
1381 | ||
bb5cf160 | 1382 | static void svc_tcp_init(struct svc_sock *svsk, struct svc_serv *serv) |
1da177e4 LT |
1383 | { |
1384 | struct sock *sk = svsk->sk_sk; | |
1385 | struct tcp_sock *tp = tcp_sk(sk); | |
1386 | ||
bb5cf160 | 1387 | svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv); |
1da177e4 LT |
1388 | |
1389 | if (sk->sk_state == TCP_LISTEN) { | |
1390 | dprintk("setting up TCP socket for listening\n"); | |
02fc6c36 | 1391 | set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1392 | sk->sk_data_ready = svc_tcp_listen_data_ready; |
02fc6c36 | 1393 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1394 | } else { |
1395 | dprintk("setting up TCP socket for reading\n"); | |
1396 | sk->sk_state_change = svc_tcp_state_change; | |
1397 | sk->sk_data_ready = svc_tcp_data_ready; | |
1398 | sk->sk_write_space = svc_write_space; | |
1399 | ||
1400 | svsk->sk_reclen = 0; | |
1401 | svsk->sk_tcplen = 0; | |
1402 | ||
1403 | tp->nonagle = 1; /* disable Nagle's algorithm */ | |
1404 | ||
1405 | /* initialise setting must have enough space to | |
cca5172a | 1406 | * receive and respond to one request. |
1da177e4 LT |
1407 | * svc_tcp_recvfrom will re-adjust if necessary |
1408 | */ | |
1409 | svc_sock_setbufsize(svsk->sk_sock, | |
bb5cf160 TT |
1410 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
1411 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); | |
1da177e4 | 1412 | |
02fc6c36 TT |
1413 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1414 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); | |
cca5172a | 1415 | if (sk->sk_state != TCP_ESTABLISHED) |
02fc6c36 | 1416 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1417 | } |
1418 | } | |
1419 | ||
1420 | void | |
1421 | svc_sock_update_bufs(struct svc_serv *serv) | |
1422 | { | |
1423 | /* | |
1424 | * The number of server threads has changed. Update | |
1425 | * rcvbuf and sndbuf accordingly on all sockets | |
1426 | */ | |
1427 | struct list_head *le; | |
1428 | ||
1429 | spin_lock_bh(&serv->sv_lock); | |
1430 | list_for_each(le, &serv->sv_permsocks) { | |
cca5172a | 1431 | struct svc_sock *svsk = |
7a182083 | 1432 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
02fc6c36 | 1433 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1434 | } |
1435 | list_for_each(le, &serv->sv_tempsocks) { | |
1436 | struct svc_sock *svsk = | |
7a182083 | 1437 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
02fc6c36 | 1438 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1439 | } |
1440 | spin_unlock_bh(&serv->sv_lock); | |
1441 | } | |
1442 | ||
e1b3157f TT |
1443 | /* |
1444 | * Make sure that we don't have too many active connections. If we | |
1445 | * have, something must be dropped. | |
1446 | * | |
1447 | * There's no point in trying to do random drop here for DoS | |
1448 | * prevention. The NFS clients does 1 reconnect in 15 seconds. An | |
1449 | * attacker can easily beat that. | |
1450 | * | |
1451 | * The only somewhat efficient mechanism would be if drop old | |
1452 | * connections from the same IP first. But right now we don't even | |
1453 | * record the client IP in svc_sock. | |
1454 | */ | |
1455 | static void svc_check_conn_limits(struct svc_serv *serv) | |
1456 | { | |
1457 | if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) { | |
1458 | struct svc_sock *svsk = NULL; | |
1459 | spin_lock_bh(&serv->sv_lock); | |
1460 | if (!list_empty(&serv->sv_tempsocks)) { | |
1461 | if (net_ratelimit()) { | |
1462 | /* Try to help the admin */ | |
1463 | printk(KERN_NOTICE "%s: too many open TCP " | |
1464 | "sockets, consider increasing the " | |
1465 | "number of nfsd threads\n", | |
1466 | serv->sv_name); | |
1467 | } | |
1468 | /* | |
1469 | * Always select the oldest socket. It's not fair, | |
1470 | * but so is life | |
1471 | */ | |
1472 | svsk = list_entry(serv->sv_tempsocks.prev, | |
1473 | struct svc_sock, | |
7a182083 | 1474 | sk_xprt.xpt_list); |
02fc6c36 | 1475 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
e1b3157f TT |
1476 | svc_xprt_get(&svsk->sk_xprt); |
1477 | } | |
1478 | spin_unlock_bh(&serv->sv_lock); | |
1479 | ||
1480 | if (svsk) { | |
f6150c3c | 1481 | svc_xprt_enqueue(&svsk->sk_xprt); |
e1b3157f TT |
1482 | svc_xprt_put(&svsk->sk_xprt); |
1483 | } | |
1484 | } | |
1485 | } | |
1486 | ||
1da177e4 | 1487 | /* |
3262c816 GB |
1488 | * Receive the next request on any socket. This code is carefully |
1489 | * organised not to touch any cachelines in the shared svc_serv | |
1490 | * structure, only cachelines in the local svc_pool. | |
1da177e4 LT |
1491 | */ |
1492 | int | |
6fb2b47f | 1493 | svc_recv(struct svc_rqst *rqstp, long timeout) |
1da177e4 | 1494 | { |
27459f09 | 1495 | struct svc_sock *svsk = NULL; |
6fb2b47f | 1496 | struct svc_serv *serv = rqstp->rq_server; |
3262c816 | 1497 | struct svc_pool *pool = rqstp->rq_pool; |
44524359 | 1498 | int len, i; |
1da177e4 LT |
1499 | int pages; |
1500 | struct xdr_buf *arg; | |
1501 | DECLARE_WAITQUEUE(wait, current); | |
1502 | ||
1503 | dprintk("svc: server %p waiting for data (to = %ld)\n", | |
1504 | rqstp, timeout); | |
1505 | ||
1506 | if (rqstp->rq_sock) | |
cca5172a | 1507 | printk(KERN_ERR |
1da177e4 LT |
1508 | "svc_recv: service %p, socket not NULL!\n", |
1509 | rqstp); | |
1510 | if (waitqueue_active(&rqstp->rq_wait)) | |
cca5172a | 1511 | printk(KERN_ERR |
1da177e4 LT |
1512 | "svc_recv: service %p, wait queue active!\n", |
1513 | rqstp); | |
1514 | ||
1da177e4 LT |
1515 | |
1516 | /* now allocate needed pages. If we get a failure, sleep briefly */ | |
c6b0a9f8 | 1517 | pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE; |
44524359 N |
1518 | for (i=0; i < pages ; i++) |
1519 | while (rqstp->rq_pages[i] == NULL) { | |
1520 | struct page *p = alloc_page(GFP_KERNEL); | |
1521 | if (!p) | |
1522 | schedule_timeout_uninterruptible(msecs_to_jiffies(500)); | |
1523 | rqstp->rq_pages[i] = p; | |
1da177e4 | 1524 | } |
250f3915 N |
1525 | rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ |
1526 | BUG_ON(pages >= RPCSVC_MAXPAGES); | |
1da177e4 LT |
1527 | |
1528 | /* Make arg->head point to first page and arg->pages point to rest */ | |
1529 | arg = &rqstp->rq_arg; | |
44524359 | 1530 | arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); |
1da177e4 | 1531 | arg->head[0].iov_len = PAGE_SIZE; |
44524359 | 1532 | arg->pages = rqstp->rq_pages + 1; |
1da177e4 LT |
1533 | arg->page_base = 0; |
1534 | /* save at least one page for response */ | |
1535 | arg->page_len = (pages-2)*PAGE_SIZE; | |
1536 | arg->len = (pages-1)*PAGE_SIZE; | |
1537 | arg->tail[0].iov_len = 0; | |
3e1d1d28 CL |
1538 | |
1539 | try_to_freeze(); | |
1887b935 | 1540 | cond_resched(); |
1da177e4 LT |
1541 | if (signalled()) |
1542 | return -EINTR; | |
1543 | ||
3262c816 GB |
1544 | spin_lock_bh(&pool->sp_lock); |
1545 | if ((svsk = svc_sock_dequeue(pool)) != NULL) { | |
1da177e4 | 1546 | rqstp->rq_sock = svsk; |
e1b3157f | 1547 | svc_xprt_get(&svsk->sk_xprt); |
c6b0a9f8 | 1548 | rqstp->rq_reserved = serv->sv_max_mesg; |
7a90e8cc | 1549 | atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved); |
1da177e4 LT |
1550 | } else { |
1551 | /* No data pending. Go to sleep */ | |
3262c816 | 1552 | svc_thread_enqueue(pool, rqstp); |
1da177e4 LT |
1553 | |
1554 | /* | |
1555 | * We have to be able to interrupt this wait | |
1556 | * to bring down the daemons ... | |
1557 | */ | |
1558 | set_current_state(TASK_INTERRUPTIBLE); | |
1559 | add_wait_queue(&rqstp->rq_wait, &wait); | |
3262c816 | 1560 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 LT |
1561 | |
1562 | schedule_timeout(timeout); | |
1563 | ||
3e1d1d28 | 1564 | try_to_freeze(); |
1da177e4 | 1565 | |
3262c816 | 1566 | spin_lock_bh(&pool->sp_lock); |
1da177e4 LT |
1567 | remove_wait_queue(&rqstp->rq_wait, &wait); |
1568 | ||
1569 | if (!(svsk = rqstp->rq_sock)) { | |
3262c816 GB |
1570 | svc_thread_dequeue(pool, rqstp); |
1571 | spin_unlock_bh(&pool->sp_lock); | |
1da177e4 LT |
1572 | dprintk("svc: server %p, no data yet\n", rqstp); |
1573 | return signalled()? -EINTR : -EAGAIN; | |
1574 | } | |
1575 | } | |
3262c816 | 1576 | spin_unlock_bh(&pool->sp_lock); |
1da177e4 | 1577 | |
d7979ae4 | 1578 | len = 0; |
02fc6c36 TT |
1579 | if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) { |
1580 | dprintk("svc_recv: found XPT_CLOSE\n"); | |
7a182083 | 1581 | svc_delete_xprt(&svsk->sk_xprt); |
02fc6c36 | 1582 | } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) { |
38a417cc TT |
1583 | struct svc_xprt *newxpt; |
1584 | newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt); | |
b700cbb1 TT |
1585 | if (newxpt) { |
1586 | /* | |
1587 | * We know this module_get will succeed because the | |
1588 | * listener holds a reference too | |
1589 | */ | |
1590 | __module_get(newxpt->xpt_class->xcl_owner); | |
bb5cf160 | 1591 | svc_check_conn_limits(svsk->sk_xprt.xpt_server); |
6bc5ab13 | 1592 | svc_xprt_received(newxpt); |
b700cbb1 | 1593 | } |
a6046f71 | 1594 | svc_xprt_received(&svsk->sk_xprt); |
d7979ae4 TT |
1595 | } else { |
1596 | dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n", | |
e1b3157f TT |
1597 | rqstp, pool->sp_id, svsk, |
1598 | atomic_read(&svsk->sk_xprt.xpt_ref.refcount)); | |
d7979ae4 TT |
1599 | len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp); |
1600 | dprintk("svc: got len=%d\n", len); | |
1601 | } | |
1da177e4 LT |
1602 | |
1603 | /* No data, incomplete (TCP) read, or accept() */ | |
1604 | if (len == 0 || len == -EAGAIN) { | |
1605 | rqstp->rq_res.len = 0; | |
1606 | svc_sock_release(rqstp); | |
1607 | return -EAGAIN; | |
1608 | } | |
02fc6c36 | 1609 | clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 1610 | |
bcdb81ae | 1611 | rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp)); |
1da177e4 LT |
1612 | rqstp->rq_chandle.defer = svc_defer; |
1613 | ||
1614 | if (serv->sv_stats) | |
1615 | serv->sv_stats->netcnt++; | |
1616 | return len; | |
1617 | } | |
1618 | ||
cca5172a | 1619 | /* |
1da177e4 LT |
1620 | * Drop request |
1621 | */ | |
1622 | void | |
1623 | svc_drop(struct svc_rqst *rqstp) | |
1624 | { | |
1625 | dprintk("svc: socket %p dropped request\n", rqstp->rq_sock); | |
1626 | svc_sock_release(rqstp); | |
1627 | } | |
1628 | ||
1629 | /* | |
1630 | * Return reply to client. | |
1631 | */ | |
1632 | int | |
1633 | svc_send(struct svc_rqst *rqstp) | |
1634 | { | |
a50fea26 | 1635 | struct svc_xprt *xprt; |
1da177e4 LT |
1636 | int len; |
1637 | struct xdr_buf *xb; | |
1638 | ||
a50fea26 TT |
1639 | xprt = rqstp->rq_xprt; |
1640 | if (!xprt) | |
1da177e4 | 1641 | return -EFAULT; |
1da177e4 LT |
1642 | |
1643 | /* release the receive skb before sending the reply */ | |
5148bf4e | 1644 | rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp); |
1da177e4 LT |
1645 | |
1646 | /* calculate over-all length */ | |
1647 | xb = & rqstp->rq_res; | |
1648 | xb->len = xb->head[0].iov_len + | |
1649 | xb->page_len + | |
1650 | xb->tail[0].iov_len; | |
1651 | ||
a50fea26 TT |
1652 | /* Grab mutex to serialize outgoing data. */ |
1653 | mutex_lock(&xprt->xpt_mutex); | |
1654 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) | |
1da177e4 LT |
1655 | len = -ENOTCONN; |
1656 | else | |
a50fea26 TT |
1657 | len = xprt->xpt_ops->xpo_sendto(rqstp); |
1658 | mutex_unlock(&xprt->xpt_mutex); | |
1da177e4 LT |
1659 | svc_sock_release(rqstp); |
1660 | ||
1661 | if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) | |
1662 | return 0; | |
1663 | return len; | |
1664 | } | |
1665 | ||
36bdfc8b GB |
1666 | /* |
1667 | * Timer function to close old temporary sockets, using | |
1668 | * a mark-and-sweep algorithm. | |
1669 | */ | |
1670 | static void | |
1671 | svc_age_temp_sockets(unsigned long closure) | |
1672 | { | |
1673 | struct svc_serv *serv = (struct svc_serv *)closure; | |
1674 | struct svc_sock *svsk; | |
1675 | struct list_head *le, *next; | |
1676 | LIST_HEAD(to_be_aged); | |
1677 | ||
1678 | dprintk("svc_age_temp_sockets\n"); | |
1679 | ||
1680 | if (!spin_trylock_bh(&serv->sv_lock)) { | |
1681 | /* busy, try again 1 sec later */ | |
1682 | dprintk("svc_age_temp_sockets: busy\n"); | |
1683 | mod_timer(&serv->sv_temptimer, jiffies + HZ); | |
1684 | return; | |
1685 | } | |
1686 | ||
1687 | list_for_each_safe(le, next, &serv->sv_tempsocks) { | |
7a182083 | 1688 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
36bdfc8b | 1689 | |
02fc6c36 | 1690 | if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags)) |
36bdfc8b | 1691 | continue; |
e1b3157f | 1692 | if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1 |
02fc6c36 | 1693 | || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)) |
36bdfc8b | 1694 | continue; |
e1b3157f | 1695 | svc_xprt_get(&svsk->sk_xprt); |
36bdfc8b | 1696 | list_move(le, &to_be_aged); |
02fc6c36 TT |
1697 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
1698 | set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags); | |
36bdfc8b GB |
1699 | } |
1700 | spin_unlock_bh(&serv->sv_lock); | |
1701 | ||
1702 | while (!list_empty(&to_be_aged)) { | |
1703 | le = to_be_aged.next; | |
7a182083 | 1704 | /* fiddling the sk_xprt.xpt_list node is safe 'cos we're XPT_DETACHED */ |
36bdfc8b | 1705 | list_del_init(le); |
7a182083 | 1706 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
36bdfc8b | 1707 | |
4bc6c497 | 1708 | dprintk("queuing svsk %p for closing\n", svsk); |
36bdfc8b GB |
1709 | |
1710 | /* a thread will dequeue and close it soon */ | |
f6150c3c | 1711 | svc_xprt_enqueue(&svsk->sk_xprt); |
e1b3157f | 1712 | svc_xprt_put(&svsk->sk_xprt); |
36bdfc8b GB |
1713 | } |
1714 | ||
1715 | mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); | |
1716 | } | |
1717 | ||
1da177e4 LT |
1718 | /* |
1719 | * Initialize socket for RPC use and create svc_sock struct | |
1720 | * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF. | |
1721 | */ | |
6b174337 CL |
1722 | static struct svc_sock *svc_setup_socket(struct svc_serv *serv, |
1723 | struct socket *sock, | |
1724 | int *errp, int flags) | |
1da177e4 LT |
1725 | { |
1726 | struct svc_sock *svsk; | |
1727 | struct sock *inet; | |
6b174337 CL |
1728 | int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); |
1729 | int is_temporary = flags & SVC_SOCK_TEMPORARY; | |
1da177e4 LT |
1730 | |
1731 | dprintk("svc: svc_setup_socket %p\n", sock); | |
0da974f4 | 1732 | if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) { |
1da177e4 LT |
1733 | *errp = -ENOMEM; |
1734 | return NULL; | |
1735 | } | |
1da177e4 LT |
1736 | |
1737 | inet = sock->sk; | |
1738 | ||
1739 | /* Register socket with portmapper */ | |
1740 | if (*errp >= 0 && pmap_register) | |
1741 | *errp = svc_register(serv, inet->sk_protocol, | |
1742 | ntohs(inet_sk(inet)->sport)); | |
1743 | ||
1744 | if (*errp < 0) { | |
1745 | kfree(svsk); | |
1746 | return NULL; | |
1747 | } | |
1748 | ||
02fc6c36 | 1749 | set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
1750 | inet->sk_user_data = svsk; |
1751 | svsk->sk_sock = sock; | |
1752 | svsk->sk_sk = inet; | |
1753 | svsk->sk_ostate = inet->sk_state_change; | |
1754 | svsk->sk_odata = inet->sk_data_ready; | |
1755 | svsk->sk_owspace = inet->sk_write_space; | |
7ac1bea5 | 1756 | spin_lock_init(&svsk->sk_lock); |
1da177e4 | 1757 | INIT_LIST_HEAD(&svsk->sk_deferred); |
1da177e4 LT |
1758 | |
1759 | /* Initialize the socket */ | |
1760 | if (sock->type == SOCK_DGRAM) | |
bb5cf160 | 1761 | svc_udp_init(svsk, serv); |
1da177e4 | 1762 | else |
bb5cf160 | 1763 | svc_tcp_init(svsk, serv); |
1da177e4 LT |
1764 | |
1765 | spin_lock_bh(&serv->sv_lock); | |
6b174337 | 1766 | if (is_temporary) { |
02fc6c36 | 1767 | set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
7a182083 | 1768 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks); |
1da177e4 | 1769 | serv->sv_tmpcnt++; |
36bdfc8b GB |
1770 | if (serv->sv_temptimer.function == NULL) { |
1771 | /* setup timer to age temp sockets */ | |
1772 | setup_timer(&serv->sv_temptimer, svc_age_temp_sockets, | |
1773 | (unsigned long)serv); | |
1774 | mod_timer(&serv->sv_temptimer, | |
1775 | jiffies + svc_conn_age_period * HZ); | |
1776 | } | |
1da177e4 | 1777 | } else { |
02fc6c36 | 1778 | clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
7a182083 | 1779 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks); |
1da177e4 LT |
1780 | } |
1781 | spin_unlock_bh(&serv->sv_lock); | |
1782 | ||
1783 | dprintk("svc: svc_setup_socket created %p (inet %p)\n", | |
1784 | svsk, svsk->sk_sk); | |
1785 | ||
1da177e4 LT |
1786 | return svsk; |
1787 | } | |
1788 | ||
b41b66d6 N |
1789 | int svc_addsock(struct svc_serv *serv, |
1790 | int fd, | |
1791 | char *name_return, | |
1792 | int *proto) | |
1793 | { | |
1794 | int err = 0; | |
1795 | struct socket *so = sockfd_lookup(fd, &err); | |
1796 | struct svc_sock *svsk = NULL; | |
1797 | ||
1798 | if (!so) | |
1799 | return err; | |
1800 | if (so->sk->sk_family != AF_INET) | |
1801 | err = -EAFNOSUPPORT; | |
1802 | else if (so->sk->sk_protocol != IPPROTO_TCP && | |
1803 | so->sk->sk_protocol != IPPROTO_UDP) | |
1804 | err = -EPROTONOSUPPORT; | |
1805 | else if (so->state > SS_UNCONNECTED) | |
1806 | err = -EISCONN; | |
1807 | else { | |
6b174337 | 1808 | svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS); |
e79eff1f | 1809 | if (svsk) { |
a6046f71 | 1810 | svc_xprt_received(&svsk->sk_xprt); |
b41b66d6 | 1811 | err = 0; |
e79eff1f | 1812 | } |
b41b66d6 N |
1813 | } |
1814 | if (err) { | |
1815 | sockfd_put(so); | |
1816 | return err; | |
1817 | } | |
1818 | if (proto) *proto = so->sk->sk_protocol; | |
1819 | return one_sock_name(name_return, svsk); | |
1820 | } | |
1821 | EXPORT_SYMBOL_GPL(svc_addsock); | |
1822 | ||
1da177e4 LT |
1823 | /* |
1824 | * Create socket for RPC service. | |
1825 | */ | |
b700cbb1 TT |
1826 | static struct svc_xprt *svc_create_socket(struct svc_serv *serv, |
1827 | int protocol, | |
1828 | struct sockaddr *sin, int len, | |
1829 | int flags) | |
1da177e4 LT |
1830 | { |
1831 | struct svc_sock *svsk; | |
1832 | struct socket *sock; | |
1833 | int error; | |
1834 | int type; | |
ad06e4bd | 1835 | char buf[RPC_MAX_ADDRBUFLEN]; |
1da177e4 | 1836 | |
ad06e4bd CL |
1837 | dprintk("svc: svc_create_socket(%s, %d, %s)\n", |
1838 | serv->sv_program->pg_name, protocol, | |
77f1f67a | 1839 | __svc_print_addr(sin, buf, sizeof(buf))); |
1da177e4 LT |
1840 | |
1841 | if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { | |
1842 | printk(KERN_WARNING "svc: only UDP and TCP " | |
1843 | "sockets supported\n"); | |
b700cbb1 | 1844 | return ERR_PTR(-EINVAL); |
1da177e4 LT |
1845 | } |
1846 | type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; | |
1847 | ||
77f1f67a CL |
1848 | error = sock_create_kern(sin->sa_family, type, protocol, &sock); |
1849 | if (error < 0) | |
b700cbb1 | 1850 | return ERR_PTR(error); |
1da177e4 | 1851 | |
ed07536e PZ |
1852 | svc_reclassify_socket(sock); |
1853 | ||
18114746 | 1854 | if (type == SOCK_STREAM) |
77f1f67a CL |
1855 | sock->sk->sk_reuse = 1; /* allow address reuse */ |
1856 | error = kernel_bind(sock, sin, len); | |
18114746 ES |
1857 | if (error < 0) |
1858 | goto bummer; | |
1da177e4 LT |
1859 | |
1860 | if (protocol == IPPROTO_TCP) { | |
e6242e92 | 1861 | if ((error = kernel_listen(sock, 64)) < 0) |
1da177e4 LT |
1862 | goto bummer; |
1863 | } | |
1864 | ||
e79eff1f | 1865 | if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) { |
a6046f71 | 1866 | svc_xprt_received(&svsk->sk_xprt); |
b700cbb1 | 1867 | return (struct svc_xprt *)svsk; |
e79eff1f | 1868 | } |
1da177e4 LT |
1869 | |
1870 | bummer: | |
1871 | dprintk("svc: svc_create_socket error = %d\n", -error); | |
1872 | sock_release(sock); | |
b700cbb1 | 1873 | return ERR_PTR(error); |
1da177e4 LT |
1874 | } |
1875 | ||
755cceab TT |
1876 | /* |
1877 | * Detach the svc_sock from the socket so that no | |
1878 | * more callbacks occur. | |
1879 | */ | |
1880 | static void svc_sock_detach(struct svc_xprt *xprt) | |
1881 | { | |
1882 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1883 | struct sock *sk = svsk->sk_sk; | |
1884 | ||
1885 | dprintk("svc: svc_sock_detach(%p)\n", svsk); | |
1886 | ||
1887 | /* put back the old socket callbacks */ | |
1888 | sk->sk_state_change = svsk->sk_ostate; | |
1889 | sk->sk_data_ready = svsk->sk_odata; | |
1890 | sk->sk_write_space = svsk->sk_owspace; | |
1891 | } | |
1892 | ||
1893 | /* | |
1894 | * Free the svc_sock's socket resources and the svc_sock itself. | |
1895 | */ | |
1896 | static void svc_sock_free(struct svc_xprt *xprt) | |
1897 | { | |
1898 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); | |
1899 | dprintk("svc: svc_sock_free(%p)\n", svsk); | |
1900 | ||
1901 | if (svsk->sk_info_authunix != NULL) | |
1902 | svcauth_unix_info_release(svsk->sk_info_authunix); | |
1903 | if (svsk->sk_sock->file) | |
1904 | sockfd_put(svsk->sk_sock); | |
1905 | else | |
1906 | sock_release(svsk->sk_sock); | |
1907 | kfree(svsk); | |
1908 | } | |
1909 | ||
1da177e4 | 1910 | /* |
7a182083 | 1911 | * Remove a dead transport |
1da177e4 | 1912 | */ |
7a182083 | 1913 | static void svc_delete_xprt(struct svc_xprt *xprt) |
1da177e4 | 1914 | { |
7a182083 | 1915 | struct svc_serv *serv = xprt->xpt_server; |
1da177e4 | 1916 | |
7a182083 TT |
1917 | dprintk("svc: svc_delete_xprt(%p)\n", xprt); |
1918 | xprt->xpt_ops->xpo_detach(xprt); | |
1da177e4 LT |
1919 | |
1920 | spin_lock_bh(&serv->sv_lock); | |
7a182083 TT |
1921 | if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags)) |
1922 | list_del_init(&xprt->xpt_list); | |
cca5172a | 1923 | /* |
7a182083 TT |
1924 | * We used to delete the transport from whichever list |
1925 | * it's sk_xprt.xpt_ready node was on, but we don't actually | |
3262c816 GB |
1926 | * need to. This is because the only time we're called |
1927 | * while still attached to a queue, the queue itself | |
1928 | * is about to be destroyed (in svc_destroy). | |
1929 | */ | |
7a182083 TT |
1930 | if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) { |
1931 | BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2); | |
1932 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) | |
1da177e4 | 1933 | serv->sv_tmpcnt--; |
7a182083 | 1934 | svc_xprt_put(xprt); |
aaf68cfb | 1935 | } |
d6740df9 | 1936 | spin_unlock_bh(&serv->sv_lock); |
aaf68cfb N |
1937 | } |
1938 | ||
7a182083 | 1939 | static void svc_close_xprt(struct svc_xprt *xprt) |
aaf68cfb | 1940 | { |
7a182083 TT |
1941 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
1942 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) | |
aaf68cfb N |
1943 | /* someone else will have to effect the close */ |
1944 | return; | |
1945 | ||
7a182083 TT |
1946 | svc_xprt_get(xprt); |
1947 | svc_delete_xprt(xprt); | |
1948 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1949 | svc_xprt_put(xprt); | |
1da177e4 LT |
1950 | } |
1951 | ||
7a182083 | 1952 | void svc_close_all(struct list_head *xprt_list) |
cda1fd4a | 1953 | { |
7a182083 TT |
1954 | struct svc_xprt *xprt; |
1955 | struct svc_xprt *tmp; | |
1956 | ||
1957 | list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) { | |
1958 | set_bit(XPT_CLOSE, &xprt->xpt_flags); | |
1959 | if (test_bit(XPT_BUSY, &xprt->xpt_flags)) { | |
1960 | /* Waiting to be processed, but no threads left, | |
1961 | * So just remove it from the waiting list | |
1962 | */ | |
1963 | list_del_init(&xprt->xpt_ready); | |
1964 | clear_bit(XPT_BUSY, &xprt->xpt_flags); | |
1965 | } | |
1966 | svc_close_xprt(xprt); | |
cda1fd4a | 1967 | } |
cda1fd4a N |
1968 | } |
1969 | ||
1da177e4 | 1970 | /* |
cca5172a | 1971 | * Handle defer and revisit of requests |
1da177e4 LT |
1972 | */ |
1973 | ||
1974 | static void svc_revisit(struct cache_deferred_req *dreq, int too_many) | |
1975 | { | |
1976 | struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle); | |
1da177e4 LT |
1977 | struct svc_sock *svsk; |
1978 | ||
1979 | if (too_many) { | |
e1b3157f | 1980 | svc_xprt_put(&dr->svsk->sk_xprt); |
1da177e4 LT |
1981 | kfree(dr); |
1982 | return; | |
1983 | } | |
1984 | dprintk("revisit queued\n"); | |
1985 | svsk = dr->svsk; | |
1986 | dr->svsk = NULL; | |
7ac1bea5 | 1987 | spin_lock(&svsk->sk_lock); |
1da177e4 | 1988 | list_add(&dr->handle.recent, &svsk->sk_deferred); |
7ac1bea5 | 1989 | spin_unlock(&svsk->sk_lock); |
02fc6c36 | 1990 | set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
f6150c3c | 1991 | svc_xprt_enqueue(&svsk->sk_xprt); |
e1b3157f | 1992 | svc_xprt_put(&svsk->sk_xprt); |
1da177e4 LT |
1993 | } |
1994 | ||
1995 | static struct cache_deferred_req * | |
1996 | svc_defer(struct cache_req *req) | |
1997 | { | |
1998 | struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); | |
1999 | int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len); | |
2000 | struct svc_deferred_req *dr; | |
2001 | ||
2002 | if (rqstp->rq_arg.page_len) | |
2003 | return NULL; /* if more than a page, give up FIXME */ | |
2004 | if (rqstp->rq_deferred) { | |
2005 | dr = rqstp->rq_deferred; | |
2006 | rqstp->rq_deferred = NULL; | |
2007 | } else { | |
2008 | int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; | |
2009 | /* FIXME maybe discard if size too large */ | |
2010 | dr = kmalloc(size, GFP_KERNEL); | |
2011 | if (dr == NULL) | |
2012 | return NULL; | |
2013 | ||
2014 | dr->handle.owner = rqstp->rq_server; | |
2015 | dr->prot = rqstp->rq_prot; | |
24422222 CL |
2016 | memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); |
2017 | dr->addrlen = rqstp->rq_addrlen; | |
1918e341 | 2018 | dr->daddr = rqstp->rq_daddr; |
1da177e4 LT |
2019 | dr->argslen = rqstp->rq_arg.len >> 2; |
2020 | memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2); | |
2021 | } | |
e1b3157f | 2022 | svc_xprt_get(rqstp->rq_xprt); |
1da177e4 | 2023 | dr->svsk = rqstp->rq_sock; |
1da177e4 LT |
2024 | |
2025 | dr->handle.revisit = svc_revisit; | |
2026 | return &dr->handle; | |
2027 | } | |
2028 | ||
2029 | /* | |
2030 | * recv data from a deferred request into an active one | |
2031 | */ | |
2032 | static int svc_deferred_recv(struct svc_rqst *rqstp) | |
2033 | { | |
2034 | struct svc_deferred_req *dr = rqstp->rq_deferred; | |
2035 | ||
2036 | rqstp->rq_arg.head[0].iov_base = dr->args; | |
2037 | rqstp->rq_arg.head[0].iov_len = dr->argslen<<2; | |
2038 | rqstp->rq_arg.page_len = 0; | |
2039 | rqstp->rq_arg.len = dr->argslen<<2; | |
2040 | rqstp->rq_prot = dr->prot; | |
24422222 CL |
2041 | memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); |
2042 | rqstp->rq_addrlen = dr->addrlen; | |
1918e341 | 2043 | rqstp->rq_daddr = dr->daddr; |
44524359 | 2044 | rqstp->rq_respages = rqstp->rq_pages; |
1da177e4 LT |
2045 | return dr->argslen<<2; |
2046 | } | |
2047 | ||
2048 | ||
2049 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk) | |
2050 | { | |
2051 | struct svc_deferred_req *dr = NULL; | |
cca5172a | 2052 | |
02fc6c36 | 2053 | if (!test_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags)) |
1da177e4 | 2054 | return NULL; |
7ac1bea5 | 2055 | spin_lock(&svsk->sk_lock); |
02fc6c36 | 2056 | clear_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
1da177e4 LT |
2057 | if (!list_empty(&svsk->sk_deferred)) { |
2058 | dr = list_entry(svsk->sk_deferred.next, | |
2059 | struct svc_deferred_req, | |
2060 | handle.recent); | |
2061 | list_del_init(&dr->handle.recent); | |
02fc6c36 | 2062 | set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
1da177e4 | 2063 | } |
7ac1bea5 | 2064 | spin_unlock(&svsk->sk_lock); |
1da177e4 LT |
2065 | return dr; |
2066 | } |