blob: 13ab0c6fed01981a831386728e45f0fce30b1fc4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080021#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#ifdef RPC_DEBUG
26#define RPCDBG_FACILITY RPCDBG_SCHED
27#define RPC_TASK_MAGIC_ID 0xf00baa
28static 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)
Christoph Lametere18b8902006-12-06 20:33:20 -080037static struct kmem_cache *rpc_task_slabp __read_mostly;
38static struct kmem_cache *rpc_buffer_slabp __read_mostly;
Eric Dumazetba899662005-08-26 12:05:31 -070039static mempool_t *rpc_task_mempool __read_mostly;
40static mempool_t *rpc_buffer_mempool __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42static void __rpc_default_timer(struct rpc_task *task);
43static void rpciod_killall(void);
David Howells65f27f32006-11-22 14:55:48 +000044static void rpc_async_schedule(struct work_struct *);
Trond Myklebustbde8f002007-01-24 11:54:53 -080045static void rpc_release_task(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 * RPC tasks sit here while waiting for conditions to improve.
49 */
50static RPC_WAITQ(delay_queue, "delayq");
51
52/*
53 * All RPC tasks are linked into this list
54 */
55static LIST_HEAD(all_tasks);
56
57/*
58 * rpciod-related stuff
59 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080060static DEFINE_MUTEX(rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061static unsigned int rpciod_users;
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050062struct workqueue_struct *rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Spinlock for other critical sections of code.
66 */
67static 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 */
74static 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 */
89static 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 */
107static 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 */
128static void
129rpc_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 */
142static 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 */
168static 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;
Chuck Levere19b63d2006-03-20 13:44:15 -0500179 queue->qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 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 */
189static 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 */
205static 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);
Chuck Levere19b63d2006-03-20 13:44:15 -0500214 queue->qlen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 dprintk("RPC: %4d removed from queue %p \"%s\"\n",
216 task->tk_pid, queue, rpc_qname(queue));
217}
218
219static 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
225static 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
231static 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
237static 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
251void 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
256void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
257{
258 __rpc_init_priority_wait_queue(queue, qname, 0);
259}
260EXPORT_SYMBOL(rpc_init_wait_queue);
261
Trond Myklebust44c28872006-01-03 09:55:06 +0100262static int rpc_wait_bit_interruptible(void *word)
263{
264 if (signal_pending(current))
265 return -ERESTARTSYS;
266 schedule();
267 return 0;
268}
269
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500270static void rpc_set_active(struct rpc_task *task)
271{
272 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
273 return;
274 spin_lock(&rpc_sched_lock);
275#ifdef RPC_DEBUG
276 task->tk_magic = RPC_TASK_MAGIC_ID;
277 task->tk_pid = rpc_task_id++;
278#endif
279 /* Add to global list of all tasks */
280 list_add_tail(&task->tk_task, &all_tasks);
281 spin_unlock(&rpc_sched_lock);
282}
283
Trond Myklebust44c28872006-01-03 09:55:06 +0100284/*
285 * Mark an RPC call as having completed by clearing the 'active' bit
286 */
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500287static void rpc_mark_complete_task(struct rpc_task *task)
Trond Myklebust44c28872006-01-03 09:55:06 +0100288{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500289 smp_mb__before_clear_bit();
290 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
291 smp_mb__after_clear_bit();
Trond Myklebust44c28872006-01-03 09:55:06 +0100292 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
293}
294
295/*
296 * Allow callers to wait for completion of an RPC call
297 */
298int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
299{
300 if (action == NULL)
301 action = rpc_wait_bit_interruptible;
302 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
303 action, TASK_INTERRUPTIBLE);
304}
305EXPORT_SYMBOL(__rpc_wait_for_completion_task);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307/*
308 * Make an RPC task runnable.
309 *
310 * Note: If the task is ASYNC, this must be called with
311 * the spinlock held to protect the wait queue operation.
312 */
313static void rpc_make_runnable(struct rpc_task *task)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 BUG_ON(task->tk_timeout_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 rpc_clear_queued(task);
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100317 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return;
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100319 /* We might have raced */
320 if (RPC_IS_QUEUED(task)) {
321 rpc_clear_running(task);
322 return;
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 if (RPC_IS_ASYNC(task)) {
325 int status;
326
David Howells65f27f32006-11-22 14:55:48 +0000327 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 status = queue_work(task->tk_workqueue, &task->u.tk_work);
329 if (status < 0) {
330 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
331 task->tk_status = status;
332 return;
333 }
334 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000335 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * Prepare for sleeping on a wait queue.
340 * By always appending tasks to the list we ensure FIFO behavior.
341 * NB: An RPC task will only receive interrupt-driven events as long
342 * as it's on a wait queue.
343 */
344static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
345 rpc_action action, rpc_action timer)
346{
347 dprintk("RPC: %4d sleep_on(queue \"%s\" time %ld)\n", task->tk_pid,
348 rpc_qname(q), jiffies);
349
350 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
351 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
352 return;
353 }
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 __rpc_add_wait_queue(q, task);
356
357 BUG_ON(task->tk_callback != NULL);
358 task->tk_callback = action;
359 __rpc_add_timer(task, timer);
360}
361
362void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
363 rpc_action action, rpc_action timer)
364{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500365 /* Mark the task as being activated if so needed */
366 rpc_set_active(task);
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /*
369 * Protect the queue operations.
370 */
371 spin_lock_bh(&q->lock);
372 __rpc_sleep_on(q, task, action, timer);
373 spin_unlock_bh(&q->lock);
374}
375
376/**
377 * __rpc_do_wake_up_task - wake up a single rpc_task
378 * @task: task to be woken up
379 *
380 * Caller must hold queue->lock, and have cleared the task queued flag.
381 */
382static void __rpc_do_wake_up_task(struct rpc_task *task)
383{
384 dprintk("RPC: %4d __rpc_wake_up_task (now %ld)\n", task->tk_pid, jiffies);
385
386#ifdef RPC_DEBUG
387 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
388#endif
389 /* Has the task been executed yet? If not, we cannot wake it up! */
390 if (!RPC_IS_ACTIVATED(task)) {
391 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
392 return;
393 }
394
395 __rpc_disable_timer(task);
396 __rpc_remove_wait_queue(task);
397
398 rpc_make_runnable(task);
399
400 dprintk("RPC: __rpc_wake_up_task done\n");
401}
402
403/*
404 * Wake up the specified task
405 */
406static void __rpc_wake_up_task(struct rpc_task *task)
407{
408 if (rpc_start_wakeup(task)) {
409 if (RPC_IS_QUEUED(task))
410 __rpc_do_wake_up_task(task);
411 rpc_finish_wakeup(task);
412 }
413}
414
415/*
416 * Default timeout handler if none specified by user
417 */
418static void
419__rpc_default_timer(struct rpc_task *task)
420{
421 dprintk("RPC: %d timeout (default timer)\n", task->tk_pid);
422 task->tk_status = -ETIMEDOUT;
423 rpc_wake_up_task(task);
424}
425
426/*
427 * Wake up the specified task
428 */
429void rpc_wake_up_task(struct rpc_task *task)
430{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500431 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (rpc_start_wakeup(task)) {
433 if (RPC_IS_QUEUED(task)) {
434 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
435
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500436 /* Note: we're already in a bh-safe context */
437 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 __rpc_do_wake_up_task(task);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500439 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441 rpc_finish_wakeup(task);
442 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500443 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * Wake up the next task on a priority queue.
448 */
449static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
450{
451 struct list_head *q;
452 struct rpc_task *task;
453
454 /*
455 * Service a batch of tasks from a single cookie.
456 */
457 q = &queue->tasks[queue->priority];
458 if (!list_empty(q)) {
459 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
460 if (queue->cookie == task->tk_cookie) {
461 if (--queue->nr)
462 goto out;
463 list_move_tail(&task->u.tk_wait.list, q);
464 }
465 /*
466 * Check if we need to switch queues.
467 */
468 if (--queue->count)
469 goto new_cookie;
470 }
471
472 /*
473 * Service the next queue.
474 */
475 do {
476 if (q == &queue->tasks[0])
477 q = &queue->tasks[queue->maxpriority];
478 else
479 q = q - 1;
480 if (!list_empty(q)) {
481 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
482 goto new_queue;
483 }
484 } while (q != &queue->tasks[queue->priority]);
485
486 rpc_reset_waitqueue_priority(queue);
487 return NULL;
488
489new_queue:
490 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
491new_cookie:
492 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
493out:
494 __rpc_wake_up_task(task);
495 return task;
496}
497
498/*
499 * Wake up the next task on the wait queue.
500 */
501struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
502{
503 struct rpc_task *task = NULL;
504
505 dprintk("RPC: wake_up_next(%p \"%s\")\n", queue, rpc_qname(queue));
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500506 rcu_read_lock_bh();
507 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (RPC_IS_PRIORITY(queue))
509 task = __rpc_wake_up_next_priority(queue);
510 else {
511 task_for_first(task, &queue->tasks[0])
512 __rpc_wake_up_task(task);
513 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500514 spin_unlock(&queue->lock);
515 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
517 return task;
518}
519
520/**
521 * rpc_wake_up - wake up all rpc_tasks
522 * @queue: rpc_wait_queue on which the tasks are sleeping
523 *
524 * Grabs queue->lock
525 */
526void rpc_wake_up(struct rpc_wait_queue *queue)
527{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800528 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800530
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500531 rcu_read_lock_bh();
532 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 head = &queue->tasks[queue->maxpriority];
534 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800535 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 __rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (head == &queue->tasks[0])
538 break;
539 head--;
540 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500541 spin_unlock(&queue->lock);
542 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
545/**
546 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
547 * @queue: rpc_wait_queue on which the tasks are sleeping
548 * @status: status value to set
549 *
550 * Grabs queue->lock
551 */
552void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
553{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800554 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500557 rcu_read_lock_bh();
558 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 head = &queue->tasks[queue->maxpriority];
560 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800561 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 task->tk_status = status;
563 __rpc_wake_up_task(task);
564 }
565 if (head == &queue->tasks[0])
566 break;
567 head--;
568 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500569 spin_unlock(&queue->lock);
570 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Trond Myklebust80147932006-08-31 18:24:08 -0400573static void __rpc_atrun(struct rpc_task *task)
574{
575 rpc_wake_up_task(task);
576}
577
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578/*
579 * Run a task at a later time
580 */
Trond Myklebust80147932006-08-31 18:24:08 -0400581void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 task->tk_timeout = delay;
584 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
585}
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100588 * Helper to call task->tk_ops->rpc_call_prepare
589 */
590static void rpc_prepare_task(struct rpc_task *task)
591{
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400592 lock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100593 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400594 unlock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100595}
596
597/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100598 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000599 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100600void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000601{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100602 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100603 if (task->tk_ops->rpc_call_done != NULL) {
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400604 lock_kernel();
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100605 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400606 unlock_kernel();
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000607 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100608 WARN_ON(RPC_ASSASSINATED(task));
609 /* Always release the RPC slot and buffer memory */
610 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000611 }
612 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000613}
Trond Myklebustabbcf282006-01-03 09:55:03 +0100614EXPORT_SYMBOL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000615
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400616void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
617{
618 if (ops->rpc_release != NULL) {
619 lock_kernel();
620 ops->rpc_release(calldata);
621 unlock_kernel();
622 }
623}
624
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000625/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * This is the RPC `scheduler' (or rather, the finite state machine).
627 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800628static void __rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 int status = 0;
631
632 dprintk("RPC: %4d rpc_execute flgs %x\n",
633 task->tk_pid, task->tk_flags);
634
635 BUG_ON(RPC_IS_QUEUED(task));
636
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000637 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 /*
639 * Garbage collection of pending timers...
640 */
641 rpc_delete_timer(task);
642
643 /*
644 * Execute any pending callback.
645 */
646 if (RPC_DO_CALLBACK(task)) {
647 /* Define a callback save pointer */
648 void (*save_callback)(struct rpc_task *);
649
650 /*
651 * If a callback exists, save it, reset it,
652 * call it.
653 * The save is needed to stop from resetting
654 * another callback set within the callback handler
655 * - Dave
656 */
657 save_callback=task->tk_callback;
658 task->tk_callback=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 save_callback(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 }
661
662 /*
663 * Perform the next FSM step.
664 * tk_action may be NULL when the task has been killed
665 * by someone else.
666 */
667 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100668 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100670 task->tk_action(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 /*
674 * Lockless check for whether task is sleeping or not.
675 */
676 if (!RPC_IS_QUEUED(task))
677 continue;
678 rpc_clear_running(task);
679 if (RPC_IS_ASYNC(task)) {
680 /* Careful! we may have raced... */
681 if (RPC_IS_QUEUED(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800682 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (rpc_test_and_set_running(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800684 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 continue;
686 }
687
688 /* sync task: sleep here */
689 dprintk("RPC: %4d sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000690 /* Note: Caller should be using rpc_clnt_sigmask() */
691 status = out_of_line_wait_on_bit(&task->tk_runstate,
692 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
693 TASK_INTERRUPTIBLE);
694 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * When a sync task receives a signal, it exits with
697 * -ERESTARTSYS. In order to catch any callbacks that
698 * clean up after sleeping on some queue, we don't
699 * break the loop here, but go around once more.
700 */
Trond Myklebust96651ab2005-06-22 17:16:21 +0000701 dprintk("RPC: %4d got signal\n", task->tk_pid);
702 task->tk_flags |= RPC_TASK_KILLED;
703 rpc_exit(task, -ERESTARTSYS);
704 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706 rpc_set_running(task);
707 dprintk("RPC: %4d sync task resuming\n", task->tk_pid);
708 }
709
Trond Myklebuste60859a2006-01-03 09:55:10 +0100710 dprintk("RPC: %4d, return %d, status %d\n", task->tk_pid, status, task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Release all resources associated with the task */
712 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715/*
716 * User-visible entry point to the scheduler.
717 *
718 * This may be called recursively if e.g. an async NFS task updates
719 * the attributes and finds that dirty pages must be flushed.
720 * NOTE: Upon exit of this function the task is guaranteed to be
721 * released. In particular note that tk_release() will have
722 * been called, so your task memory may have been freed.
723 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800724void rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
Trond Myklebust44c28872006-01-03 09:55:06 +0100726 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 rpc_set_running(task);
Trond Myklebust2efef832007-02-03 13:38:41 -0800728 __rpc_execute(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
David Howells65f27f32006-11-22 14:55:48 +0000731static void rpc_async_schedule(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
David Howells65f27f32006-11-22 14:55:48 +0000733 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Chuck Lever02107142006-01-03 09:55:49 +0100736/**
737 * rpc_malloc - allocate an RPC buffer
738 * @task: RPC task that will use this buffer
739 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 *
741 * We try to ensure that some NFS reads and writes can always proceed
742 * by using a mempool when allocating 'small' buffers.
743 * In order to avoid memory starvation triggering more writebacks of
744 * NFS requests, we use GFP_NOFS rather than GFP_KERNEL.
745 */
Chuck Lever02107142006-01-03 09:55:49 +0100746void * rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Chuck Lever02107142006-01-03 09:55:49 +0100748 struct rpc_rqst *req = task->tk_rqstp;
Al Virodd0fc662005-10-07 07:46:04 +0100749 gfp_t gfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (task->tk_flags & RPC_TASK_SWAPPER)
752 gfp = GFP_ATOMIC;
753 else
754 gfp = GFP_NOFS;
755
756 if (size > RPC_BUFFER_MAXSIZE) {
Chuck Lever02107142006-01-03 09:55:49 +0100757 req->rq_buffer = kmalloc(size, gfp);
758 if (req->rq_buffer)
759 req->rq_bufsize = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } else {
Chuck Lever02107142006-01-03 09:55:49 +0100761 req->rq_buffer = mempool_alloc(rpc_buffer_mempool, gfp);
762 if (req->rq_buffer)
763 req->rq_bufsize = RPC_BUFFER_MAXSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Chuck Lever02107142006-01-03 09:55:49 +0100765 return req->rq_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Chuck Lever02107142006-01-03 09:55:49 +0100768/**
769 * rpc_free - free buffer allocated via rpc_malloc
770 * @task: RPC task with a buffer to be freed
771 *
772 */
773void rpc_free(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Chuck Lever02107142006-01-03 09:55:49 +0100775 struct rpc_rqst *req = task->tk_rqstp;
776
777 if (req->rq_buffer) {
778 if (req->rq_bufsize == RPC_BUFFER_MAXSIZE)
779 mempool_free(req->rq_buffer, rpc_buffer_mempool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 else
Chuck Lever02107142006-01-03 09:55:49 +0100781 kfree(req->rq_buffer);
782 req->rq_buffer = NULL;
783 req->rq_bufsize = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
785}
786
787/*
788 * Creation and deletion of RPC task structures
789 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100790void rpc_init_task(struct rpc_task *task, struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
792 memset(task, 0, sizeof(*task));
793 init_timer(&task->tk_timer);
794 task->tk_timer.data = (unsigned long) task;
795 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
Trond Myklebust44c28872006-01-03 09:55:06 +0100796 atomic_set(&task->tk_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 task->tk_client = clnt;
798 task->tk_flags = flags;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100799 task->tk_ops = tk_ops;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100800 if (tk_ops->rpc_call_prepare != NULL)
801 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100802 task->tk_calldata = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804 /* Initialize retry counters */
805 task->tk_garb_retry = 2;
806 task->tk_cred_retry = 2;
807
808 task->tk_priority = RPC_PRIORITY_NORMAL;
809 task->tk_cookie = (unsigned long)current;
810
811 /* Initialize workqueue for async tasks */
812 task->tk_workqueue = rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (clnt) {
815 atomic_inc(&clnt->cl_users);
816 if (clnt->cl_softrtry)
817 task->tk_flags |= RPC_TASK_SOFT;
818 if (!clnt->cl_intr)
819 task->tk_flags |= RPC_TASK_NOINTR;
820 }
821
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100822 BUG_ON(task->tk_ops == NULL);
823
Chuck Leveref759a22006-03-20 13:44:17 -0500824 /* starting timestamp */
825 task->tk_start = jiffies;
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dprintk("RPC: %4d new task procpid %d\n", task->tk_pid,
828 current->pid);
829}
830
831static struct rpc_task *
832rpc_alloc_task(void)
833{
834 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
835}
836
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500837static void rpc_free_task(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500839 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 dprintk("RPC: %4d freeing task\n", task->tk_pid);
841 mempool_free(task, rpc_task_mempool);
842}
843
844/*
845 * Create a new task for the specified client. We have to
846 * clean up after an allocation failure, as the client may
847 * have specified "oneshot".
848 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100849struct rpc_task *rpc_new_task(struct rpc_clnt *clnt, int flags, const struct rpc_call_ops *tk_ops, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
851 struct rpc_task *task;
852
853 task = rpc_alloc_task();
854 if (!task)
855 goto cleanup;
856
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100857 rpc_init_task(task, clnt, flags, tk_ops, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 dprintk("RPC: %4d allocated task\n", task->tk_pid);
860 task->tk_flags |= RPC_TASK_DYNAMIC;
861out:
862 return task;
863
864cleanup:
865 /* Check whether to release the client */
866 if (clnt) {
867 printk("rpc_new_task: failed, users=%d, oneshot=%d\n",
868 atomic_read(&clnt->cl_users), clnt->cl_oneshot);
869 atomic_inc(&clnt->cl_users); /* pretend we were used ... */
870 rpc_release_client(clnt);
871 }
872 goto out;
873}
874
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500875
876void rpc_put_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100878 const struct rpc_call_ops *tk_ops = task->tk_ops;
879 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500881 if (!atomic_dec_and_test(&task->tk_count))
882 return;
883 /* Release resources */
884 if (task->tk_rqstp)
885 xprt_release(task);
886 if (task->tk_msg.rpc_cred)
887 rpcauth_unbindcred(task);
888 if (task->tk_client) {
889 rpc_release_client(task->tk_client);
890 task->tk_client = NULL;
891 }
892 if (task->tk_flags & RPC_TASK_DYNAMIC)
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500893 call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400894 rpc_release_calldata(tk_ops, calldata);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500895}
896EXPORT_SYMBOL(rpc_put_task);
897
Trond Myklebustbde8f002007-01-24 11:54:53 -0800898static void rpc_release_task(struct rpc_task *task)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500899{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900#ifdef RPC_DEBUG
901 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
902#endif
Trond Myklebust44c28872006-01-03 09:55:06 +0100903 dprintk("RPC: %4d release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 /* Remove from global task list */
906 spin_lock(&rpc_sched_lock);
907 list_del(&task->tk_task);
908 spin_unlock(&rpc_sched_lock);
909
910 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Synchronously delete any running timer */
913 rpc_delete_timer(task);
914
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915#ifdef RPC_DEBUG
916 task->tk_magic = 0;
917#endif
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500918 /* Wake up anyone who is waiting for task completion */
919 rpc_mark_complete_task(task);
920
921 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922}
923
924/**
Trond Myklebust44c28872006-01-03 09:55:06 +0100925 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
Martin Waitz99acf042006-02-01 03:06:56 -0800926 * @clnt: pointer to RPC client
927 * @flags: RPC flags
928 * @ops: RPC call ops
929 * @data: user call data
Trond Myklebust44c28872006-01-03 09:55:06 +0100930 */
931struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
932 const struct rpc_call_ops *ops,
933 void *data)
934{
935 struct rpc_task *task;
936 task = rpc_new_task(clnt, flags, ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500937 if (task == NULL) {
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400938 rpc_release_calldata(ops, data);
Trond Myklebust44c28872006-01-03 09:55:06 +0100939 return ERR_PTR(-ENOMEM);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500940 }
Trond Myklebust44c28872006-01-03 09:55:06 +0100941 atomic_inc(&task->tk_count);
942 rpc_execute(task);
943 return task;
944}
945EXPORT_SYMBOL(rpc_run_task);
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947/*
948 * Kill all tasks for the given client.
949 * XXX: kill their descendants as well?
950 */
951void rpc_killall_tasks(struct rpc_clnt *clnt)
952{
953 struct rpc_task *rovr;
954 struct list_head *le;
955
956 dprintk("RPC: killing all tasks for client %p\n", clnt);
957
958 /*
959 * Spin lock all_tasks to prevent changes...
960 */
961 spin_lock(&rpc_sched_lock);
962 alltask_for_each(rovr, le, &all_tasks) {
963 if (! RPC_IS_ACTIVATED(rovr))
964 continue;
965 if (!clnt || rovr->tk_client == clnt) {
966 rovr->tk_flags |= RPC_TASK_KILLED;
967 rpc_exit(rovr, -EIO);
968 rpc_wake_up_task(rovr);
969 }
970 }
971 spin_unlock(&rpc_sched_lock);
972}
973
974static DECLARE_MUTEX_LOCKED(rpciod_running);
975
976static void rpciod_killall(void)
977{
978 unsigned long flags;
979
980 while (!list_empty(&all_tasks)) {
981 clear_thread_flag(TIF_SIGPENDING);
982 rpc_killall_tasks(NULL);
983 flush_workqueue(rpciod_workqueue);
984 if (!list_empty(&all_tasks)) {
985 dprintk("rpciod_killall: waiting for tasks to exit\n");
986 yield();
987 }
988 }
989
990 spin_lock_irqsave(&current->sighand->siglock, flags);
991 recalc_sigpending();
992 spin_unlock_irqrestore(&current->sighand->siglock, flags);
993}
994
995/*
996 * Start up the rpciod process if it's not already running.
997 */
998int
999rpciod_up(void)
1000{
1001 struct workqueue_struct *wq;
1002 int error = 0;
1003
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001004 mutex_lock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 dprintk("rpciod_up: users %d\n", rpciod_users);
1006 rpciod_users++;
1007 if (rpciod_workqueue)
1008 goto out;
1009 /*
1010 * If there's no pid, we should be the first user.
1011 */
1012 if (rpciod_users > 1)
1013 printk(KERN_WARNING "rpciod_up: no workqueue, %d users??\n", rpciod_users);
1014 /*
1015 * Create the rpciod thread and wait for it to start.
1016 */
1017 error = -ENOMEM;
1018 wq = create_workqueue("rpciod");
1019 if (wq == NULL) {
1020 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
1021 rpciod_users--;
1022 goto out;
1023 }
1024 rpciod_workqueue = wq;
1025 error = 0;
1026out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001027 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return error;
1029}
1030
1031void
1032rpciod_down(void)
1033{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001034 mutex_lock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dprintk("rpciod_down sema %d\n", rpciod_users);
1036 if (rpciod_users) {
1037 if (--rpciod_users)
1038 goto out;
1039 } else
1040 printk(KERN_WARNING "rpciod_down: no users??\n");
1041
1042 if (!rpciod_workqueue) {
1043 dprintk("rpciod_down: Nothing to do!\n");
1044 goto out;
1045 }
1046 rpciod_killall();
1047
1048 destroy_workqueue(rpciod_workqueue);
1049 rpciod_workqueue = NULL;
1050 out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001051 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054#ifdef RPC_DEBUG
1055void rpc_show_tasks(void)
1056{
1057 struct list_head *le;
1058 struct rpc_task *t;
1059
1060 spin_lock(&rpc_sched_lock);
1061 if (list_empty(&all_tasks)) {
1062 spin_unlock(&rpc_sched_lock);
1063 return;
1064 }
1065 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001066 "-rpcwait -action- ---ops--\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 alltask_for_each(t, le, &all_tasks) {
1068 const char *rpc_waitq = "none";
1069
1070 if (RPC_IS_QUEUED(t))
1071 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
1072
1073 printk("%05d %04d %04x %06d %8p %6d %8p %08ld %8s %8p %8p\n",
1074 t->tk_pid,
1075 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1076 t->tk_flags, t->tk_status,
1077 t->tk_client,
1078 (t->tk_client ? t->tk_client->cl_prog : 0),
1079 t->tk_rqstp, t->tk_timeout,
1080 rpc_waitq,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001081 t->tk_action, t->tk_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 spin_unlock(&rpc_sched_lock);
1084}
1085#endif
1086
1087void
1088rpc_destroy_mempool(void)
1089{
1090 if (rpc_buffer_mempool)
1091 mempool_destroy(rpc_buffer_mempool);
1092 if (rpc_task_mempool)
1093 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001094 if (rpc_task_slabp)
1095 kmem_cache_destroy(rpc_task_slabp);
1096 if (rpc_buffer_slabp)
1097 kmem_cache_destroy(rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100int
1101rpc_init_mempool(void)
1102{
1103 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1104 sizeof(struct rpc_task),
1105 0, SLAB_HWCACHE_ALIGN,
1106 NULL, NULL);
1107 if (!rpc_task_slabp)
1108 goto err_nomem;
1109 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1110 RPC_BUFFER_MAXSIZE,
1111 0, SLAB_HWCACHE_ALIGN,
1112 NULL, NULL);
1113 if (!rpc_buffer_slabp)
1114 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001115 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1116 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (!rpc_task_mempool)
1118 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001119 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1120 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 if (!rpc_buffer_mempool)
1122 goto err_nomem;
1123 return 0;
1124err_nomem:
1125 rpc_destroy_mempool();
1126 return -ENOMEM;
1127}