Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/net/sunrpc/svcsock.c |
| 3 | * |
| 4 | * These are the RPC server socket internals. |
| 5 | * |
| 6 | * The server scheduling algorithm does not always distribute the load |
| 7 | * evenly when servicing a single client. May need to modify the |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 8 | * svc_xprt_enqueue procedure... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * TCP support is largely untested and may be a little slow. The problem |
| 11 | * is that we currently do two separate recvfrom's, one for the 4-byte |
| 12 | * record length, and the second for the actual record. This could possibly |
| 13 | * be improved by always reading a minimum size of around 100 bytes and |
| 14 | * tucking any superfluous bytes away in a temporary store. Still, that |
| 15 | * leaves write requests out in the rain. An alternative may be to peek at |
| 16 | * the first skb in the queue, and if it matches the next TCP sequence |
| 17 | * number, to extract the record marker. Yuck. |
| 18 | * |
| 19 | * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> |
| 20 | */ |
| 21 | |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 22 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/sched.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/fcntl.h> |
| 26 | #include <linux/net.h> |
| 27 | #include <linux/in.h> |
| 28 | #include <linux/inet.h> |
| 29 | #include <linux/udp.h> |
Andrew Morton | 91483c4 | 2005-08-09 20:20:07 -0700 | [diff] [blame] | 30 | #include <linux/tcp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/unistd.h> |
| 32 | #include <linux/slab.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/skbuff.h> |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 35 | #include <linux/file.h> |
Nigel Cunningham | 7dfb710 | 2006-12-06 20:34:23 -0800 | [diff] [blame] | 36 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <net/sock.h> |
| 38 | #include <net/checksum.h> |
| 39 | #include <net/ip.h> |
Chuck Lever | b92503b | 2007-02-12 00:53:36 -0800 | [diff] [blame] | 40 | #include <net/ipv6.h> |
Arnaldo Carvalho de Melo | c752f07 | 2005-08-09 20:08:28 -0700 | [diff] [blame] | 41 | #include <net/tcp_states.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include <asm/uaccess.h> |
| 43 | #include <asm/ioctls.h> |
| 44 | |
| 45 | #include <linux/sunrpc/types.h> |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 46 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <linux/sunrpc/xdr.h> |
| 48 | #include <linux/sunrpc/svcsock.h> |
| 49 | #include <linux/sunrpc/stats.h> |
| 50 | |
| 51 | /* SMP locking strategy: |
| 52 | * |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 53 | * svc_pool->sp_lock protects most of the fields of that pool. |
| 54 | * svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt. |
| 55 | * when both need to be taken (rare), svc_serv->sv_lock is first. |
| 56 | * BKL protects svc_serv->sv_nrthread. |
NeilBrown | 7ac1bea | 2007-05-09 02:34:48 -0700 | [diff] [blame] | 57 | * svc_sock->sk_lock protects the svc_sock->sk_deferred list |
| 58 | * and the ->sk_info_authunix cache. |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 59 | * svc_sock->sk_xprt.xpt_flags.XPT_BUSY prevents a svc_sock being |
| 60 | * enqueued multiply. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * |
| 62 | * Some flags can be set to certain values at any time |
| 63 | * providing that certain rules are followed: |
| 64 | * |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 65 | * XPT_CONN, XPT_DATA, can be set or cleared at any time. |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 66 | * after a set, svc_xprt_enqueue must be called. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | * after a clear, the socket must be read/accepted |
| 68 | * if this succeeds, it must be set again. |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 69 | * XPT_CLOSE can set at any time. It is never cleared. |
| 70 | * xpt_ref contains a bias of '1' until XPT_DEAD is set. |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 71 | * so when xprt_ref hits zero, we know the transport is dead |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 72 | * and no-one is using it. |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 73 | * XPT_DEAD can only be set while XPT_BUSY is held which ensures |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 74 | * no other thread will be using the socket or will try to |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 75 | * set XPT_DEAD. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | * |
| 77 | */ |
| 78 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 79 | #define RPCDBG_FACILITY RPCDBG_SVCXPRT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
| 81 | |
| 82 | static struct svc_sock *svc_setup_socket(struct svc_serv *, struct socket *, |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 83 | int *errp, int flags); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 84 | static void svc_delete_xprt(struct svc_xprt *xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | static void svc_udp_data_ready(struct sock *, int); |
| 86 | static int svc_udp_recvfrom(struct svc_rqst *); |
| 87 | static int svc_udp_sendto(struct svc_rqst *); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 88 | static void svc_close_xprt(struct svc_xprt *xprt); |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 89 | static void svc_sock_detach(struct svc_xprt *); |
| 90 | static void svc_sock_free(struct svc_xprt *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
| 92 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk); |
| 93 | static int svc_deferred_recv(struct svc_rqst *rqstp); |
| 94 | static struct cache_deferred_req *svc_defer(struct cache_req *req); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 95 | static struct svc_xprt *svc_create_socket(struct svc_serv *, int, |
| 96 | struct sockaddr *, int, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 98 | /* apparently the "standard" is that clients close |
| 99 | * idle connections after 5 minutes, servers after |
| 100 | * 6 minutes |
| 101 | * http://www.connectathon.org/talks96/nfstcp.pdf |
| 102 | */ |
| 103 | static int svc_conn_age_period = 6*60; |
| 104 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 105 | #ifdef CONFIG_DEBUG_LOCK_ALLOC |
| 106 | static struct lock_class_key svc_key[2]; |
| 107 | static struct lock_class_key svc_slock_key[2]; |
| 108 | |
| 109 | static inline void svc_reclassify_socket(struct socket *sock) |
| 110 | { |
| 111 | struct sock *sk = sock->sk; |
John Heffner | 02b3d34 | 2007-09-12 10:42:12 +0200 | [diff] [blame] | 112 | BUG_ON(sock_owned_by_user(sk)); |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 113 | switch (sk->sk_family) { |
| 114 | case AF_INET: |
| 115 | sock_lock_init_class_and_name(sk, "slock-AF_INET-NFSD", |
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 | }; |
Frank van Maarseveen | a974769 | 2007-07-09 22:21:39 +0200 | [diff] [blame] | 626 | struct sockaddr *sin; |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 627 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 629 | len = kernel_recvmsg(svsk->sk_sock, &msg, iov, nr, buflen, |
| 630 | msg.msg_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | |
| 632 | /* sock_recvmsg doesn't fill in the name/namelen, so we must.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | */ |
Chuck Lever | 067d781 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 634 | memcpy(&rqstp->rq_addr, &svsk->sk_remote, svsk->sk_remotelen); |
| 635 | rqstp->rq_addrlen = svsk->sk_remotelen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | |
Frank van Maarseveen | a974769 | 2007-07-09 22:21:39 +0200 | [diff] [blame] | 637 | /* Destination address in request is needed for binding the |
| 638 | * source address in RPC callbacks later. |
| 639 | */ |
| 640 | sin = (struct sockaddr *)&svsk->sk_local; |
| 641 | switch (sin->sa_family) { |
| 642 | case AF_INET: |
| 643 | rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr; |
| 644 | break; |
| 645 | case AF_INET6: |
| 646 | rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr; |
| 647 | break; |
| 648 | } |
| 649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | dprintk("svc: socket %p recvfrom(%p, %Zu) = %d\n", |
Chuck Lever | 1ba9510 | 2007-02-12 00:53:31 -0800 | [diff] [blame] | 651 | svsk, iov[0].iov_base, iov[0].iov_len, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | |
| 653 | return len; |
| 654 | } |
| 655 | |
| 656 | /* |
| 657 | * Set socket snd and rcv buffer lengths |
| 658 | */ |
| 659 | static inline void |
| 660 | svc_sock_setbufsize(struct socket *sock, unsigned int snd, unsigned int rcv) |
| 661 | { |
| 662 | #if 0 |
| 663 | mm_segment_t oldfs; |
| 664 | oldfs = get_fs(); set_fs(KERNEL_DS); |
| 665 | sock_setsockopt(sock, SOL_SOCKET, SO_SNDBUF, |
| 666 | (char*)&snd, sizeof(snd)); |
| 667 | sock_setsockopt(sock, SOL_SOCKET, SO_RCVBUF, |
| 668 | (char*)&rcv, sizeof(rcv)); |
| 669 | #else |
| 670 | /* sock_setsockopt limits use to sysctl_?mem_max, |
| 671 | * which isn't acceptable. Until that is made conditional |
| 672 | * on not having CAP_SYS_RESOURCE or similar, we go direct... |
| 673 | * DaveM said I could! |
| 674 | */ |
| 675 | lock_sock(sock->sk); |
| 676 | sock->sk->sk_sndbuf = snd * 2; |
| 677 | sock->sk->sk_rcvbuf = rcv * 2; |
| 678 | sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK|SOCK_RCVBUF_LOCK; |
| 679 | release_sock(sock->sk); |
| 680 | #endif |
| 681 | } |
| 682 | /* |
| 683 | * INET callback when data has been received on the socket. |
| 684 | */ |
| 685 | static void |
| 686 | svc_udp_data_ready(struct sock *sk, int count) |
| 687 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 688 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 690 | if (svsk) { |
| 691 | dprintk("svc: socket %p(inet %p), count=%d, busy=%d\n", |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 692 | svsk, sk, count, |
| 693 | test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
| 694 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 695 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
| 698 | wake_up_interruptible(sk->sk_sleep); |
| 699 | } |
| 700 | |
| 701 | /* |
| 702 | * INET callback when space is newly available on the socket. |
| 703 | */ |
| 704 | static void |
| 705 | svc_write_space(struct sock *sk) |
| 706 | { |
| 707 | struct svc_sock *svsk = (struct svc_sock *)(sk->sk_user_data); |
| 708 | |
| 709 | if (svsk) { |
| 710 | dprintk("svc: socket %p(inet %p), write_space busy=%d\n", |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 711 | svsk, sk, test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 712 | svc_xprt_enqueue(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 716 | dprintk("RPC svc_write_space: someone sleeping on %p\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | svsk); |
| 718 | wake_up_interruptible(sk->sk_sleep); |
| 719 | } |
| 720 | } |
| 721 | |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 722 | static inline void svc_udp_get_dest_address(struct svc_rqst *rqstp, |
| 723 | struct cmsghdr *cmh) |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 724 | { |
| 725 | switch (rqstp->rq_sock->sk_sk->sk_family) { |
| 726 | case AF_INET: { |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 727 | struct in_pktinfo *pki = CMSG_DATA(cmh); |
| 728 | rqstp->rq_daddr.addr.s_addr = pki->ipi_spec_dst.s_addr; |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 729 | break; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 730 | } |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 731 | case AF_INET6: { |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 732 | struct in6_pktinfo *pki = CMSG_DATA(cmh); |
| 733 | ipv6_addr_copy(&rqstp->rq_daddr.addr6, &pki->ipi6_addr); |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 734 | break; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 735 | } |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 736 | } |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | /* |
| 740 | * Receive a datagram from a UDP socket. |
| 741 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | static int |
| 743 | svc_udp_recvfrom(struct svc_rqst *rqstp) |
| 744 | { |
| 745 | struct svc_sock *svsk = rqstp->rq_sock; |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 746 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | struct sk_buff *skb; |
David S. Miller | bc375ea | 2007-04-12 13:35:59 -0700 | [diff] [blame] | 748 | union { |
| 749 | struct cmsghdr hdr; |
| 750 | long all[SVC_PKTINFO_SPACE / sizeof(long)]; |
| 751 | } buffer; |
| 752 | struct cmsghdr *cmh = &buffer.hdr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | int err, len; |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 754 | struct msghdr msg = { |
| 755 | .msg_name = svc_addr(rqstp), |
| 756 | .msg_control = cmh, |
| 757 | .msg_controllen = sizeof(buffer), |
| 758 | .msg_flags = MSG_DONTWAIT, |
| 759 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 761 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* udp sockets need large rcvbuf as all pending |
| 763 | * requests are still in that buffer. sndbuf must |
| 764 | * also be large enough that there is enough space |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 765 | * for one reply per thread. We count all threads |
| 766 | * rather than threads in a particular pool, which |
| 767 | * provides an upper bound on the number of threads |
| 768 | * which will access the socket. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | */ |
| 770 | svc_sock_setbufsize(svsk->sk_sock, |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 771 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
| 772 | (serv->sv_nrthreads+3) * serv->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | |
| 774 | if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) { |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 775 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | return svc_deferred_recv(rqstp); |
| 777 | } |
| 778 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 779 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
NeilBrown | 05ed690 | 2007-05-09 02:34:55 -0700 | [diff] [blame] | 780 | skb = NULL; |
| 781 | err = kernel_recvmsg(svsk->sk_sock, &msg, NULL, |
| 782 | 0, 0, MSG_PEEK | MSG_DONTWAIT); |
| 783 | if (err >= 0) |
| 784 | skb = skb_recv_datagram(svsk->sk_sk, 0, 1, &err); |
| 785 | |
| 786 | if (skb == NULL) { |
| 787 | if (err != -EAGAIN) { |
| 788 | /* possibly an icmp error */ |
| 789 | dprintk("svc: recvfrom returned error %d\n", -err); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 790 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 792 | svc_xprt_received(&svsk->sk_xprt); |
NeilBrown | 05ed690 | 2007-05-09 02:34:55 -0700 | [diff] [blame] | 793 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | } |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 795 | rqstp->rq_addrlen = sizeof(rqstp->rq_addr); |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 796 | if (skb->tstamp.tv64 == 0) { |
| 797 | skb->tstamp = ktime_get_real(); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 798 | /* Don't enable netstamp, sunrpc doesn't |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | need that much accuracy */ |
| 800 | } |
Eric Dumazet | b7aa0bf | 2007-04-19 16:16:32 -0700 | [diff] [blame] | 801 | svsk->sk_sk->sk_stamp = skb->tstamp; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 802 | 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] | 803 | |
| 804 | /* |
| 805 | * Maybe more packets - kick another thread ASAP. |
| 806 | */ |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 807 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | |
| 809 | len = skb->len - sizeof(struct udphdr); |
| 810 | rqstp->rq_arg.len = len; |
| 811 | |
Chuck Lever | 9575648 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 812 | rqstp->rq_prot = IPPROTO_UDP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 814 | if (cmh->cmsg_level != IPPROTO_IP || |
| 815 | cmh->cmsg_type != IP_PKTINFO) { |
| 816 | if (net_ratelimit()) |
| 817 | printk("rpcsvc: received unknown control message:" |
| 818 | "%d/%d\n", |
| 819 | cmh->cmsg_level, cmh->cmsg_type); |
| 820 | skb_free_datagram(svsk->sk_sk, skb); |
| 821 | return 0; |
| 822 | } |
| 823 | svc_udp_get_dest_address(rqstp, cmh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | |
| 825 | if (skb_is_nonlinear(skb)) { |
| 826 | /* we have to copy */ |
| 827 | local_bh_disable(); |
| 828 | if (csum_partial_copy_to_xdr(&rqstp->rq_arg, skb)) { |
| 829 | local_bh_enable(); |
| 830 | /* checksum error */ |
| 831 | skb_free_datagram(svsk->sk_sk, skb); |
| 832 | return 0; |
| 833 | } |
| 834 | local_bh_enable(); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 835 | skb_free_datagram(svsk->sk_sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } else { |
| 837 | /* we can use it in-place */ |
| 838 | rqstp->rq_arg.head[0].iov_base = skb->data + sizeof(struct udphdr); |
| 839 | rqstp->rq_arg.head[0].iov_len = len; |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 840 | if (skb_checksum_complete(skb)) { |
| 841 | skb_free_datagram(svsk->sk_sk, skb); |
| 842 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | } |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 844 | rqstp->rq_xprt_ctxt = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | rqstp->rq_arg.page_base = 0; |
| 848 | if (len <= rqstp->rq_arg.head[0].iov_len) { |
| 849 | rqstp->rq_arg.head[0].iov_len = len; |
| 850 | rqstp->rq_arg.page_len = 0; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 851 | rqstp->rq_respages = rqstp->rq_pages+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | } else { |
| 853 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 854 | rqstp->rq_respages = rqstp->rq_pages + 1 + |
Ilpo Järvinen | 172589c | 2007-08-28 15:50:33 -0700 | [diff] [blame] | 855 | DIV_ROUND_UP(rqstp->rq_arg.page_len, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | if (serv->sv_stats) |
| 859 | serv->sv_stats->netudpcnt++; |
| 860 | |
| 861 | return len; |
| 862 | } |
| 863 | |
| 864 | static int |
| 865 | svc_udp_sendto(struct svc_rqst *rqstp) |
| 866 | { |
| 867 | int error; |
| 868 | |
| 869 | error = svc_sendto(rqstp, &rqstp->rq_res); |
| 870 | if (error == -ECONNREFUSED) |
| 871 | /* ICMP error on earlier request. */ |
| 872 | error = svc_sendto(rqstp, &rqstp->rq_res); |
| 873 | |
| 874 | return error; |
| 875 | } |
| 876 | |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 877 | static void svc_udp_prep_reply_hdr(struct svc_rqst *rqstp) |
| 878 | { |
| 879 | } |
| 880 | |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 881 | static int svc_udp_has_wspace(struct svc_xprt *xprt) |
| 882 | { |
| 883 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 884 | struct svc_serv *serv = xprt->xpt_server; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 885 | unsigned long required; |
| 886 | |
| 887 | /* |
| 888 | * Set the SOCK_NOSPACE flag before checking the available |
| 889 | * sock space. |
| 890 | */ |
| 891 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
Tom Tucker | 7a90e8c | 2007-12-30 21:07:55 -0600 | [diff] [blame] | 892 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 893 | if (required*2 > sock_wspace(svsk->sk_sk)) |
| 894 | return 0; |
| 895 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
| 896 | return 1; |
| 897 | } |
| 898 | |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 899 | static struct svc_xprt *svc_udp_accept(struct svc_xprt *xprt) |
| 900 | { |
| 901 | BUG(); |
| 902 | return NULL; |
| 903 | } |
| 904 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 905 | static struct svc_xprt *svc_udp_create(struct svc_serv *serv, |
| 906 | struct sockaddr *sa, int salen, |
| 907 | int flags) |
| 908 | { |
| 909 | return svc_create_socket(serv, IPPROTO_UDP, sa, salen, flags); |
| 910 | } |
| 911 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 912 | static struct svc_xprt_ops svc_udp_ops = { |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 913 | .xpo_create = svc_udp_create, |
Tom Tucker | 5d13799 | 2007-12-30 21:07:23 -0600 | [diff] [blame] | 914 | .xpo_recvfrom = svc_udp_recvfrom, |
| 915 | .xpo_sendto = svc_udp_sendto, |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 916 | .xpo_release_rqst = svc_release_skb, |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 917 | .xpo_detach = svc_sock_detach, |
| 918 | .xpo_free = svc_sock_free, |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 919 | .xpo_prep_reply_hdr = svc_udp_prep_reply_hdr, |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 920 | .xpo_has_wspace = svc_udp_has_wspace, |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 921 | .xpo_accept = svc_udp_accept, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 922 | }; |
| 923 | |
| 924 | static struct svc_xprt_class svc_udp_class = { |
| 925 | .xcl_name = "udp", |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 926 | .xcl_owner = THIS_MODULE, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 927 | .xcl_ops = &svc_udp_ops, |
Tom Tucker | 4902315 | 2007-12-30 21:07:21 -0600 | [diff] [blame] | 928 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_UDP, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 929 | }; |
| 930 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 931 | 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] | 932 | { |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 933 | int one = 1; |
| 934 | mm_segment_t oldfs; |
| 935 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 936 | svc_xprt_init(&svc_udp_class, &svsk->sk_xprt, serv); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 937 | clear_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | svsk->sk_sk->sk_data_ready = svc_udp_data_ready; |
| 939 | svsk->sk_sk->sk_write_space = svc_write_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | /* initialise setting must have enough space to |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 942 | * receive and respond to one request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | * svc_udp_recvfrom will re-adjust if necessary |
| 944 | */ |
| 945 | svc_sock_setbufsize(svsk->sk_sock, |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 946 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
| 947 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 949 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); /* might have come in before data_ready set up */ |
| 950 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
NeilBrown | 7a37f57 | 2007-03-06 01:42:21 -0800 | [diff] [blame] | 951 | |
| 952 | oldfs = get_fs(); |
| 953 | set_fs(KERNEL_DS); |
| 954 | /* make sure we get destination address info */ |
| 955 | svsk->sk_sock->ops->setsockopt(svsk->sk_sock, IPPROTO_IP, IP_PKTINFO, |
| 956 | (char __user *)&one, sizeof(one)); |
| 957 | set_fs(oldfs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* |
| 961 | * A data_ready event on a listening socket means there's a connection |
| 962 | * pending. Do not use state_change as a substitute for it. |
| 963 | */ |
| 964 | static void |
| 965 | svc_tcp_listen_data_ready(struct sock *sk, int count_unused) |
| 966 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 967 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | |
| 969 | dprintk("svc: socket %p TCP (listen) state change %d\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 970 | sk, sk->sk_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 972 | /* |
| 973 | * This callback may called twice when a new connection |
| 974 | * is established as a child socket inherits everything |
| 975 | * from a parent LISTEN socket. |
| 976 | * 1) data_ready method of the parent socket will be called |
| 977 | * when one of child sockets become ESTABLISHED. |
| 978 | * 2) data_ready method of the child socket may be called |
| 979 | * when it receives data before the socket is accepted. |
| 980 | * In case of 2, we should ignore it silently. |
| 981 | */ |
| 982 | if (sk->sk_state == TCP_LISTEN) { |
| 983 | if (svsk) { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 984 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 985 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 986 | } else |
| 987 | printk("svc: socket %p: no user data\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
| 991 | wake_up_interruptible_all(sk->sk_sleep); |
| 992 | } |
| 993 | |
| 994 | /* |
| 995 | * A state change on a connected socket means it's dying or dead. |
| 996 | */ |
| 997 | static void |
| 998 | svc_tcp_state_change(struct sock *sk) |
| 999 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1000 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
| 1002 | dprintk("svc: socket %p TCP (connected) state change %d (svsk %p)\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1003 | sk, sk->sk_state, sk->sk_user_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1005 | if (!svsk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | printk("svc: socket %p: no user data\n", sk); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1007 | else { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1008 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1009 | svc_xprt_enqueue(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -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_all(sk->sk_sleep); |
| 1013 | } |
| 1014 | |
| 1015 | static void |
| 1016 | svc_tcp_data_ready(struct sock *sk, int count) |
| 1017 | { |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1018 | struct svc_sock *svsk = (struct svc_sock *)sk->sk_user_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1019 | |
| 1020 | dprintk("svc: socket %p TCP data ready (svsk %p)\n", |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1021 | sk, sk->sk_user_data); |
| 1022 | if (svsk) { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1023 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1024 | svc_xprt_enqueue(&svsk->sk_xprt); |
Neil Brown | 939bb7e | 2005-09-13 01:25:39 -0700 | [diff] [blame] | 1025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | if (sk->sk_sleep && waitqueue_active(sk->sk_sleep)) |
| 1027 | wake_up_interruptible(sk->sk_sleep); |
| 1028 | } |
| 1029 | |
Chuck Lever | bcdb81a | 2007-02-12 00:53:37 -0800 | [diff] [blame] | 1030 | static inline int svc_port_is_privileged(struct sockaddr *sin) |
| 1031 | { |
| 1032 | switch (sin->sa_family) { |
| 1033 | case AF_INET: |
| 1034 | return ntohs(((struct sockaddr_in *)sin)->sin_port) |
| 1035 | < PROT_SOCK; |
Chuck Lever | bcdb81a | 2007-02-12 00:53:37 -0800 | [diff] [blame] | 1036 | case AF_INET6: |
| 1037 | return ntohs(((struct sockaddr_in6 *)sin)->sin6_port) |
| 1038 | < PROT_SOCK; |
Chuck Lever | bcdb81a | 2007-02-12 00:53:37 -0800 | [diff] [blame] | 1039 | default: |
| 1040 | return 0; |
| 1041 | } |
| 1042 | } |
| 1043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | /* |
| 1045 | * Accept a TCP connection |
| 1046 | */ |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1047 | static struct svc_xprt *svc_tcp_accept(struct svc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1049 | 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] | 1050 | struct sockaddr_storage addr; |
| 1051 | struct sockaddr *sin = (struct sockaddr *) &addr; |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1052 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | struct socket *sock = svsk->sk_sock; |
| 1054 | struct socket *newsock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | struct svc_sock *newsvsk; |
| 1056 | int err, slen; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1057 | char buf[RPC_MAX_ADDRBUFLEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | |
| 1059 | dprintk("svc: tcp_accept %p sock %p\n", svsk, sock); |
| 1060 | if (!sock) |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1061 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1063 | clear_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1064 | err = kernel_accept(sock, &newsock, O_NONBLOCK); |
| 1065 | if (err < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | if (err == -ENOMEM) |
| 1067 | printk(KERN_WARNING "%s: no more sockets!\n", |
| 1068 | serv->sv_name); |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1069 | else if (err != -EAGAIN && net_ratelimit()) |
| 1070 | printk(KERN_WARNING "%s: accept failed (err %d)!\n", |
| 1071 | serv->sv_name, -err); |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1072 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | } |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1074 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 1076 | err = kernel_getpeername(newsock, sin, &slen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | if (err < 0) { |
| 1078 | if (net_ratelimit()) |
| 1079 | printk(KERN_WARNING "%s: peername failed (err %d)!\n", |
| 1080 | serv->sv_name, -err); |
| 1081 | goto failed; /* aborted connection or whatever */ |
| 1082 | } |
| 1083 | |
| 1084 | /* Ideally, we would want to reject connections from unauthorized |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1085 | * hosts here, but when we get encryption, the IP of the host won't |
| 1086 | * tell us anything. For now just warn about unpriv connections. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | */ |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 1088 | if (!svc_port_is_privileged(sin)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | dprintk(KERN_WARNING |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1090 | "%s: connect from unprivileged port: %s\n", |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1091 | serv->sv_name, |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 1092 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1094 | dprintk("%s: connect from %s\n", serv->sv_name, |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 1095 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | |
| 1097 | /* make sure that a write doesn't block forever when |
| 1098 | * low on memory |
| 1099 | */ |
| 1100 | newsock->sk->sk_sndtimeo = HZ*30; |
| 1101 | |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1102 | if (!(newsvsk = svc_setup_socket(serv, newsock, &err, |
| 1103 | (SVC_SOCK_ANONYMOUS | SVC_SOCK_TEMPORARY)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | goto failed; |
akpm@linux-foundation.org | cdd88b9 | 2007-02-12 00:53:38 -0800 | [diff] [blame] | 1105 | memcpy(&newsvsk->sk_remote, sin, slen); |
Chuck Lever | 067d781 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1106 | newsvsk->sk_remotelen = slen; |
Frank van Maarseveen | a974769 | 2007-07-09 22:21:39 +0200 | [diff] [blame] | 1107 | err = kernel_getsockname(newsock, sin, &slen); |
| 1108 | if (unlikely(err < 0)) { |
| 1109 | dprintk("svc_tcp_accept: kernel_getsockname error %d\n", -err); |
| 1110 | slen = offsetof(struct sockaddr, sa_data); |
| 1111 | } |
| 1112 | memcpy(&newsvsk->sk_local, sin, slen); |
Chuck Lever | 067d781 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1113 | |
Tom Tucker | f9f3cc4 | 2007-12-30 21:07:40 -0600 | [diff] [blame] | 1114 | if (serv->sv_stats) |
| 1115 | serv->sv_stats->nettcpconn++; |
| 1116 | |
| 1117 | return &newsvsk->sk_xprt; |
| 1118 | |
| 1119 | failed: |
| 1120 | sock_release(newsock); |
| 1121 | return NULL; |
| 1122 | } |
| 1123 | |
| 1124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | * Receive data from a TCP socket. |
| 1126 | */ |
| 1127 | static int |
| 1128 | svc_tcp_recvfrom(struct svc_rqst *rqstp) |
| 1129 | { |
| 1130 | struct svc_sock *svsk = rqstp->rq_sock; |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1131 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | int len; |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1133 | struct kvec *vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | int pnum, vlen; |
| 1135 | |
| 1136 | dprintk("svc: tcp_recv %p data %d conn %d close %d\n", |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1137 | svsk, test_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags), |
| 1138 | test_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags), |
| 1139 | test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | |
| 1141 | if ((rqstp->rq_deferred = svc_deferred_dequeue(svsk))) { |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1142 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | return svc_deferred_recv(rqstp); |
| 1144 | } |
| 1145 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1146 | if (test_and_clear_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1147 | /* sndbuf needs to have room for one request |
| 1148 | * per thread, otherwise we can stall even when the |
| 1149 | * network isn't a bottleneck. |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1150 | * |
| 1151 | * We count all threads rather than threads in a |
| 1152 | * particular pool, which provides an upper bound |
| 1153 | * on the number of threads which will access the socket. |
| 1154 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | * rcvbuf just needs to be able to hold a few requests. |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1156 | * Normally they will be removed from the queue |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | * as soon a a complete request arrives. |
| 1158 | */ |
| 1159 | svc_sock_setbufsize(svsk->sk_sock, |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 1160 | (serv->sv_nrthreads+3) * serv->sv_max_mesg, |
| 1161 | 3 * serv->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1163 | clear_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | |
| 1165 | /* Receive data. If we haven't got the record length yet, get |
| 1166 | * the next four bytes. Otherwise try to gobble up as much as |
| 1167 | * possible up to the complete record length. |
| 1168 | */ |
| 1169 | if (svsk->sk_tcplen < 4) { |
| 1170 | unsigned long want = 4 - svsk->sk_tcplen; |
| 1171 | struct kvec iov; |
| 1172 | |
| 1173 | iov.iov_base = ((char *) &svsk->sk_reclen) + svsk->sk_tcplen; |
| 1174 | iov.iov_len = want; |
| 1175 | if ((len = svc_recvfrom(rqstp, &iov, 1, want)) < 0) |
| 1176 | goto error; |
| 1177 | svsk->sk_tcplen += len; |
| 1178 | |
| 1179 | if (len < want) { |
| 1180 | dprintk("svc: short recvfrom while reading record length (%d of %lu)\n", |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1181 | len, want); |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1182 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | return -EAGAIN; /* record header not complete */ |
| 1184 | } |
| 1185 | |
| 1186 | svsk->sk_reclen = ntohl(svsk->sk_reclen); |
| 1187 | if (!(svsk->sk_reclen & 0x80000000)) { |
| 1188 | /* FIXME: technically, a record can be fragmented, |
| 1189 | * and non-terminal fragments will not have the top |
| 1190 | * bit set in the fragment length header. |
| 1191 | * But apparently no known nfs clients send fragmented |
| 1192 | * records. */ |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 1193 | if (net_ratelimit()) |
| 1194 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" |
| 1195 | " (non-terminal)\n", |
| 1196 | (unsigned long) svsk->sk_reclen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | goto err_delete; |
| 1198 | } |
| 1199 | svsk->sk_reclen &= 0x7fffffff; |
| 1200 | dprintk("svc: TCP record, %d bytes\n", svsk->sk_reclen); |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 1201 | if (svsk->sk_reclen > serv->sv_max_mesg) { |
NeilBrown | 34e9a63 | 2007-01-29 13:19:52 -0800 | [diff] [blame] | 1202 | if (net_ratelimit()) |
| 1203 | printk(KERN_NOTICE "RPC: bad TCP reclen 0x%08lx" |
| 1204 | " (large)\n", |
| 1205 | (unsigned long) svsk->sk_reclen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | goto err_delete; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | /* Check whether enough data is available */ |
| 1211 | len = svc_recv_available(svsk); |
| 1212 | if (len < 0) |
| 1213 | goto error; |
| 1214 | |
| 1215 | if (len < svsk->sk_reclen) { |
| 1216 | dprintk("svc: incomplete TCP record (%d of %d)\n", |
| 1217 | len, svsk->sk_reclen); |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1218 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | return -EAGAIN; /* record not complete */ |
| 1220 | } |
| 1221 | len = svsk->sk_reclen; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1222 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
NeilBrown | 3cc03b1 | 2006-10-04 02:15:47 -0700 | [diff] [blame] | 1224 | vec = rqstp->rq_vec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | vec[0] = rqstp->rq_arg.head[0]; |
| 1226 | vlen = PAGE_SIZE; |
| 1227 | pnum = 1; |
| 1228 | while (vlen < len) { |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1229 | vec[pnum].iov_base = page_address(rqstp->rq_pages[pnum]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | vec[pnum].iov_len = PAGE_SIZE; |
| 1231 | pnum++; |
| 1232 | vlen += PAGE_SIZE; |
| 1233 | } |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1234 | rqstp->rq_respages = &rqstp->rq_pages[pnum]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | |
| 1236 | /* Now receive data */ |
| 1237 | len = svc_recvfrom(rqstp, vec, pnum, len); |
| 1238 | if (len < 0) |
| 1239 | goto error; |
| 1240 | |
| 1241 | dprintk("svc: TCP complete record (%d bytes)\n", len); |
| 1242 | rqstp->rq_arg.len = len; |
| 1243 | rqstp->rq_arg.page_base = 0; |
| 1244 | if (len <= rqstp->rq_arg.head[0].iov_len) { |
| 1245 | rqstp->rq_arg.head[0].iov_len = len; |
| 1246 | rqstp->rq_arg.page_len = 0; |
| 1247 | } else { |
| 1248 | rqstp->rq_arg.page_len = len - rqstp->rq_arg.head[0].iov_len; |
| 1249 | } |
| 1250 | |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 1251 | rqstp->rq_xprt_ctxt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | rqstp->rq_prot = IPPROTO_TCP; |
| 1253 | |
| 1254 | /* Reset TCP read info */ |
| 1255 | svsk->sk_reclen = 0; |
| 1256 | svsk->sk_tcplen = 0; |
| 1257 | |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1258 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | if (serv->sv_stats) |
| 1260 | serv->sv_stats->nettcpcnt++; |
| 1261 | |
| 1262 | return len; |
| 1263 | |
| 1264 | err_delete: |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1265 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | return -EAGAIN; |
| 1267 | |
| 1268 | error: |
| 1269 | if (len == -EAGAIN) { |
| 1270 | dprintk("RPC: TCP recvfrom got EAGAIN\n"); |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1271 | svc_xprt_received(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1272 | } else { |
| 1273 | printk(KERN_NOTICE "%s: recvfrom returned errno %d\n", |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1274 | svsk->sk_xprt.xpt_server->sv_name, -len); |
Olaf Kirch | 93fbf1a | 2006-01-06 00:19:56 -0800 | [diff] [blame] | 1275 | goto err_delete; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | return len; |
| 1279 | } |
| 1280 | |
| 1281 | /* |
| 1282 | * Send out data on TCP socket. |
| 1283 | */ |
| 1284 | static int |
| 1285 | svc_tcp_sendto(struct svc_rqst *rqstp) |
| 1286 | { |
| 1287 | struct xdr_buf *xbufp = &rqstp->rq_res; |
| 1288 | int sent; |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 1289 | __be32 reclen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | |
| 1291 | /* Set up the first element of the reply kvec. |
| 1292 | * Any other kvecs that may be in use have been taken |
| 1293 | * care of by the server implementation itself. |
| 1294 | */ |
| 1295 | reclen = htonl(0x80000000|((xbufp->len ) - 4)); |
| 1296 | memcpy(xbufp->head[0].iov_base, &reclen, 4); |
| 1297 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1298 | if (test_bit(XPT_DEAD, &rqstp->rq_sock->sk_xprt.xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | return -ENOTCONN; |
| 1300 | |
| 1301 | sent = svc_sendto(rqstp, &rqstp->rq_res); |
| 1302 | if (sent != xbufp->len) { |
| 1303 | 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] | 1304 | rqstp->rq_sock->sk_xprt.xpt_server->sv_name, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | (sent<0)?"got error":"sent only", |
| 1306 | sent, xbufp->len); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1307 | set_bit(XPT_CLOSE, &rqstp->rq_sock->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1308 | svc_xprt_enqueue(rqstp->rq_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | sent = -EAGAIN; |
| 1310 | } |
| 1311 | return sent; |
| 1312 | } |
| 1313 | |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 1314 | /* |
| 1315 | * Setup response header. TCP has a 4B record length field. |
| 1316 | */ |
| 1317 | static void svc_tcp_prep_reply_hdr(struct svc_rqst *rqstp) |
| 1318 | { |
| 1319 | struct kvec *resv = &rqstp->rq_res.head[0]; |
| 1320 | |
| 1321 | /* tcp needs a space for the record length... */ |
| 1322 | svc_putnl(resv, 0); |
| 1323 | } |
| 1324 | |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 1325 | static int svc_tcp_has_wspace(struct svc_xprt *xprt) |
| 1326 | { |
| 1327 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1328 | struct svc_serv *serv = svsk->sk_xprt.xpt_server; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 1329 | int required; |
| 1330 | int wspace; |
| 1331 | |
| 1332 | /* |
| 1333 | * Set the SOCK_NOSPACE flag before checking the available |
| 1334 | * sock space. |
| 1335 | */ |
| 1336 | set_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
Tom Tucker | 7a90e8c | 2007-12-30 21:07:55 -0600 | [diff] [blame] | 1337 | required = atomic_read(&svsk->sk_xprt.xpt_reserved) + serv->sv_max_mesg; |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 1338 | wspace = sk_stream_wspace(svsk->sk_sk); |
| 1339 | |
| 1340 | if (wspace < sk_stream_min_wspace(svsk->sk_sk)) |
| 1341 | return 0; |
| 1342 | if (required * 2 > wspace) |
| 1343 | return 0; |
| 1344 | |
| 1345 | clear_bit(SOCK_NOSPACE, &svsk->sk_sock->flags); |
| 1346 | return 1; |
| 1347 | } |
| 1348 | |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1349 | static struct svc_xprt *svc_tcp_create(struct svc_serv *serv, |
| 1350 | struct sockaddr *sa, int salen, |
| 1351 | int flags) |
| 1352 | { |
| 1353 | return svc_create_socket(serv, IPPROTO_TCP, sa, salen, flags); |
| 1354 | } |
| 1355 | |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1356 | static struct svc_xprt_ops svc_tcp_ops = { |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1357 | .xpo_create = svc_tcp_create, |
Tom Tucker | 5d13799 | 2007-12-30 21:07:23 -0600 | [diff] [blame] | 1358 | .xpo_recvfrom = svc_tcp_recvfrom, |
| 1359 | .xpo_sendto = svc_tcp_sendto, |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 1360 | .xpo_release_rqst = svc_release_skb, |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1361 | .xpo_detach = svc_sock_detach, |
| 1362 | .xpo_free = svc_sock_free, |
Tom Tucker | e831fe6 | 2007-12-30 21:07:29 -0600 | [diff] [blame] | 1363 | .xpo_prep_reply_hdr = svc_tcp_prep_reply_hdr, |
Tom Tucker | 323bee3 | 2007-12-30 21:07:31 -0600 | [diff] [blame] | 1364 | .xpo_has_wspace = svc_tcp_has_wspace, |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1365 | .xpo_accept = svc_tcp_accept, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1366 | }; |
| 1367 | |
| 1368 | static struct svc_xprt_class svc_tcp_class = { |
| 1369 | .xcl_name = "tcp", |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1370 | .xcl_owner = THIS_MODULE, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1371 | .xcl_ops = &svc_tcp_ops, |
Tom Tucker | 4902315 | 2007-12-30 21:07:21 -0600 | [diff] [blame] | 1372 | .xcl_max_payload = RPCSVC_MAXPAYLOAD_TCP, |
Tom Tucker | 360d8738 | 2007-12-30 21:07:17 -0600 | [diff] [blame] | 1373 | }; |
| 1374 | |
| 1375 | void svc_init_xprt_sock(void) |
| 1376 | { |
| 1377 | svc_reg_xprt_class(&svc_tcp_class); |
| 1378 | svc_reg_xprt_class(&svc_udp_class); |
| 1379 | } |
| 1380 | |
| 1381 | void svc_cleanup_xprt_sock(void) |
| 1382 | { |
| 1383 | svc_unreg_xprt_class(&svc_tcp_class); |
| 1384 | svc_unreg_xprt_class(&svc_udp_class); |
| 1385 | } |
| 1386 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1387 | 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] | 1388 | { |
| 1389 | struct sock *sk = svsk->sk_sk; |
| 1390 | struct tcp_sock *tp = tcp_sk(sk); |
| 1391 | |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1392 | svc_xprt_init(&svc_tcp_class, &svsk->sk_xprt, serv); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 1393 | set_bit(XPT_CACHE_AUTH, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | if (sk->sk_state == TCP_LISTEN) { |
| 1395 | dprintk("setting up TCP socket for listening\n"); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1396 | set_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | sk->sk_data_ready = svc_tcp_listen_data_ready; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1398 | set_bit(XPT_CONN, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1399 | } else { |
| 1400 | dprintk("setting up TCP socket for reading\n"); |
| 1401 | sk->sk_state_change = svc_tcp_state_change; |
| 1402 | sk->sk_data_ready = svc_tcp_data_ready; |
| 1403 | sk->sk_write_space = svc_write_space; |
| 1404 | |
| 1405 | svsk->sk_reclen = 0; |
| 1406 | svsk->sk_tcplen = 0; |
| 1407 | |
| 1408 | tp->nonagle = 1; /* disable Nagle's algorithm */ |
| 1409 | |
| 1410 | /* initialise setting must have enough space to |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1411 | * receive and respond to one request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | * svc_tcp_recvfrom will re-adjust if necessary |
| 1413 | */ |
| 1414 | svc_sock_setbufsize(svsk->sk_sock, |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1415 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg, |
| 1416 | 3 * svsk->sk_xprt.xpt_server->sv_max_mesg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1418 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
| 1419 | set_bit(XPT_DATA, &svsk->sk_xprt.xpt_flags); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1420 | if (sk->sk_state != TCP_ESTABLISHED) |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1421 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | void |
| 1426 | svc_sock_update_bufs(struct svc_serv *serv) |
| 1427 | { |
| 1428 | /* |
| 1429 | * The number of server threads has changed. Update |
| 1430 | * rcvbuf and sndbuf accordingly on all sockets |
| 1431 | */ |
| 1432 | struct list_head *le; |
| 1433 | |
| 1434 | spin_lock_bh(&serv->sv_lock); |
| 1435 | list_for_each(le, &serv->sv_permsocks) { |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1436 | struct svc_sock *svsk = |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1437 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1438 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1439 | } |
| 1440 | list_for_each(le, &serv->sv_tempsocks) { |
| 1441 | struct svc_sock *svsk = |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1442 | list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1443 | set_bit(XPT_CHNGBUF, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
| 1445 | spin_unlock_bh(&serv->sv_lock); |
| 1446 | } |
| 1447 | |
| 1448 | /* |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1449 | * Make sure that we don't have too many active connections. If we |
| 1450 | * have, something must be dropped. |
| 1451 | * |
| 1452 | * There's no point in trying to do random drop here for DoS |
| 1453 | * prevention. The NFS clients does 1 reconnect in 15 seconds. An |
| 1454 | * attacker can easily beat that. |
| 1455 | * |
| 1456 | * The only somewhat efficient mechanism would be if drop old |
| 1457 | * connections from the same IP first. But right now we don't even |
| 1458 | * record the client IP in svc_sock. |
| 1459 | */ |
| 1460 | static void svc_check_conn_limits(struct svc_serv *serv) |
| 1461 | { |
| 1462 | if (serv->sv_tmpcnt > (serv->sv_nrthreads+3)*20) { |
| 1463 | struct svc_sock *svsk = NULL; |
| 1464 | spin_lock_bh(&serv->sv_lock); |
| 1465 | if (!list_empty(&serv->sv_tempsocks)) { |
| 1466 | if (net_ratelimit()) { |
| 1467 | /* Try to help the admin */ |
| 1468 | printk(KERN_NOTICE "%s: too many open TCP " |
| 1469 | "sockets, consider increasing the " |
| 1470 | "number of nfsd threads\n", |
| 1471 | serv->sv_name); |
| 1472 | } |
| 1473 | /* |
| 1474 | * Always select the oldest socket. It's not fair, |
| 1475 | * but so is life |
| 1476 | */ |
| 1477 | svsk = list_entry(serv->sv_tempsocks.prev, |
| 1478 | struct svc_sock, |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1479 | sk_xprt.xpt_list); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1480 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1481 | svc_xprt_get(&svsk->sk_xprt); |
| 1482 | } |
| 1483 | spin_unlock_bh(&serv->sv_lock); |
| 1484 | |
| 1485 | if (svsk) { |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1486 | svc_xprt_enqueue(&svsk->sk_xprt); |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1487 | svc_xprt_put(&svsk->sk_xprt); |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /* |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1493 | * Receive the next request on any socket. This code is carefully |
| 1494 | * organised not to touch any cachelines in the shared svc_serv |
| 1495 | * structure, only cachelines in the local svc_pool. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | */ |
| 1497 | int |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 1498 | svc_recv(struct svc_rqst *rqstp, long timeout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1499 | { |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 1500 | struct svc_sock *svsk = NULL; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 1501 | struct svc_serv *serv = rqstp->rq_server; |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1502 | struct svc_pool *pool = rqstp->rq_pool; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1503 | int len, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | int pages; |
| 1505 | struct xdr_buf *arg; |
| 1506 | DECLARE_WAITQUEUE(wait, current); |
| 1507 | |
| 1508 | dprintk("svc: server %p waiting for data (to = %ld)\n", |
| 1509 | rqstp, timeout); |
| 1510 | |
| 1511 | if (rqstp->rq_sock) |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1512 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | "svc_recv: service %p, socket not NULL!\n", |
| 1514 | rqstp); |
| 1515 | if (waitqueue_active(&rqstp->rq_wait)) |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1516 | printk(KERN_ERR |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | "svc_recv: service %p, wait queue active!\n", |
| 1518 | rqstp); |
| 1519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | /* now allocate needed pages. If we get a failure, sleep briefly */ |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 1522 | pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1523 | for (i=0; i < pages ; i++) |
| 1524 | while (rqstp->rq_pages[i] == NULL) { |
| 1525 | struct page *p = alloc_page(GFP_KERNEL); |
| 1526 | if (!p) |
| 1527 | schedule_timeout_uninterruptible(msecs_to_jiffies(500)); |
| 1528 | rqstp->rq_pages[i] = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
NeilBrown | 250f391 | 2007-01-26 00:56:59 -0800 | [diff] [blame] | 1530 | rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */ |
| 1531 | BUG_ON(pages >= RPCSVC_MAXPAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
| 1533 | /* Make arg->head point to first page and arg->pages point to rest */ |
| 1534 | arg = &rqstp->rq_arg; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1535 | arg->head[0].iov_base = page_address(rqstp->rq_pages[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | arg->head[0].iov_len = PAGE_SIZE; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 1537 | arg->pages = rqstp->rq_pages + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | arg->page_base = 0; |
| 1539 | /* save at least one page for response */ |
| 1540 | arg->page_len = (pages-2)*PAGE_SIZE; |
| 1541 | arg->len = (pages-1)*PAGE_SIZE; |
| 1542 | arg->tail[0].iov_len = 0; |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 1543 | |
| 1544 | try_to_freeze(); |
NeilBrown | 1887b93 | 2005-11-15 00:09:10 -0800 | [diff] [blame] | 1545 | cond_resched(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | if (signalled()) |
| 1547 | return -EINTR; |
| 1548 | |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1549 | spin_lock_bh(&pool->sp_lock); |
| 1550 | if ((svsk = svc_sock_dequeue(pool)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | rqstp->rq_sock = svsk; |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1552 | svc_xprt_get(&svsk->sk_xprt); |
NeilBrown | c6b0a9f | 2006-10-06 00:44:05 -0700 | [diff] [blame] | 1553 | rqstp->rq_reserved = serv->sv_max_mesg; |
Tom Tucker | 7a90e8c | 2007-12-30 21:07:55 -0600 | [diff] [blame] | 1554 | atomic_add(rqstp->rq_reserved, &svsk->sk_xprt.xpt_reserved); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1555 | } else { |
| 1556 | /* No data pending. Go to sleep */ |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1557 | svc_thread_enqueue(pool, rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | |
| 1559 | /* |
| 1560 | * We have to be able to interrupt this wait |
| 1561 | * to bring down the daemons ... |
| 1562 | */ |
| 1563 | set_current_state(TASK_INTERRUPTIBLE); |
| 1564 | add_wait_queue(&rqstp->rq_wait, &wait); |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1565 | spin_unlock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | |
| 1567 | schedule_timeout(timeout); |
| 1568 | |
Christoph Lameter | 3e1d1d2 | 2005-06-24 23:13:50 -0700 | [diff] [blame] | 1569 | try_to_freeze(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1571 | spin_lock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | remove_wait_queue(&rqstp->rq_wait, &wait); |
| 1573 | |
| 1574 | if (!(svsk = rqstp->rq_sock)) { |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1575 | svc_thread_dequeue(pool, rqstp); |
| 1576 | spin_unlock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | dprintk("svc: server %p, no data yet\n", rqstp); |
| 1578 | return signalled()? -EINTR : -EAGAIN; |
| 1579 | } |
| 1580 | } |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1581 | spin_unlock_bh(&pool->sp_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | |
Tom Tucker | d7979ae | 2007-12-30 21:07:34 -0600 | [diff] [blame] | 1583 | len = 0; |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1584 | if (test_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags)) { |
| 1585 | dprintk("svc_recv: found XPT_CLOSE\n"); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1586 | svc_delete_xprt(&svsk->sk_xprt); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1587 | } else if (test_bit(XPT_LISTENER, &svsk->sk_xprt.xpt_flags)) { |
Tom Tucker | 38a417c | 2007-12-30 21:07:36 -0600 | [diff] [blame] | 1588 | struct svc_xprt *newxpt; |
| 1589 | newxpt = svsk->sk_xprt.xpt_ops->xpo_accept(&svsk->sk_xprt); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1590 | if (newxpt) { |
| 1591 | /* |
| 1592 | * We know this module_get will succeed because the |
| 1593 | * listener holds a reference too |
| 1594 | */ |
| 1595 | __module_get(newxpt->xpt_class->xcl_owner); |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1596 | svc_check_conn_limits(svsk->sk_xprt.xpt_server); |
Tom Tucker | 6bc5ab1 | 2007-12-30 21:08:03 -0600 | [diff] [blame] | 1597 | svc_xprt_received(newxpt); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1598 | } |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1599 | svc_xprt_received(&svsk->sk_xprt); |
Tom Tucker | d7979ae | 2007-12-30 21:07:34 -0600 | [diff] [blame] | 1600 | } else { |
| 1601 | dprintk("svc: server %p, pool %u, socket %p, inuse=%d\n", |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1602 | rqstp, pool->sp_id, svsk, |
| 1603 | atomic_read(&svsk->sk_xprt.xpt_ref.refcount)); |
Tom Tucker | d7979ae | 2007-12-30 21:07:34 -0600 | [diff] [blame] | 1604 | len = svsk->sk_xprt.xpt_ops->xpo_recvfrom(rqstp); |
| 1605 | dprintk("svc: got len=%d\n", len); |
| 1606 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
| 1608 | /* No data, incomplete (TCP) read, or accept() */ |
| 1609 | if (len == 0 || len == -EAGAIN) { |
| 1610 | rqstp->rq_res.len = 0; |
| 1611 | svc_sock_release(rqstp); |
| 1612 | return -EAGAIN; |
| 1613 | } |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1614 | clear_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1615 | |
Chuck Lever | bcdb81a | 2007-02-12 00:53:37 -0800 | [diff] [blame] | 1616 | rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | rqstp->rq_chandle.defer = svc_defer; |
| 1618 | |
| 1619 | if (serv->sv_stats) |
| 1620 | serv->sv_stats->netcnt++; |
| 1621 | return len; |
| 1622 | } |
| 1623 | |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1624 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | * Drop request |
| 1626 | */ |
| 1627 | void |
| 1628 | svc_drop(struct svc_rqst *rqstp) |
| 1629 | { |
| 1630 | dprintk("svc: socket %p dropped request\n", rqstp->rq_sock); |
| 1631 | svc_sock_release(rqstp); |
| 1632 | } |
| 1633 | |
| 1634 | /* |
| 1635 | * Return reply to client. |
| 1636 | */ |
| 1637 | int |
| 1638 | svc_send(struct svc_rqst *rqstp) |
| 1639 | { |
Tom Tucker | a50fea2 | 2007-12-30 21:07:59 -0600 | [diff] [blame] | 1640 | struct svc_xprt *xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | int len; |
| 1642 | struct xdr_buf *xb; |
| 1643 | |
Tom Tucker | a50fea2 | 2007-12-30 21:07:59 -0600 | [diff] [blame] | 1644 | xprt = rqstp->rq_xprt; |
| 1645 | if (!xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1646 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | |
| 1648 | /* release the receive skb before sending the reply */ |
Tom Tucker | 5148bf4 | 2007-12-30 21:07:25 -0600 | [diff] [blame] | 1649 | rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1650 | |
| 1651 | /* calculate over-all length */ |
| 1652 | xb = & rqstp->rq_res; |
| 1653 | xb->len = xb->head[0].iov_len + |
| 1654 | xb->page_len + |
| 1655 | xb->tail[0].iov_len; |
| 1656 | |
Tom Tucker | a50fea2 | 2007-12-30 21:07:59 -0600 | [diff] [blame] | 1657 | /* Grab mutex to serialize outgoing data. */ |
| 1658 | mutex_lock(&xprt->xpt_mutex); |
| 1659 | if (test_bit(XPT_DEAD, &xprt->xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1660 | len = -ENOTCONN; |
| 1661 | else |
Tom Tucker | a50fea2 | 2007-12-30 21:07:59 -0600 | [diff] [blame] | 1662 | len = xprt->xpt_ops->xpo_sendto(rqstp); |
| 1663 | mutex_unlock(&xprt->xpt_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | svc_sock_release(rqstp); |
| 1665 | |
| 1666 | if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN) |
| 1667 | return 0; |
| 1668 | return len; |
| 1669 | } |
| 1670 | |
| 1671 | /* |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1672 | * Timer function to close old temporary sockets, using |
| 1673 | * a mark-and-sweep algorithm. |
| 1674 | */ |
| 1675 | static void |
| 1676 | svc_age_temp_sockets(unsigned long closure) |
| 1677 | { |
| 1678 | struct svc_serv *serv = (struct svc_serv *)closure; |
| 1679 | struct svc_sock *svsk; |
| 1680 | struct list_head *le, *next; |
| 1681 | LIST_HEAD(to_be_aged); |
| 1682 | |
| 1683 | dprintk("svc_age_temp_sockets\n"); |
| 1684 | |
| 1685 | if (!spin_trylock_bh(&serv->sv_lock)) { |
| 1686 | /* busy, try again 1 sec later */ |
| 1687 | dprintk("svc_age_temp_sockets: busy\n"); |
| 1688 | mod_timer(&serv->sv_temptimer, jiffies + HZ); |
| 1689 | return; |
| 1690 | } |
| 1691 | |
| 1692 | list_for_each_safe(le, next, &serv->sv_tempsocks) { |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1693 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1694 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1695 | if (!test_and_set_bit(XPT_OLD, &svsk->sk_xprt.xpt_flags)) |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1696 | continue; |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1697 | if (atomic_read(&svsk->sk_xprt.xpt_ref.refcount) > 1 |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1698 | || test_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags)) |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1699 | continue; |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1700 | svc_xprt_get(&svsk->sk_xprt); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1701 | list_move(le, &to_be_aged); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1702 | set_bit(XPT_CLOSE, &svsk->sk_xprt.xpt_flags); |
| 1703 | set_bit(XPT_DETACHED, &svsk->sk_xprt.xpt_flags); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1704 | } |
| 1705 | spin_unlock_bh(&serv->sv_lock); |
| 1706 | |
| 1707 | while (!list_empty(&to_be_aged)) { |
| 1708 | le = to_be_aged.next; |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1709 | /* 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] | 1710 | list_del_init(le); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1711 | svsk = list_entry(le, struct svc_sock, sk_xprt.xpt_list); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1712 | |
Tom Tucker | 4bc6c49 | 2007-12-30 21:08:05 -0600 | [diff] [blame] | 1713 | dprintk("queuing svsk %p for closing\n", svsk); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1714 | |
| 1715 | /* a thread will dequeue and close it soon */ |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1716 | svc_xprt_enqueue(&svsk->sk_xprt); |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1717 | svc_xprt_put(&svsk->sk_xprt); |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ); |
| 1721 | } |
| 1722 | |
| 1723 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | * Initialize socket for RPC use and create svc_sock struct |
| 1725 | * XXX: May want to setsockopt SO_SNDBUF and SO_RCVBUF. |
| 1726 | */ |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1727 | static struct svc_sock *svc_setup_socket(struct svc_serv *serv, |
| 1728 | struct socket *sock, |
| 1729 | int *errp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | { |
| 1731 | struct svc_sock *svsk; |
| 1732 | struct sock *inet; |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1733 | int pmap_register = !(flags & SVC_SOCK_ANONYMOUS); |
| 1734 | int is_temporary = flags & SVC_SOCK_TEMPORARY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | dprintk("svc: svc_setup_socket %p\n", sock); |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 1737 | if (!(svsk = kzalloc(sizeof(*svsk), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | *errp = -ENOMEM; |
| 1739 | return NULL; |
| 1740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | |
| 1742 | inet = sock->sk; |
| 1743 | |
| 1744 | /* Register socket with portmapper */ |
| 1745 | if (*errp >= 0 && pmap_register) |
| 1746 | *errp = svc_register(serv, inet->sk_protocol, |
| 1747 | ntohs(inet_sk(inet)->sport)); |
| 1748 | |
| 1749 | if (*errp < 0) { |
| 1750 | kfree(svsk); |
| 1751 | return NULL; |
| 1752 | } |
| 1753 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1754 | set_bit(XPT_BUSY, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | inet->sk_user_data = svsk; |
| 1756 | svsk->sk_sock = sock; |
| 1757 | svsk->sk_sk = inet; |
| 1758 | svsk->sk_ostate = inet->sk_state_change; |
| 1759 | svsk->sk_odata = inet->sk_data_ready; |
| 1760 | svsk->sk_owspace = inet->sk_write_space; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | INIT_LIST_HEAD(&svsk->sk_deferred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1762 | |
| 1763 | /* Initialize the socket */ |
| 1764 | if (sock->type == SOCK_DGRAM) |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1765 | svc_udp_init(svsk, serv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | else |
Tom Tucker | bb5cf16 | 2007-12-30 21:07:50 -0600 | [diff] [blame] | 1767 | svc_tcp_init(svsk, serv); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | |
| 1769 | spin_lock_bh(&serv->sv_lock); |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1770 | if (is_temporary) { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1771 | set_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1772 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_tempsocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | serv->sv_tmpcnt++; |
Greg Banks | 36bdfc8 | 2006-10-02 02:17:54 -0700 | [diff] [blame] | 1774 | if (serv->sv_temptimer.function == NULL) { |
| 1775 | /* setup timer to age temp sockets */ |
| 1776 | setup_timer(&serv->sv_temptimer, svc_age_temp_sockets, |
| 1777 | (unsigned long)serv); |
| 1778 | mod_timer(&serv->sv_temptimer, |
| 1779 | jiffies + svc_conn_age_period * HZ); |
| 1780 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | } else { |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1782 | clear_bit(XPT_TEMP, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1783 | list_add(&svsk->sk_xprt.xpt_list, &serv->sv_permsocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1784 | } |
| 1785 | spin_unlock_bh(&serv->sv_lock); |
| 1786 | |
| 1787 | dprintk("svc: svc_setup_socket created %p (inet %p)\n", |
| 1788 | svsk, svsk->sk_sk); |
| 1789 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | return svsk; |
| 1791 | } |
| 1792 | |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1793 | int svc_addsock(struct svc_serv *serv, |
| 1794 | int fd, |
| 1795 | char *name_return, |
| 1796 | int *proto) |
| 1797 | { |
| 1798 | int err = 0; |
| 1799 | struct socket *so = sockfd_lookup(fd, &err); |
| 1800 | struct svc_sock *svsk = NULL; |
| 1801 | |
| 1802 | if (!so) |
| 1803 | return err; |
| 1804 | if (so->sk->sk_family != AF_INET) |
| 1805 | err = -EAFNOSUPPORT; |
| 1806 | else if (so->sk->sk_protocol != IPPROTO_TCP && |
| 1807 | so->sk->sk_protocol != IPPROTO_UDP) |
| 1808 | err = -EPROTONOSUPPORT; |
| 1809 | else if (so->state > SS_UNCONNECTED) |
| 1810 | err = -EISCONN; |
| 1811 | else { |
Chuck Lever | 6b17433 | 2007-02-12 00:53:28 -0800 | [diff] [blame] | 1812 | svsk = svc_setup_socket(serv, so, &err, SVC_SOCK_DEFAULTS); |
NeilBrown | e79eff1 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1813 | if (svsk) { |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1814 | svc_xprt_received(&svsk->sk_xprt); |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1815 | err = 0; |
NeilBrown | e79eff1 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1816 | } |
NeilBrown | b41b66d | 2006-10-02 02:17:48 -0700 | [diff] [blame] | 1817 | } |
| 1818 | if (err) { |
| 1819 | sockfd_put(so); |
| 1820 | return err; |
| 1821 | } |
| 1822 | if (proto) *proto = so->sk->sk_protocol; |
| 1823 | return one_sock_name(name_return, svsk); |
| 1824 | } |
| 1825 | EXPORT_SYMBOL_GPL(svc_addsock); |
| 1826 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | /* |
| 1828 | * Create socket for RPC service. |
| 1829 | */ |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1830 | static struct svc_xprt *svc_create_socket(struct svc_serv *serv, |
| 1831 | int protocol, |
| 1832 | struct sockaddr *sin, int len, |
| 1833 | int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | { |
| 1835 | struct svc_sock *svsk; |
| 1836 | struct socket *sock; |
| 1837 | int error; |
| 1838 | int type; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1839 | char buf[RPC_MAX_ADDRBUFLEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 1841 | dprintk("svc: svc_create_socket(%s, %d, %s)\n", |
| 1842 | serv->sv_program->pg_name, protocol, |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1843 | __svc_print_addr(sin, buf, sizeof(buf))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | |
| 1845 | if (protocol != IPPROTO_UDP && protocol != IPPROTO_TCP) { |
| 1846 | printk(KERN_WARNING "svc: only UDP and TCP " |
| 1847 | "sockets supported\n"); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1848 | return ERR_PTR(-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | } |
| 1850 | type = (protocol == IPPROTO_UDP)? SOCK_DGRAM : SOCK_STREAM; |
| 1851 | |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1852 | error = sock_create_kern(sin->sa_family, type, protocol, &sock); |
| 1853 | if (error < 0) |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1854 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
Peter Zijlstra | ed07536 | 2006-12-06 20:35:24 -0800 | [diff] [blame] | 1856 | svc_reclassify_socket(sock); |
| 1857 | |
Eric Sesterhenn | 1811474 | 2006-09-28 14:37:07 -0700 | [diff] [blame] | 1858 | if (type == SOCK_STREAM) |
Chuck Lever | 77f1f67 | 2007-02-12 00:53:39 -0800 | [diff] [blame] | 1859 | sock->sk->sk_reuse = 1; /* allow address reuse */ |
| 1860 | error = kernel_bind(sock, sin, len); |
Eric Sesterhenn | 1811474 | 2006-09-28 14:37:07 -0700 | [diff] [blame] | 1861 | if (error < 0) |
| 1862 | goto bummer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
| 1864 | if (protocol == IPPROTO_TCP) { |
Sridhar Samudrala | e6242e9 | 2006-08-07 20:58:01 -0700 | [diff] [blame] | 1865 | if ((error = kernel_listen(sock, 64)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | goto bummer; |
| 1867 | } |
| 1868 | |
NeilBrown | e79eff1 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1869 | if ((svsk = svc_setup_socket(serv, sock, &error, flags)) != NULL) { |
Tom Tucker | a6046f7 | 2007-12-30 21:08:01 -0600 | [diff] [blame] | 1870 | svc_xprt_received(&svsk->sk_xprt); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1871 | return (struct svc_xprt *)svsk; |
NeilBrown | e79eff1 | 2007-02-12 00:53:30 -0800 | [diff] [blame] | 1872 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
| 1874 | bummer: |
| 1875 | dprintk("svc: svc_create_socket error = %d\n", -error); |
| 1876 | sock_release(sock); |
Tom Tucker | b700cbb | 2007-12-30 21:07:42 -0600 | [diff] [blame] | 1877 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | /* |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1881 | * Detach the svc_sock from the socket so that no |
| 1882 | * more callbacks occur. |
| 1883 | */ |
| 1884 | static void svc_sock_detach(struct svc_xprt *xprt) |
| 1885 | { |
| 1886 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 1887 | struct sock *sk = svsk->sk_sk; |
| 1888 | |
| 1889 | dprintk("svc: svc_sock_detach(%p)\n", svsk); |
| 1890 | |
| 1891 | /* put back the old socket callbacks */ |
| 1892 | sk->sk_state_change = svsk->sk_ostate; |
| 1893 | sk->sk_data_ready = svsk->sk_odata; |
| 1894 | sk->sk_write_space = svsk->sk_owspace; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * Free the svc_sock's socket resources and the svc_sock itself. |
| 1899 | */ |
| 1900 | static void svc_sock_free(struct svc_xprt *xprt) |
| 1901 | { |
| 1902 | struct svc_sock *svsk = container_of(xprt, struct svc_sock, sk_xprt); |
| 1903 | dprintk("svc: svc_sock_free(%p)\n", svsk); |
| 1904 | |
Tom Tucker | 755ccea | 2007-12-30 21:07:27 -0600 | [diff] [blame] | 1905 | if (svsk->sk_sock->file) |
| 1906 | sockfd_put(svsk->sk_sock); |
| 1907 | else |
| 1908 | sock_release(svsk->sk_sock); |
| 1909 | kfree(svsk); |
| 1910 | } |
| 1911 | |
| 1912 | /* |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1913 | * Remove a dead transport |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | */ |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1915 | static void svc_delete_xprt(struct svc_xprt *xprt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | { |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1917 | struct svc_serv *serv = xprt->xpt_server; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1919 | dprintk("svc: svc_delete_xprt(%p)\n", xprt); |
| 1920 | xprt->xpt_ops->xpo_detach(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | |
| 1922 | spin_lock_bh(&serv->sv_lock); |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1923 | if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags)) |
| 1924 | list_del_init(&xprt->xpt_list); |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1925 | /* |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1926 | * We used to delete the transport from whichever list |
| 1927 | * it's sk_xprt.xpt_ready node was on, but we don't actually |
Greg Banks | 3262c81 | 2006-10-02 02:17:58 -0700 | [diff] [blame] | 1928 | * need to. This is because the only time we're called |
| 1929 | * while still attached to a queue, the queue itself |
| 1930 | * is about to be destroyed (in svc_destroy). |
| 1931 | */ |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1932 | if (!test_and_set_bit(XPT_DEAD, &xprt->xpt_flags)) { |
| 1933 | BUG_ON(atomic_read(&xprt->xpt_ref.refcount) < 2); |
| 1934 | if (test_bit(XPT_TEMP, &xprt->xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | serv->sv_tmpcnt--; |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1936 | svc_xprt_put(xprt); |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 1937 | } |
Neil Brown | d6740df | 2006-10-29 22:46:45 -0800 | [diff] [blame] | 1938 | spin_unlock_bh(&serv->sv_lock); |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1941 | static void svc_close_xprt(struct svc_xprt *xprt) |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 1942 | { |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1943 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
| 1944 | if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) |
NeilBrown | aaf68cf | 2007-02-08 14:20:30 -0800 | [diff] [blame] | 1945 | /* someone else will have to effect the close */ |
| 1946 | return; |
| 1947 | |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1948 | svc_xprt_get(xprt); |
| 1949 | svc_delete_xprt(xprt); |
| 1950 | clear_bit(XPT_BUSY, &xprt->xpt_flags); |
| 1951 | svc_xprt_put(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | } |
| 1953 | |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1954 | void svc_close_all(struct list_head *xprt_list) |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame] | 1955 | { |
Tom Tucker | 7a18208 | 2007-12-30 21:07:53 -0600 | [diff] [blame] | 1956 | struct svc_xprt *xprt; |
| 1957 | struct svc_xprt *tmp; |
| 1958 | |
| 1959 | list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) { |
| 1960 | set_bit(XPT_CLOSE, &xprt->xpt_flags); |
| 1961 | if (test_bit(XPT_BUSY, &xprt->xpt_flags)) { |
| 1962 | /* Waiting to be processed, but no threads left, |
| 1963 | * So just remove it from the waiting list |
| 1964 | */ |
| 1965 | list_del_init(&xprt->xpt_ready); |
| 1966 | clear_bit(XPT_BUSY, &xprt->xpt_flags); |
| 1967 | } |
| 1968 | svc_close_xprt(xprt); |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame] | 1969 | } |
NeilBrown | cda1fd4 | 2007-03-06 01:42:22 -0800 | [diff] [blame] | 1970 | } |
| 1971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | /* |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 1973 | * Handle defer and revisit of requests |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1974 | */ |
| 1975 | |
| 1976 | static void svc_revisit(struct cache_deferred_req *dreq, int too_many) |
| 1977 | { |
| 1978 | struct svc_deferred_req *dr = container_of(dreq, struct svc_deferred_req, handle); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | struct svc_sock *svsk; |
| 1980 | |
| 1981 | if (too_many) { |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1982 | svc_xprt_put(&dr->svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | kfree(dr); |
| 1984 | return; |
| 1985 | } |
| 1986 | dprintk("revisit queued\n"); |
| 1987 | svsk = dr->svsk; |
| 1988 | dr->svsk = NULL; |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 1989 | spin_lock(&svsk->sk_xprt.xpt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | list_add(&dr->handle.recent, &svsk->sk_deferred); |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 1991 | spin_unlock(&svsk->sk_xprt.xpt_lock); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 1992 | set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
Tom Tucker | f6150c3 | 2007-12-30 21:07:57 -0600 | [diff] [blame] | 1993 | svc_xprt_enqueue(&svsk->sk_xprt); |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 1994 | svc_xprt_put(&svsk->sk_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | static struct cache_deferred_req * |
| 1998 | svc_defer(struct cache_req *req) |
| 1999 | { |
| 2000 | struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle); |
| 2001 | int size = sizeof(struct svc_deferred_req) + (rqstp->rq_arg.len); |
| 2002 | struct svc_deferred_req *dr; |
| 2003 | |
| 2004 | if (rqstp->rq_arg.page_len) |
| 2005 | return NULL; /* if more than a page, give up FIXME */ |
| 2006 | if (rqstp->rq_deferred) { |
| 2007 | dr = rqstp->rq_deferred; |
| 2008 | rqstp->rq_deferred = NULL; |
| 2009 | } else { |
| 2010 | int skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len; |
| 2011 | /* FIXME maybe discard if size too large */ |
| 2012 | dr = kmalloc(size, GFP_KERNEL); |
| 2013 | if (dr == NULL) |
| 2014 | return NULL; |
| 2015 | |
| 2016 | dr->handle.owner = rqstp->rq_server; |
| 2017 | dr->prot = rqstp->rq_prot; |
Chuck Lever | 2442222 | 2007-02-12 00:53:33 -0800 | [diff] [blame] | 2018 | memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen); |
| 2019 | dr->addrlen = rqstp->rq_addrlen; |
J. Bruce Fields | 1918e34 | 2006-01-18 17:43:16 -0800 | [diff] [blame] | 2020 | dr->daddr = rqstp->rq_daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | dr->argslen = rqstp->rq_arg.len >> 2; |
| 2022 | memcpy(dr->args, rqstp->rq_arg.head[0].iov_base-skip, dr->argslen<<2); |
| 2023 | } |
Tom Tucker | e1b3157 | 2007-12-30 21:07:46 -0600 | [diff] [blame] | 2024 | svc_xprt_get(rqstp->rq_xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | dr->svsk = rqstp->rq_sock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | |
| 2027 | dr->handle.revisit = svc_revisit; |
| 2028 | return &dr->handle; |
| 2029 | } |
| 2030 | |
| 2031 | /* |
| 2032 | * recv data from a deferred request into an active one |
| 2033 | */ |
| 2034 | static int svc_deferred_recv(struct svc_rqst *rqstp) |
| 2035 | { |
| 2036 | struct svc_deferred_req *dr = rqstp->rq_deferred; |
| 2037 | |
| 2038 | rqstp->rq_arg.head[0].iov_base = dr->args; |
| 2039 | rqstp->rq_arg.head[0].iov_len = dr->argslen<<2; |
| 2040 | rqstp->rq_arg.page_len = 0; |
| 2041 | rqstp->rq_arg.len = dr->argslen<<2; |
| 2042 | rqstp->rq_prot = dr->prot; |
Chuck Lever | 2442222 | 2007-02-12 00:53:33 -0800 | [diff] [blame] | 2043 | memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen); |
| 2044 | rqstp->rq_addrlen = dr->addrlen; |
J. Bruce Fields | 1918e34 | 2006-01-18 17:43:16 -0800 | [diff] [blame] | 2045 | rqstp->rq_daddr = dr->daddr; |
NeilBrown | 4452435 | 2006-10-04 02:15:46 -0700 | [diff] [blame] | 2046 | rqstp->rq_respages = rqstp->rq_pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | return dr->argslen<<2; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | static struct svc_deferred_req *svc_deferred_dequeue(struct svc_sock *svsk) |
| 2052 | { |
| 2053 | struct svc_deferred_req *dr = NULL; |
YOSHIFUJI Hideaki | cca5172 | 2007-02-09 15:38:13 -0800 | [diff] [blame] | 2054 | |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 2055 | if (!test_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | return NULL; |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 2057 | spin_lock(&svsk->sk_xprt.xpt_lock); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 2058 | clear_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | if (!list_empty(&svsk->sk_deferred)) { |
| 2060 | dr = list_entry(svsk->sk_deferred.next, |
| 2061 | struct svc_deferred_req, |
| 2062 | handle.recent); |
| 2063 | list_del_init(&dr->handle.recent); |
Tom Tucker | 02fc6c3 | 2007-12-30 21:07:48 -0600 | [diff] [blame] | 2064 | set_bit(XPT_DEFERRED, &svsk->sk_xprt.xpt_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | } |
Tom Tucker | def13d7 | 2007-12-30 21:08:08 -0600 | [diff] [blame^] | 2066 | spin_unlock(&svsk->sk_xprt.xpt_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | return dr; |
| 2068 | } |