ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / sunrpc / sched.c
1 /*
2  * linux/net/sunrpc/sched.c
3  *
4  * Scheduling for synchronous and asynchronous RPC requests.
5  *
6  * Copyright (C) 1996 Olaf Kirch, <okir@monad.swb.de>
7  * 
8  * TCP NFS related read + write fixes
9  * (C) 1999 Dave Airlie, University of Limerick, Ireland <airlied@linux.ie>
10  */
11
12 #include <linux/module.h>
13
14 #include <linux/sched.h>
15 #include <linux/interrupt.h>
16 #include <linux/slab.h>
17 #include <linux/mempool.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/spinlock.h>
21 #include <linux/suspend.h>
22
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/xprt.h>
25
26 #ifdef RPC_DEBUG
27 #define RPCDBG_FACILITY         RPCDBG_SCHED
28 static int                      rpc_task_id;
29 #endif
30
31 /*
32  * RPC slabs and memory pools
33  */
34 #define RPC_BUFFER_MAXSIZE      (2048)
35 #define RPC_BUFFER_POOLSIZE     (8)
36 #define RPC_TASK_POOLSIZE       (8)
37 static kmem_cache_t     *rpc_task_slabp;
38 static kmem_cache_t     *rpc_buffer_slabp;
39 static mempool_t        *rpc_task_mempool;
40 static mempool_t        *rpc_buffer_mempool;
41
42 static void                     __rpc_default_timer(struct rpc_task *task);
43 static void                     rpciod_killall(void);
44
45 /*
46  * When an asynchronous RPC task is activated within a bottom half
47  * handler, or while executing another RPC task, it is put on
48  * schedq, and rpciod is woken up.
49  */
50 static RPC_WAITQ(schedq, "schedq");
51
52 /*
53  * RPC tasks that create another task (e.g. for contacting the portmapper)
54  * will wait on this queue for their child's completion
55  */
56 static RPC_WAITQ(childq, "childq");
57
58 /*
59  * RPC tasks sit here while waiting for conditions to improve.
60  */
61 static RPC_WAITQ(delay_queue, "delayq");
62
63 /*
64  * All RPC tasks are linked into this list
65  */
66 static LIST_HEAD(all_tasks);
67
68 /*
69  * rpciod-related stuff
70  */
71 static DECLARE_WAIT_QUEUE_HEAD(rpciod_idle);
72 static DECLARE_COMPLETION(rpciod_killer);
73 static DECLARE_MUTEX(rpciod_sema);
74 static unsigned int             rpciod_users;
75 static pid_t                    rpciod_pid;
76 static int                      rpc_inhibit;
77
78 /*
79  * Spinlock for wait queues. Access to the latter also has to be
80  * interrupt-safe in order to allow timers to wake up sleeping tasks.
81  */
82 static spinlock_t rpc_queue_lock = SPIN_LOCK_UNLOCKED;
83 /*
84  * Spinlock for other critical sections of code.
85  */
86 static spinlock_t rpc_sched_lock = SPIN_LOCK_UNLOCKED;
87
88 /*
89  * Disable the timer for a given RPC task. Should be called with
90  * rpc_queue_lock and bh_disabled in order to avoid races within
91  * rpc_run_timer().
92  */
93 static inline void
94 __rpc_disable_timer(struct rpc_task *task)
95 {
96         dprintk("RPC: %4d disabling timer\n", task->tk_pid);
97         task->tk_timeout_fn = NULL;
98         task->tk_timeout = 0;
99 }
100
101 /*
102  * Run a timeout function.
103  * We use the callback in order to allow __rpc_wake_up_task()
104  * and friends to disable the timer synchronously on SMP systems
105  * without calling del_timer_sync(). The latter could cause a
106  * deadlock if called while we're holding spinlocks...
107  */
108 static void
109 rpc_run_timer(struct rpc_task *task)
110 {
111         void (*callback)(struct rpc_task *);
112
113         spin_lock_bh(&rpc_queue_lock);
114         callback = task->tk_timeout_fn;
115         task->tk_timeout_fn = NULL;
116         spin_unlock_bh(&rpc_queue_lock);
117         if (callback) {
118                 dprintk("RPC: %4d running timer\n", task->tk_pid);
119                 callback(task);
120         }
121 }
122
123 /*
124  * Set up a timer for the current task.
125  */
126 static inline void
127 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
128 {
129         if (!task->tk_timeout)
130                 return;
131
132         dprintk("RPC: %4d setting alarm for %lu ms\n",
133                         task->tk_pid, task->tk_timeout * 1000 / HZ);
134
135         if (timer)
136                 task->tk_timeout_fn = timer;
137         else
138                 task->tk_timeout_fn = __rpc_default_timer;
139         mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
140 }
141
142 /*
143  * Set up a timer for an already sleeping task.
144  */
145 void rpc_add_timer(struct rpc_task *task, rpc_action timer)
146 {
147         spin_lock_bh(&rpc_queue_lock);
148         if (!RPC_IS_RUNNING(task))
149                 __rpc_add_timer(task, timer);
150         spin_unlock_bh(&rpc_queue_lock);
151 }
152
153 /*
154  * Delete any timer for the current task. Because we use del_timer_sync(),
155  * this function should never be called while holding rpc_queue_lock.
156  */
157 static inline void
158 rpc_delete_timer(struct rpc_task *task)
159 {
160         if (del_timer_sync(&task->tk_timer))
161                 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
162 }
163
164 /*
165  * Add new request to a priority queue.
166  */
167 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
168 {
169         struct list_head *q;
170         struct rpc_task *t;
171
172         q = &queue->tasks[task->tk_priority];
173         if (unlikely(task->tk_priority > queue->maxpriority))
174                 q = &queue->tasks[queue->maxpriority];
175         list_for_each_entry(t, q, tk_list) {
176                 if (t->tk_cookie == task->tk_cookie) {
177                         list_add_tail(&task->tk_list, &t->tk_links);
178                         return;
179                 }
180         }
181         list_add_tail(&task->tk_list, q);
182 }
183
184 /*
185  * Add new request to wait queue.
186  *
187  * Swapper tasks always get inserted at the head of the queue.
188  * This should avoid many nasty memory deadlocks and hopefully
189  * improve overall performance.
190  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
191  */
192 static int __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
193 {
194         if (task->tk_rpcwait == queue)
195                 return 0;
196
197         if (task->tk_rpcwait) {
198                 printk(KERN_WARNING "RPC: doubly enqueued task!\n");
199                 return -EWOULDBLOCK;
200         }
201         if (RPC_IS_PRIORITY(queue))
202                 __rpc_add_wait_queue_priority(queue, task);
203         else if (RPC_IS_SWAPPER(task))
204                 list_add(&task->tk_list, &queue->tasks[0]);
205         else
206                 list_add_tail(&task->tk_list, &queue->tasks[0]);
207         task->tk_rpcwait = queue;
208
209         dprintk("RPC: %4d added to queue %p \"%s\"\n",
210                                 task->tk_pid, queue, rpc_qname(queue));
211
212         return 0;
213 }
214
215 int rpc_add_wait_queue(struct rpc_wait_queue *q, struct rpc_task *task)
216 {
217         int             result;
218
219         spin_lock_bh(&rpc_queue_lock);
220         result = __rpc_add_wait_queue(q, task);
221         spin_unlock_bh(&rpc_queue_lock);
222         return result;
223 }
224
225 /*
226  * Remove request from a priority queue.
227  */
228 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
229 {
230         struct rpc_task *t;
231
232         if (!list_empty(&task->tk_links)) {
233                 t = list_entry(task->tk_links.next, struct rpc_task, tk_list);
234                 list_move(&t->tk_list, &task->tk_list);
235                 list_splice_init(&task->tk_links, &t->tk_links);
236         }
237         list_del(&task->tk_list);
238 }
239
240 /*
241  * Remove request from queue.
242  * Note: must be called with spin lock held.
243  */
244 static void __rpc_remove_wait_queue(struct rpc_task *task)
245 {
246         struct rpc_wait_queue *queue = task->tk_rpcwait;
247
248         if (!queue)
249                 return;
250
251         if (RPC_IS_PRIORITY(queue))
252                 __rpc_remove_wait_queue_priority(task);
253         else
254                 list_del(&task->tk_list);
255         task->tk_rpcwait = NULL;
256
257         dprintk("RPC: %4d removed from queue %p \"%s\"\n",
258                                 task->tk_pid, queue, rpc_qname(queue));
259 }
260
261 void
262 rpc_remove_wait_queue(struct rpc_task *task)
263 {
264         if (!task->tk_rpcwait)
265                 return;
266         spin_lock_bh(&rpc_queue_lock);
267         __rpc_remove_wait_queue(task);
268         spin_unlock_bh(&rpc_queue_lock);
269 }
270
271 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
272 {
273         queue->priority = priority;
274         queue->count = 1 << (priority * 2);
275 }
276
277 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
278 {
279         queue->cookie = cookie;
280         queue->nr = RPC_BATCH_COUNT;
281 }
282
283 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
284 {
285         rpc_set_waitqueue_priority(queue, queue->maxpriority);
286         rpc_set_waitqueue_cookie(queue, 0);
287 }
288
289 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
290 {
291         int i;
292
293         for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
294                 INIT_LIST_HEAD(&queue->tasks[i]);
295         queue->maxpriority = maxprio;
296         rpc_reset_waitqueue_priority(queue);
297 #ifdef RPC_DEBUG
298         queue->name = qname;
299 #endif
300 }
301
302 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
303 {
304         __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
305 }
306
307 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
308 {
309         __rpc_init_priority_wait_queue(queue, qname, 0);
310 }
311 EXPORT_SYMBOL(rpc_init_wait_queue);
312
313 /*
314  * Make an RPC task runnable.
315  *
316  * Note: If the task is ASYNC, this must be called with 
317  * the spinlock held to protect the wait queue operation.
318  */
319 static inline void
320 rpc_make_runnable(struct rpc_task *task)
321 {
322         if (task->tk_timeout_fn) {
323                 printk(KERN_ERR "RPC: task w/ running timer in rpc_make_runnable!!\n");
324                 return;
325         }
326         rpc_set_running(task);
327         if (RPC_IS_ASYNC(task)) {
328                 if (RPC_IS_SLEEPING(task)) {
329                         int status;
330                         status = __rpc_add_wait_queue(&schedq, task);
331                         if (status < 0) {
332                                 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
333                                 task->tk_status = status;
334                                 return;
335                         }
336                         rpc_clear_sleeping(task);
337                         wake_up(&rpciod_idle);
338                 }
339         } else {
340                 rpc_clear_sleeping(task);
341                 wake_up(&task->tk_wait);
342         }
343 }
344
345 /*
346  * Place a newly initialized task on the schedq.
347  */
348 static inline void
349 rpc_schedule_run(struct rpc_task *task)
350 {
351         /* Don't run a child twice! */
352         if (RPC_IS_ACTIVATED(task))
353                 return;
354         task->tk_active = 1;
355         rpc_set_sleeping(task);
356         rpc_make_runnable(task);
357 }
358
359 /*
360  *      For other people who may need to wake the I/O daemon
361  *      but should (for now) know nothing about its innards
362  */
363 void rpciod_wake_up(void)
364 {
365         if(rpciod_pid==0)
366                 printk(KERN_ERR "rpciod: wot no daemon?\n");
367         wake_up(&rpciod_idle);
368 }
369
370 /*
371  * Prepare for sleeping on a wait queue.
372  * By always appending tasks to the list we ensure FIFO behavior.
373  * NB: An RPC task will only receive interrupt-driven events as long
374  * as it's on a wait queue.
375  */
376 static void
377 __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
378                         rpc_action action, rpc_action timer)
379 {
380         int status;
381
382         dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
383                                 rpc_qname(q), jiffies);
384
385         if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
386                 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
387                 return;
388         }
389
390         /* Mark the task as being activated if so needed */
391         if (!RPC_IS_ACTIVATED(task)) {
392                 task->tk_active = 1;
393                 rpc_set_sleeping(task);
394         }
395
396         status = __rpc_add_wait_queue(q, task);
397         if (status) {
398                 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
399                 task->tk_status = status;
400         } else {
401                 rpc_clear_running(task);
402                 if (task->tk_callback) {
403                         dprintk(KERN_ERR "RPC: %4d overwrites an active callback\n", task->tk_pid);
404                         BUG();
405                 }
406                 task->tk_callback = action;
407                 __rpc_add_timer(task, timer);
408         }
409 }
410
411 void
412 rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
413                                 rpc_action action, rpc_action timer)
414 {
415         /*
416          * Protect the queue operations.
417          */
418         spin_lock_bh(&rpc_queue_lock);
419         __rpc_sleep_on(q, task, action, timer);
420         spin_unlock_bh(&rpc_queue_lock);
421 }
422
423 /**
424  * __rpc_wake_up_task - wake up a single rpc_task
425  * @task: task to be woken up
426  *
427  * Caller must hold rpc_queue_lock
428  */
429 static void
430 __rpc_wake_up_task(struct rpc_task *task)
431 {
432         dprintk("RPC: %4d __rpc_wake_up_task (now %ld inh %d)\n",
433                                         task->tk_pid, jiffies, rpc_inhibit);
434
435 #ifdef RPC_DEBUG
436         if (task->tk_magic != 0xf00baa) {
437                 printk(KERN_ERR "RPC: attempt to wake up non-existing task!\n");
438                 rpc_debug = ~0;
439                 rpc_show_tasks();
440                 return;
441         }
442 #endif
443         /* Has the task been executed yet? If not, we cannot wake it up! */
444         if (!RPC_IS_ACTIVATED(task)) {
445                 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
446                 return;
447         }
448         if (RPC_IS_RUNNING(task))
449                 return;
450
451         __rpc_disable_timer(task);
452         if (task->tk_rpcwait != &schedq)
453                 __rpc_remove_wait_queue(task);
454
455         rpc_make_runnable(task);
456
457         dprintk("RPC:      __rpc_wake_up_task done\n");
458 }
459
460 /*
461  * Default timeout handler if none specified by user
462  */
463 static void
464 __rpc_default_timer(struct rpc_task *task)
465 {
466         dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
467         task->tk_status = -ETIMEDOUT;
468         rpc_wake_up_task(task);
469 }
470
471 /*
472  * Wake up the specified task
473  */
474 void
475 rpc_wake_up_task(struct rpc_task *task)
476 {
477         if (RPC_IS_RUNNING(task))
478                 return;
479         spin_lock_bh(&rpc_queue_lock);
480         __rpc_wake_up_task(task);
481         spin_unlock_bh(&rpc_queue_lock);
482 }
483
484 /*
485  * Wake up the next task on a priority queue.
486  */
487 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
488 {
489         struct list_head *q;
490         struct rpc_task *task;
491
492         /*
493          * Service a batch of tasks from a single cookie.
494          */
495         q = &queue->tasks[queue->priority];
496         if (!list_empty(q)) {
497                 task = list_entry(q->next, struct rpc_task, tk_list);
498                 if (queue->cookie == task->tk_cookie) {
499                         if (--queue->nr)
500                                 goto out;
501                         list_move_tail(&task->tk_list, q);
502                 }
503                 /*
504                  * Check if we need to switch queues.
505                  */
506                 if (--queue->count)
507                         goto new_cookie;
508         }
509
510         /*
511          * Service the next queue.
512          */
513         do {
514                 if (q == &queue->tasks[0])
515                         q = &queue->tasks[queue->maxpriority];
516                 else
517                         q = q - 1;
518                 if (!list_empty(q)) {
519                         task = list_entry(q->next, struct rpc_task, tk_list);
520                         goto new_queue;
521                 }
522         } while (q != &queue->tasks[queue->priority]);
523
524         rpc_reset_waitqueue_priority(queue);
525         return NULL;
526
527 new_queue:
528         rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
529 new_cookie:
530         rpc_set_waitqueue_cookie(queue, task->tk_cookie);
531 out:
532         __rpc_wake_up_task(task);
533         return task;
534 }
535
536 /*
537  * Wake up the next task on the wait queue.
538  */
539 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
540 {
541         struct rpc_task *task = NULL;
542
543         dprintk("RPC:      wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
544         spin_lock_bh(&rpc_queue_lock);
545         if (RPC_IS_PRIORITY(queue))
546                 task = __rpc_wake_up_next_priority(queue);
547         else {
548                 task_for_first(task, &queue->tasks[0])
549                         __rpc_wake_up_task(task);
550         }
551         spin_unlock_bh(&rpc_queue_lock);
552
553         return task;
554 }
555
556 /**
557  * rpc_wake_up - wake up all rpc_tasks
558  * @queue: rpc_wait_queue on which the tasks are sleeping
559  *
560  * Grabs rpc_queue_lock
561  */
562 void rpc_wake_up(struct rpc_wait_queue *queue)
563 {
564         struct rpc_task *task;
565
566         struct list_head *head;
567         spin_lock_bh(&rpc_queue_lock);
568         head = &queue->tasks[queue->maxpriority];
569         for (;;) {
570                 while (!list_empty(head)) {
571                         task = list_entry(head->next, struct rpc_task, tk_list);
572                         __rpc_wake_up_task(task);
573                 }
574                 if (head == &queue->tasks[0])
575                         break;
576                 head--;
577         }
578         spin_unlock_bh(&rpc_queue_lock);
579 }
580
581 /**
582  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
583  * @queue: rpc_wait_queue on which the tasks are sleeping
584  * @status: status value to set
585  *
586  * Grabs rpc_queue_lock
587  */
588 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
589 {
590         struct list_head *head;
591         struct rpc_task *task;
592
593         spin_lock_bh(&rpc_queue_lock);
594         head = &queue->tasks[queue->maxpriority];
595         for (;;) {
596                 while (!list_empty(head)) {
597                         task = list_entry(head->next, struct rpc_task, tk_list);
598                         task->tk_status = status;
599                         __rpc_wake_up_task(task);
600                 }
601                 if (head == &queue->tasks[0])
602                         break;
603                 head--;
604         }
605         spin_unlock_bh(&rpc_queue_lock);
606 }
607
608 /*
609  * Run a task at a later time
610  */
611 static void     __rpc_atrun(struct rpc_task *);
612 void
613 rpc_delay(struct rpc_task *task, unsigned long delay)
614 {
615         task->tk_timeout = delay;
616         rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
617 }
618
619 static void
620 __rpc_atrun(struct rpc_task *task)
621 {
622         task->tk_status = 0;
623         rpc_wake_up_task(task);
624 }
625
626 /*
627  * This is the RPC `scheduler' (or rather, the finite state machine).
628  */
629 static int
630 __rpc_execute(struct rpc_task *task)
631 {
632         int             status = 0;
633
634         dprintk("RPC: %4d rpc_execute flgs %x\n",
635                                 task->tk_pid, task->tk_flags);
636
637         if (!RPC_IS_RUNNING(task)) {
638                 printk(KERN_WARNING "RPC: rpc_execute called for sleeping task!!\n");
639                 return 0;
640         }
641
642  restarted:
643         while (1) {
644                 /*
645                  * Execute any pending callback.
646                  */
647                 if (RPC_DO_CALLBACK(task)) {
648                         /* Define a callback save pointer */
649                         void (*save_callback)(struct rpc_task *);
650         
651                         /* 
652                          * If a callback exists, save it, reset it,
653                          * call it.
654                          * The save is needed to stop from resetting
655                          * another callback set within the callback handler
656                          * - Dave
657                          */
658                         save_callback=task->tk_callback;
659                         task->tk_callback=NULL;
660                         save_callback(task);
661                 }
662
663                 /*
664                  * Perform the next FSM step.
665                  * tk_action may be NULL when the task has been killed
666                  * by someone else.
667                  */
668                 if (RPC_IS_RUNNING(task)) {
669                         /*
670                          * Garbage collection of pending timers...
671                          */
672                         rpc_delete_timer(task);
673                         if (!task->tk_action)
674                                 break;
675                         task->tk_action(task);
676                         /* micro-optimization to avoid spinlock */
677                         if (RPC_IS_RUNNING(task))
678                                 continue;
679                 }
680
681                 /*
682                  * Check whether task is sleeping.
683                  */
684                 spin_lock_bh(&rpc_queue_lock);
685                 if (!RPC_IS_RUNNING(task)) {
686                         rpc_set_sleeping(task);
687                         if (RPC_IS_ASYNC(task)) {
688                                 spin_unlock_bh(&rpc_queue_lock);
689                                 return 0;
690                         }
691                 }
692                 spin_unlock_bh(&rpc_queue_lock);
693
694                 if (!RPC_IS_SLEEPING(task))
695                         continue;
696                 /* sync task: sleep here */
697                 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
698                 if (current->pid == rpciod_pid)
699                         printk(KERN_ERR "RPC: rpciod waiting on sync task!\n");
700
701                 if (!task->tk_client->cl_intr) {
702                         __wait_event(task->tk_wait, !RPC_IS_SLEEPING(task));
703                 } else {
704                         __wait_event_interruptible(task->tk_wait, !RPC_IS_SLEEPING(task), status);
705                         /*
706                          * When a sync task receives a signal, it exits with
707                          * -ERESTARTSYS. In order to catch any callbacks that
708                          * clean up after sleeping on some queue, we don't
709                          * break the loop here, but go around once more.
710                          */
711                         if (status == -ERESTARTSYS) {
712                                 dprintk("RPC: %4d got signal\n", task->tk_pid);
713                                 task->tk_flags |= RPC_TASK_KILLED;
714                                 rpc_exit(task, -ERESTARTSYS);
715                                 rpc_wake_up_task(task);
716                         }
717                 }
718                 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
719         }
720
721         if (task->tk_exit) {
722                 task->tk_exit(task);
723                 /* If tk_action is non-null, the user wants us to restart */
724                 if (task->tk_action) {
725                         if (!RPC_ASSASSINATED(task)) {
726                                 /* Release RPC slot and buffer memory */
727                                 if (task->tk_rqstp)
728                                         xprt_release(task);
729                                 rpc_free(task);
730                                 goto restarted;
731                         }
732                         printk(KERN_ERR "RPC: dead task tries to walk away.\n");
733                 }
734         }
735
736         dprintk("RPC: %4d exit() = %d\n", task->tk_pid, task->tk_status);
737         status = task->tk_status;
738
739         /* Release all resources associated with the task */
740         rpc_release_task(task);
741
742         return status;
743 }
744
745 /*
746  * User-visible entry point to the scheduler.
747  *
748  * This may be called recursively if e.g. an async NFS task updates
749  * the attributes and finds that dirty pages must be flushed.
750  * NOTE: Upon exit of this function the task is guaranteed to be
751  *       released. In particular note that tk_release() will have
752  *       been called, so your task memory may have been freed.
753  */
754 int
755 rpc_execute(struct rpc_task *task)
756 {
757         int status = -EIO;
758         if (rpc_inhibit) {
759                 printk(KERN_INFO "RPC: execution inhibited!\n");
760                 goto out_release;
761         }
762
763         status = -EWOULDBLOCK;
764         if (task->tk_active) {
765                 printk(KERN_ERR "RPC: active task was run twice!\n");
766                 goto out_err;
767         }
768
769         task->tk_active = 1;
770         rpc_set_running(task);
771         return __rpc_execute(task);
772  out_release:
773         rpc_release_task(task);
774  out_err:
775         return status;
776 }
777
778 /*
779  * This is our own little scheduler for async RPC tasks.
780  */
781 static void
782 __rpc_schedule(void)
783 {
784         struct rpc_task *task;
785         int             count = 0;
786
787         dprintk("RPC:      rpc_schedule enter\n");
788         while (1) {
789
790                 task_for_first(task, &schedq.tasks[0]) {
791                         __rpc_remove_wait_queue(task);
792                         spin_unlock_bh(&rpc_queue_lock);
793
794                         __rpc_execute(task);
795                         spin_lock_bh(&rpc_queue_lock);
796                 } else {
797                         break;
798                 }
799
800                 if (++count >= 200 || need_resched()) {
801                         count = 0;
802                         spin_unlock_bh(&rpc_queue_lock);
803                         schedule();
804                         spin_lock_bh(&rpc_queue_lock);
805                 }
806         }
807         dprintk("RPC:      rpc_schedule leave\n");
808 }
809
810 /*
811  * Allocate memory for RPC purposes.
812  *
813  * We try to ensure that some NFS reads and writes can always proceed
814  * by using a mempool when allocating 'small' buffers.
815  * In order to avoid memory starvation triggering more writebacks of
816  * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
817  */
818 void *
819 rpc_malloc(struct rpc_task *task, size_t size)
820 {
821         int     gfp;
822
823         if (task->tk_flags & RPC_TASK_SWAPPER)
824                 gfp = GFP_ATOMIC;
825         else
826                 gfp = GFP_NOFS;
827
828         if (size > RPC_BUFFER_MAXSIZE) {
829                 task->tk_buffer =  kmalloc(size, gfp);
830                 if (task->tk_buffer)
831                         task->tk_bufsize = size;
832         } else {
833                 task->tk_buffer =  mempool_alloc(rpc_buffer_mempool, gfp);
834                 if (task->tk_buffer)
835                         task->tk_bufsize = RPC_BUFFER_MAXSIZE;
836         }
837         return task->tk_buffer;
838 }
839
840 void
841 rpc_free(struct rpc_task *task)
842 {
843         if (task->tk_buffer) {
844                 if (task->tk_bufsize == RPC_BUFFER_MAXSIZE)
845                         mempool_free(task->tk_buffer, rpc_buffer_mempool);
846                 else
847                         kfree(task->tk_buffer);
848                 task->tk_buffer = NULL;
849                 task->tk_bufsize = 0;
850         }
851 }
852
853 /*
854  * Creation and deletion of RPC task structures
855  */
856 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, rpc_action callback, int flags)
857 {
858         memset(task, 0, sizeof(*task));
859         init_timer(&task->tk_timer);
860         task->tk_timer.data     = (unsigned long) task;
861         task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
862         task->tk_client = clnt;
863         task->tk_flags  = flags;
864         task->tk_exit   = callback;
865         init_waitqueue_head(&task->tk_wait);
866         if (current->uid != current->fsuid || current->gid != current->fsgid)
867                 task->tk_flags |= RPC_TASK_SETUID;
868
869         /* Initialize retry counters */
870         task->tk_garb_retry = 2;
871         task->tk_cred_retry = 2;
872         task->tk_suid_retry = 1;
873
874         task->tk_priority = RPC_PRIORITY_NORMAL;
875         task->tk_cookie = (unsigned long)current;
876         INIT_LIST_HEAD(&task->tk_links);
877
878         /* Add to global list of all tasks */
879         spin_lock(&rpc_sched_lock);
880         list_add(&task->tk_task, &all_tasks);
881         spin_unlock(&rpc_sched_lock);
882
883         if (clnt) {
884                 atomic_inc(&clnt->cl_users);
885                 if (clnt->cl_softrtry)
886                         task->tk_flags |= RPC_TASK_SOFT;
887         }
888
889 #ifdef RPC_DEBUG
890         task->tk_magic = 0xf00baa;
891         task->tk_pid = rpc_task_id++;
892 #endif
893         dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
894                                 current->pid);
895 }
896
897 static struct rpc_task *
898 rpc_alloc_task(void)
899 {
900         return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
901 }
902
903 static void
904 rpc_default_free_task(struct rpc_task *task)
905 {
906         dprintk("RPC: %4d freeing task\n", task->tk_pid);
907         mempool_free(task, rpc_task_mempool);
908 }
909
910 /*
911  * Create a new task for the specified client.  We have to
912  * clean up after an allocation failure, as the client may
913  * have specified "oneshot".
914  */
915 struct rpc_task *
916 rpc_new_task(struct rpc_clnt *clnt, rpc_action callback, int flags)
917 {
918         struct rpc_task *task;
919
920         task = rpc_alloc_task();
921         if (!task)
922                 goto cleanup;
923
924         rpc_init_task(task, clnt, callback, flags);
925
926         /* Replace tk_release */
927         task->tk_release = rpc_default_free_task;
928
929         dprintk("RPC: %4d allocated task\n", task->tk_pid);
930         task->tk_flags |= RPC_TASK_DYNAMIC;
931 out:
932         return task;
933
934 cleanup:
935         /* Check whether to release the client */
936         if (clnt) {
937                 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
938                         atomic_read(&clnt->cl_users), clnt->cl_oneshot);
939                 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
940                 rpc_release_client(clnt);
941         }
942         goto out;
943 }
944
945 void
946 rpc_release_task(struct rpc_task *task)
947 {
948         dprintk("RPC: %4d release task\n", task->tk_pid);
949
950 #ifdef RPC_DEBUG
951         if (task->tk_magic != 0xf00baa) {
952                 printk(KERN_ERR "RPC: attempt to release a non-existing task!\n");
953                 rpc_debug = ~0;
954                 rpc_show_tasks();
955                 return;
956         }
957 #endif
958
959         /* Remove from global task list */
960         spin_lock(&rpc_sched_lock);
961         list_del(&task->tk_task);
962         spin_unlock(&rpc_sched_lock);
963
964         /* Protect the execution below. */
965         spin_lock_bh(&rpc_queue_lock);
966
967         /* Disable timer to prevent zombie wakeup */
968         __rpc_disable_timer(task);
969
970         /* Remove from any wait queue we're still on */
971         __rpc_remove_wait_queue(task);
972
973         task->tk_active = 0;
974
975         spin_unlock_bh(&rpc_queue_lock);
976
977         /* Synchronously delete any running timer */
978         rpc_delete_timer(task);
979
980         /* Release resources */
981         if (task->tk_rqstp)
982                 xprt_release(task);
983         if (task->tk_msg.rpc_cred)
984                 rpcauth_unbindcred(task);
985         rpc_free(task);
986         if (task->tk_client) {
987                 rpc_release_client(task->tk_client);
988                 task->tk_client = NULL;
989         }
990
991 #ifdef RPC_DEBUG
992         task->tk_magic = 0;
993 #endif
994         if (task->tk_release)
995                 task->tk_release(task);
996 }
997
998 /**
999  * rpc_find_parent - find the parent of a child task.
1000  * @child: child task
1001  *
1002  * Checks that the parent task is still sleeping on the
1003  * queue 'childq'. If so returns a pointer to the parent.
1004  * Upon failure returns NULL.
1005  *
1006  * Caller must hold rpc_queue_lock
1007  */
1008 static inline struct rpc_task *
1009 rpc_find_parent(struct rpc_task *child)
1010 {
1011         struct rpc_task *task, *parent;
1012         struct list_head *le;
1013
1014         parent = (struct rpc_task *) child->tk_calldata;
1015         task_for_each(task, le, &childq.tasks[0])
1016                 if (task == parent)
1017                         return parent;
1018
1019         return NULL;
1020 }
1021
1022 static void
1023 rpc_child_exit(struct rpc_task *child)
1024 {
1025         struct rpc_task *parent;
1026
1027         spin_lock_bh(&rpc_queue_lock);
1028         if ((parent = rpc_find_parent(child)) != NULL) {
1029                 parent->tk_status = child->tk_status;
1030                 __rpc_wake_up_task(parent);
1031         }
1032         spin_unlock_bh(&rpc_queue_lock);
1033 }
1034
1035 /*
1036  * Note: rpc_new_task releases the client after a failure.
1037  */
1038 struct rpc_task *
1039 rpc_new_child(struct rpc_clnt *clnt, struct rpc_task *parent)
1040 {
1041         struct rpc_task *task;
1042
1043         task = rpc_new_task(clnt, NULL, RPC_TASK_ASYNC | RPC_TASK_CHILD);
1044         if (!task)
1045                 goto fail;
1046         task->tk_exit = rpc_child_exit;
1047         task->tk_calldata = parent;
1048         return task;
1049
1050 fail:
1051         parent->tk_status = -ENOMEM;
1052         return NULL;
1053 }
1054
1055 void
1056 rpc_run_child(struct rpc_task *task, struct rpc_task *child, rpc_action func)
1057 {
1058         spin_lock_bh(&rpc_queue_lock);
1059         /* N.B. Is it possible for the child to have already finished? */
1060         __rpc_sleep_on(&childq, task, func, NULL);
1061         rpc_schedule_run(child);
1062         spin_unlock_bh(&rpc_queue_lock);
1063 }
1064
1065 /*
1066  * Kill all tasks for the given client.
1067  * XXX: kill their descendants as well?
1068  */
1069 void
1070 rpc_killall_tasks(struct rpc_clnt *clnt)
1071 {
1072         struct rpc_task *rovr;
1073         struct list_head *le;
1074
1075         dprintk("RPC:      killing all tasks for client %p\n", clnt);
1076
1077         /*
1078          * Spin lock all_tasks to prevent changes...
1079          */
1080         spin_lock(&rpc_sched_lock);
1081         alltask_for_each(rovr, le, &all_tasks)
1082                 if (!clnt || rovr->tk_client == clnt) {
1083                         rovr->tk_flags |= RPC_TASK_KILLED;
1084                         rpc_exit(rovr, -EIO);
1085                         rpc_wake_up_task(rovr);
1086                 }
1087         spin_unlock(&rpc_sched_lock);
1088 }
1089
1090 static DECLARE_MUTEX_LOCKED(rpciod_running);
1091
1092 static inline int
1093 rpciod_task_pending(void)
1094 {
1095         return !list_empty(&schedq.tasks[0]);
1096 }
1097
1098
1099 /*
1100  * This is the rpciod kernel thread
1101  */
1102 static int
1103 rpciod(void *ptr)
1104 {
1105         int             rounds = 0;
1106
1107         lock_kernel();
1108         /*
1109          * Let our maker know we're running ...
1110          */
1111         rpciod_pid = current->pid;
1112         up(&rpciod_running);
1113
1114         daemonize("rpciod");
1115         allow_signal(SIGKILL);
1116
1117         dprintk("RPC: rpciod starting (pid %d)\n", rpciod_pid);
1118         spin_lock_bh(&rpc_queue_lock);
1119         while (rpciod_users) {
1120                 DEFINE_WAIT(wait);
1121                 if (signalled()) {
1122                         spin_unlock_bh(&rpc_queue_lock);
1123                         rpciod_killall();
1124                         flush_signals(current);
1125                         spin_lock_bh(&rpc_queue_lock);
1126                 }
1127                 __rpc_schedule();
1128                 if (current->flags & PF_FREEZE) {
1129                         spin_unlock_bh(&rpc_queue_lock);
1130                         refrigerator(PF_FREEZE);
1131                         spin_lock_bh(&rpc_queue_lock);
1132                 }
1133
1134                 if (++rounds >= 64) {   /* safeguard */
1135                         spin_unlock_bh(&rpc_queue_lock);
1136                         schedule();
1137                         rounds = 0;
1138                         spin_lock_bh(&rpc_queue_lock);
1139                 }
1140
1141                 dprintk("RPC: rpciod back to sleep\n");
1142                 prepare_to_wait(&rpciod_idle, &wait, TASK_INTERRUPTIBLE);
1143                 if (!rpciod_task_pending() && !signalled()) {
1144                         spin_unlock_bh(&rpc_queue_lock);
1145                         schedule();
1146                         rounds = 0;
1147                         spin_lock_bh(&rpc_queue_lock);
1148                 }
1149                 finish_wait(&rpciod_idle, &wait);
1150                 dprintk("RPC: switch to rpciod\n");
1151         }
1152         spin_unlock_bh(&rpc_queue_lock);
1153
1154         dprintk("RPC: rpciod shutdown commences\n");
1155         if (!list_empty(&all_tasks)) {
1156                 printk(KERN_ERR "rpciod: active tasks at shutdown?!\n");
1157                 rpciod_killall();
1158         }
1159
1160         dprintk("RPC: rpciod exiting\n");
1161         unlock_kernel();
1162
1163         rpciod_pid = 0;
1164         complete_and_exit(&rpciod_killer, 0);
1165         return 0;
1166 }
1167
1168 static void
1169 rpciod_killall(void)
1170 {
1171         unsigned long flags;
1172
1173         while (!list_empty(&all_tasks)) {
1174                 clear_thread_flag(TIF_SIGPENDING);
1175                 rpc_killall_tasks(NULL);
1176                 spin_lock_bh(&rpc_queue_lock);
1177                 __rpc_schedule();
1178                 spin_unlock_bh(&rpc_queue_lock);
1179                 if (!list_empty(&all_tasks)) {
1180                         dprintk("rpciod_killall: waiting for tasks to exit\n");
1181                         yield();
1182                 }
1183         }
1184
1185         spin_lock_irqsave(&current->sighand->siglock, flags);
1186         recalc_sigpending();
1187         spin_unlock_irqrestore(&current->sighand->siglock, flags);
1188 }
1189
1190 /*
1191  * Start up the rpciod process if it's not already running.
1192  */
1193 int
1194 rpciod_up(void)
1195 {
1196         int error = 0;
1197
1198         down(&rpciod_sema);
1199         dprintk("rpciod_up: pid %d, users %d\n", rpciod_pid, rpciod_users);
1200         rpciod_users++;
1201         if (rpciod_pid)
1202                 goto out;
1203         /*
1204          * If there's no pid, we should be the first user.
1205          */
1206         if (rpciod_users > 1)
1207                 printk(KERN_WARNING "rpciod_up: no pid, %d users??\n", rpciod_users);
1208         /*
1209          * Create the rpciod thread and wait for it to start.
1210          */
1211         error = kernel_thread(rpciod, NULL, 0);
1212         if (error < 0) {
1213                 printk(KERN_WARNING "rpciod_up: create thread failed, error=%d\n", error);
1214                 rpciod_users--;
1215                 goto out;
1216         }
1217         down(&rpciod_running);
1218         error = 0;
1219 out:
1220         up(&rpciod_sema);
1221         return error;
1222 }
1223
1224 void
1225 rpciod_down(void)
1226 {
1227         down(&rpciod_sema);
1228         dprintk("rpciod_down pid %d sema %d\n", rpciod_pid, rpciod_users);
1229         if (rpciod_users) {
1230                 if (--rpciod_users)
1231                         goto out;
1232         } else
1233                 printk(KERN_WARNING "rpciod_down: pid=%d, no users??\n", rpciod_pid);
1234
1235         if (!rpciod_pid) {
1236                 dprintk("rpciod_down: Nothing to do!\n");
1237                 goto out;
1238         }
1239
1240         kill_proc(rpciod_pid, SIGKILL, 1);
1241         wait_for_completion(&rpciod_killer);
1242  out:
1243         up(&rpciod_sema);
1244 }
1245
1246 #ifdef RPC_DEBUG
1247 void rpc_show_tasks(void)
1248 {
1249         struct list_head *le;
1250         struct rpc_task *t;
1251
1252         spin_lock(&rpc_sched_lock);
1253         if (list_empty(&all_tasks)) {
1254                 spin_unlock(&rpc_sched_lock);
1255                 return;
1256         }
1257         printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1258                 "-rpcwait -action- --exit--\n");
1259         alltask_for_each(t, le, &all_tasks)
1260                 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1261                         t->tk_pid,
1262                         (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1263                         t->tk_flags, t->tk_status,
1264                         t->tk_client,
1265                         (t->tk_client ? t->tk_client->cl_prog : 0),
1266                         t->tk_rqstp, t->tk_timeout,
1267                         rpc_qname(t->tk_rpcwait),
1268                         t->tk_action, t->tk_exit);
1269         spin_unlock(&rpc_sched_lock);
1270 }
1271 #endif
1272
1273 void
1274 rpc_destroy_mempool(void)
1275 {
1276         if (rpc_buffer_mempool)
1277                 mempool_destroy(rpc_buffer_mempool);
1278         if (rpc_task_mempool)
1279                 mempool_destroy(rpc_task_mempool);
1280         if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
1281                 printk(KERN_INFO "rpc_task: not all structures were freed\n");
1282         if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
1283                 printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
1284 }
1285
1286 int
1287 rpc_init_mempool(void)
1288 {
1289         rpc_task_slabp = kmem_cache_create("rpc_tasks",
1290                                              sizeof(struct rpc_task),
1291                                              0, SLAB_HWCACHE_ALIGN,
1292                                              NULL, NULL);
1293         if (!rpc_task_slabp)
1294                 goto err_nomem;
1295         rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1296                                              RPC_BUFFER_MAXSIZE,
1297                                              0, SLAB_HWCACHE_ALIGN,
1298                                              NULL, NULL);
1299         if (!rpc_buffer_slabp)
1300                 goto err_nomem;
1301         rpc_task_mempool = mempool_create(RPC_TASK_POOLSIZE,
1302                                             mempool_alloc_slab,
1303                                             mempool_free_slab,
1304                                             rpc_task_slabp);
1305         if (!rpc_task_mempool)
1306                 goto err_nomem;
1307         rpc_buffer_mempool = mempool_create(RPC_BUFFER_POOLSIZE,
1308                                             mempool_alloc_slab,
1309                                             mempool_free_slab,
1310                                             rpc_buffer_slabp);
1311         if (!rpc_buffer_mempool)
1312                 goto err_nomem;
1313         return 0;
1314 err_nomem:
1315         rpc_destroy_mempool();
1316         return -ENOMEM;
1317 }