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