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