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