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