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