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