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