Merge branch 'stacktrace-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[pandora-kernel.git] / net / sunrpc / svc_xprt.c
1 /*
2  * linux/net/sunrpc/svc_xprt.c
3  *
4  * Author: Tom Tucker <tom@opengridcomputing.com>
5  */
6
7 #include <linux/sched.h>
8 #include <linux/errno.h>
9 #include <linux/freezer.h>
10 #include <linux/kthread.h>
11 #include <net/sock.h>
12 #include <linux/sunrpc/stats.h>
13 #include <linux/sunrpc/svc_xprt.h>
14
15 #define RPCDBG_FACILITY RPCDBG_SVCXPRT
16
17 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt);
18 static int svc_deferred_recv(struct svc_rqst *rqstp);
19 static struct cache_deferred_req *svc_defer(struct cache_req *req);
20 static void svc_age_temp_xprts(unsigned long closure);
21
22 /* apparently the "standard" is that clients close
23  * idle connections after 5 minutes, servers after
24  * 6 minutes
25  *   http://www.connectathon.org/talks96/nfstcp.pdf
26  */
27 static int svc_conn_age_period = 6*60;
28
29 /* List of registered transport classes */
30 static DEFINE_SPINLOCK(svc_xprt_class_lock);
31 static LIST_HEAD(svc_xprt_class_list);
32
33 /* SMP locking strategy:
34  *
35  *      svc_pool->sp_lock protects most of the fields of that pool.
36  *      svc_serv->sv_lock protects sv_tempsocks, sv_permsocks, sv_tmpcnt.
37  *      when both need to be taken (rare), svc_serv->sv_lock is first.
38  *      BKL protects svc_serv->sv_nrthread.
39  *      svc_sock->sk_lock protects the svc_sock->sk_deferred list
40  *             and the ->sk_info_authunix cache.
41  *
42  *      The XPT_BUSY bit in xprt->xpt_flags prevents a transport being
43  *      enqueued multiply. During normal transport processing this bit
44  *      is set by svc_xprt_enqueue and cleared by svc_xprt_received.
45  *      Providers should not manipulate this bit directly.
46  *
47  *      Some flags can be set to certain values at any time
48  *      providing that certain rules are followed:
49  *
50  *      XPT_CONN, XPT_DATA:
51  *              - Can be set or cleared at any time.
52  *              - After a set, svc_xprt_enqueue must be called to enqueue
53  *                the transport for processing.
54  *              - After a clear, the transport must be read/accepted.
55  *                If this succeeds, it must be set again.
56  *      XPT_CLOSE:
57  *              - Can set at any time. It is never cleared.
58  *      XPT_DEAD:
59  *              - Can only be set while XPT_BUSY is held which ensures
60  *                that no other thread will be using the transport or will
61  *                try to set XPT_DEAD.
62  */
63
64 int svc_reg_xprt_class(struct svc_xprt_class *xcl)
65 {
66         struct svc_xprt_class *cl;
67         int res = -EEXIST;
68
69         dprintk("svc: Adding svc transport class '%s'\n", xcl->xcl_name);
70
71         INIT_LIST_HEAD(&xcl->xcl_list);
72         spin_lock(&svc_xprt_class_lock);
73         /* Make sure there isn't already a class with the same name */
74         list_for_each_entry(cl, &svc_xprt_class_list, xcl_list) {
75                 if (strcmp(xcl->xcl_name, cl->xcl_name) == 0)
76                         goto out;
77         }
78         list_add_tail(&xcl->xcl_list, &svc_xprt_class_list);
79         res = 0;
80 out:
81         spin_unlock(&svc_xprt_class_lock);
82         return res;
83 }
84 EXPORT_SYMBOL_GPL(svc_reg_xprt_class);
85
86 void svc_unreg_xprt_class(struct svc_xprt_class *xcl)
87 {
88         dprintk("svc: Removing svc transport class '%s'\n", xcl->xcl_name);
89         spin_lock(&svc_xprt_class_lock);
90         list_del_init(&xcl->xcl_list);
91         spin_unlock(&svc_xprt_class_lock);
92 }
93 EXPORT_SYMBOL_GPL(svc_unreg_xprt_class);
94
95 /*
96  * Format the transport list for printing
97  */
98 int svc_print_xprts(char *buf, int maxlen)
99 {
100         struct list_head *le;
101         char tmpstr[80];
102         int len = 0;
103         buf[0] = '\0';
104
105         spin_lock(&svc_xprt_class_lock);
106         list_for_each(le, &svc_xprt_class_list) {
107                 int slen;
108                 struct svc_xprt_class *xcl =
109                         list_entry(le, struct svc_xprt_class, xcl_list);
110
111                 sprintf(tmpstr, "%s %d\n", xcl->xcl_name, xcl->xcl_max_payload);
112                 slen = strlen(tmpstr);
113                 if (len + slen > maxlen)
114                         break;
115                 len += slen;
116                 strcat(buf, tmpstr);
117         }
118         spin_unlock(&svc_xprt_class_lock);
119
120         return len;
121 }
122
123 static void svc_xprt_free(struct kref *kref)
124 {
125         struct svc_xprt *xprt =
126                 container_of(kref, struct svc_xprt, xpt_ref);
127         struct module *owner = xprt->xpt_class->xcl_owner;
128         if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)
129             && xprt->xpt_auth_cache != NULL)
130                 svcauth_unix_info_release(xprt->xpt_auth_cache);
131         xprt->xpt_ops->xpo_free(xprt);
132         module_put(owner);
133 }
134
135 void svc_xprt_put(struct svc_xprt *xprt)
136 {
137         kref_put(&xprt->xpt_ref, svc_xprt_free);
138 }
139 EXPORT_SYMBOL_GPL(svc_xprt_put);
140
141 /*
142  * Called by transport drivers to initialize the transport independent
143  * portion of the transport instance.
144  */
145 void svc_xprt_init(struct svc_xprt_class *xcl, struct svc_xprt *xprt,
146                    struct svc_serv *serv)
147 {
148         memset(xprt, 0, sizeof(*xprt));
149         xprt->xpt_class = xcl;
150         xprt->xpt_ops = xcl->xcl_ops;
151         kref_init(&xprt->xpt_ref);
152         xprt->xpt_server = serv;
153         INIT_LIST_HEAD(&xprt->xpt_list);
154         INIT_LIST_HEAD(&xprt->xpt_ready);
155         INIT_LIST_HEAD(&xprt->xpt_deferred);
156         mutex_init(&xprt->xpt_mutex);
157         spin_lock_init(&xprt->xpt_lock);
158         set_bit(XPT_BUSY, &xprt->xpt_flags);
159 }
160 EXPORT_SYMBOL_GPL(svc_xprt_init);
161
162 static struct svc_xprt *__svc_xpo_create(struct svc_xprt_class *xcl,
163                                          struct svc_serv *serv,
164                                          const int family,
165                                          const unsigned short port,
166                                          int flags)
167 {
168         struct sockaddr_in sin = {
169                 .sin_family             = AF_INET,
170                 .sin_addr.s_addr        = htonl(INADDR_ANY),
171                 .sin_port               = htons(port),
172         };
173         struct sockaddr_in6 sin6 = {
174                 .sin6_family            = AF_INET6,
175                 .sin6_addr              = IN6ADDR_ANY_INIT,
176                 .sin6_port              = htons(port),
177         };
178         struct sockaddr *sap;
179         size_t len;
180
181         switch (family) {
182         case PF_INET:
183                 sap = (struct sockaddr *)&sin;
184                 len = sizeof(sin);
185                 break;
186         case PF_INET6:
187                 sap = (struct sockaddr *)&sin6;
188                 len = sizeof(sin6);
189                 break;
190         default:
191                 return ERR_PTR(-EAFNOSUPPORT);
192         }
193
194         return xcl->xcl_ops->xpo_create(serv, sap, len, flags);
195 }
196
197 int svc_create_xprt(struct svc_serv *serv, const char *xprt_name,
198                     const int family, const unsigned short port,
199                     int flags)
200 {
201         struct svc_xprt_class *xcl;
202
203         dprintk("svc: creating transport %s[%d]\n", xprt_name, port);
204         spin_lock(&svc_xprt_class_lock);
205         list_for_each_entry(xcl, &svc_xprt_class_list, xcl_list) {
206                 struct svc_xprt *newxprt;
207
208                 if (strcmp(xprt_name, xcl->xcl_name))
209                         continue;
210
211                 if (!try_module_get(xcl->xcl_owner))
212                         goto err;
213
214                 spin_unlock(&svc_xprt_class_lock);
215                 newxprt = __svc_xpo_create(xcl, serv, family, port, flags);
216                 if (IS_ERR(newxprt)) {
217                         module_put(xcl->xcl_owner);
218                         return PTR_ERR(newxprt);
219                 }
220
221                 clear_bit(XPT_TEMP, &newxprt->xpt_flags);
222                 spin_lock_bh(&serv->sv_lock);
223                 list_add(&newxprt->xpt_list, &serv->sv_permsocks);
224                 spin_unlock_bh(&serv->sv_lock);
225                 clear_bit(XPT_BUSY, &newxprt->xpt_flags);
226                 return svc_xprt_local_port(newxprt);
227         }
228  err:
229         spin_unlock(&svc_xprt_class_lock);
230         dprintk("svc: transport %s not found\n", xprt_name);
231         return -ENOENT;
232 }
233 EXPORT_SYMBOL_GPL(svc_create_xprt);
234
235 /*
236  * Copy the local and remote xprt addresses to the rqstp structure
237  */
238 void svc_xprt_copy_addrs(struct svc_rqst *rqstp, struct svc_xprt *xprt)
239 {
240         struct sockaddr *sin;
241
242         memcpy(&rqstp->rq_addr, &xprt->xpt_remote, xprt->xpt_remotelen);
243         rqstp->rq_addrlen = xprt->xpt_remotelen;
244
245         /*
246          * Destination address in request is needed for binding the
247          * source address in RPC replies/callbacks later.
248          */
249         sin = (struct sockaddr *)&xprt->xpt_local;
250         switch (sin->sa_family) {
251         case AF_INET:
252                 rqstp->rq_daddr.addr = ((struct sockaddr_in *)sin)->sin_addr;
253                 break;
254         case AF_INET6:
255                 rqstp->rq_daddr.addr6 = ((struct sockaddr_in6 *)sin)->sin6_addr;
256                 break;
257         }
258 }
259 EXPORT_SYMBOL_GPL(svc_xprt_copy_addrs);
260
261 /**
262  * svc_print_addr - Format rq_addr field for printing
263  * @rqstp: svc_rqst struct containing address to print
264  * @buf: target buffer for formatted address
265  * @len: length of target buffer
266  *
267  */
268 char *svc_print_addr(struct svc_rqst *rqstp, char *buf, size_t len)
269 {
270         return __svc_print_addr(svc_addr(rqstp), buf, len);
271 }
272 EXPORT_SYMBOL_GPL(svc_print_addr);
273
274 /*
275  * Queue up an idle server thread.  Must have pool->sp_lock held.
276  * Note: this is really a stack rather than a queue, so that we only
277  * use as many different threads as we need, and the rest don't pollute
278  * the cache.
279  */
280 static void svc_thread_enqueue(struct svc_pool *pool, struct svc_rqst *rqstp)
281 {
282         list_add(&rqstp->rq_list, &pool->sp_threads);
283 }
284
285 /*
286  * Dequeue an nfsd thread.  Must have pool->sp_lock held.
287  */
288 static void svc_thread_dequeue(struct svc_pool *pool, struct svc_rqst *rqstp)
289 {
290         list_del(&rqstp->rq_list);
291 }
292
293 /*
294  * Queue up a transport with data pending. If there are idle nfsd
295  * processes, wake 'em up.
296  *
297  */
298 void svc_xprt_enqueue(struct svc_xprt *xprt)
299 {
300         struct svc_serv *serv = xprt->xpt_server;
301         struct svc_pool *pool;
302         struct svc_rqst *rqstp;
303         int cpu;
304
305         if (!(xprt->xpt_flags &
306               ((1<<XPT_CONN)|(1<<XPT_DATA)|(1<<XPT_CLOSE)|(1<<XPT_DEFERRED))))
307                 return;
308
309         cpu = get_cpu();
310         pool = svc_pool_for_cpu(xprt->xpt_server, cpu);
311         put_cpu();
312
313         spin_lock_bh(&pool->sp_lock);
314
315         if (!list_empty(&pool->sp_threads) &&
316             !list_empty(&pool->sp_sockets))
317                 printk(KERN_ERR
318                        "svc_xprt_enqueue: "
319                        "threads and transports both waiting??\n");
320
321         if (test_bit(XPT_DEAD, &xprt->xpt_flags)) {
322                 /* Don't enqueue dead transports */
323                 dprintk("svc: transport %p is dead, not enqueued\n", xprt);
324                 goto out_unlock;
325         }
326
327         /* Mark transport as busy. It will remain in this state until
328          * the provider calls svc_xprt_received. We update XPT_BUSY
329          * atomically because it also guards against trying to enqueue
330          * the transport twice.
331          */
332         if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags)) {
333                 /* Don't enqueue transport while already enqueued */
334                 dprintk("svc: transport %p busy, not enqueued\n", xprt);
335                 goto out_unlock;
336         }
337         BUG_ON(xprt->xpt_pool != NULL);
338         xprt->xpt_pool = pool;
339
340         /* Handle pending connection */
341         if (test_bit(XPT_CONN, &xprt->xpt_flags))
342                 goto process;
343
344         /* Handle close in-progress */
345         if (test_bit(XPT_CLOSE, &xprt->xpt_flags))
346                 goto process;
347
348         /* Check if we have space to reply to a request */
349         if (!xprt->xpt_ops->xpo_has_wspace(xprt)) {
350                 /* Don't enqueue while not enough space for reply */
351                 dprintk("svc: no write space, transport %p  not enqueued\n",
352                         xprt);
353                 xprt->xpt_pool = NULL;
354                 clear_bit(XPT_BUSY, &xprt->xpt_flags);
355                 goto out_unlock;
356         }
357
358  process:
359         if (!list_empty(&pool->sp_threads)) {
360                 rqstp = list_entry(pool->sp_threads.next,
361                                    struct svc_rqst,
362                                    rq_list);
363                 dprintk("svc: transport %p served by daemon %p\n",
364                         xprt, rqstp);
365                 svc_thread_dequeue(pool, rqstp);
366                 if (rqstp->rq_xprt)
367                         printk(KERN_ERR
368                                 "svc_xprt_enqueue: server %p, rq_xprt=%p!\n",
369                                 rqstp, rqstp->rq_xprt);
370                 rqstp->rq_xprt = xprt;
371                 svc_xprt_get(xprt);
372                 rqstp->rq_reserved = serv->sv_max_mesg;
373                 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
374                 BUG_ON(xprt->xpt_pool != pool);
375                 wake_up(&rqstp->rq_wait);
376         } else {
377                 dprintk("svc: transport %p put into queue\n", xprt);
378                 list_add_tail(&xprt->xpt_ready, &pool->sp_sockets);
379                 BUG_ON(xprt->xpt_pool != pool);
380         }
381
382 out_unlock:
383         spin_unlock_bh(&pool->sp_lock);
384 }
385 EXPORT_SYMBOL_GPL(svc_xprt_enqueue);
386
387 /*
388  * Dequeue the first transport.  Must be called with the pool->sp_lock held.
389  */
390 static struct svc_xprt *svc_xprt_dequeue(struct svc_pool *pool)
391 {
392         struct svc_xprt *xprt;
393
394         if (list_empty(&pool->sp_sockets))
395                 return NULL;
396
397         xprt = list_entry(pool->sp_sockets.next,
398                           struct svc_xprt, xpt_ready);
399         list_del_init(&xprt->xpt_ready);
400
401         dprintk("svc: transport %p dequeued, inuse=%d\n",
402                 xprt, atomic_read(&xprt->xpt_ref.refcount));
403
404         return xprt;
405 }
406
407 /*
408  * svc_xprt_received conditionally queues the transport for processing
409  * by another thread. The caller must hold the XPT_BUSY bit and must
410  * not thereafter touch transport data.
411  *
412  * Note: XPT_DATA only gets cleared when a read-attempt finds no (or
413  * insufficient) data.
414  */
415 void svc_xprt_received(struct svc_xprt *xprt)
416 {
417         BUG_ON(!test_bit(XPT_BUSY, &xprt->xpt_flags));
418         xprt->xpt_pool = NULL;
419         clear_bit(XPT_BUSY, &xprt->xpt_flags);
420         svc_xprt_enqueue(xprt);
421 }
422 EXPORT_SYMBOL_GPL(svc_xprt_received);
423
424 /**
425  * svc_reserve - change the space reserved for the reply to a request.
426  * @rqstp:  The request in question
427  * @space: new max space to reserve
428  *
429  * Each request reserves some space on the output queue of the transport
430  * to make sure the reply fits.  This function reduces that reserved
431  * space to be the amount of space used already, plus @space.
432  *
433  */
434 void svc_reserve(struct svc_rqst *rqstp, int space)
435 {
436         space += rqstp->rq_res.head[0].iov_len;
437
438         if (space < rqstp->rq_reserved) {
439                 struct svc_xprt *xprt = rqstp->rq_xprt;
440                 atomic_sub((rqstp->rq_reserved - space), &xprt->xpt_reserved);
441                 rqstp->rq_reserved = space;
442
443                 svc_xprt_enqueue(xprt);
444         }
445 }
446 EXPORT_SYMBOL_GPL(svc_reserve);
447
448 static void svc_xprt_release(struct svc_rqst *rqstp)
449 {
450         struct svc_xprt *xprt = rqstp->rq_xprt;
451
452         rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
453
454         kfree(rqstp->rq_deferred);
455         rqstp->rq_deferred = NULL;
456
457         svc_free_res_pages(rqstp);
458         rqstp->rq_res.page_len = 0;
459         rqstp->rq_res.page_base = 0;
460
461         /* Reset response buffer and release
462          * the reservation.
463          * But first, check that enough space was reserved
464          * for the reply, otherwise we have a bug!
465          */
466         if ((rqstp->rq_res.len) >  rqstp->rq_reserved)
467                 printk(KERN_ERR "RPC request reserved %d but used %d\n",
468                        rqstp->rq_reserved,
469                        rqstp->rq_res.len);
470
471         rqstp->rq_res.head[0].iov_len = 0;
472         svc_reserve(rqstp, 0);
473         rqstp->rq_xprt = NULL;
474
475         svc_xprt_put(xprt);
476 }
477
478 /*
479  * External function to wake up a server waiting for data
480  * This really only makes sense for services like lockd
481  * which have exactly one thread anyway.
482  */
483 void svc_wake_up(struct svc_serv *serv)
484 {
485         struct svc_rqst *rqstp;
486         unsigned int i;
487         struct svc_pool *pool;
488
489         for (i = 0; i < serv->sv_nrpools; i++) {
490                 pool = &serv->sv_pools[i];
491
492                 spin_lock_bh(&pool->sp_lock);
493                 if (!list_empty(&pool->sp_threads)) {
494                         rqstp = list_entry(pool->sp_threads.next,
495                                            struct svc_rqst,
496                                            rq_list);
497                         dprintk("svc: daemon %p woken up.\n", rqstp);
498                         /*
499                         svc_thread_dequeue(pool, rqstp);
500                         rqstp->rq_xprt = NULL;
501                          */
502                         wake_up(&rqstp->rq_wait);
503                 }
504                 spin_unlock_bh(&pool->sp_lock);
505         }
506 }
507 EXPORT_SYMBOL_GPL(svc_wake_up);
508
509 int svc_port_is_privileged(struct sockaddr *sin)
510 {
511         switch (sin->sa_family) {
512         case AF_INET:
513                 return ntohs(((struct sockaddr_in *)sin)->sin_port)
514                         < PROT_SOCK;
515         case AF_INET6:
516                 return ntohs(((struct sockaddr_in6 *)sin)->sin6_port)
517                         < PROT_SOCK;
518         default:
519                 return 0;
520         }
521 }
522
523 /*
524  * Make sure that we don't have too many active connections. If we have,
525  * something must be dropped. It's not clear what will happen if we allow
526  * "too many" connections, but when dealing with network-facing software,
527  * we have to code defensively. Here we do that by imposing hard limits.
528  *
529  * There's no point in trying to do random drop here for DoS
530  * prevention. The NFS clients does 1 reconnect in 15 seconds. An
531  * attacker can easily beat that.
532  *
533  * The only somewhat efficient mechanism would be if drop old
534  * connections from the same IP first. But right now we don't even
535  * record the client IP in svc_sock.
536  *
537  * single-threaded services that expect a lot of clients will probably
538  * need to set sv_maxconn to override the default value which is based
539  * on the number of threads
540  */
541 static void svc_check_conn_limits(struct svc_serv *serv)
542 {
543         unsigned int limit = serv->sv_maxconn ? serv->sv_maxconn :
544                                 (serv->sv_nrthreads+3) * 20;
545
546         if (serv->sv_tmpcnt > limit) {
547                 struct svc_xprt *xprt = NULL;
548                 spin_lock_bh(&serv->sv_lock);
549                 if (!list_empty(&serv->sv_tempsocks)) {
550                         if (net_ratelimit()) {
551                                 /* Try to help the admin */
552                                 printk(KERN_NOTICE "%s: too many open  "
553                                        "connections, consider increasing %s\n",
554                                        serv->sv_name, serv->sv_maxconn ?
555                                        "the max number of connections." :
556                                        "the number of threads.");
557                         }
558                         /*
559                          * Always select the oldest connection. It's not fair,
560                          * but so is life
561                          */
562                         xprt = list_entry(serv->sv_tempsocks.prev,
563                                           struct svc_xprt,
564                                           xpt_list);
565                         set_bit(XPT_CLOSE, &xprt->xpt_flags);
566                         svc_xprt_get(xprt);
567                 }
568                 spin_unlock_bh(&serv->sv_lock);
569
570                 if (xprt) {
571                         svc_xprt_enqueue(xprt);
572                         svc_xprt_put(xprt);
573                 }
574         }
575 }
576
577 /*
578  * Receive the next request on any transport.  This code is carefully
579  * organised not to touch any cachelines in the shared svc_serv
580  * structure, only cachelines in the local svc_pool.
581  */
582 int svc_recv(struct svc_rqst *rqstp, long timeout)
583 {
584         struct svc_xprt         *xprt = NULL;
585         struct svc_serv         *serv = rqstp->rq_server;
586         struct svc_pool         *pool = rqstp->rq_pool;
587         int                     len, i;
588         int                     pages;
589         struct xdr_buf          *arg;
590         DECLARE_WAITQUEUE(wait, current);
591
592         dprintk("svc: server %p waiting for data (to = %ld)\n",
593                 rqstp, timeout);
594
595         if (rqstp->rq_xprt)
596                 printk(KERN_ERR
597                         "svc_recv: service %p, transport not NULL!\n",
598                          rqstp);
599         if (waitqueue_active(&rqstp->rq_wait))
600                 printk(KERN_ERR
601                         "svc_recv: service %p, wait queue active!\n",
602                          rqstp);
603
604         /* now allocate needed pages.  If we get a failure, sleep briefly */
605         pages = (serv->sv_max_mesg + PAGE_SIZE) / PAGE_SIZE;
606         for (i = 0; i < pages ; i++)
607                 while (rqstp->rq_pages[i] == NULL) {
608                         struct page *p = alloc_page(GFP_KERNEL);
609                         if (!p) {
610                                 set_current_state(TASK_INTERRUPTIBLE);
611                                 if (signalled() || kthread_should_stop()) {
612                                         set_current_state(TASK_RUNNING);
613                                         return -EINTR;
614                                 }
615                                 schedule_timeout(msecs_to_jiffies(500));
616                         }
617                         rqstp->rq_pages[i] = p;
618                 }
619         rqstp->rq_pages[i++] = NULL; /* this might be seen in nfs_read_actor */
620         BUG_ON(pages >= RPCSVC_MAXPAGES);
621
622         /* Make arg->head point to first page and arg->pages point to rest */
623         arg = &rqstp->rq_arg;
624         arg->head[0].iov_base = page_address(rqstp->rq_pages[0]);
625         arg->head[0].iov_len = PAGE_SIZE;
626         arg->pages = rqstp->rq_pages + 1;
627         arg->page_base = 0;
628         /* save at least one page for response */
629         arg->page_len = (pages-2)*PAGE_SIZE;
630         arg->len = (pages-1)*PAGE_SIZE;
631         arg->tail[0].iov_len = 0;
632
633         try_to_freeze();
634         cond_resched();
635         if (signalled() || kthread_should_stop())
636                 return -EINTR;
637
638         spin_lock_bh(&pool->sp_lock);
639         xprt = svc_xprt_dequeue(pool);
640         if (xprt) {
641                 rqstp->rq_xprt = xprt;
642                 svc_xprt_get(xprt);
643                 rqstp->rq_reserved = serv->sv_max_mesg;
644                 atomic_add(rqstp->rq_reserved, &xprt->xpt_reserved);
645         } else {
646                 /* No data pending. Go to sleep */
647                 svc_thread_enqueue(pool, rqstp);
648
649                 /*
650                  * We have to be able to interrupt this wait
651                  * to bring down the daemons ...
652                  */
653                 set_current_state(TASK_INTERRUPTIBLE);
654
655                 /*
656                  * checking kthread_should_stop() here allows us to avoid
657                  * locking and signalling when stopping kthreads that call
658                  * svc_recv. If the thread has already been woken up, then
659                  * we can exit here without sleeping. If not, then it
660                  * it'll be woken up quickly during the schedule_timeout
661                  */
662                 if (kthread_should_stop()) {
663                         set_current_state(TASK_RUNNING);
664                         spin_unlock_bh(&pool->sp_lock);
665                         return -EINTR;
666                 }
667
668                 add_wait_queue(&rqstp->rq_wait, &wait);
669                 spin_unlock_bh(&pool->sp_lock);
670
671                 schedule_timeout(timeout);
672
673                 try_to_freeze();
674
675                 spin_lock_bh(&pool->sp_lock);
676                 remove_wait_queue(&rqstp->rq_wait, &wait);
677
678                 xprt = rqstp->rq_xprt;
679                 if (!xprt) {
680                         svc_thread_dequeue(pool, rqstp);
681                         spin_unlock_bh(&pool->sp_lock);
682                         dprintk("svc: server %p, no data yet\n", rqstp);
683                         if (signalled() || kthread_should_stop())
684                                 return -EINTR;
685                         else
686                                 return -EAGAIN;
687                 }
688         }
689         spin_unlock_bh(&pool->sp_lock);
690
691         len = 0;
692         if (test_bit(XPT_CLOSE, &xprt->xpt_flags)) {
693                 dprintk("svc_recv: found XPT_CLOSE\n");
694                 svc_delete_xprt(xprt);
695         } else if (test_bit(XPT_LISTENER, &xprt->xpt_flags)) {
696                 struct svc_xprt *newxpt;
697                 newxpt = xprt->xpt_ops->xpo_accept(xprt);
698                 if (newxpt) {
699                         /*
700                          * We know this module_get will succeed because the
701                          * listener holds a reference too
702                          */
703                         __module_get(newxpt->xpt_class->xcl_owner);
704                         svc_check_conn_limits(xprt->xpt_server);
705                         spin_lock_bh(&serv->sv_lock);
706                         set_bit(XPT_TEMP, &newxpt->xpt_flags);
707                         list_add(&newxpt->xpt_list, &serv->sv_tempsocks);
708                         serv->sv_tmpcnt++;
709                         if (serv->sv_temptimer.function == NULL) {
710                                 /* setup timer to age temp transports */
711                                 setup_timer(&serv->sv_temptimer,
712                                             svc_age_temp_xprts,
713                                             (unsigned long)serv);
714                                 mod_timer(&serv->sv_temptimer,
715                                           jiffies + svc_conn_age_period * HZ);
716                         }
717                         spin_unlock_bh(&serv->sv_lock);
718                         svc_xprt_received(newxpt);
719                 }
720                 svc_xprt_received(xprt);
721         } else {
722                 dprintk("svc: server %p, pool %u, transport %p, inuse=%d\n",
723                         rqstp, pool->sp_id, xprt,
724                         atomic_read(&xprt->xpt_ref.refcount));
725                 rqstp->rq_deferred = svc_deferred_dequeue(xprt);
726                 if (rqstp->rq_deferred) {
727                         svc_xprt_received(xprt);
728                         len = svc_deferred_recv(rqstp);
729                 } else
730                         len = xprt->xpt_ops->xpo_recvfrom(rqstp);
731                 dprintk("svc: got len=%d\n", len);
732         }
733
734         /* No data, incomplete (TCP) read, or accept() */
735         if (len == 0 || len == -EAGAIN) {
736                 rqstp->rq_res.len = 0;
737                 svc_xprt_release(rqstp);
738                 return -EAGAIN;
739         }
740         clear_bit(XPT_OLD, &xprt->xpt_flags);
741
742         rqstp->rq_secure = svc_port_is_privileged(svc_addr(rqstp));
743         rqstp->rq_chandle.defer = svc_defer;
744
745         if (serv->sv_stats)
746                 serv->sv_stats->netcnt++;
747         return len;
748 }
749 EXPORT_SYMBOL_GPL(svc_recv);
750
751 /*
752  * Drop request
753  */
754 void svc_drop(struct svc_rqst *rqstp)
755 {
756         dprintk("svc: xprt %p dropped request\n", rqstp->rq_xprt);
757         svc_xprt_release(rqstp);
758 }
759 EXPORT_SYMBOL_GPL(svc_drop);
760
761 /*
762  * Return reply to client.
763  */
764 int svc_send(struct svc_rqst *rqstp)
765 {
766         struct svc_xprt *xprt;
767         int             len;
768         struct xdr_buf  *xb;
769
770         xprt = rqstp->rq_xprt;
771         if (!xprt)
772                 return -EFAULT;
773
774         /* release the receive skb before sending the reply */
775         rqstp->rq_xprt->xpt_ops->xpo_release_rqst(rqstp);
776
777         /* calculate over-all length */
778         xb = &rqstp->rq_res;
779         xb->len = xb->head[0].iov_len +
780                 xb->page_len +
781                 xb->tail[0].iov_len;
782
783         /* Grab mutex to serialize outgoing data. */
784         mutex_lock(&xprt->xpt_mutex);
785         if (test_bit(XPT_DEAD, &xprt->xpt_flags))
786                 len = -ENOTCONN;
787         else
788                 len = xprt->xpt_ops->xpo_sendto(rqstp);
789         mutex_unlock(&xprt->xpt_mutex);
790         svc_xprt_release(rqstp);
791
792         if (len == -ECONNREFUSED || len == -ENOTCONN || len == -EAGAIN)
793                 return 0;
794         return len;
795 }
796
797 /*
798  * Timer function to close old temporary transports, using
799  * a mark-and-sweep algorithm.
800  */
801 static void svc_age_temp_xprts(unsigned long closure)
802 {
803         struct svc_serv *serv = (struct svc_serv *)closure;
804         struct svc_xprt *xprt;
805         struct list_head *le, *next;
806         LIST_HEAD(to_be_aged);
807
808         dprintk("svc_age_temp_xprts\n");
809
810         if (!spin_trylock_bh(&serv->sv_lock)) {
811                 /* busy, try again 1 sec later */
812                 dprintk("svc_age_temp_xprts: busy\n");
813                 mod_timer(&serv->sv_temptimer, jiffies + HZ);
814                 return;
815         }
816
817         list_for_each_safe(le, next, &serv->sv_tempsocks) {
818                 xprt = list_entry(le, struct svc_xprt, xpt_list);
819
820                 /* First time through, just mark it OLD. Second time
821                  * through, close it. */
822                 if (!test_and_set_bit(XPT_OLD, &xprt->xpt_flags))
823                         continue;
824                 if (atomic_read(&xprt->xpt_ref.refcount) > 1
825                     || test_bit(XPT_BUSY, &xprt->xpt_flags))
826                         continue;
827                 svc_xprt_get(xprt);
828                 list_move(le, &to_be_aged);
829                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
830                 set_bit(XPT_DETACHED, &xprt->xpt_flags);
831         }
832         spin_unlock_bh(&serv->sv_lock);
833
834         while (!list_empty(&to_be_aged)) {
835                 le = to_be_aged.next;
836                 /* fiddling the xpt_list node is safe 'cos we're XPT_DETACHED */
837                 list_del_init(le);
838                 xprt = list_entry(le, struct svc_xprt, xpt_list);
839
840                 dprintk("queuing xprt %p for closing\n", xprt);
841
842                 /* a thread will dequeue and close it soon */
843                 svc_xprt_enqueue(xprt);
844                 svc_xprt_put(xprt);
845         }
846
847         mod_timer(&serv->sv_temptimer, jiffies + svc_conn_age_period * HZ);
848 }
849
850 /*
851  * Remove a dead transport
852  */
853 void svc_delete_xprt(struct svc_xprt *xprt)
854 {
855         struct svc_serv *serv = xprt->xpt_server;
856         struct svc_deferred_req *dr;
857
858         /* Only do this once */
859         if (test_and_set_bit(XPT_DEAD, &xprt->xpt_flags))
860                 return;
861
862         dprintk("svc: svc_delete_xprt(%p)\n", xprt);
863         xprt->xpt_ops->xpo_detach(xprt);
864
865         spin_lock_bh(&serv->sv_lock);
866         if (!test_and_set_bit(XPT_DETACHED, &xprt->xpt_flags))
867                 list_del_init(&xprt->xpt_list);
868         /*
869          * We used to delete the transport from whichever list
870          * it's sk_xprt.xpt_ready node was on, but we don't actually
871          * need to.  This is because the only time we're called
872          * while still attached to a queue, the queue itself
873          * is about to be destroyed (in svc_destroy).
874          */
875         if (test_bit(XPT_TEMP, &xprt->xpt_flags))
876                 serv->sv_tmpcnt--;
877
878         for (dr = svc_deferred_dequeue(xprt); dr;
879              dr = svc_deferred_dequeue(xprt)) {
880                 svc_xprt_put(xprt);
881                 kfree(dr);
882         }
883
884         svc_xprt_put(xprt);
885         spin_unlock_bh(&serv->sv_lock);
886 }
887
888 void svc_close_xprt(struct svc_xprt *xprt)
889 {
890         set_bit(XPT_CLOSE, &xprt->xpt_flags);
891         if (test_and_set_bit(XPT_BUSY, &xprt->xpt_flags))
892                 /* someone else will have to effect the close */
893                 return;
894
895         svc_xprt_get(xprt);
896         svc_delete_xprt(xprt);
897         clear_bit(XPT_BUSY, &xprt->xpt_flags);
898         svc_xprt_put(xprt);
899 }
900 EXPORT_SYMBOL_GPL(svc_close_xprt);
901
902 void svc_close_all(struct list_head *xprt_list)
903 {
904         struct svc_xprt *xprt;
905         struct svc_xprt *tmp;
906
907         list_for_each_entry_safe(xprt, tmp, xprt_list, xpt_list) {
908                 set_bit(XPT_CLOSE, &xprt->xpt_flags);
909                 if (test_bit(XPT_BUSY, &xprt->xpt_flags)) {
910                         /* Waiting to be processed, but no threads left,
911                          * So just remove it from the waiting list
912                          */
913                         list_del_init(&xprt->xpt_ready);
914                         clear_bit(XPT_BUSY, &xprt->xpt_flags);
915                 }
916                 svc_close_xprt(xprt);
917         }
918 }
919
920 /*
921  * Handle defer and revisit of requests
922  */
923
924 static void svc_revisit(struct cache_deferred_req *dreq, int too_many)
925 {
926         struct svc_deferred_req *dr =
927                 container_of(dreq, struct svc_deferred_req, handle);
928         struct svc_xprt *xprt = dr->xprt;
929
930         spin_lock(&xprt->xpt_lock);
931         set_bit(XPT_DEFERRED, &xprt->xpt_flags);
932         if (too_many || test_bit(XPT_DEAD, &xprt->xpt_flags)) {
933                 spin_unlock(&xprt->xpt_lock);
934                 dprintk("revisit canceled\n");
935                 svc_xprt_put(xprt);
936                 kfree(dr);
937                 return;
938         }
939         dprintk("revisit queued\n");
940         dr->xprt = NULL;
941         list_add(&dr->handle.recent, &xprt->xpt_deferred);
942         spin_unlock(&xprt->xpt_lock);
943         svc_xprt_enqueue(xprt);
944         svc_xprt_put(xprt);
945 }
946
947 /*
948  * Save the request off for later processing. The request buffer looks
949  * like this:
950  *
951  * <xprt-header><rpc-header><rpc-pagelist><rpc-tail>
952  *
953  * This code can only handle requests that consist of an xprt-header
954  * and rpc-header.
955  */
956 static struct cache_deferred_req *svc_defer(struct cache_req *req)
957 {
958         struct svc_rqst *rqstp = container_of(req, struct svc_rqst, rq_chandle);
959         struct svc_deferred_req *dr;
960
961         if (rqstp->rq_arg.page_len)
962                 return NULL; /* if more than a page, give up FIXME */
963         if (rqstp->rq_deferred) {
964                 dr = rqstp->rq_deferred;
965                 rqstp->rq_deferred = NULL;
966         } else {
967                 size_t skip;
968                 size_t size;
969                 /* FIXME maybe discard if size too large */
970                 size = sizeof(struct svc_deferred_req) + rqstp->rq_arg.len;
971                 dr = kmalloc(size, GFP_KERNEL);
972                 if (dr == NULL)
973                         return NULL;
974
975                 dr->handle.owner = rqstp->rq_server;
976                 dr->prot = rqstp->rq_prot;
977                 memcpy(&dr->addr, &rqstp->rq_addr, rqstp->rq_addrlen);
978                 dr->addrlen = rqstp->rq_addrlen;
979                 dr->daddr = rqstp->rq_daddr;
980                 dr->argslen = rqstp->rq_arg.len >> 2;
981                 dr->xprt_hlen = rqstp->rq_xprt_hlen;
982
983                 /* back up head to the start of the buffer and copy */
984                 skip = rqstp->rq_arg.len - rqstp->rq_arg.head[0].iov_len;
985                 memcpy(dr->args, rqstp->rq_arg.head[0].iov_base - skip,
986                        dr->argslen << 2);
987         }
988         svc_xprt_get(rqstp->rq_xprt);
989         dr->xprt = rqstp->rq_xprt;
990
991         dr->handle.revisit = svc_revisit;
992         return &dr->handle;
993 }
994
995 /*
996  * recv data from a deferred request into an active one
997  */
998 static int svc_deferred_recv(struct svc_rqst *rqstp)
999 {
1000         struct svc_deferred_req *dr = rqstp->rq_deferred;
1001
1002         /* setup iov_base past transport header */
1003         rqstp->rq_arg.head[0].iov_base = dr->args + (dr->xprt_hlen>>2);
1004         /* The iov_len does not include the transport header bytes */
1005         rqstp->rq_arg.head[0].iov_len = (dr->argslen<<2) - dr->xprt_hlen;
1006         rqstp->rq_arg.page_len = 0;
1007         /* The rq_arg.len includes the transport header bytes */
1008         rqstp->rq_arg.len     = dr->argslen<<2;
1009         rqstp->rq_prot        = dr->prot;
1010         memcpy(&rqstp->rq_addr, &dr->addr, dr->addrlen);
1011         rqstp->rq_addrlen     = dr->addrlen;
1012         /* Save off transport header len in case we get deferred again */
1013         rqstp->rq_xprt_hlen   = dr->xprt_hlen;
1014         rqstp->rq_daddr       = dr->daddr;
1015         rqstp->rq_respages    = rqstp->rq_pages;
1016         return (dr->argslen<<2) - dr->xprt_hlen;
1017 }
1018
1019
1020 static struct svc_deferred_req *svc_deferred_dequeue(struct svc_xprt *xprt)
1021 {
1022         struct svc_deferred_req *dr = NULL;
1023
1024         if (!test_bit(XPT_DEFERRED, &xprt->xpt_flags))
1025                 return NULL;
1026         spin_lock(&xprt->xpt_lock);
1027         clear_bit(XPT_DEFERRED, &xprt->xpt_flags);
1028         if (!list_empty(&xprt->xpt_deferred)) {
1029                 dr = list_entry(xprt->xpt_deferred.next,
1030                                 struct svc_deferred_req,
1031                                 handle.recent);
1032                 list_del_init(&dr->handle.recent);
1033                 set_bit(XPT_DEFERRED, &xprt->xpt_flags);
1034         }
1035         spin_unlock(&xprt->xpt_lock);
1036         return dr;
1037 }
1038
1039 /**
1040  * svc_find_xprt - find an RPC transport instance
1041  * @serv: pointer to svc_serv to search
1042  * @xcl_name: C string containing transport's class name
1043  * @af: Address family of transport's local address
1044  * @port: transport's IP port number
1045  *
1046  * Return the transport instance pointer for the endpoint accepting
1047  * connections/peer traffic from the specified transport class,
1048  * address family and port.
1049  *
1050  * Specifying 0 for the address family or port is effectively a
1051  * wild-card, and will result in matching the first transport in the
1052  * service's list that has a matching class name.
1053  */
1054 struct svc_xprt *svc_find_xprt(struct svc_serv *serv, const char *xcl_name,
1055                                const sa_family_t af, const unsigned short port)
1056 {
1057         struct svc_xprt *xprt;
1058         struct svc_xprt *found = NULL;
1059
1060         /* Sanity check the args */
1061         if (serv == NULL || xcl_name == NULL)
1062                 return found;
1063
1064         spin_lock_bh(&serv->sv_lock);
1065         list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1066                 if (strcmp(xprt->xpt_class->xcl_name, xcl_name))
1067                         continue;
1068                 if (af != AF_UNSPEC && af != xprt->xpt_local.ss_family)
1069                         continue;
1070                 if (port != 0 && port != svc_xprt_local_port(xprt))
1071                         continue;
1072                 found = xprt;
1073                 svc_xprt_get(xprt);
1074                 break;
1075         }
1076         spin_unlock_bh(&serv->sv_lock);
1077         return found;
1078 }
1079 EXPORT_SYMBOL_GPL(svc_find_xprt);
1080
1081 /*
1082  * Format a buffer with a list of the active transports. A zero for
1083  * the buflen parameter disables target buffer overflow checking.
1084  */
1085 int svc_xprt_names(struct svc_serv *serv, char *buf, int buflen)
1086 {
1087         struct svc_xprt *xprt;
1088         char xprt_str[64];
1089         int totlen = 0;
1090         int len;
1091
1092         /* Sanity check args */
1093         if (!serv)
1094                 return 0;
1095
1096         spin_lock_bh(&serv->sv_lock);
1097         list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
1098                 len = snprintf(xprt_str, sizeof(xprt_str),
1099                                "%s %d\n", xprt->xpt_class->xcl_name,
1100                                svc_xprt_local_port(xprt));
1101                 /* If the string was truncated, replace with error string */
1102                 if (len >= sizeof(xprt_str))
1103                         strcpy(xprt_str, "name-too-long\n");
1104                 /* Don't overflow buffer */
1105                 len = strlen(xprt_str);
1106                 if (buflen && (len + totlen >= buflen))
1107                         break;
1108                 strcpy(buf+totlen, xprt_str);
1109                 totlen += len;
1110         }
1111         spin_unlock_bh(&serv->sv_lock);
1112         return totlen;
1113 }
1114 EXPORT_SYMBOL_GPL(svc_xprt_names);