SUNRPC: Support for RPC child tasks no longer needed
[linux-2.6-block.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/mutex.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 #define RPC_TASK_MAGIC_ID       0xf00baa
29 static int                      rpc_task_id;
30 #endif
31
32 /*
33  * RPC slabs and memory pools
34  */
35 #define RPC_BUFFER_MAXSIZE      (2048)
36 #define RPC_BUFFER_POOLSIZE     (8)
37 #define RPC_TASK_POOLSIZE       (8)
38 static kmem_cache_t     *rpc_task_slabp __read_mostly;
39 static kmem_cache_t     *rpc_buffer_slabp __read_mostly;
40 static mempool_t        *rpc_task_mempool __read_mostly;
41 static mempool_t        *rpc_buffer_mempool __read_mostly;
42
43 static void                     __rpc_default_timer(struct rpc_task *task);
44 static void                     rpciod_killall(void);
45 static void                     rpc_async_schedule(void *);
46
47 /*
48  * RPC tasks sit here while waiting for conditions to improve.
49  */
50 static RPC_WAITQ(delay_queue, "delayq");
51
52 /*
53  * All RPC tasks are linked into this list
54  */
55 static LIST_HEAD(all_tasks);
56
57 /*
58  * rpciod-related stuff
59  */
60 static DEFINE_MUTEX(rpciod_mutex);
61 static unsigned int             rpciod_users;
62 struct workqueue_struct *rpciod_workqueue;
63
64 /*
65  * Spinlock for other critical sections of code.
66  */
67 static DEFINE_SPINLOCK(rpc_sched_lock);
68
69 /*
70  * Disable the timer for a given RPC task. Should be called with
71  * queue->lock and bh_disabled in order to avoid races within
72  * rpc_run_timer().
73  */
74 static inline void
75 __rpc_disable_timer(struct rpc_task *task)
76 {
77         dprintk("RPC: %4d disabling timer\n", task->tk_pid);
78         task->tk_timeout_fn = NULL;
79         task->tk_timeout = 0;
80 }
81
82 /*
83  * Run a timeout function.
84  * We use the callback in order to allow __rpc_wake_up_task()
85  * and friends to disable the timer synchronously on SMP systems
86  * without calling del_timer_sync(). The latter could cause a
87  * deadlock if called while we're holding spinlocks...
88  */
89 static void rpc_run_timer(struct rpc_task *task)
90 {
91         void (*callback)(struct rpc_task *);
92
93         callback = task->tk_timeout_fn;
94         task->tk_timeout_fn = NULL;
95         if (callback && RPC_IS_QUEUED(task)) {
96                 dprintk("RPC: %4d running timer\n", task->tk_pid);
97                 callback(task);
98         }
99         smp_mb__before_clear_bit();
100         clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
101         smp_mb__after_clear_bit();
102 }
103
104 /*
105  * Set up a timer for the current task.
106  */
107 static inline void
108 __rpc_add_timer(struct rpc_task *task, rpc_action timer)
109 {
110         if (!task->tk_timeout)
111                 return;
112
113         dprintk("RPC: %4d setting alarm for %lu ms\n",
114                         task->tk_pid, task->tk_timeout * 1000 / HZ);
115
116         if (timer)
117                 task->tk_timeout_fn = timer;
118         else
119                 task->tk_timeout_fn = __rpc_default_timer;
120         set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
121         mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
122 }
123
124 /*
125  * Delete any timer for the current task. Because we use del_timer_sync(),
126  * this function should never be called while holding queue->lock.
127  */
128 static void
129 rpc_delete_timer(struct rpc_task *task)
130 {
131         if (RPC_IS_QUEUED(task))
132                 return;
133         if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
134                 del_singleshot_timer_sync(&task->tk_timer);
135                 dprintk("RPC: %4d deleting timer\n", task->tk_pid);
136         }
137 }
138
139 /*
140  * Add new request to a priority queue.
141  */
142 static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
143 {
144         struct list_head *q;
145         struct rpc_task *t;
146
147         INIT_LIST_HEAD(&task->u.tk_wait.links);
148         q = &queue->tasks[task->tk_priority];
149         if (unlikely(task->tk_priority > queue->maxpriority))
150                 q = &queue->tasks[queue->maxpriority];
151         list_for_each_entry(t, q, u.tk_wait.list) {
152                 if (t->tk_cookie == task->tk_cookie) {
153                         list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
154                         return;
155                 }
156         }
157         list_add_tail(&task->u.tk_wait.list, q);
158 }
159
160 /*
161  * Add new request to wait queue.
162  *
163  * Swapper tasks always get inserted at the head of the queue.
164  * This should avoid many nasty memory deadlocks and hopefully
165  * improve overall performance.
166  * Everyone else gets appended to the queue to ensure proper FIFO behavior.
167  */
168 static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
169 {
170         BUG_ON (RPC_IS_QUEUED(task));
171
172         if (RPC_IS_PRIORITY(queue))
173                 __rpc_add_wait_queue_priority(queue, task);
174         else if (RPC_IS_SWAPPER(task))
175                 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
176         else
177                 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
178         task->u.tk_wait.rpc_waitq = queue;
179         queue->qlen++;
180         rpc_set_queued(task);
181
182         dprintk("RPC: %4d added to queue %p \"%s\"\n",
183                                 task->tk_pid, queue, rpc_qname(queue));
184 }
185
186 /*
187  * Remove request from a priority queue.
188  */
189 static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
190 {
191         struct rpc_task *t;
192
193         if (!list_empty(&task->u.tk_wait.links)) {
194                 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
195                 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
196                 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
197         }
198         list_del(&task->u.tk_wait.list);
199 }
200
201 /*
202  * Remove request from queue.
203  * Note: must be called with spin lock held.
204  */
205 static void __rpc_remove_wait_queue(struct rpc_task *task)
206 {
207         struct rpc_wait_queue *queue;
208         queue = task->u.tk_wait.rpc_waitq;
209
210         if (RPC_IS_PRIORITY(queue))
211                 __rpc_remove_wait_queue_priority(task);
212         else
213                 list_del(&task->u.tk_wait.list);
214         queue->qlen--;
215         dprintk("RPC: %4d removed from queue %p \"%s\"\n",
216                                 task->tk_pid, queue, rpc_qname(queue));
217 }
218
219 static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
220 {
221         queue->priority = priority;
222         queue->count = 1 << (priority * 2);
223 }
224
225 static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
226 {
227         queue->cookie = cookie;
228         queue->nr = RPC_BATCH_COUNT;
229 }
230
231 static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
232 {
233         rpc_set_waitqueue_priority(queue, queue->maxpriority);
234         rpc_set_waitqueue_cookie(queue, 0);
235 }
236
237 static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
238 {
239         int i;
240
241         spin_lock_init(&queue->lock);
242         for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
243                 INIT_LIST_HEAD(&queue->tasks[i]);
244         queue->maxpriority = maxprio;
245         rpc_reset_waitqueue_priority(queue);
246 #ifdef RPC_DEBUG
247         queue->name = qname;
248 #endif
249 }
250
251 void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
252 {
253         __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
254 }
255
256 void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
257 {
258         __rpc_init_priority_wait_queue(queue, qname, 0);
259 }
260 EXPORT_SYMBOL(rpc_init_wait_queue);
261
262 static int rpc_wait_bit_interruptible(void *word)
263 {
264         if (signal_pending(current))
265                 return -ERESTARTSYS;
266         schedule();
267         return 0;
268 }
269
270 /*
271  * Mark an RPC call as having completed by clearing the 'active' bit
272  */
273 static inline void rpc_mark_complete_task(struct rpc_task *task)
274 {
275         rpc_clear_active(task);
276         wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
277 }
278
279 /*
280  * Allow callers to wait for completion of an RPC call
281  */
282 int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
283 {
284         if (action == NULL)
285                 action = rpc_wait_bit_interruptible;
286         return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
287                         action, TASK_INTERRUPTIBLE);
288 }
289 EXPORT_SYMBOL(__rpc_wait_for_completion_task);
290
291 /*
292  * Make an RPC task runnable.
293  *
294  * Note: If the task is ASYNC, this must be called with 
295  * the spinlock held to protect the wait queue operation.
296  */
297 static void rpc_make_runnable(struct rpc_task *task)
298 {
299         int do_ret;
300
301         BUG_ON(task->tk_timeout_fn);
302         do_ret = rpc_test_and_set_running(task);
303         rpc_clear_queued(task);
304         if (do_ret)
305                 return;
306         if (RPC_IS_ASYNC(task)) {
307                 int status;
308
309                 INIT_WORK(&task->u.tk_work, rpc_async_schedule, (void *)task);
310                 status = queue_work(task->tk_workqueue, &task->u.tk_work);
311                 if (status < 0) {
312                         printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
313                         task->tk_status = status;
314                         return;
315                 }
316         } else
317                 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
318 }
319
320 /*
321  * Prepare for sleeping on a wait queue.
322  * By always appending tasks to the list we ensure FIFO behavior.
323  * NB: An RPC task will only receive interrupt-driven events as long
324  * as it's on a wait queue.
325  */
326 static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
327                         rpc_action action, rpc_action timer)
328 {
329         dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
330                                 rpc_qname(q), jiffies);
331
332         if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
333                 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
334                 return;
335         }
336
337         /* Mark the task as being activated if so needed */
338         rpc_set_active(task);
339
340         __rpc_add_wait_queue(q, task);
341
342         BUG_ON(task->tk_callback != NULL);
343         task->tk_callback = action;
344         __rpc_add_timer(task, timer);
345 }
346
347 void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
348                                 rpc_action action, rpc_action timer)
349 {
350         /*
351          * Protect the queue operations.
352          */
353         spin_lock_bh(&q->lock);
354         __rpc_sleep_on(q, task, action, timer);
355         spin_unlock_bh(&q->lock);
356 }
357
358 /**
359  * __rpc_do_wake_up_task - wake up a single rpc_task
360  * @task: task to be woken up
361  *
362  * Caller must hold queue->lock, and have cleared the task queued flag.
363  */
364 static void __rpc_do_wake_up_task(struct rpc_task *task)
365 {
366         dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
367
368 #ifdef RPC_DEBUG
369         BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
370 #endif
371         /* Has the task been executed yet? If not, we cannot wake it up! */
372         if (!RPC_IS_ACTIVATED(task)) {
373                 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
374                 return;
375         }
376
377         __rpc_disable_timer(task);
378         __rpc_remove_wait_queue(task);
379
380         rpc_make_runnable(task);
381
382         dprintk("RPC:      __rpc_wake_up_task done\n");
383 }
384
385 /*
386  * Wake up the specified task
387  */
388 static void __rpc_wake_up_task(struct rpc_task *task)
389 {
390         if (rpc_start_wakeup(task)) {
391                 if (RPC_IS_QUEUED(task))
392                         __rpc_do_wake_up_task(task);
393                 rpc_finish_wakeup(task);
394         }
395 }
396
397 /*
398  * Default timeout handler if none specified by user
399  */
400 static void
401 __rpc_default_timer(struct rpc_task *task)
402 {
403         dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
404         task->tk_status = -ETIMEDOUT;
405         rpc_wake_up_task(task);
406 }
407
408 /*
409  * Wake up the specified task
410  */
411 void rpc_wake_up_task(struct rpc_task *task)
412 {
413         if (rpc_start_wakeup(task)) {
414                 if (RPC_IS_QUEUED(task)) {
415                         struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
416
417                         spin_lock_bh(&queue->lock);
418                         __rpc_do_wake_up_task(task);
419                         spin_unlock_bh(&queue->lock);
420                 }
421                 rpc_finish_wakeup(task);
422         }
423 }
424
425 /*
426  * Wake up the next task on a priority queue.
427  */
428 static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
429 {
430         struct list_head *q;
431         struct rpc_task *task;
432
433         /*
434          * Service a batch of tasks from a single cookie.
435          */
436         q = &queue->tasks[queue->priority];
437         if (!list_empty(q)) {
438                 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
439                 if (queue->cookie == task->tk_cookie) {
440                         if (--queue->nr)
441                                 goto out;
442                         list_move_tail(&task->u.tk_wait.list, q);
443                 }
444                 /*
445                  * Check if we need to switch queues.
446                  */
447                 if (--queue->count)
448                         goto new_cookie;
449         }
450
451         /*
452          * Service the next queue.
453          */
454         do {
455                 if (q == &queue->tasks[0])
456                         q = &queue->tasks[queue->maxpriority];
457                 else
458                         q = q - 1;
459                 if (!list_empty(q)) {
460                         task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
461                         goto new_queue;
462                 }
463         } while (q != &queue->tasks[queue->priority]);
464
465         rpc_reset_waitqueue_priority(queue);
466         return NULL;
467
468 new_queue:
469         rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
470 new_cookie:
471         rpc_set_waitqueue_cookie(queue, task->tk_cookie);
472 out:
473         __rpc_wake_up_task(task);
474         return task;
475 }
476
477 /*
478  * Wake up the next task on the wait queue.
479  */
480 struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
481 {
482         struct rpc_task *task = NULL;
483
484         dprintk("RPC:      wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
485         spin_lock_bh(&queue->lock);
486         if (RPC_IS_PRIORITY(queue))
487                 task = __rpc_wake_up_next_priority(queue);
488         else {
489                 task_for_first(task, &queue->tasks[0])
490                         __rpc_wake_up_task(task);
491         }
492         spin_unlock_bh(&queue->lock);
493
494         return task;
495 }
496
497 /**
498  * rpc_wake_up - wake up all rpc_tasks
499  * @queue: rpc_wait_queue on which the tasks are sleeping
500  *
501  * Grabs queue->lock
502  */
503 void rpc_wake_up(struct rpc_wait_queue *queue)
504 {
505         struct rpc_task *task, *next;
506         struct list_head *head;
507
508         spin_lock_bh(&queue->lock);
509         head = &queue->tasks[queue->maxpriority];
510         for (;;) {
511                 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
512                         __rpc_wake_up_task(task);
513                 if (head == &queue->tasks[0])
514                         break;
515                 head--;
516         }
517         spin_unlock_bh(&queue->lock);
518 }
519
520 /**
521  * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
522  * @queue: rpc_wait_queue on which the tasks are sleeping
523  * @status: status value to set
524  *
525  * Grabs queue->lock
526  */
527 void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
528 {
529         struct rpc_task *task, *next;
530         struct list_head *head;
531
532         spin_lock_bh(&queue->lock);
533         head = &queue->tasks[queue->maxpriority];
534         for (;;) {
535                 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
536                         task->tk_status = status;
537                         __rpc_wake_up_task(task);
538                 }
539                 if (head == &queue->tasks[0])
540                         break;
541                 head--;
542         }
543         spin_unlock_bh(&queue->lock);
544 }
545
546 /*
547  * Run a task at a later time
548  */
549 static void     __rpc_atrun(struct rpc_task *);
550 void
551 rpc_delay(struct rpc_task *task, unsigned long delay)
552 {
553         task->tk_timeout = delay;
554         rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
555 }
556
557 static void
558 __rpc_atrun(struct rpc_task *task)
559 {
560         task->tk_status = 0;
561         rpc_wake_up_task(task);
562 }
563
564 /*
565  * Helper to call task->tk_ops->rpc_call_prepare
566  */
567 static void rpc_prepare_task(struct rpc_task *task)
568 {
569         task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
570 }
571
572 /*
573  * Helper that calls task->tk_ops->rpc_call_done if it exists
574  */
575 void rpc_exit_task(struct rpc_task *task)
576 {
577         task->tk_action = NULL;
578         if (task->tk_ops->rpc_call_done != NULL) {
579                 task->tk_ops->rpc_call_done(task, task->tk_calldata);
580                 if (task->tk_action != NULL) {
581                         WARN_ON(RPC_ASSASSINATED(task));
582                         /* Always release the RPC slot and buffer memory */
583                         xprt_release(task);
584                 }
585         }
586 }
587 EXPORT_SYMBOL(rpc_exit_task);
588
589 /*
590  * This is the RPC `scheduler' (or rather, the finite state machine).
591  */
592 static int __rpc_execute(struct rpc_task *task)
593 {
594         int             status = 0;
595
596         dprintk("RPC: %4d rpc_execute flgs %x\n",
597                                 task->tk_pid, task->tk_flags);
598
599         BUG_ON(RPC_IS_QUEUED(task));
600
601         for (;;) {
602                 /*
603                  * Garbage collection of pending timers...
604                  */
605                 rpc_delete_timer(task);
606
607                 /*
608                  * Execute any pending callback.
609                  */
610                 if (RPC_DO_CALLBACK(task)) {
611                         /* Define a callback save pointer */
612                         void (*save_callback)(struct rpc_task *);
613         
614                         /* 
615                          * If a callback exists, save it, reset it,
616                          * call it.
617                          * The save is needed to stop from resetting
618                          * another callback set within the callback handler
619                          * - Dave
620                          */
621                         save_callback=task->tk_callback;
622                         task->tk_callback=NULL;
623                         lock_kernel();
624                         save_callback(task);
625                         unlock_kernel();
626                 }
627
628                 /*
629                  * Perform the next FSM step.
630                  * tk_action may be NULL when the task has been killed
631                  * by someone else.
632                  */
633                 if (!RPC_IS_QUEUED(task)) {
634                         if (task->tk_action == NULL)
635                                 break;
636                         lock_kernel();
637                         task->tk_action(task);
638                         unlock_kernel();
639                 }
640
641                 /*
642                  * Lockless check for whether task is sleeping or not.
643                  */
644                 if (!RPC_IS_QUEUED(task))
645                         continue;
646                 rpc_clear_running(task);
647                 if (RPC_IS_ASYNC(task)) {
648                         /* Careful! we may have raced... */
649                         if (RPC_IS_QUEUED(task))
650                                 return 0;
651                         if (rpc_test_and_set_running(task))
652                                 return 0;
653                         continue;
654                 }
655
656                 /* sync task: sleep here */
657                 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
658                 /* Note: Caller should be using rpc_clnt_sigmask() */
659                 status = out_of_line_wait_on_bit(&task->tk_runstate,
660                                 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
661                                 TASK_INTERRUPTIBLE);
662                 if (status == -ERESTARTSYS) {
663                         /*
664                          * When a sync task receives a signal, it exits with
665                          * -ERESTARTSYS. In order to catch any callbacks that
666                          * clean up after sleeping on some queue, we don't
667                          * break the loop here, but go around once more.
668                          */
669                         dprintk("RPC: %4d got signal\n", task->tk_pid);
670                         task->tk_flags |= RPC_TASK_KILLED;
671                         rpc_exit(task, -ERESTARTSYS);
672                         rpc_wake_up_task(task);
673                 }
674                 rpc_set_running(task);
675                 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
676         }
677
678         dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
679         /* Wake up anyone who is waiting for task completion */
680         rpc_mark_complete_task(task);
681         /* Release all resources associated with the task */
682         rpc_release_task(task);
683         return status;
684 }
685
686 /*
687  * User-visible entry point to the scheduler.
688  *
689  * This may be called recursively if e.g. an async NFS task updates
690  * the attributes and finds that dirty pages must be flushed.
691  * NOTE: Upon exit of this function the task is guaranteed to be
692  *       released. In particular note that tk_release() will have
693  *       been called, so your task memory may have been freed.
694  */
695 int
696 rpc_execute(struct rpc_task *task)
697 {
698         rpc_set_active(task);
699         rpc_set_running(task);
700         return __rpc_execute(task);
701 }
702
703 static void rpc_async_schedule(void *arg)
704 {
705         __rpc_execute((struct rpc_task *)arg);
706 }
707
708 /**
709  * rpc_malloc - allocate an RPC buffer
710  * @task: RPC task that will use this buffer
711  * @size: requested byte size
712  *
713  * We try to ensure that some NFS reads and writes can always proceed
714  * by using a mempool when allocating 'small' buffers.
715  * In order to avoid memory starvation triggering more writebacks of
716  * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
717  */
718 void * rpc_malloc(struct rpc_task *task, size_t size)
719 {
720         struct rpc_rqst *req = task->tk_rqstp;
721         gfp_t   gfp;
722
723         if (task->tk_flags & RPC_TASK_SWAPPER)
724                 gfp = GFP_ATOMIC;
725         else
726                 gfp = GFP_NOFS;
727
728         if (size > RPC_BUFFER_MAXSIZE) {
729                 req->rq_buffer = kmalloc(size, gfp);
730                 if (req->rq_buffer)
731                         req->rq_bufsize = size;
732         } else {
733                 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
734                 if (req->rq_buffer)
735                         req->rq_bufsize = RPC_BUFFER_MAXSIZE;
736         }
737         return req->rq_buffer;
738 }
739
740 /**
741  * rpc_free - free buffer allocated via rpc_malloc
742  * @task: RPC task with a buffer to be freed
743  *
744  */
745 void rpc_free(struct rpc_task *task)
746 {
747         struct rpc_rqst *req = task->tk_rqstp;
748
749         if (req->rq_buffer) {
750                 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
751                         mempool_free(req->rq_buffer, rpc_buffer_mempool);
752                 else
753                         kfree(req->rq_buffer);
754                 req->rq_buffer = NULL;
755                 req->rq_bufsize = 0;
756         }
757 }
758
759 /*
760  * Creation and deletion of RPC task structures
761  */
762 void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
763 {
764         memset(task, 0, sizeof(*task));
765         init_timer(&task->tk_timer);
766         task->tk_timer.data     = (unsigned long) task;
767         task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
768         atomic_set(&task->tk_count, 1);
769         task->tk_client = clnt;
770         task->tk_flags  = flags;
771         task->tk_ops = tk_ops;
772         if (tk_ops->rpc_call_prepare != NULL)
773                 task->tk_action = rpc_prepare_task;
774         task->tk_calldata = calldata;
775
776         /* Initialize retry counters */
777         task->tk_garb_retry = 2;
778         task->tk_cred_retry = 2;
779
780         task->tk_priority = RPC_PRIORITY_NORMAL;
781         task->tk_cookie = (unsigned long)current;
782
783         /* Initialize workqueue for async tasks */
784         task->tk_workqueue = rpciod_workqueue;
785
786         if (clnt) {
787                 atomic_inc(&clnt->cl_users);
788                 if (clnt->cl_softrtry)
789                         task->tk_flags |= RPC_TASK_SOFT;
790                 if (!clnt->cl_intr)
791                         task->tk_flags |= RPC_TASK_NOINTR;
792         }
793
794 #ifdef RPC_DEBUG
795         task->tk_magic = RPC_TASK_MAGIC_ID;
796         task->tk_pid = rpc_task_id++;
797 #endif
798         /* Add to global list of all tasks */
799         spin_lock(&rpc_sched_lock);
800         list_add_tail(&task->tk_task, &all_tasks);
801         spin_unlock(&rpc_sched_lock);
802
803         BUG_ON(task->tk_ops == NULL);
804
805         /* starting timestamp */
806         task->tk_start = jiffies;
807
808         dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
809                                 current->pid);
810 }
811
812 static struct rpc_task *
813 rpc_alloc_task(void)
814 {
815         return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
816 }
817
818 static void rpc_free_task(struct rpc_task *task)
819 {
820         dprintk("RPC: %4d freeing task\n", task->tk_pid);
821         mempool_free(task, rpc_task_mempool);
822 }
823
824 /*
825  * Create a new task for the specified client.  We have to
826  * clean up after an allocation failure, as the client may
827  * have specified "oneshot".
828  */
829 struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
830 {
831         struct rpc_task *task;
832
833         task = rpc_alloc_task();
834         if (!task)
835                 goto cleanup;
836
837         rpc_init_task(task, clnt, flags, tk_ops, calldata);
838
839         dprintk("RPC: %4d allocated task\n", task->tk_pid);
840         task->tk_flags |= RPC_TASK_DYNAMIC;
841 out:
842         return task;
843
844 cleanup:
845         /* Check whether to release the client */
846         if (clnt) {
847                 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
848                         atomic_read(&clnt->cl_users), clnt->cl_oneshot);
849                 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
850                 rpc_release_client(clnt);
851         }
852         goto out;
853 }
854
855 void rpc_release_task(struct rpc_task *task)
856 {
857         const struct rpc_call_ops *tk_ops = task->tk_ops;
858         void *calldata = task->tk_calldata;
859
860 #ifdef RPC_DEBUG
861         BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
862 #endif
863         if (!atomic_dec_and_test(&task->tk_count))
864                 return;
865         dprintk("RPC: %4d release task\n", task->tk_pid);
866
867         /* Remove from global task list */
868         spin_lock(&rpc_sched_lock);
869         list_del(&task->tk_task);
870         spin_unlock(&rpc_sched_lock);
871
872         BUG_ON (RPC_IS_QUEUED(task));
873
874         /* Synchronously delete any running timer */
875         rpc_delete_timer(task);
876
877         /* Release resources */
878         if (task->tk_rqstp)
879                 xprt_release(task);
880         if (task->tk_msg.rpc_cred)
881                 rpcauth_unbindcred(task);
882         if (task->tk_client) {
883                 rpc_release_client(task->tk_client);
884                 task->tk_client = NULL;
885         }
886
887 #ifdef RPC_DEBUG
888         task->tk_magic = 0;
889 #endif
890         if (task->tk_flags & RPC_TASK_DYNAMIC)
891                 rpc_free_task(task);
892         if (tk_ops->rpc_release)
893                 tk_ops->rpc_release(calldata);
894 }
895
896 /**
897  * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
898  * @clnt: pointer to RPC client
899  * @flags: RPC flags
900  * @ops: RPC call ops
901  * @data: user call data
902  */
903 struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
904                                         const struct rpc_call_ops *ops,
905                                         void *data)
906 {
907         struct rpc_task *task;
908         task = rpc_new_task(clnt, flags, ops, data);
909         if (task == NULL) {
910                 if (ops->rpc_release != NULL)
911                         ops->rpc_release(data);
912                 return ERR_PTR(-ENOMEM);
913         }
914         atomic_inc(&task->tk_count);
915         rpc_execute(task);
916         return task;
917 }
918 EXPORT_SYMBOL(rpc_run_task);
919
920 /*
921  * Kill all tasks for the given client.
922  * XXX: kill their descendants as well?
923  */
924 void rpc_killall_tasks(struct rpc_clnt *clnt)
925 {
926         struct rpc_task *rovr;
927         struct list_head *le;
928
929         dprintk("RPC:      killing all tasks for client %p\n", clnt);
930
931         /*
932          * Spin lock all_tasks to prevent changes...
933          */
934         spin_lock(&rpc_sched_lock);
935         alltask_for_each(rovr, le, &all_tasks) {
936                 if (! RPC_IS_ACTIVATED(rovr))
937                         continue;
938                 if (!clnt || rovr->tk_client == clnt) {
939                         rovr->tk_flags |= RPC_TASK_KILLED;
940                         rpc_exit(rovr, -EIO);
941                         rpc_wake_up_task(rovr);
942                 }
943         }
944         spin_unlock(&rpc_sched_lock);
945 }
946
947 static DECLARE_MUTEX_LOCKED(rpciod_running);
948
949 static void rpciod_killall(void)
950 {
951         unsigned long flags;
952
953         while (!list_empty(&all_tasks)) {
954                 clear_thread_flag(TIF_SIGPENDING);
955                 rpc_killall_tasks(NULL);
956                 flush_workqueue(rpciod_workqueue);
957                 if (!list_empty(&all_tasks)) {
958                         dprintk("rpciod_killall: waiting for tasks to exit\n");
959                         yield();
960                 }
961         }
962
963         spin_lock_irqsave(&current->sighand->siglock, flags);
964         recalc_sigpending();
965         spin_unlock_irqrestore(&current->sighand->siglock, flags);
966 }
967
968 /*
969  * Start up the rpciod process if it's not already running.
970  */
971 int
972 rpciod_up(void)
973 {
974         struct workqueue_struct *wq;
975         int error = 0;
976
977         mutex_lock(&rpciod_mutex);
978         dprintk("rpciod_up: users %d\n", rpciod_users);
979         rpciod_users++;
980         if (rpciod_workqueue)
981                 goto out;
982         /*
983          * If there's no pid, we should be the first user.
984          */
985         if (rpciod_users > 1)
986                 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
987         /*
988          * Create the rpciod thread and wait for it to start.
989          */
990         error = -ENOMEM;
991         wq = create_workqueue("rpciod");
992         if (wq == NULL) {
993                 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
994                 rpciod_users--;
995                 goto out;
996         }
997         rpciod_workqueue = wq;
998         error = 0;
999 out:
1000         mutex_unlock(&rpciod_mutex);
1001         return error;
1002 }
1003
1004 void
1005 rpciod_down(void)
1006 {
1007         mutex_lock(&rpciod_mutex);
1008         dprintk("rpciod_down sema %d\n", rpciod_users);
1009         if (rpciod_users) {
1010                 if (--rpciod_users)
1011                         goto out;
1012         } else
1013                 printk(KERN_WARNING "rpciod_down: no users??\n");
1014
1015         if (!rpciod_workqueue) {
1016                 dprintk("rpciod_down: Nothing to do!\n");
1017                 goto out;
1018         }
1019         rpciod_killall();
1020
1021         destroy_workqueue(rpciod_workqueue);
1022         rpciod_workqueue = NULL;
1023  out:
1024         mutex_unlock(&rpciod_mutex);
1025 }
1026
1027 #ifdef RPC_DEBUG
1028 void rpc_show_tasks(void)
1029 {
1030         struct list_head *le;
1031         struct rpc_task *t;
1032
1033         spin_lock(&rpc_sched_lock);
1034         if (list_empty(&all_tasks)) {
1035                 spin_unlock(&rpc_sched_lock);
1036                 return;
1037         }
1038         printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
1039                 "-rpcwait -action- ---ops--\n");
1040         alltask_for_each(t, le, &all_tasks) {
1041                 const char *rpc_waitq = "none";
1042
1043                 if (RPC_IS_QUEUED(t))
1044                         rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1045
1046                 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1047                         t->tk_pid,
1048                         (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1049                         t->tk_flags, t->tk_status,
1050                         t->tk_client,
1051                         (t->tk_client ? t->tk_client->cl_prog : 0),
1052                         t->tk_rqstp, t->tk_timeout,
1053                         rpc_waitq,
1054                         t->tk_action, t->tk_ops);
1055         }
1056         spin_unlock(&rpc_sched_lock);
1057 }
1058 #endif
1059
1060 void
1061 rpc_destroy_mempool(void)
1062 {
1063         if (rpc_buffer_mempool)
1064                 mempool_destroy(rpc_buffer_mempool);
1065         if (rpc_task_mempool)
1066                 mempool_destroy(rpc_task_mempool);
1067         if (rpc_task_slabp && kmem_cache_destroy(rpc_task_slabp))
1068                 printk(KERN_INFO "rpc_task: not all structures were freed\n");
1069         if (rpc_buffer_slabp && kmem_cache_destroy(rpc_buffer_slabp))
1070                 printk(KERN_INFO "rpc_buffers: not all structures were freed\n");
1071 }
1072
1073 int
1074 rpc_init_mempool(void)
1075 {
1076         rpc_task_slabp = kmem_cache_create("rpc_tasks",
1077                                              sizeof(struct rpc_task),
1078                                              0, SLAB_HWCACHE_ALIGN,
1079                                              NULL, NULL);
1080         if (!rpc_task_slabp)
1081                 goto err_nomem;
1082         rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1083                                              RPC_BUFFER_MAXSIZE,
1084                                              0, SLAB_HWCACHE_ALIGN,
1085                                              NULL, NULL);
1086         if (!rpc_buffer_slabp)
1087                 goto err_nomem;
1088         rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1089                                                     rpc_task_slabp);
1090         if (!rpc_task_mempool)
1091                 goto err_nomem;
1092         rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1093                                                       rpc_buffer_slabp);
1094         if (!rpc_buffer_mempool)
1095                 goto err_nomem;
1096         return 0;
1097 err_nomem:
1098         rpc_destroy_mempool();
1099         return -ENOMEM;
1100 }