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