blob: c0f8d25caf575307d12c6d39fcd64f77958f2a9c [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * rpciod-related stuff
52 */
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -080053static DEFINE_MUTEX(rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -040054static atomic_t rpciod_users = ATOMIC_INIT(0);
Trond Myklebust24c5d9d2006-03-20 13:44:08 -050055struct workqueue_struct *rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 * Disable the timer for a given RPC task. Should be called with
59 * queue->lock and bh_disabled in order to avoid races within
60 * rpc_run_timer().
61 */
62static inline void
63__rpc_disable_timer(struct rpc_task *task)
64{
Chuck Lever46121cf2007-01-31 12:14:08 -050065 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 task->tk_timeout_fn = NULL;
67 task->tk_timeout = 0;
68}
69
70/*
71 * Run a timeout function.
72 * We use the callback in order to allow __rpc_wake_up_task()
73 * and friends to disable the timer synchronously on SMP systems
74 * without calling del_timer_sync(). The latter could cause a
75 * deadlock if called while we're holding spinlocks...
76 */
77static void rpc_run_timer(struct rpc_task *task)
78{
79 void (*callback)(struct rpc_task *);
80
81 callback = task->tk_timeout_fn;
82 task->tk_timeout_fn = NULL;
83 if (callback && RPC_IS_QUEUED(task)) {
Chuck Lever46121cf2007-01-31 12:14:08 -050084 dprintk("RPC: %5u running timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 callback(task);
86 }
87 smp_mb__before_clear_bit();
88 clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
89 smp_mb__after_clear_bit();
90}
91
92/*
93 * Set up a timer for the current task.
94 */
95static inline void
96__rpc_add_timer(struct rpc_task *task, rpc_action timer)
97{
98 if (!task->tk_timeout)
99 return;
100
Chuck Lever46121cf2007-01-31 12:14:08 -0500101 dprintk("RPC: %5u setting alarm for %lu ms\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 task->tk_pid, task->tk_timeout * 1000 / HZ);
103
104 if (timer)
105 task->tk_timeout_fn = timer;
106 else
107 task->tk_timeout_fn = __rpc_default_timer;
108 set_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate);
109 mod_timer(&task->tk_timer, jiffies + task->tk_timeout);
110}
111
112/*
113 * Delete any timer for the current task. Because we use del_timer_sync(),
114 * this function should never be called while holding queue->lock.
115 */
116static void
117rpc_delete_timer(struct rpc_task *task)
118{
119 if (RPC_IS_QUEUED(task))
120 return;
121 if (test_and_clear_bit(RPC_TASK_HAS_TIMER, &task->tk_runstate)) {
122 del_singleshot_timer_sync(&task->tk_timer);
Chuck Lever46121cf2007-01-31 12:14:08 -0500123 dprintk("RPC: %5u deleting timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125}
126
127/*
128 * Add new request to a priority queue.
129 */
130static void __rpc_add_wait_queue_priority(struct rpc_wait_queue *queue, struct rpc_task *task)
131{
132 struct list_head *q;
133 struct rpc_task *t;
134
135 INIT_LIST_HEAD(&task->u.tk_wait.links);
136 q = &queue->tasks[task->tk_priority];
137 if (unlikely(task->tk_priority > queue->maxpriority))
138 q = &queue->tasks[queue->maxpriority];
139 list_for_each_entry(t, q, u.tk_wait.list) {
140 if (t->tk_cookie == task->tk_cookie) {
141 list_add_tail(&task->u.tk_wait.list, &t->u.tk_wait.links);
142 return;
143 }
144 }
145 list_add_tail(&task->u.tk_wait.list, q);
146}
147
148/*
149 * Add new request to wait queue.
150 *
151 * Swapper tasks always get inserted at the head of the queue.
152 * This should avoid many nasty memory deadlocks and hopefully
153 * improve overall performance.
154 * Everyone else gets appended to the queue to ensure proper FIFO behavior.
155 */
156static void __rpc_add_wait_queue(struct rpc_wait_queue *queue, struct rpc_task *task)
157{
158 BUG_ON (RPC_IS_QUEUED(task));
159
160 if (RPC_IS_PRIORITY(queue))
161 __rpc_add_wait_queue_priority(queue, task);
162 else if (RPC_IS_SWAPPER(task))
163 list_add(&task->u.tk_wait.list, &queue->tasks[0]);
164 else
165 list_add_tail(&task->u.tk_wait.list, &queue->tasks[0]);
166 task->u.tk_wait.rpc_waitq = queue;
Chuck Levere19b63d2006-03-20 13:44:15 -0500167 queue->qlen++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 rpc_set_queued(task);
169
Chuck Lever46121cf2007-01-31 12:14:08 -0500170 dprintk("RPC: %5u added to queue %p \"%s\"\n",
171 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174/*
175 * Remove request from a priority queue.
176 */
177static void __rpc_remove_wait_queue_priority(struct rpc_task *task)
178{
179 struct rpc_task *t;
180
181 if (!list_empty(&task->u.tk_wait.links)) {
182 t = list_entry(task->u.tk_wait.links.next, struct rpc_task, u.tk_wait.list);
183 list_move(&t->u.tk_wait.list, &task->u.tk_wait.list);
184 list_splice_init(&task->u.tk_wait.links, &t->u.tk_wait.links);
185 }
186 list_del(&task->u.tk_wait.list);
187}
188
189/*
190 * Remove request from queue.
191 * Note: must be called with spin lock held.
192 */
193static void __rpc_remove_wait_queue(struct rpc_task *task)
194{
195 struct rpc_wait_queue *queue;
196 queue = task->u.tk_wait.rpc_waitq;
197
198 if (RPC_IS_PRIORITY(queue))
199 __rpc_remove_wait_queue_priority(task);
200 else
201 list_del(&task->u.tk_wait.list);
Chuck Levere19b63d2006-03-20 13:44:15 -0500202 queue->qlen--;
Chuck Lever46121cf2007-01-31 12:14:08 -0500203 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
204 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static inline void rpc_set_waitqueue_priority(struct rpc_wait_queue *queue, int priority)
208{
209 queue->priority = priority;
210 queue->count = 1 << (priority * 2);
211}
212
213static inline void rpc_set_waitqueue_cookie(struct rpc_wait_queue *queue, unsigned long cookie)
214{
215 queue->cookie = cookie;
216 queue->nr = RPC_BATCH_COUNT;
217}
218
219static inline void rpc_reset_waitqueue_priority(struct rpc_wait_queue *queue)
220{
221 rpc_set_waitqueue_priority(queue, queue->maxpriority);
222 rpc_set_waitqueue_cookie(queue, 0);
223}
224
225static void __rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname, int maxprio)
226{
227 int i;
228
229 spin_lock_init(&queue->lock);
230 for (i = 0; i < ARRAY_SIZE(queue->tasks); i++)
231 INIT_LIST_HEAD(&queue->tasks[i]);
232 queue->maxpriority = maxprio;
233 rpc_reset_waitqueue_priority(queue);
234#ifdef RPC_DEBUG
235 queue->name = qname;
236#endif
237}
238
239void rpc_init_priority_wait_queue(struct rpc_wait_queue *queue, const char *qname)
240{
241 __rpc_init_priority_wait_queue(queue, qname, RPC_PRIORITY_HIGH);
242}
243
244void rpc_init_wait_queue(struct rpc_wait_queue *queue, const char *qname)
245{
246 __rpc_init_priority_wait_queue(queue, qname, 0);
247}
248EXPORT_SYMBOL(rpc_init_wait_queue);
249
Trond Myklebust44c28872006-01-03 09:55:06 +0100250static int rpc_wait_bit_interruptible(void *word)
251{
252 if (signal_pending(current))
253 return -ERESTARTSYS;
254 schedule();
255 return 0;
256}
257
Trond Myklebustc44fe702007-06-16 14:17:01 -0400258#ifdef RPC_DEBUG
259static void rpc_task_set_debuginfo(struct rpc_task *task)
260{
261 static atomic_t rpc_pid;
262
263 task->tk_magic = RPC_TASK_MAGIC_ID;
264 task->tk_pid = atomic_inc_return(&rpc_pid);
265}
266#else
267static inline void rpc_task_set_debuginfo(struct rpc_task *task)
268{
269}
270#endif
271
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500272static void rpc_set_active(struct rpc_task *task)
273{
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400274 struct rpc_clnt *clnt;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500275 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
276 return;
Trond Myklebustc44fe702007-06-16 14:17:01 -0400277 rpc_task_set_debuginfo(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500278 /* Add to global list of all tasks */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400279 clnt = task->tk_client;
280 if (clnt != NULL) {
281 spin_lock(&clnt->cl_lock);
282 list_add_tail(&task->tk_task, &clnt->cl_tasks);
283 spin_unlock(&clnt->cl_lock);
284 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500285}
286
Trond Myklebust44c28872006-01-03 09:55:06 +0100287/*
288 * Mark an RPC call as having completed by clearing the 'active' bit
289 */
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500290static void rpc_mark_complete_task(struct rpc_task *task)
Trond Myklebust44c28872006-01-03 09:55:06 +0100291{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500292 smp_mb__before_clear_bit();
293 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
294 smp_mb__after_clear_bit();
Trond Myklebust44c28872006-01-03 09:55:06 +0100295 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
296}
297
298/*
299 * Allow callers to wait for completion of an RPC call
300 */
301int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
302{
303 if (action == NULL)
304 action = rpc_wait_bit_interruptible;
305 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
306 action, TASK_INTERRUPTIBLE);
307}
308EXPORT_SYMBOL(__rpc_wait_for_completion_task);
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/*
311 * Make an RPC task runnable.
312 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800313 * Note: If the task is ASYNC, this must be called with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * the spinlock held to protect the wait queue operation.
315 */
316static void rpc_make_runnable(struct rpc_task *task)
317{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 BUG_ON(task->tk_timeout_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 rpc_clear_queued(task);
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100320 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return;
Christophe Saoutcc4dc592006-11-05 18:42:48 +0100322 /* We might have raced */
323 if (RPC_IS_QUEUED(task)) {
324 rpc_clear_running(task);
325 return;
326 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (RPC_IS_ASYNC(task)) {
328 int status;
329
David Howells65f27f32006-11-22 14:55:48 +0000330 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 status = queue_work(task->tk_workqueue, &task->u.tk_work);
332 if (status < 0) {
333 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
334 task->tk_status = status;
335 return;
336 }
337 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000338 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
341/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 * Prepare for sleeping on a wait queue.
343 * By always appending tasks to the list we ensure FIFO behavior.
344 * NB: An RPC task will only receive interrupt-driven events as long
345 * as it's on a wait queue.
346 */
347static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
348 rpc_action action, rpc_action timer)
349{
Chuck Lever46121cf2007-01-31 12:14:08 -0500350 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
351 task->tk_pid, rpc_qname(q), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
354 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
355 return;
356 }
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 __rpc_add_wait_queue(q, task);
359
360 BUG_ON(task->tk_callback != NULL);
361 task->tk_callback = action;
362 __rpc_add_timer(task, timer);
363}
364
365void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
366 rpc_action action, rpc_action timer)
367{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500368 /* Mark the task as being activated if so needed */
369 rpc_set_active(task);
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /*
372 * Protect the queue operations.
373 */
374 spin_lock_bh(&q->lock);
375 __rpc_sleep_on(q, task, action, timer);
376 spin_unlock_bh(&q->lock);
377}
378
379/**
380 * __rpc_do_wake_up_task - wake up a single rpc_task
381 * @task: task to be woken up
382 *
383 * Caller must hold queue->lock, and have cleared the task queued flag.
384 */
385static void __rpc_do_wake_up_task(struct rpc_task *task)
386{
Chuck Lever46121cf2007-01-31 12:14:08 -0500387 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
388 task->tk_pid, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390#ifdef RPC_DEBUG
391 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
392#endif
393 /* Has the task been executed yet? If not, we cannot wake it up! */
394 if (!RPC_IS_ACTIVATED(task)) {
395 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
396 return;
397 }
398
399 __rpc_disable_timer(task);
400 __rpc_remove_wait_queue(task);
401
402 rpc_make_runnable(task);
403
Chuck Lever46121cf2007-01-31 12:14:08 -0500404 dprintk("RPC: __rpc_wake_up_task done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407/*
408 * Wake up the specified task
409 */
410static void __rpc_wake_up_task(struct rpc_task *task)
411{
412 if (rpc_start_wakeup(task)) {
413 if (RPC_IS_QUEUED(task))
414 __rpc_do_wake_up_task(task);
415 rpc_finish_wakeup(task);
416 }
417}
418
419/*
420 * Default timeout handler if none specified by user
421 */
422static void
423__rpc_default_timer(struct rpc_task *task)
424{
Chuck Lever46121cf2007-01-31 12:14:08 -0500425 dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 task->tk_status = -ETIMEDOUT;
427 rpc_wake_up_task(task);
428}
429
430/*
431 * Wake up the specified task
432 */
433void rpc_wake_up_task(struct rpc_task *task)
434{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500435 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (rpc_start_wakeup(task)) {
437 if (RPC_IS_QUEUED(task)) {
438 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
439
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500440 /* Note: we're already in a bh-safe context */
441 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 __rpc_do_wake_up_task(task);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500443 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445 rpc_finish_wakeup(task);
446 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500447 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * Wake up the next task on a priority queue.
452 */
453static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
454{
455 struct list_head *q;
456 struct rpc_task *task;
457
458 /*
459 * Service a batch of tasks from a single cookie.
460 */
461 q = &queue->tasks[queue->priority];
462 if (!list_empty(q)) {
463 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
464 if (queue->cookie == task->tk_cookie) {
465 if (--queue->nr)
466 goto out;
467 list_move_tail(&task->u.tk_wait.list, q);
468 }
469 /*
470 * Check if we need to switch queues.
471 */
472 if (--queue->count)
473 goto new_cookie;
474 }
475
476 /*
477 * Service the next queue.
478 */
479 do {
480 if (q == &queue->tasks[0])
481 q = &queue->tasks[queue->maxpriority];
482 else
483 q = q - 1;
484 if (!list_empty(q)) {
485 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
486 goto new_queue;
487 }
488 } while (q != &queue->tasks[queue->priority]);
489
490 rpc_reset_waitqueue_priority(queue);
491 return NULL;
492
493new_queue:
494 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
495new_cookie:
496 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
497out:
498 __rpc_wake_up_task(task);
499 return task;
500}
501
502/*
503 * Wake up the next task on the wait queue.
504 */
505struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
506{
507 struct rpc_task *task = NULL;
508
Chuck Lever46121cf2007-01-31 12:14:08 -0500509 dprintk("RPC: wake_up_next(%p \"%s\")\n",
510 queue, rpc_qname(queue));
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500511 rcu_read_lock_bh();
512 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (RPC_IS_PRIORITY(queue))
514 task = __rpc_wake_up_next_priority(queue);
515 else {
516 task_for_first(task, &queue->tasks[0])
517 __rpc_wake_up_task(task);
518 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500519 spin_unlock(&queue->lock);
520 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 return task;
523}
524
525/**
526 * rpc_wake_up - wake up all rpc_tasks
527 * @queue: rpc_wait_queue on which the tasks are sleeping
528 *
529 * Grabs queue->lock
530 */
531void rpc_wake_up(struct rpc_wait_queue *queue)
532{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800533 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800535
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500536 rcu_read_lock_bh();
537 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 head = &queue->tasks[queue->maxpriority];
539 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800540 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 __rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (head == &queue->tasks[0])
543 break;
544 head--;
545 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500546 spin_unlock(&queue->lock);
547 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/**
551 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
552 * @queue: rpc_wait_queue on which the tasks are sleeping
553 * @status: status value to set
554 *
555 * Grabs queue->lock
556 */
557void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
558{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800559 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500562 rcu_read_lock_bh();
563 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 head = &queue->tasks[queue->maxpriority];
565 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800566 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 task->tk_status = status;
568 __rpc_wake_up_task(task);
569 }
570 if (head == &queue->tasks[0])
571 break;
572 head--;
573 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500574 spin_unlock(&queue->lock);
575 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Trond Myklebust80147932006-08-31 18:24:08 -0400578static void __rpc_atrun(struct rpc_task *task)
579{
580 rpc_wake_up_task(task);
581}
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/*
584 * Run a task at a later time
585 */
Trond Myklebust80147932006-08-31 18:24:08 -0400586void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 task->tk_timeout = delay;
589 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
590}
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100593 * Helper to call task->tk_ops->rpc_call_prepare
594 */
595static void rpc_prepare_task(struct rpc_task *task)
596{
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400597 lock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100598 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400599 unlock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100600}
601
602/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100603 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000604 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100605void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000606{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100607 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100608 if (task->tk_ops->rpc_call_done != NULL) {
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400609 lock_kernel();
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100610 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400611 unlock_kernel();
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000612 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100613 WARN_ON(RPC_ASSASSINATED(task));
614 /* Always release the RPC slot and buffer memory */
615 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000616 }
617 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000618}
Trond Myklebustabbcf282006-01-03 09:55:03 +0100619EXPORT_SYMBOL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000620
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400621void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
622{
623 if (ops->rpc_release != NULL) {
624 lock_kernel();
625 ops->rpc_release(calldata);
626 unlock_kernel();
627 }
628}
629
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * This is the RPC `scheduler' (or rather, the finite state machine).
632 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800633static void __rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 int status = 0;
636
Chuck Lever46121cf2007-01-31 12:14:08 -0500637 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
638 task->tk_pid, task->tk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 BUG_ON(RPC_IS_QUEUED(task));
641
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000642 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 /*
644 * Garbage collection of pending timers...
645 */
646 rpc_delete_timer(task);
647
648 /*
649 * Execute any pending callback.
650 */
651 if (RPC_DO_CALLBACK(task)) {
652 /* Define a callback save pointer */
653 void (*save_callback)(struct rpc_task *);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800654
655 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * If a callback exists, save it, reset it,
657 * call it.
658 * The save is needed to stop from resetting
659 * another callback set within the callback handler
660 * - Dave
661 */
662 save_callback=task->tk_callback;
663 task->tk_callback=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 save_callback(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 }
666
667 /*
668 * Perform the next FSM step.
669 * tk_action may be NULL when the task has been killed
670 * by someone else.
671 */
672 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100673 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100675 task->tk_action(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 /*
679 * Lockless check for whether task is sleeping or not.
680 */
681 if (!RPC_IS_QUEUED(task))
682 continue;
683 rpc_clear_running(task);
684 if (RPC_IS_ASYNC(task)) {
685 /* Careful! we may have raced... */
686 if (RPC_IS_QUEUED(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800687 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (rpc_test_and_set_running(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800689 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
691 }
692
693 /* sync task: sleep here */
Chuck Lever46121cf2007-01-31 12:14:08 -0500694 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000695 /* Note: Caller should be using rpc_clnt_sigmask() */
696 status = out_of_line_wait_on_bit(&task->tk_runstate,
697 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
698 TASK_INTERRUPTIBLE);
699 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 /*
701 * When a sync task receives a signal, it exits with
702 * -ERESTARTSYS. In order to catch any callbacks that
703 * clean up after sleeping on some queue, we don't
704 * break the loop here, but go around once more.
705 */
Chuck Lever46121cf2007-01-31 12:14:08 -0500706 dprintk("RPC: %5u got signal\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000707 task->tk_flags |= RPC_TASK_KILLED;
708 rpc_exit(task, -ERESTARTSYS);
709 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711 rpc_set_running(task);
Chuck Lever46121cf2007-01-31 12:14:08 -0500712 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 }
714
Chuck Lever46121cf2007-01-31 12:14:08 -0500715 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
716 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 /* Release all resources associated with the task */
718 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
722 * User-visible entry point to the scheduler.
723 *
724 * This may be called recursively if e.g. an async NFS task updates
725 * the attributes and finds that dirty pages must be flushed.
726 * NOTE: Upon exit of this function the task is guaranteed to be
727 * released. In particular note that tk_release() will have
728 * been called, so your task memory may have been freed.
729 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800730void rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Trond Myklebust44c28872006-01-03 09:55:06 +0100732 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 rpc_set_running(task);
Trond Myklebust2efef832007-02-03 13:38:41 -0800734 __rpc_execute(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
David Howells65f27f32006-11-22 14:55:48 +0000737static void rpc_async_schedule(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
David Howells65f27f32006-11-22 14:55:48 +0000739 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400742struct rpc_buffer {
743 size_t len;
744 char data[];
745};
746
Chuck Lever02107142006-01-03 09:55:49 +0100747/**
748 * rpc_malloc - allocate an RPC buffer
749 * @task: RPC task that will use this buffer
750 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 *
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400752 * To prevent rpciod from hanging, this allocator never sleeps,
753 * returning NULL if the request cannot be serviced immediately.
754 * The caller can arrange to sleep in a way that is safe for rpciod.
755 *
756 * Most requests are 'small' (under 2KiB) and can be serviced from a
757 * mempool, ensuring that NFS reads and writes can always proceed,
758 * and that there is good locality of reference for these buffers.
759 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 * In order to avoid memory starvation triggering more writebacks of
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400761 * NFS requests, we avoid using GFP_KERNEL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400763void *rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400765 struct rpc_buffer *buf;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400766 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400768 size += sizeof(struct rpc_buffer);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400769 if (size <= RPC_BUFFER_MAXSIZE)
770 buf = mempool_alloc(rpc_buffer_mempool, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 else
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400772 buf = kmalloc(size, gfp);
Peter Zijlstraddce40d2007-05-09 08:30:11 +0200773
774 if (!buf)
775 return NULL;
776
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400777 buf->len = size;
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200778 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400779 task->tk_pid, size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400780 return &buf->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Chuck Lever02107142006-01-03 09:55:49 +0100783/**
784 * rpc_free - free buffer allocated via rpc_malloc
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400785 * @buffer: buffer to free
Chuck Lever02107142006-01-03 09:55:49 +0100786 *
787 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400788void rpc_free(void *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400790 size_t size;
791 struct rpc_buffer *buf;
Chuck Lever02107142006-01-03 09:55:49 +0100792
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400793 if (!buffer)
794 return;
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400795
796 buf = container_of(buffer, struct rpc_buffer, data);
797 size = buf->len;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400798
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200799 dprintk("RPC: freeing buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400800 size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400801
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400802 if (size <= RPC_BUFFER_MAXSIZE)
803 mempool_free(buf, rpc_buffer_mempool);
804 else
805 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
808/*
809 * Creation and deletion of RPC task structures
810 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100811void 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 -0700812{
813 memset(task, 0, sizeof(*task));
814 init_timer(&task->tk_timer);
815 task->tk_timer.data = (unsigned long) task;
816 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
Trond Myklebust44c28872006-01-03 09:55:06 +0100817 atomic_set(&task->tk_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 task->tk_client = clnt;
819 task->tk_flags = flags;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100820 task->tk_ops = tk_ops;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100821 if (tk_ops->rpc_call_prepare != NULL)
822 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100823 task->tk_calldata = calldata;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400824 INIT_LIST_HEAD(&task->tk_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* Initialize retry counters */
827 task->tk_garb_retry = 2;
828 task->tk_cred_retry = 2;
829
830 task->tk_priority = RPC_PRIORITY_NORMAL;
831 task->tk_cookie = (unsigned long)current;
832
833 /* Initialize workqueue for async tasks */
834 task->tk_workqueue = rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (clnt) {
Trond Myklebust34f52e32007-06-14 16:40:31 -0400837 kref_get(&clnt->cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (clnt->cl_softrtry)
839 task->tk_flags |= RPC_TASK_SOFT;
840 if (!clnt->cl_intr)
841 task->tk_flags |= RPC_TASK_NOINTR;
842 }
843
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100844 BUG_ON(task->tk_ops == NULL);
845
Chuck Leveref759a22006-03-20 13:44:17 -0500846 /* starting timestamp */
847 task->tk_start = jiffies;
848
Chuck Lever46121cf2007-01-31 12:14:08 -0500849 dprintk("RPC: new task initialized, procpid %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 current->pid);
851}
852
853static struct rpc_task *
854rpc_alloc_task(void)
855{
856 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
857}
858
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500859static void rpc_free_task(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500861 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
Chuck Lever46121cf2007-01-31 12:14:08 -0500862 dprintk("RPC: %5u freeing task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 mempool_free(task, rpc_task_mempool);
864}
865
866/*
Trond Myklebust90c57552007-06-09 19:49:36 -0400867 * Create a new task for the specified client.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100869struct 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 -0700870{
871 struct rpc_task *task;
872
873 task = rpc_alloc_task();
874 if (!task)
Trond Myklebust90c57552007-06-09 19:49:36 -0400875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100877 rpc_init_task(task, clnt, flags, tk_ops, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Chuck Lever46121cf2007-01-31 12:14:08 -0500879 dprintk("RPC: allocated task %p\n", task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 task->tk_flags |= RPC_TASK_DYNAMIC;
881out:
882 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883}
884
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500885
886void rpc_put_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100888 const struct rpc_call_ops *tk_ops = task->tk_ops;
889 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500891 if (!atomic_dec_and_test(&task->tk_count))
892 return;
893 /* Release resources */
894 if (task->tk_rqstp)
895 xprt_release(task);
896 if (task->tk_msg.rpc_cred)
897 rpcauth_unbindcred(task);
898 if (task->tk_client) {
899 rpc_release_client(task->tk_client);
900 task->tk_client = NULL;
901 }
902 if (task->tk_flags & RPC_TASK_DYNAMIC)
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500903 call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400904 rpc_release_calldata(tk_ops, calldata);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500905}
906EXPORT_SYMBOL(rpc_put_task);
907
Trond Myklebustbde8f002007-01-24 11:54:53 -0800908static void rpc_release_task(struct rpc_task *task)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500909{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910#ifdef RPC_DEBUG
911 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
912#endif
Chuck Lever46121cf2007-01-31 12:14:08 -0500913 dprintk("RPC: %5u release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Trond Myklebust6529eba2007-06-14 16:40:14 -0400915 if (!list_empty(&task->tk_task)) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400916 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400917 /* Remove from client task list */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400918 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400919 list_del(&task->tk_task);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400920 spin_unlock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 /* Synchronously delete any running timer */
925 rpc_delete_timer(task);
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927#ifdef RPC_DEBUG
928 task->tk_magic = 0;
929#endif
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500930 /* Wake up anyone who is waiting for task completion */
931 rpc_mark_complete_task(task);
932
933 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
936/**
Trond Myklebust44c28872006-01-03 09:55:06 +0100937 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
Martin Waitz99acf042006-02-01 03:06:56 -0800938 * @clnt: pointer to RPC client
939 * @flags: RPC flags
940 * @ops: RPC call ops
941 * @data: user call data
Trond Myklebust44c28872006-01-03 09:55:06 +0100942 */
943struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
944 const struct rpc_call_ops *ops,
945 void *data)
946{
947 struct rpc_task *task;
948 task = rpc_new_task(clnt, flags, ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500949 if (task == NULL) {
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400950 rpc_release_calldata(ops, data);
Trond Myklebust44c28872006-01-03 09:55:06 +0100951 return ERR_PTR(-ENOMEM);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500952 }
Trond Myklebust44c28872006-01-03 09:55:06 +0100953 atomic_inc(&task->tk_count);
954 rpc_execute(task);
955 return task;
956}
957EXPORT_SYMBOL(rpc_run_task);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * Kill all tasks for the given client.
961 * XXX: kill their descendants as well?
962 */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400963void rpc_killall_tasks(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct rpc_task *rovr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400968 if (list_empty(&clnt->cl_tasks))
969 return;
970 dprintk("RPC: killing all tasks for client %p\n", clnt);
971 /*
972 * Spin lock all_tasks to prevent changes...
973 */
974 spin_lock(&clnt->cl_lock);
975 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (! RPC_IS_ACTIVATED(rovr))
977 continue;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400978 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 rovr->tk_flags |= RPC_TASK_KILLED;
980 rpc_exit(rovr, -EIO);
981 rpc_wake_up_task(rovr);
982 }
983 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400984 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987/*
988 * Start up the rpciod process if it's not already running.
989 */
990int
991rpciod_up(void)
992{
993 struct workqueue_struct *wq;
994 int error = 0;
995
Trond Myklebustab418d72007-06-14 17:08:36 -0400996 if (atomic_inc_not_zero(&rpciod_users))
997 return 0;
998
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -0800999 mutex_lock(&rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -04001000
1001 /* Guard against races with rpciod_down() */
1002 if (rpciod_workqueue != NULL)
1003 goto out_ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 /*
1005 * Create the rpciod thread and wait for it to start.
1006 */
Trond Myklebustab418d72007-06-14 17:08:36 -04001007 dprintk("RPC: creating workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 error = -ENOMEM;
1009 wq = create_workqueue("rpciod");
Trond Myklebustab418d72007-06-14 17:08:36 -04001010 if (wq == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto out;
Trond Myklebustab418d72007-06-14 17:08:36 -04001012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 rpciod_workqueue = wq;
1014 error = 0;
Trond Myklebustab418d72007-06-14 17:08:36 -04001015out_ok:
1016 atomic_inc(&rpciod_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001018 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return error;
1020}
1021
1022void
1023rpciod_down(void)
1024{
Trond Myklebustab418d72007-06-14 17:08:36 -04001025 if (!atomic_dec_and_test(&rpciod_users))
1026 return;
1027
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001028 mutex_lock(&rpciod_mutex);
Trond Myklebustab418d72007-06-14 17:08:36 -04001029 dprintk("RPC: destroying workqueue rpciod\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Trond Myklebustab418d72007-06-14 17:08:36 -04001031 if (atomic_read(&rpciod_users) == 0 && rpciod_workqueue != NULL) {
Trond Myklebustab418d72007-06-14 17:08:36 -04001032 destroy_workqueue(rpciod_workqueue);
1033 rpciod_workqueue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001035 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036}
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038void
1039rpc_destroy_mempool(void)
1040{
1041 if (rpc_buffer_mempool)
1042 mempool_destroy(rpc_buffer_mempool);
1043 if (rpc_task_mempool)
1044 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001045 if (rpc_task_slabp)
1046 kmem_cache_destroy(rpc_task_slabp);
1047 if (rpc_buffer_slabp)
1048 kmem_cache_destroy(rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049}
1050
1051int
1052rpc_init_mempool(void)
1053{
1054 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1055 sizeof(struct rpc_task),
1056 0, SLAB_HWCACHE_ALIGN,
1057 NULL, NULL);
1058 if (!rpc_task_slabp)
1059 goto err_nomem;
1060 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1061 RPC_BUFFER_MAXSIZE,
1062 0, SLAB_HWCACHE_ALIGN,
1063 NULL, NULL);
1064 if (!rpc_buffer_slabp)
1065 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001066 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1067 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (!rpc_task_mempool)
1069 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001070 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1071 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (!rpc_buffer_mempool)
1073 goto err_nomem;
1074 return 0;
1075err_nomem:
1076 rpc_destroy_mempool();
1077 return -ENOMEM;
1078}