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