blob: 05825154ddd903ac86de6aaeb2f1c8e5a7ea887f [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>
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -08007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#endif
29
30/*
31 * RPC slabs and memory pools
32 */
33#define RPC_BUFFER_MAXSIZE (2048)
34#define RPC_BUFFER_POOLSIZE (8)
35#define RPC_TASK_POOLSIZE (8)
Christoph Lametere18b8902006-12-06 20:33:20 -080036static struct kmem_cache *rpc_task_slabp __read_mostly;
37static struct kmem_cache *rpc_buffer_slabp __read_mostly;
Eric Dumazetba899662005-08-26 12:05:31 -070038static mempool_t *rpc_task_mempool __read_mostly;
39static mempool_t *rpc_buffer_mempool __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static void __rpc_default_timer(struct rpc_task *task);
David Howells65f27f32006-11-22 14:55:48 +000042static void rpc_async_schedule(struct work_struct *);
Trond Myklebustbde8f002007-01-24 11:54:53 -080043static void rpc_release_task(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * RPC tasks sit here while waiting for conditions to improve.
47 */
48static RPC_WAITQ(delay_queue, "delayq");
49
50/*
Trond Myklebust6529eba2007-06-14 16:40:14 -040051 * All RPC clients are linked into this list
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Trond Myklebust6529eba2007-06-14 16:40:14 -040053static LIST_HEAD(all_clients);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55/*
56 * rpciod-related stuff
57 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080058static DEFINE_MUTEX(rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -040059static atomic_t rpciod_users = ATOMIC_INIT(0);
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050060struct workqueue_struct *rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Spinlock for other critical sections of code.
64 */
65static DEFINE_SPINLOCK(rpc_sched_lock);
66
67/*
68 * Disable the timer for a given RPC task. Should be called with
69 * queue->lock and bh_disabled in order to avoid races within
70 * rpc_run_timer().
71 */
72static inline void
73__rpc_disable_timer(struct rpc_task *task)
74{
Chuck Lever46121cf2007-01-31 12:14:08 -050075 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 task->tk_timeout_fn = NULL;
77 task->tk_timeout = 0;
78}
79
80/*
81 * Run a timeout function.
82 * We use the callback in order to allow __rpc_wake_up_task()
83 * and friends to disable the timer synchronously on SMP systems
84 * without calling del_timer_sync(). The latter could cause a
85 * deadlock if called while we're holding spinlocks...
86 */
87static void rpc_run_timer(struct rpc_task *task)
88{
89 void (*callback)(struct rpc_task *);
90
91 callback = task->tk_timeout_fn;
92 task->tk_timeout_fn = NULL;
93 if (callback && RPC_IS_QUEUED(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -050094 dprintk("RPC: %5u running timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 callback(task);
96 }
97 smp_mb__before_clear_bit();
98 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
99 smp_mb__after_clear_bit();
100}
101
102/*
103 * Set up a timer for the current task.
104 */
105static inline void
106__rpc_add_timer(struct rpc_task *task, rpc_action timer)
107{
108 if (!task->tk_timeout)
109 return;
110
Chuck Lever46121cf2007-01-31 12:14:08 -0500111 dprintk("RPC: %5u setting alarm for %lu ms\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 task->tk_pid, task->tk_timeout * 1000 / HZ);
113
114 if (timer)
115 task->tk_timeout_fn = timer;
116 else
117 task->tk_timeout_fn = __rpc_default_timer;
118 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
119 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
120}
121
122/*
123 * Delete any timer for the current task. Because we use del_timer_sync(),
124 * this function should never be called while holding queue->lock.
125 */
126static void
127rpc_delete_timer(struct rpc_task *task)
128{
129 if (RPC_IS_QUEUED(task))
130 return;
131 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
132 del_singleshot_timer_sync(&task->tk_timer);
Chuck Lever46121cf2007-01-31 12:14:08 -0500133 dprintk("RPC: %5u deleting timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135}
136
137/*
138 * Add new request to a priority queue.
139 */
140static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
141{
142 struct list_head *q;
143 struct rpc_task *t;
144
145 INIT_LIST_HEAD(&task->u.tk_wait.links);
146 q = &queue->tasks[task->tk_priority];
147 if (unlikely(task->tk_priority > queue->maxpriority))
148 q = &queue->tasks[queue->maxpriority];
149 list_for_each_entry(t, q, u.tk_wait.list) {
150 if (t->tk_cookie == task->tk_cookie) {
151 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
152 return;
153 }
154 }
155 list_add_tail(&task->u.tk_wait.list, q);
156}
157
158/*
159 * Add new request to wait queue.
160 *
161 * Swapper tasks always get inserted at the head of the queue.
162 * This should avoid many nasty memory deadlocks and hopefully
163 * improve overall performance.
164 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
165 */
166static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
167{
168 BUG_ON (RPC_IS_QUEUED(task));
169
170 if (RPC_IS_PRIORITY(queue))
171 __rpc_add_wait_queue_priority(queue, task);
172 else if (RPC_IS_SWAPPER(task))
173 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
174 else
175 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
176 task->u.tk_wait.rpc_waitq = queue;
Chuck Levere19b63d2006-03-20 13:44:15 -0500177 queue->qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 rpc_set_queued(task);
179
Chuck Lever46121cf2007-01-31 12:14:08 -0500180 dprintk("RPC: %5u added to queue %p \"%s\"\n",
181 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
185 * Remove request from a priority queue.
186 */
187static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
188{
189 struct rpc_task *t;
190
191 if (!list_empty(&task->u.tk_wait.links)) {
192 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
193 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
194 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
195 }
196 list_del(&task->u.tk_wait.list);
197}
198
199/*
200 * Remove request from queue.
201 * Note: must be called with spin lock held.
202 */
203static void __rpc_remove_wait_queue(struct rpc_task *task)
204{
205 struct rpc_wait_queue *queue;
206 queue = task->u.tk_wait.rpc_waitq;
207
208 if (RPC_IS_PRIORITY(queue))
209 __rpc_remove_wait_queue_priority(task);
210 else
211 list_del(&task->u.tk_wait.list);
Chuck Levere19b63d2006-03-20 13:44:15 -0500212 queue->qlen--;
Chuck Lever46121cf2007-01-31 12:14:08 -0500213 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
214 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
218{
219 queue->priority = priority;
220 queue->count = 1 << (priority * 2);
221}
222
223static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
224{
225 queue->cookie = cookie;
226 queue->nr = RPC_BATCH_COUNT;
227}
228
229static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
230{
231 rpc_set_waitqueue_priority(queue, queue->maxpriority);
232 rpc_set_waitqueue_cookie(queue, 0);
233}
234
235static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
236{
237 int i;
238
239 spin_lock_init(&queue->lock);
240 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
241 INIT_LIST_HEAD(&queue->tasks[i]);
242 queue->maxpriority = maxprio;
243 rpc_reset_waitqueue_priority(queue);
244#ifdef RPC_DEBUG
245 queue->name = qname;
246#endif
247}
248
249void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
250{
251 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
252}
253
254void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
255{
256 __rpc_init_priority_wait_queue(queue, qname, 0);
257}
258EXPORT_SYMBOL(rpc_init_wait_queue);
259
Trond Myklebust44c28872006-01-03 09:55:06 +0100260static int rpc_wait_bit_interruptible(void *word)
261{
262 if (signal_pending(current))
263 return -ERESTARTSYS;
264 schedule();
265 return 0;
266}
267
Trond Myklebustc44fe702007-06-16 14:17:01 -0400268#ifdef RPC_DEBUG
269static void rpc_task_set_debuginfo(struct rpc_task *task)
270{
271 static atomic_t rpc_pid;
272
273 task->tk_magic = RPC_TASK_MAGIC_ID;
274 task->tk_pid = atomic_inc_return(&rpc_pid);
275}
276#else
277static inline void rpc_task_set_debuginfo(struct rpc_task *task)
278{
279}
280#endif
281
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500282static void rpc_set_active(struct rpc_task *task)
283{
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400284 struct rpc_clnt *clnt;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500285 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
286 return;
Trond Myklebustc44fe702007-06-16 14:17:01 -0400287 rpc_task_set_debuginfo(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500288 /* Add to global list of all tasks */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400289 clnt = task->tk_client;
290 if (clnt != NULL) {
291 spin_lock(&clnt->cl_lock);
292 list_add_tail(&task->tk_task, &clnt->cl_tasks);
293 spin_unlock(&clnt->cl_lock);
294 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500295}
296
Trond Myklebust44c28872006-01-03 09:55:06 +0100297/*
298 * Mark an RPC call as having completed by clearing the 'active' bit
299 */
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500300static void rpc_mark_complete_task(struct rpc_task *task)
Trond Myklebust44c28872006-01-03 09:55:06 +0100301{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500302 smp_mb__before_clear_bit();
303 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
304 smp_mb__after_clear_bit();
Trond Myklebust44c28872006-01-03 09:55:06 +0100305 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
306}
307
308/*
309 * Allow callers to wait for completion of an RPC call
310 */
311int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
312{
313 if (action == NULL)
314 action = rpc_wait_bit_interruptible;
315 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
316 action, TASK_INTERRUPTIBLE);
317}
318EXPORT_SYMBOL(__rpc_wait_for_completion_task);
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320/*
321 * Make an RPC task runnable.
322 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800323 * Note: If the task is ASYNC, this must be called with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * the spinlock held to protect the wait queue operation.
325 */
326static void rpc_make_runnable(struct rpc_task *task)
327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 BUG_ON(task->tk_timeout_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 rpc_clear_queued(task);
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100330 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return;
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100332 /* We might have raced */
333 if (RPC_IS_QUEUED(task)) {
334 rpc_clear_running(task);
335 return;
336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (RPC_IS_ASYNC(task)) {
338 int status;
339
David Howells65f27f32006-11-22 14:55:48 +0000340 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 status = queue_work(task->tk_workqueue, &task->u.tk_work);
342 if (status < 0) {
343 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
344 task->tk_status = status;
345 return;
346 }
347 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000348 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
351/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 * Prepare for sleeping on a wait queue.
353 * By always appending tasks to the list we ensure FIFO behavior.
354 * NB: An RPC task will only receive interrupt-driven events as long
355 * as it's on a wait queue.
356 */
357static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
358 rpc_action action, rpc_action timer)
359{
Chuck Lever46121cf2007-01-31 12:14:08 -0500360 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
361 task->tk_pid, rpc_qname(q), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
364 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
365 return;
366 }
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 __rpc_add_wait_queue(q, task);
369
370 BUG_ON(task->tk_callback != NULL);
371 task->tk_callback = action;
372 __rpc_add_timer(task, timer);
373}
374
375void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
376 rpc_action action, rpc_action timer)
377{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500378 /* Mark the task as being activated if so needed */
379 rpc_set_active(task);
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 /*
382 * Protect the queue operations.
383 */
384 spin_lock_bh(&q->lock);
385 __rpc_sleep_on(q, task, action, timer);
386 spin_unlock_bh(&q->lock);
387}
388
389/**
390 * __rpc_do_wake_up_task - wake up a single rpc_task
391 * @task: task to be woken up
392 *
393 * Caller must hold queue->lock, and have cleared the task queued flag.
394 */
395static void __rpc_do_wake_up_task(struct rpc_task *task)
396{
Chuck Lever46121cf2007-01-31 12:14:08 -0500397 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
398 task->tk_pid, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400#ifdef RPC_DEBUG
401 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
402#endif
403 /* Has the task been executed yet? If not, we cannot wake it up! */
404 if (!RPC_IS_ACTIVATED(task)) {
405 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
406 return;
407 }
408
409 __rpc_disable_timer(task);
410 __rpc_remove_wait_queue(task);
411
412 rpc_make_runnable(task);
413
Chuck Lever46121cf2007-01-31 12:14:08 -0500414 dprintk("RPC: __rpc_wake_up_task done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
417/*
418 * Wake up the specified task
419 */
420static void __rpc_wake_up_task(struct rpc_task *task)
421{
422 if (rpc_start_wakeup(task)) {
423 if (RPC_IS_QUEUED(task))
424 __rpc_do_wake_up_task(task);
425 rpc_finish_wakeup(task);
426 }
427}
428
429/*
430 * Default timeout handler if none specified by user
431 */
432static void
433__rpc_default_timer(struct rpc_task *task)
434{
Chuck Lever46121cf2007-01-31 12:14:08 -0500435 dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 task->tk_status = -ETIMEDOUT;
437 rpc_wake_up_task(task);
438}
439
440/*
441 * Wake up the specified task
442 */
443void rpc_wake_up_task(struct rpc_task *task)
444{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500445 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (rpc_start_wakeup(task)) {
447 if (RPC_IS_QUEUED(task)) {
448 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
449
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500450 /* Note: we're already in a bh-safe context */
451 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 __rpc_do_wake_up_task(task);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500453 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 rpc_finish_wakeup(task);
456 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500457 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
461 * Wake up the next task on a priority queue.
462 */
463static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
464{
465 struct list_head *q;
466 struct rpc_task *task;
467
468 /*
469 * Service a batch of tasks from a single cookie.
470 */
471 q = &queue->tasks[queue->priority];
472 if (!list_empty(q)) {
473 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
474 if (queue->cookie == task->tk_cookie) {
475 if (--queue->nr)
476 goto out;
477 list_move_tail(&task->u.tk_wait.list, q);
478 }
479 /*
480 * Check if we need to switch queues.
481 */
482 if (--queue->count)
483 goto new_cookie;
484 }
485
486 /*
487 * Service the next queue.
488 */
489 do {
490 if (q == &queue->tasks[0])
491 q = &queue->tasks[queue->maxpriority];
492 else
493 q = q - 1;
494 if (!list_empty(q)) {
495 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
496 goto new_queue;
497 }
498 } while (q != &queue->tasks[queue->priority]);
499
500 rpc_reset_waitqueue_priority(queue);
501 return NULL;
502
503new_queue:
504 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
505new_cookie:
506 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
507out:
508 __rpc_wake_up_task(task);
509 return task;
510}
511
512/*
513 * Wake up the next task on the wait queue.
514 */
515struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
516{
517 struct rpc_task *task = NULL;
518
Chuck Lever46121cf2007-01-31 12:14:08 -0500519 dprintk("RPC: wake_up_next(%p \"%s\")\n",
520 queue, rpc_qname(queue));
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500521 rcu_read_lock_bh();
522 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (RPC_IS_PRIORITY(queue))
524 task = __rpc_wake_up_next_priority(queue);
525 else {
526 task_for_first(task, &queue->tasks[0])
527 __rpc_wake_up_task(task);
528 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500529 spin_unlock(&queue->lock);
530 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 return task;
533}
534
535/**
536 * rpc_wake_up - wake up all rpc_tasks
537 * @queue: rpc_wait_queue on which the tasks are sleeping
538 *
539 * Grabs queue->lock
540 */
541void rpc_wake_up(struct rpc_wait_queue *queue)
542{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800543 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800545
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500546 rcu_read_lock_bh();
547 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 head = &queue->tasks[queue->maxpriority];
549 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800550 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 __rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (head == &queue->tasks[0])
553 break;
554 head--;
555 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500556 spin_unlock(&queue->lock);
557 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560/**
561 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
562 * @queue: rpc_wait_queue on which the tasks are sleeping
563 * @status: status value to set
564 *
565 * Grabs queue->lock
566 */
567void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
568{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800569 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500572 rcu_read_lock_bh();
573 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 head = &queue->tasks[queue->maxpriority];
575 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800576 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 task->tk_status = status;
578 __rpc_wake_up_task(task);
579 }
580 if (head == &queue->tasks[0])
581 break;
582 head--;
583 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500584 spin_unlock(&queue->lock);
585 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
Trond Myklebust80147932006-08-31 18:24:08 -0400588static void __rpc_atrun(struct rpc_task *task)
589{
590 rpc_wake_up_task(task);
591}
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593/*
594 * Run a task at a later time
595 */
Trond Myklebust80147932006-08-31 18:24:08 -0400596void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597{
598 task->tk_timeout = delay;
599 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
600}
601
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100603 * Helper to call task->tk_ops->rpc_call_prepare
604 */
605static void rpc_prepare_task(struct rpc_task *task)
606{
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400607 lock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100608 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400609 unlock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100610}
611
612/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100613 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000614 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100615void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000616{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100617 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100618 if (task->tk_ops->rpc_call_done != NULL) {
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400619 lock_kernel();
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100620 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400621 unlock_kernel();
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000622 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100623 WARN_ON(RPC_ASSASSINATED(task));
624 /* Always release the RPC slot and buffer memory */
625 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000626 }
627 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000628}
Trond Myklebustabbcf282006-01-03 09:55:03 +0100629EXPORT_SYMBOL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000630
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400631void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
632{
633 if (ops->rpc_release != NULL) {
634 lock_kernel();
635 ops->rpc_release(calldata);
636 unlock_kernel();
637 }
638}
639
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000640/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * This is the RPC `scheduler' (or rather, the finite state machine).
642 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800643static void __rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 int status = 0;
646
Chuck Lever46121cf2007-01-31 12:14:08 -0500647 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
648 task->tk_pid, task->tk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 BUG_ON(RPC_IS_QUEUED(task));
651
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000652 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 /*
654 * Garbage collection of pending timers...
655 */
656 rpc_delete_timer(task);
657
658 /*
659 * Execute any pending callback.
660 */
661 if (RPC_DO_CALLBACK(task)) {
662 /* Define a callback save pointer */
663 void (*save_callback)(struct rpc_task *);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800664
665 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * If a callback exists, save it, reset it,
667 * call it.
668 * The save is needed to stop from resetting
669 * another callback set within the callback handler
670 * - Dave
671 */
672 save_callback=task->tk_callback;
673 task->tk_callback=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 save_callback(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676
677 /*
678 * Perform the next FSM step.
679 * tk_action may be NULL when the task has been killed
680 * by someone else.
681 */
682 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100683 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100685 task->tk_action(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 /*
689 * Lockless check for whether task is sleeping or not.
690 */
691 if (!RPC_IS_QUEUED(task))
692 continue;
693 rpc_clear_running(task);
694 if (RPC_IS_ASYNC(task)) {
695 /* Careful! we may have raced... */
696 if (RPC_IS_QUEUED(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 if (rpc_test_and_set_running(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 continue;
701 }
702
703 /* sync task: sleep here */
Chuck Lever46121cf2007-01-31 12:14:08 -0500704 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000705 /* Note: Caller should be using rpc_clnt_sigmask() */
706 status = out_of_line_wait_on_bit(&task->tk_runstate,
707 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
708 TASK_INTERRUPTIBLE);
709 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * When a sync task receives a signal, it exits with
712 * -ERESTARTSYS. In order to catch any callbacks that
713 * clean up after sleeping on some queue, we don't
714 * break the loop here, but go around once more.
715 */
Chuck Lever46121cf2007-01-31 12:14:08 -0500716 dprintk("RPC: %5u got signal\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000717 task->tk_flags |= RPC_TASK_KILLED;
718 rpc_exit(task, -ERESTARTSYS);
719 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
721 rpc_set_running(task);
Chuck Lever46121cf2007-01-31 12:14:08 -0500722 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724
Chuck Lever46121cf2007-01-31 12:14:08 -0500725 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
726 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /* Release all resources associated with the task */
728 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731/*
732 * User-visible entry point to the scheduler.
733 *
734 * This may be called recursively if e.g. an async NFS task updates
735 * the attributes and finds that dirty pages must be flushed.
736 * NOTE: Upon exit of this function the task is guaranteed to be
737 * released. In particular note that tk_release() will have
738 * been called, so your task memory may have been freed.
739 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800740void rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741{
Trond Myklebust44c28872006-01-03 09:55:06 +0100742 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 rpc_set_running(task);
Trond Myklebust2efef832007-02-03 13:38:41 -0800744 __rpc_execute(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
David Howells65f27f32006-11-22 14:55:48 +0000747static void rpc_async_schedule(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
David Howells65f27f32006-11-22 14:55:48 +0000749 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400752struct rpc_buffer {
753 size_t len;
754 char data[];
755};
756
Chuck Lever02107142006-01-03 09:55:49 +0100757/**
758 * rpc_malloc - allocate an RPC buffer
759 * @task: RPC task that will use this buffer
760 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 *
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400762 * To prevent rpciod from hanging, this allocator never sleeps,
763 * returning NULL if the request cannot be serviced immediately.
764 * The caller can arrange to sleep in a way that is safe for rpciod.
765 *
766 * Most requests are 'small' (under 2KiB) and can be serviced from a
767 * mempool, ensuring that NFS reads and writes can always proceed,
768 * and that there is good locality of reference for these buffers.
769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 * In order to avoid memory starvation triggering more writebacks of
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400771 * NFS requests, we avoid using GFP_KERNEL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400773void *rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400775 struct rpc_buffer *buf;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400776 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400778 size += sizeof(struct rpc_buffer);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400779 if (size <= RPC_BUFFER_MAXSIZE)
780 buf = mempool_alloc(rpc_buffer_mempool, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 else
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400782 buf = kmalloc(size, gfp);
Peter Zijlstraddce40d2007-05-09 08:30:11 +0200783
784 if (!buf)
785 return NULL;
786
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400787 buf->len = size;
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200788 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400789 task->tk_pid, size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400790 return &buf->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Chuck Lever02107142006-01-03 09:55:49 +0100793/**
794 * rpc_free - free buffer allocated via rpc_malloc
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400795 * @buffer: buffer to free
Chuck Lever02107142006-01-03 09:55:49 +0100796 *
797 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400798void rpc_free(void *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400800 size_t size;
801 struct rpc_buffer *buf;
Chuck Lever02107142006-01-03 09:55:49 +0100802
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400803 if (!buffer)
804 return;
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400805
806 buf = container_of(buffer, struct rpc_buffer, data);
807 size = buf->len;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400808
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200809 dprintk("RPC: freeing buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400810 size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400811
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400812 if (size <= RPC_BUFFER_MAXSIZE)
813 mempool_free(buf, rpc_buffer_mempool);
814 else
815 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/*
819 * Creation and deletion of RPC task structures
820 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100821void 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 -0700822{
823 memset(task, 0, sizeof(*task));
824 init_timer(&task->tk_timer);
825 task->tk_timer.data = (unsigned long) task;
826 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
Trond Myklebust44c28872006-01-03 09:55:06 +0100827 atomic_set(&task->tk_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 task->tk_client = clnt;
829 task->tk_flags = flags;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100830 task->tk_ops = tk_ops;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100831 if (tk_ops->rpc_call_prepare != NULL)
832 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100833 task->tk_calldata = calldata;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400834 INIT_LIST_HEAD(&task->tk_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 /* Initialize retry counters */
837 task->tk_garb_retry = 2;
838 task->tk_cred_retry = 2;
839
840 task->tk_priority = RPC_PRIORITY_NORMAL;
841 task->tk_cookie = (unsigned long)current;
842
843 /* Initialize workqueue for async tasks */
844 task->tk_workqueue = rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 if (clnt) {
Trond Myklebust34f52e32007-06-14 16:40:31 -0400847 kref_get(&clnt->cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (clnt->cl_softrtry)
849 task->tk_flags |= RPC_TASK_SOFT;
850 if (!clnt->cl_intr)
851 task->tk_flags |= RPC_TASK_NOINTR;
852 }
853
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100854 BUG_ON(task->tk_ops == NULL);
855
Chuck Leveref759a22006-03-20 13:44:17 -0500856 /* starting timestamp */
857 task->tk_start = jiffies;
858
Chuck Lever46121cf2007-01-31 12:14:08 -0500859 dprintk("RPC: new task initialized, procpid %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 current->pid);
861}
862
863static struct rpc_task *
864rpc_alloc_task(void)
865{
866 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
867}
868
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500869static void rpc_free_task(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500871 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
Chuck Lever46121cf2007-01-31 12:14:08 -0500872 dprintk("RPC: %5u freeing task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 mempool_free(task, rpc_task_mempool);
874}
875
876/*
Trond Myklebust90c57552007-06-09 19:49:36 -0400877 * Create a new task for the specified client.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100879struct 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 -0700880{
881 struct rpc_task *task;
882
883 task = rpc_alloc_task();
884 if (!task)
Trond Myklebust90c57552007-06-09 19:49:36 -0400885 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100887 rpc_init_task(task, clnt, flags, tk_ops, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Chuck Lever46121cf2007-01-31 12:14:08 -0500889 dprintk("RPC: allocated task %p\n", task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 task->tk_flags |= RPC_TASK_DYNAMIC;
891out:
892 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500895
896void rpc_put_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100898 const struct rpc_call_ops *tk_ops = task->tk_ops;
899 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500901 if (!atomic_dec_and_test(&task->tk_count))
902 return;
903 /* Release resources */
904 if (task->tk_rqstp)
905 xprt_release(task);
906 if (task->tk_msg.rpc_cred)
907 rpcauth_unbindcred(task);
908 if (task->tk_client) {
909 rpc_release_client(task->tk_client);
910 task->tk_client = NULL;
911 }
912 if (task->tk_flags & RPC_TASK_DYNAMIC)
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500913 call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400914 rpc_release_calldata(tk_ops, calldata);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500915}
916EXPORT_SYMBOL(rpc_put_task);
917
Trond Myklebustbde8f002007-01-24 11:54:53 -0800918static void rpc_release_task(struct rpc_task *task)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500919{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920#ifdef RPC_DEBUG
921 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
922#endif
Chuck Lever46121cf2007-01-31 12:14:08 -0500923 dprintk("RPC: %5u release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Trond Myklebust6529eba2007-06-14 16:40:14 -0400925 if (!list_empty(&task->tk_task)) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400926 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400927 /* Remove from client task list */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400928 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400929 list_del(&task->tk_task);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400930 spin_unlock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 /* Synchronously delete any running timer */
935 rpc_delete_timer(task);
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937#ifdef RPC_DEBUG
938 task->tk_magic = 0;
939#endif
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500940 /* Wake up anyone who is waiting for task completion */
941 rpc_mark_complete_task(task);
942
943 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
946/**
Trond Myklebust44c28872006-01-03 09:55:06 +0100947 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
Martin Waitz99acf042006-02-01 03:06:56 -0800948 * @clnt: pointer to RPC client
949 * @flags: RPC flags
950 * @ops: RPC call ops
951 * @data: user call data
Trond Myklebust44c28872006-01-03 09:55:06 +0100952 */
953struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
954 const struct rpc_call_ops *ops,
955 void *data)
956{
957 struct rpc_task *task;
958 task = rpc_new_task(clnt, flags, ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500959 if (task == NULL) {
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400960 rpc_release_calldata(ops, data);
Trond Myklebust44c28872006-01-03 09:55:06 +0100961 return ERR_PTR(-ENOMEM);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500962 }
Trond Myklebust44c28872006-01-03 09:55:06 +0100963 atomic_inc(&task->tk_count);
964 rpc_execute(task);
965 return task;
966}
967EXPORT_SYMBOL(rpc_run_task);
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
970 * Kill all tasks for the given client.
971 * XXX: kill their descendants as well?
972 */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400973void rpc_killall_tasks(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct rpc_task *rovr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400978 if (list_empty(&clnt->cl_tasks))
979 return;
980 dprintk("RPC: killing all tasks for client %p\n", clnt);
981 /*
982 * Spin lock all_tasks to prevent changes...
983 */
984 spin_lock(&clnt->cl_lock);
985 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (! RPC_IS_ACTIVATED(rovr))
987 continue;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400988 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 rovr->tk_flags |= RPC_TASK_KILLED;
990 rpc_exit(rovr, -EIO);
991 rpc_wake_up_task(rovr);
992 }
993 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400994 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Trond Myklebust6529eba2007-06-14 16:40:14 -0400997void rpc_register_client(struct rpc_clnt *clnt)
998{
999 spin_lock(&rpc_sched_lock);
1000 list_add(&clnt->cl_clients, &all_clients);
1001 spin_unlock(&rpc_sched_lock);
1002}
1003
1004void rpc_unregister_client(struct rpc_clnt *clnt)
1005{
1006 spin_lock(&rpc_sched_lock);
1007 list_del(&clnt->cl_clients);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001008 spin_unlock(&rpc_sched_lock);
1009}
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011/*
1012 * Start up the rpciod process if it's not already running.
1013 */
1014int
1015rpciod_up(void)
1016{
1017 struct workqueue_struct *wq;
1018 int error = 0;
1019
Trond Myklebustab418d72007-06-14 17:08:36 -04001020 if (atomic_inc_not_zero(&rpciod_users))
1021 return 0;
1022
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001023 mutex_lock(&rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -04001024
1025 /* Guard against races with rpciod_down() */
1026 if (rpciod_workqueue != NULL)
1027 goto out_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Create the rpciod thread and wait for it to start.
1030 */
Trond Myklebustab418d72007-06-14 17:08:36 -04001031 dprintk("RPC: creating workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 error = -ENOMEM;
1033 wq = create_workqueue("rpciod");
Trond Myklebustab418d72007-06-14 17:08:36 -04001034 if (wq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 goto out;
Trond Myklebustab418d72007-06-14 17:08:36 -04001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 rpciod_workqueue = wq;
1038 error = 0;
Trond Myklebustab418d72007-06-14 17:08:36 -04001039out_ok:
1040 atomic_inc(&rpciod_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001042 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return error;
1044}
1045
1046void
1047rpciod_down(void)
1048{
Trond Myklebustab418d72007-06-14 17:08:36 -04001049 if (!atomic_dec_and_test(&rpciod_users))
1050 return;
1051
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001052 mutex_lock(&rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -04001053 dprintk("RPC: destroying workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Trond Myklebustab418d72007-06-14 17:08:36 -04001055 if (atomic_read(&rpciod_users) == 0 && rpciod_workqueue != NULL) {
Trond Myklebustab418d72007-06-14 17:08:36 -04001056 destroy_workqueue(rpciod_workqueue);
1057 rpciod_workqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001059 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062#ifdef RPC_DEBUG
1063void rpc_show_tasks(void)
1064{
Trond Myklebust6529eba2007-06-14 16:40:14 -04001065 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct rpc_task *t;
1067
1068 spin_lock(&rpc_sched_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001069 if (list_empty(&all_clients))
1070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001072 "-rpcwait -action- ---ops--\n");
Trond Myklebust6529eba2007-06-14 16:40:14 -04001073 list_for_each_entry(clnt, &all_clients, cl_clients) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001074 if (list_empty(&clnt->cl_tasks))
1075 continue;
1076 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001077 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1078 const char *rpc_waitq = "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Trond Myklebust6529eba2007-06-14 16:40:14 -04001080 if (RPC_IS_QUEUED(t))
1081 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Trond Myklebust6529eba2007-06-14 16:40:14 -04001083 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1084 t->tk_pid,
1085 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1086 t->tk_flags, t->tk_status,
1087 t->tk_client,
1088 (t->tk_client ? t->tk_client->cl_prog : 0),
1089 t->tk_rqstp, t->tk_timeout,
1090 rpc_waitq,
1091 t->tk_action, t->tk_ops);
1092 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001093 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Trond Myklebust6529eba2007-06-14 16:40:14 -04001095out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 spin_unlock(&rpc_sched_lock);
1097}
1098#endif
1099
1100void
1101rpc_destroy_mempool(void)
1102{
1103 if (rpc_buffer_mempool)
1104 mempool_destroy(rpc_buffer_mempool);
1105 if (rpc_task_mempool)
1106 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001107 if (rpc_task_slabp)
1108 kmem_cache_destroy(rpc_task_slabp);
1109 if (rpc_buffer_slabp)
1110 kmem_cache_destroy(rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111}
1112
1113int
1114rpc_init_mempool(void)
1115{
1116 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1117 sizeof(struct rpc_task),
1118 0, SLAB_HWCACHE_ALIGN,
1119 NULL, NULL);
1120 if (!rpc_task_slabp)
1121 goto err_nomem;
1122 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1123 RPC_BUFFER_MAXSIZE,
1124 0, SLAB_HWCACHE_ALIGN,
1125 NULL, NULL);
1126 if (!rpc_buffer_slabp)
1127 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001128 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1129 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 if (!rpc_task_mempool)
1131 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001132 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1133 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!rpc_buffer_mempool)
1135 goto err_nomem;
1136 return 0;
1137err_nomem:
1138 rpc_destroy_mempool();
1139 return -ENOMEM;
1140}