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