blob: d95fe4e40eb468de464cd198d9abc7ef785b9533 [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);
42static void rpciod_killall(void);
David Howells65f27f32006-11-22 14:55:48 +000043static void rpc_async_schedule(struct work_struct *);
Trond Myklebustbde8f002007-01-24 11:54:53 -080044static void rpc_release_task(struct rpc_task *task);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * RPC tasks sit here while waiting for conditions to improve.
48 */
49static RPC_WAITQ(delay_queue, "delayq");
50
51/*
Trond Myklebust6529eba2007-06-14 16:40:14 -040052 * All RPC clients are linked into this list
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 */
Trond Myklebust6529eba2007-06-14 16:40:14 -040054static LIST_HEAD(all_clients);
55static DECLARE_WAIT_QUEUE_HEAD(client_kill_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
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{
Chuck Lever46121cf2007-01-31 12:14:08 -050077 dprintk("RPC: %5u disabling timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 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)) {
Chuck Lever46121cf2007-01-31 12:14:08 -050096 dprintk("RPC: %5u running timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 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
Chuck Lever46121cf2007-01-31 12:14:08 -0500113 dprintk("RPC: %5u setting alarm for %lu ms\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 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);
Chuck Lever46121cf2007-01-31 12:14:08 -0500135 dprintk("RPC: %5u deleting timer\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
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
Chuck Lever46121cf2007-01-31 12:14:08 -0500182 dprintk("RPC: %5u added to queue %p \"%s\"\n",
183 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
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--;
Chuck Lever46121cf2007-01-31 12:14:08 -0500215 dprintk("RPC: %5u removed from queue %p \"%s\"\n",
216 task->tk_pid, queue, rpc_qname(queue));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
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 Myklebustc44fe702007-06-16 14:17:01 -0400270#ifdef RPC_DEBUG
271static void rpc_task_set_debuginfo(struct rpc_task *task)
272{
273 static atomic_t rpc_pid;
274
275 task->tk_magic = RPC_TASK_MAGIC_ID;
276 task->tk_pid = atomic_inc_return(&rpc_pid);
277}
278#else
279static inline void rpc_task_set_debuginfo(struct rpc_task *task)
280{
281}
282#endif
283
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500284static void rpc_set_active(struct rpc_task *task)
285{
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400286 struct rpc_clnt *clnt;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500287 if (test_and_set_bit(RPC_TASK_ACTIVE, &task->tk_runstate) != 0)
288 return;
Trond Myklebustc44fe702007-06-16 14:17:01 -0400289 rpc_task_set_debuginfo(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500290 /* Add to global list of all tasks */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400291 clnt = task->tk_client;
292 if (clnt != NULL) {
293 spin_lock(&clnt->cl_lock);
294 list_add_tail(&task->tk_task, &clnt->cl_tasks);
295 spin_unlock(&clnt->cl_lock);
296 }
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500297}
298
Trond Myklebust44c28872006-01-03 09:55:06 +0100299/*
300 * Mark an RPC call as having completed by clearing the 'active' bit
301 */
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500302static void rpc_mark_complete_task(struct rpc_task *task)
Trond Myklebust44c28872006-01-03 09:55:06 +0100303{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500304 smp_mb__before_clear_bit();
305 clear_bit(RPC_TASK_ACTIVE, &task->tk_runstate);
306 smp_mb__after_clear_bit();
Trond Myklebust44c28872006-01-03 09:55:06 +0100307 wake_up_bit(&task->tk_runstate, RPC_TASK_ACTIVE);
308}
309
310/*
311 * Allow callers to wait for completion of an RPC call
312 */
313int __rpc_wait_for_completion_task(struct rpc_task *task, int (*action)(void *))
314{
315 if (action == NULL)
316 action = rpc_wait_bit_interruptible;
317 return wait_on_bit(&task->tk_runstate, RPC_TASK_ACTIVE,
318 action, TASK_INTERRUPTIBLE);
319}
320EXPORT_SYMBOL(__rpc_wait_for_completion_task);
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*
323 * Make an RPC task runnable.
324 *
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800325 * Note: If the task is ASYNC, this must be called with
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * the spinlock held to protect the wait queue operation.
327 */
328static void rpc_make_runnable(struct rpc_task *task)
329{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 BUG_ON(task->tk_timeout_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 rpc_clear_queued(task);
Christophe Saoutcc4dc59e2006-11-05 18:42:48 +0100332 if (rpc_test_and_set_running(task))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 return;
Christophe Saoutcc4dc59e2006-11-05 18:42:48 +0100334 /* We might have raced */
335 if (RPC_IS_QUEUED(task)) {
336 rpc_clear_running(task);
337 return;
338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (RPC_IS_ASYNC(task)) {
340 int status;
341
David Howells65f27f32006-11-22 14:55:48 +0000342 INIT_WORK(&task->u.tk_work, rpc_async_schedule);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 status = queue_work(task->tk_workqueue, &task->u.tk_work);
344 if (status < 0) {
345 printk(KERN_WARNING "RPC: failed to add task to queue: error: %d!\n", status);
346 task->tk_status = status;
347 return;
348 }
349 } else
Trond Myklebust96651ab2005-06-22 17:16:21 +0000350 wake_up_bit(&task->tk_runstate, RPC_TASK_QUEUED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * Prepare for sleeping on a wait queue.
355 * By always appending tasks to the list we ensure FIFO behavior.
356 * NB: An RPC task will only receive interrupt-driven events as long
357 * as it's on a wait queue.
358 */
359static void __rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
360 rpc_action action, rpc_action timer)
361{
Chuck Lever46121cf2007-01-31 12:14:08 -0500362 dprintk("RPC: %5u sleep_on(queue \"%s\" time %lu)\n",
363 task->tk_pid, rpc_qname(q), jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (!RPC_IS_ASYNC(task) && !RPC_IS_ACTIVATED(task)) {
366 printk(KERN_ERR "RPC: Inactive synchronous task put to sleep!\n");
367 return;
368 }
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 __rpc_add_wait_queue(q, task);
371
372 BUG_ON(task->tk_callback != NULL);
373 task->tk_callback = action;
374 __rpc_add_timer(task, timer);
375}
376
377void rpc_sleep_on(struct rpc_wait_queue *q, struct rpc_task *task,
378 rpc_action action, rpc_action timer)
379{
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500380 /* Mark the task as being activated if so needed */
381 rpc_set_active(task);
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 /*
384 * Protect the queue operations.
385 */
386 spin_lock_bh(&q->lock);
387 __rpc_sleep_on(q, task, action, timer);
388 spin_unlock_bh(&q->lock);
389}
390
391/**
392 * __rpc_do_wake_up_task - wake up a single rpc_task
393 * @task: task to be woken up
394 *
395 * Caller must hold queue->lock, and have cleared the task queued flag.
396 */
397static void __rpc_do_wake_up_task(struct rpc_task *task)
398{
Chuck Lever46121cf2007-01-31 12:14:08 -0500399 dprintk("RPC: %5u __rpc_wake_up_task (now %lu)\n",
400 task->tk_pid, jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402#ifdef RPC_DEBUG
403 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
404#endif
405 /* Has the task been executed yet? If not, we cannot wake it up! */
406 if (!RPC_IS_ACTIVATED(task)) {
407 printk(KERN_ERR "RPC: Inactive task (%p) being woken up!\n", task);
408 return;
409 }
410
411 __rpc_disable_timer(task);
412 __rpc_remove_wait_queue(task);
413
414 rpc_make_runnable(task);
415
Chuck Lever46121cf2007-01-31 12:14:08 -0500416 dprintk("RPC: __rpc_wake_up_task done\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
420 * Wake up the specified task
421 */
422static void __rpc_wake_up_task(struct rpc_task *task)
423{
424 if (rpc_start_wakeup(task)) {
425 if (RPC_IS_QUEUED(task))
426 __rpc_do_wake_up_task(task);
427 rpc_finish_wakeup(task);
428 }
429}
430
431/*
432 * Default timeout handler if none specified by user
433 */
434static void
435__rpc_default_timer(struct rpc_task *task)
436{
Chuck Lever46121cf2007-01-31 12:14:08 -0500437 dprintk("RPC: %5u timeout (default timer)\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 task->tk_status = -ETIMEDOUT;
439 rpc_wake_up_task(task);
440}
441
442/*
443 * Wake up the specified task
444 */
445void rpc_wake_up_task(struct rpc_task *task)
446{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500447 rcu_read_lock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 if (rpc_start_wakeup(task)) {
449 if (RPC_IS_QUEUED(task)) {
450 struct rpc_wait_queue *queue = task->u.tk_wait.rpc_waitq;
451
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500452 /* Note: we're already in a bh-safe context */
453 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 __rpc_do_wake_up_task(task);
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500455 spin_unlock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457 rpc_finish_wakeup(task);
458 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500459 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
462/*
463 * Wake up the next task on a priority queue.
464 */
465static struct rpc_task * __rpc_wake_up_next_priority(struct rpc_wait_queue *queue)
466{
467 struct list_head *q;
468 struct rpc_task *task;
469
470 /*
471 * Service a batch of tasks from a single cookie.
472 */
473 q = &queue->tasks[queue->priority];
474 if (!list_empty(q)) {
475 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
476 if (queue->cookie == task->tk_cookie) {
477 if (--queue->nr)
478 goto out;
479 list_move_tail(&task->u.tk_wait.list, q);
480 }
481 /*
482 * Check if we need to switch queues.
483 */
484 if (--queue->count)
485 goto new_cookie;
486 }
487
488 /*
489 * Service the next queue.
490 */
491 do {
492 if (q == &queue->tasks[0])
493 q = &queue->tasks[queue->maxpriority];
494 else
495 q = q - 1;
496 if (!list_empty(q)) {
497 task = list_entry(q->next, struct rpc_task, u.tk_wait.list);
498 goto new_queue;
499 }
500 } while (q != &queue->tasks[queue->priority]);
501
502 rpc_reset_waitqueue_priority(queue);
503 return NULL;
504
505new_queue:
506 rpc_set_waitqueue_priority(queue, (unsigned int)(q - &queue->tasks[0]));
507new_cookie:
508 rpc_set_waitqueue_cookie(queue, task->tk_cookie);
509out:
510 __rpc_wake_up_task(task);
511 return task;
512}
513
514/*
515 * Wake up the next task on the wait queue.
516 */
517struct rpc_task * rpc_wake_up_next(struct rpc_wait_queue *queue)
518{
519 struct rpc_task *task = NULL;
520
Chuck Lever46121cf2007-01-31 12:14:08 -0500521 dprintk("RPC: wake_up_next(%p \"%s\")\n",
522 queue, rpc_qname(queue));
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500523 rcu_read_lock_bh();
524 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 if (RPC_IS_PRIORITY(queue))
526 task = __rpc_wake_up_next_priority(queue);
527 else {
528 task_for_first(task, &queue->tasks[0])
529 __rpc_wake_up_task(task);
530 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500531 spin_unlock(&queue->lock);
532 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 return task;
535}
536
537/**
538 * rpc_wake_up - wake up all rpc_tasks
539 * @queue: rpc_wait_queue on which the tasks are sleeping
540 *
541 * Grabs queue->lock
542 */
543void rpc_wake_up(struct rpc_wait_queue *queue)
544{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800545 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 struct list_head *head;
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800547
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500548 rcu_read_lock_bh();
549 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 head = &queue->tasks[queue->maxpriority];
551 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800552 list_for_each_entry_safe(task, next, head, u.tk_wait.list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 __rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 if (head == &queue->tasks[0])
555 break;
556 head--;
557 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500558 spin_unlock(&queue->lock);
559 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/**
563 * rpc_wake_up_status - wake up all rpc_tasks and set their status value.
564 * @queue: rpc_wait_queue on which the tasks are sleeping
565 * @status: status value to set
566 *
567 * Grabs queue->lock
568 */
569void rpc_wake_up_status(struct rpc_wait_queue *queue, int status)
570{
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800571 struct rpc_task *task, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct list_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500574 rcu_read_lock_bh();
575 spin_lock(&queue->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 head = &queue->tasks[queue->maxpriority];
577 for (;;) {
Trond Myklebuste6d83d52006-03-13 21:20:48 -0800578 list_for_each_entry_safe(task, next, head, u.tk_wait.list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 task->tk_status = status;
580 __rpc_wake_up_task(task);
581 }
582 if (head == &queue->tasks[0])
583 break;
584 head--;
585 }
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500586 spin_unlock(&queue->lock);
587 rcu_read_unlock_bh();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Trond Myklebust80147932006-08-31 18:24:08 -0400590static void __rpc_atrun(struct rpc_task *task)
591{
592 rpc_wake_up_task(task);
593}
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/*
596 * Run a task at a later time
597 */
Trond Myklebust80147932006-08-31 18:24:08 -0400598void rpc_delay(struct rpc_task *task, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
600 task->tk_timeout = delay;
601 rpc_sleep_on(&delay_queue, task, NULL, __rpc_atrun);
602}
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604/*
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100605 * Helper to call task->tk_ops->rpc_call_prepare
606 */
607static void rpc_prepare_task(struct rpc_task *task)
608{
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400609 lock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100610 task->tk_ops->rpc_call_prepare(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400611 unlock_kernel();
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100612}
613
614/*
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100615 * Helper that calls task->tk_ops->rpc_call_done if it exists
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000616 */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100617void rpc_exit_task(struct rpc_task *task)
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000618{
Trond Myklebustabbcf282006-01-03 09:55:03 +0100619 task->tk_action = NULL;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100620 if (task->tk_ops->rpc_call_done != NULL) {
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400621 lock_kernel();
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100622 task->tk_ops->rpc_call_done(task, task->tk_calldata);
Trond Myklebust6d5fcb52006-10-18 16:01:06 -0400623 unlock_kernel();
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000624 if (task->tk_action != NULL) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100625 WARN_ON(RPC_ASSASSINATED(task));
626 /* Always release the RPC slot and buffer memory */
627 xprt_release(task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000628 }
629 }
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000630}
Trond Myklebustabbcf282006-01-03 09:55:03 +0100631EXPORT_SYMBOL(rpc_exit_task);
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000632
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400633void rpc_release_calldata(const struct rpc_call_ops *ops, void *calldata)
634{
635 if (ops->rpc_release != NULL) {
636 lock_kernel();
637 ops->rpc_release(calldata);
638 unlock_kernel();
639 }
640}
641
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000642/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * This is the RPC `scheduler' (or rather, the finite state machine).
644 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800645static void __rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
647 int status = 0;
648
Chuck Lever46121cf2007-01-31 12:14:08 -0500649 dprintk("RPC: %5u __rpc_execute flags=0x%x\n",
650 task->tk_pid, task->tk_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 BUG_ON(RPC_IS_QUEUED(task));
653
Trond Myklebustd05fdb02005-06-22 17:16:19 +0000654 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 /*
656 * Garbage collection of pending timers...
657 */
658 rpc_delete_timer(task);
659
660 /*
661 * Execute any pending callback.
662 */
663 if (RPC_DO_CALLBACK(task)) {
664 /* Define a callback save pointer */
665 void (*save_callback)(struct rpc_task *);
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800666
667 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * If a callback exists, save it, reset it,
669 * call it.
670 * The save is needed to stop from resetting
671 * another callback set within the callback handler
672 * - Dave
673 */
674 save_callback=task->tk_callback;
675 task->tk_callback=NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 save_callback(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678
679 /*
680 * Perform the next FSM step.
681 * tk_action may be NULL when the task has been killed
682 * by someone else.
683 */
684 if (!RPC_IS_QUEUED(task)) {
Trond Myklebustabbcf282006-01-03 09:55:03 +0100685 if (task->tk_action == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
Trond Myklebustabbcf282006-01-03 09:55:03 +0100687 task->tk_action(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
690 /*
691 * Lockless check for whether task is sleeping or not.
692 */
693 if (!RPC_IS_QUEUED(task))
694 continue;
695 rpc_clear_running(task);
696 if (RPC_IS_ASYNC(task)) {
697 /* Careful! we may have raced... */
698 if (RPC_IS_QUEUED(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (rpc_test_and_set_running(task))
Trond Myklebust2efef832007-02-03 13:38:41 -0800701 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 continue;
703 }
704
705 /* sync task: sleep here */
Chuck Lever46121cf2007-01-31 12:14:08 -0500706 dprintk("RPC: %5u sync task going to sleep\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000707 /* Note: Caller should be using rpc_clnt_sigmask() */
708 status = out_of_line_wait_on_bit(&task->tk_runstate,
709 RPC_TASK_QUEUED, rpc_wait_bit_interruptible,
710 TASK_INTERRUPTIBLE);
711 if (status == -ERESTARTSYS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * When a sync task receives a signal, it exits with
714 * -ERESTARTSYS. In order to catch any callbacks that
715 * clean up after sleeping on some queue, we don't
716 * break the loop here, but go around once more.
717 */
Chuck Lever46121cf2007-01-31 12:14:08 -0500718 dprintk("RPC: %5u got signal\n", task->tk_pid);
Trond Myklebust96651ab2005-06-22 17:16:21 +0000719 task->tk_flags |= RPC_TASK_KILLED;
720 rpc_exit(task, -ERESTARTSYS);
721 rpc_wake_up_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 rpc_set_running(task);
Chuck Lever46121cf2007-01-31 12:14:08 -0500724 dprintk("RPC: %5u sync task resuming\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
Chuck Lever46121cf2007-01-31 12:14:08 -0500727 dprintk("RPC: %5u return %d, status %d\n", task->tk_pid, status,
728 task->tk_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 /* Release all resources associated with the task */
730 rpc_release_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
733/*
734 * User-visible entry point to the scheduler.
735 *
736 * This may be called recursively if e.g. an async NFS task updates
737 * the attributes and finds that dirty pages must be flushed.
738 * NOTE: Upon exit of this function the task is guaranteed to be
739 * released. In particular note that tk_release() will have
740 * been called, so your task memory may have been freed.
741 */
Trond Myklebust2efef832007-02-03 13:38:41 -0800742void rpc_execute(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743{
Trond Myklebust44c28872006-01-03 09:55:06 +0100744 rpc_set_active(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 rpc_set_running(task);
Trond Myklebust2efef832007-02-03 13:38:41 -0800746 __rpc_execute(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747}
748
David Howells65f27f32006-11-22 14:55:48 +0000749static void rpc_async_schedule(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
David Howells65f27f32006-11-22 14:55:48 +0000751 __rpc_execute(container_of(work, struct rpc_task, u.tk_work));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400754struct rpc_buffer {
755 size_t len;
756 char data[];
757};
758
Chuck Lever02107142006-01-03 09:55:49 +0100759/**
760 * rpc_malloc - allocate an RPC buffer
761 * @task: RPC task that will use this buffer
762 * @size: requested byte size
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 *
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400764 * To prevent rpciod from hanging, this allocator never sleeps,
765 * returning NULL if the request cannot be serviced immediately.
766 * The caller can arrange to sleep in a way that is safe for rpciod.
767 *
768 * Most requests are 'small' (under 2KiB) and can be serviced from a
769 * mempool, ensuring that NFS reads and writes can always proceed,
770 * and that there is good locality of reference for these buffers.
771 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 * In order to avoid memory starvation triggering more writebacks of
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400773 * NFS requests, we avoid using GFP_KERNEL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400775void *rpc_malloc(struct rpc_task *task, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400777 struct rpc_buffer *buf;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400778 gfp_t gfp = RPC_IS_SWAPPER(task) ? GFP_ATOMIC : GFP_NOWAIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400780 size += sizeof(struct rpc_buffer);
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400781 if (size <= RPC_BUFFER_MAXSIZE)
782 buf = mempool_alloc(rpc_buffer_mempool, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 else
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400784 buf = kmalloc(size, gfp);
Peter Zijlstraddce40d2007-05-09 08:30:11 +0200785
786 if (!buf)
787 return NULL;
788
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400789 buf->len = size;
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200790 dprintk("RPC: %5u allocated buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400791 task->tk_pid, size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400792 return &buf->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Chuck Lever02107142006-01-03 09:55:49 +0100795/**
796 * rpc_free - free buffer allocated via rpc_malloc
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400797 * @buffer: buffer to free
Chuck Lever02107142006-01-03 09:55:49 +0100798 *
799 */
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400800void rpc_free(void *buffer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400802 size_t size;
803 struct rpc_buffer *buf;
Chuck Lever02107142006-01-03 09:55:49 +0100804
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400805 if (!buffer)
806 return;
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400807
808 buf = container_of(buffer, struct rpc_buffer, data);
809 size = buf->len;
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400810
Geert Uytterhoeven215d0672007-05-08 11:37:26 +0200811 dprintk("RPC: freeing buffer of size %zu at %p\n",
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400812 size, buf);
Chuck Leveraa3d1fa2007-05-08 18:23:28 -0400813
Chuck Leverc5a4dd82007-03-29 16:47:58 -0400814 if (size <= RPC_BUFFER_MAXSIZE)
815 mempool_free(buf, rpc_buffer_mempool);
816 else
817 kfree(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
820/*
821 * Creation and deletion of RPC task structures
822 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100823void 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 -0700824{
825 memset(task, 0, sizeof(*task));
826 init_timer(&task->tk_timer);
827 task->tk_timer.data = (unsigned long) task;
828 task->tk_timer.function = (void (*)(unsigned long)) rpc_run_timer;
Trond Myklebust44c28872006-01-03 09:55:06 +0100829 atomic_set(&task->tk_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 task->tk_client = clnt;
831 task->tk_flags = flags;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100832 task->tk_ops = tk_ops;
Trond Myklebust4ce70ad2006-01-03 09:55:05 +0100833 if (tk_ops->rpc_call_prepare != NULL)
834 task->tk_action = rpc_prepare_task;
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100835 task->tk_calldata = calldata;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400836 INIT_LIST_HEAD(&task->tk_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 /* Initialize retry counters */
839 task->tk_garb_retry = 2;
840 task->tk_cred_retry = 2;
841
842 task->tk_priority = RPC_PRIORITY_NORMAL;
843 task->tk_cookie = (unsigned long)current;
844
845 /* Initialize workqueue for async tasks */
846 task->tk_workqueue = rpciod_workqueue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 if (clnt) {
Trond Myklebust34f52e32007-06-14 16:40:31 -0400849 kref_get(&clnt->cl_kref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (clnt->cl_softrtry)
851 task->tk_flags |= RPC_TASK_SOFT;
852 if (!clnt->cl_intr)
853 task->tk_flags |= RPC_TASK_NOINTR;
854 }
855
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100856 BUG_ON(task->tk_ops == NULL);
857
Chuck Leveref759a22006-03-20 13:44:17 -0500858 /* starting timestamp */
859 task->tk_start = jiffies;
860
Chuck Lever46121cf2007-01-31 12:14:08 -0500861 dprintk("RPC: new task initialized, procpid %u\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 current->pid);
863}
864
865static struct rpc_task *
866rpc_alloc_task(void)
867{
868 return (struct rpc_task *)mempool_alloc(rpc_task_mempool, GFP_NOFS);
869}
870
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500871static void rpc_free_task(struct rcu_head *rcu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500873 struct rpc_task *task = container_of(rcu, struct rpc_task, u.tk_rcu);
Chuck Lever46121cf2007-01-31 12:14:08 -0500874 dprintk("RPC: %5u freeing task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 mempool_free(task, rpc_task_mempool);
876}
877
878/*
Trond Myklebust90c57552007-06-09 19:49:36 -0400879 * Create a new task for the specified client.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 */
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100881struct 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 -0700882{
883 struct rpc_task *task;
884
885 task = rpc_alloc_task();
886 if (!task)
Trond Myklebust90c57552007-06-09 19:49:36 -0400887 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100889 rpc_init_task(task, clnt, flags, tk_ops, calldata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Chuck Lever46121cf2007-01-31 12:14:08 -0500891 dprintk("RPC: allocated task %p\n", task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 task->tk_flags |= RPC_TASK_DYNAMIC;
893out:
894 return task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895}
896
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500897
898void rpc_put_task(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Trond Myklebust963d8fe2006-01-03 09:55:04 +0100900 const struct rpc_call_ops *tk_ops = task->tk_ops;
901 void *calldata = task->tk_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500903 if (!atomic_dec_and_test(&task->tk_count))
904 return;
905 /* Release resources */
906 if (task->tk_rqstp)
907 xprt_release(task);
908 if (task->tk_msg.rpc_cred)
909 rpcauth_unbindcred(task);
910 if (task->tk_client) {
911 rpc_release_client(task->tk_client);
912 task->tk_client = NULL;
913 }
914 if (task->tk_flags & RPC_TASK_DYNAMIC)
Trond Myklebust8aca67f2006-11-13 16:23:44 -0500915 call_rcu_bh(&task->u.tk_rcu, rpc_free_task);
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400916 rpc_release_calldata(tk_ops, calldata);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500917}
918EXPORT_SYMBOL(rpc_put_task);
919
Trond Myklebustbde8f002007-01-24 11:54:53 -0800920static void rpc_release_task(struct rpc_task *task)
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500921{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922#ifdef RPC_DEBUG
923 BUG_ON(task->tk_magic != RPC_TASK_MAGIC_ID);
924#endif
Chuck Lever46121cf2007-01-31 12:14:08 -0500925 dprintk("RPC: %5u release task\n", task->tk_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Trond Myklebust6529eba2007-06-14 16:40:14 -0400927 if (!list_empty(&task->tk_task)) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400928 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400929 /* Remove from client task list */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400930 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400931 list_del(&task->tk_task);
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400932 spin_unlock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -0400933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 BUG_ON (RPC_IS_QUEUED(task));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 /* Synchronously delete any running timer */
937 rpc_delete_timer(task);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939#ifdef RPC_DEBUG
940 task->tk_magic = 0;
941#endif
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500942 /* Wake up anyone who is waiting for task completion */
943 rpc_mark_complete_task(task);
944
945 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
948/**
Trond Myklebust44c28872006-01-03 09:55:06 +0100949 * rpc_run_task - Allocate a new RPC task, then run rpc_execute against it
Martin Waitz99acf042006-02-01 03:06:56 -0800950 * @clnt: pointer to RPC client
951 * @flags: RPC flags
952 * @ops: RPC call ops
953 * @data: user call data
Trond Myklebust44c28872006-01-03 09:55:06 +0100954 */
955struct rpc_task *rpc_run_task(struct rpc_clnt *clnt, int flags,
956 const struct rpc_call_ops *ops,
957 void *data)
958{
959 struct rpc_task *task;
960 task = rpc_new_task(clnt, flags, ops, data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500961 if (task == NULL) {
Trond Myklebustbbd5a1f2006-10-18 16:01:05 -0400962 rpc_release_calldata(ops, data);
Trond Myklebust44c28872006-01-03 09:55:06 +0100963 return ERR_PTR(-ENOMEM);
Trond Myklebust7a1218a2006-03-20 18:11:10 -0500964 }
Trond Myklebust44c28872006-01-03 09:55:06 +0100965 atomic_inc(&task->tk_count);
966 rpc_execute(task);
967 return task;
968}
969EXPORT_SYMBOL(rpc_run_task);
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971/*
972 * Kill all tasks for the given client.
973 * XXX: kill their descendants as well?
974 */
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400975void rpc_killall_tasks(struct rpc_clnt *clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
977 struct rpc_task *rovr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400980 if (list_empty(&clnt->cl_tasks))
981 return;
982 dprintk("RPC: killing all tasks for client %p\n", clnt);
983 /*
984 * Spin lock all_tasks to prevent changes...
985 */
986 spin_lock(&clnt->cl_lock);
987 list_for_each_entry(rovr, &clnt->cl_tasks, tk_task) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (! RPC_IS_ACTIVATED(rovr))
989 continue;
Trond Myklebust6529eba2007-06-14 16:40:14 -0400990 if (!(rovr->tk_flags & RPC_TASK_KILLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 rovr->tk_flags |= RPC_TASK_KILLED;
992 rpc_exit(rovr, -EIO);
993 rpc_wake_up_task(rovr);
994 }
995 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -0400996 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999static void rpciod_killall(void)
1000{
Trond Myklebust6529eba2007-06-14 16:40:14 -04001001 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 unsigned long flags;
1003
Trond Myklebust6529eba2007-06-14 16:40:14 -04001004 for(;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 clear_thread_flag(TIF_SIGPENDING);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001006
1007 spin_lock(&rpc_sched_lock);
1008 list_for_each_entry(clnt, &all_clients, cl_clients)
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001009 rpc_killall_tasks(clnt);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001010 spin_unlock(&rpc_sched_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 flush_workqueue(rpciod_workqueue);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001012 if (!list_empty(&all_clients))
1013 break;
1014 dprintk("RPC: rpciod_killall: waiting for tasks "
Chuck Lever46121cf2007-01-31 12:14:08 -05001015 "to exit\n");
Trond Myklebust6529eba2007-06-14 16:40:14 -04001016 wait_event_timeout(client_kill_wait,
1017 list_empty(&all_clients), 1*HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
1019
1020 spin_lock_irqsave(&current->sighand->siglock, flags);
1021 recalc_sigpending();
1022 spin_unlock_irqrestore(&current->sighand->siglock, flags);
1023}
1024
Trond Myklebust6529eba2007-06-14 16:40:14 -04001025void rpc_register_client(struct rpc_clnt *clnt)
1026{
1027 spin_lock(&rpc_sched_lock);
1028 list_add(&clnt->cl_clients, &all_clients);
1029 spin_unlock(&rpc_sched_lock);
1030}
1031
1032void rpc_unregister_client(struct rpc_clnt *clnt)
1033{
1034 spin_lock(&rpc_sched_lock);
1035 list_del(&clnt->cl_clients);
1036 if (list_empty(&all_clients))
1037 wake_up(&client_kill_wait);
1038 spin_unlock(&rpc_sched_lock);
1039}
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041/*
1042 * Start up the rpciod process if it's not already running.
1043 */
1044int
1045rpciod_up(void)
1046{
1047 struct workqueue_struct *wq;
1048 int error = 0;
1049
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001050 mutex_lock(&rpciod_mutex);
Chuck Lever46121cf2007-01-31 12:14:08 -05001051 dprintk("RPC: rpciod_up: users %u\n", rpciod_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 rpciod_users++;
1053 if (rpciod_workqueue)
1054 goto out;
1055 /*
1056 * If there's no pid, we should be the first user.
1057 */
1058 if (rpciod_users > 1)
Chuck Lever46121cf2007-01-31 12:14:08 -05001059 printk(KERN_WARNING "rpciod_up: no workqueue, %u users??\n", rpciod_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 /*
1061 * Create the rpciod thread and wait for it to start.
1062 */
1063 error = -ENOMEM;
1064 wq = create_workqueue("rpciod");
1065 if (wq == NULL) {
1066 printk(KERN_WARNING "rpciod_up: create workqueue failed, error=%d\n", error);
1067 rpciod_users--;
1068 goto out;
1069 }
1070 rpciod_workqueue = wq;
1071 error = 0;
1072out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001073 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 return error;
1075}
1076
1077void
1078rpciod_down(void)
1079{
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001080 mutex_lock(&rpciod_mutex);
Chuck Lever46121cf2007-01-31 12:14:08 -05001081 dprintk("RPC: rpciod_down sema %u\n", rpciod_users);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 if (rpciod_users) {
1083 if (--rpciod_users)
1084 goto out;
1085 } else
1086 printk(KERN_WARNING "rpciod_down: no users??\n");
1087
1088 if (!rpciod_workqueue) {
Chuck Lever46121cf2007-01-31 12:14:08 -05001089 dprintk("RPC: rpciod_down: Nothing to do!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto out;
1091 }
1092 rpciod_killall();
1093
1094 destroy_workqueue(rpciod_workqueue);
1095 rpciod_workqueue = NULL;
1096 out:
Arjan van de Ven4a3e2f72006-03-20 22:33:17 -08001097 mutex_unlock(&rpciod_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
1100#ifdef RPC_DEBUG
1101void rpc_show_tasks(void)
1102{
Trond Myklebust6529eba2007-06-14 16:40:14 -04001103 struct rpc_clnt *clnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 struct rpc_task *t;
1105
1106 spin_lock(&rpc_sched_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001107 if (list_empty(&all_clients))
1108 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 printk("-pid- proc flgs status -client- -prog- --rqstp- -timeout "
Trond Myklebust963d8fe2006-01-03 09:55:04 +01001110 "-rpcwait -action- ---ops--\n");
Trond Myklebust6529eba2007-06-14 16:40:14 -04001111 list_for_each_entry(clnt, &all_clients, cl_clients) {
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001112 if (list_empty(&clnt->cl_tasks))
1113 continue;
1114 spin_lock(&clnt->cl_lock);
Trond Myklebust6529eba2007-06-14 16:40:14 -04001115 list_for_each_entry(t, &clnt->cl_tasks, tk_task) {
1116 const char *rpc_waitq = "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Trond Myklebust6529eba2007-06-14 16:40:14 -04001118 if (RPC_IS_QUEUED(t))
1119 rpc_waitq = rpc_qname(t->u.tk_wait.rpc_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Trond Myklebust6529eba2007-06-14 16:40:14 -04001121 printk("%5u %04d %04x %6d %8p %6d %8p %8ld %8s %8p %8p\n",
1122 t->tk_pid,
1123 (t->tk_msg.rpc_proc ? t->tk_msg.rpc_proc->p_proc : -1),
1124 t->tk_flags, t->tk_status,
1125 t->tk_client,
1126 (t->tk_client ? t->tk_client->cl_prog : 0),
1127 t->tk_rqstp, t->tk_timeout,
1128 rpc_waitq,
1129 t->tk_action, t->tk_ops);
1130 }
Trond Myklebust4bef61f2007-06-16 14:17:01 -04001131 spin_unlock(&clnt->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
Trond Myklebust6529eba2007-06-14 16:40:14 -04001133out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 spin_unlock(&rpc_sched_lock);
1135}
1136#endif
1137
1138void
1139rpc_destroy_mempool(void)
1140{
1141 if (rpc_buffer_mempool)
1142 mempool_destroy(rpc_buffer_mempool);
1143 if (rpc_task_mempool)
1144 mempool_destroy(rpc_task_mempool);
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -07001145 if (rpc_task_slabp)
1146 kmem_cache_destroy(rpc_task_slabp);
1147 if (rpc_buffer_slabp)
1148 kmem_cache_destroy(rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151int
1152rpc_init_mempool(void)
1153{
1154 rpc_task_slabp = kmem_cache_create("rpc_tasks",
1155 sizeof(struct rpc_task),
1156 0, SLAB_HWCACHE_ALIGN,
1157 NULL, NULL);
1158 if (!rpc_task_slabp)
1159 goto err_nomem;
1160 rpc_buffer_slabp = kmem_cache_create("rpc_buffers",
1161 RPC_BUFFER_MAXSIZE,
1162 0, SLAB_HWCACHE_ALIGN,
1163 NULL, NULL);
1164 if (!rpc_buffer_slabp)
1165 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001166 rpc_task_mempool = mempool_create_slab_pool(RPC_TASK_POOLSIZE,
1167 rpc_task_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (!rpc_task_mempool)
1169 goto err_nomem;
Matthew Dobson93d23412006-03-26 01:37:50 -08001170 rpc_buffer_mempool = mempool_create_slab_pool(RPC_BUFFER_POOLSIZE,
1171 rpc_buffer_slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (!rpc_buffer_mempool)
1173 goto err_nomem;
1174 return 0;
1175err_nomem:
1176 rpc_destroy_mempool();
1177 return -ENOMEM;
1178}