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