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