[AFS]: Clean up the AFS sources
[linux-2.6/mini2440.git] / fs / afs / kafsasyncd.c
blob8ca01c2360137ec9a901229410aed310a72e458a
1 /* AFS asynchronous operation daemon
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 * The AFS async daemon is used to the following:
13 * - probe "dead" servers to see whether they've come back to life yet.
14 * - probe "live" servers that we haven't talked to for a while to see if they are better
15 * candidates for serving than what we're currently using
16 * - poll volume location servers to keep up to date volume location lists
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/sched.h>
22 #include <linux/completion.h>
23 #include <linux/freezer.h>
24 #include "cell.h"
25 #include "server.h"
26 #include "volume.h"
27 #include "kafsasyncd.h"
28 #include "kafstimod.h"
29 #include <rxrpc/call.h>
30 #include <asm/errno.h>
31 #include "internal.h"
33 static DECLARE_COMPLETION(kafsasyncd_alive);
34 static DECLARE_COMPLETION(kafsasyncd_dead);
35 static DECLARE_WAIT_QUEUE_HEAD(kafsasyncd_sleepq);
36 static struct task_struct *kafsasyncd_task;
37 static int kafsasyncd_die;
39 static int kafsasyncd(void *arg);
41 static LIST_HEAD(kafsasyncd_async_attnq);
42 static LIST_HEAD(kafsasyncd_async_busyq);
43 static DEFINE_SPINLOCK(kafsasyncd_async_lock);
45 static void kafsasyncd_null_call_attn_func(struct rxrpc_call *call)
49 static void kafsasyncd_null_call_error_func(struct rxrpc_call *call)
54 * start the async daemon
56 int afs_kafsasyncd_start(void)
58 int ret;
60 ret = kernel_thread(kafsasyncd, NULL, 0);
61 if (ret < 0)
62 return ret;
64 wait_for_completion(&kafsasyncd_alive);
66 return ret;
70 * stop the async daemon
72 void afs_kafsasyncd_stop(void)
74 /* get rid of my daemon */
75 kafsasyncd_die = 1;
76 wake_up(&kafsasyncd_sleepq);
77 wait_for_completion(&kafsasyncd_dead);
81 * probing daemon
83 static int kafsasyncd(void *arg)
85 struct afs_async_op *op;
86 int die;
88 DECLARE_WAITQUEUE(myself, current);
90 kafsasyncd_task = current;
92 printk("kAFS: Started kafsasyncd %d\n", current->pid);
94 daemonize("kafsasyncd");
96 complete(&kafsasyncd_alive);
98 /* loop around looking for things to attend to */
99 do {
100 set_current_state(TASK_INTERRUPTIBLE);
101 add_wait_queue(&kafsasyncd_sleepq, &myself);
103 for (;;) {
104 if (!list_empty(&kafsasyncd_async_attnq) ||
105 signal_pending(current) ||
106 kafsasyncd_die)
107 break;
109 schedule();
110 set_current_state(TASK_INTERRUPTIBLE);
113 remove_wait_queue(&kafsasyncd_sleepq, &myself);
114 set_current_state(TASK_RUNNING);
116 try_to_freeze();
118 /* discard pending signals */
119 afs_discard_my_signals();
121 die = kafsasyncd_die;
123 /* deal with the next asynchronous operation requiring
124 * attention */
125 if (!list_empty(&kafsasyncd_async_attnq)) {
126 struct afs_async_op *op;
128 _debug("@@@ Begin Asynchronous Operation");
130 op = NULL;
131 spin_lock(&kafsasyncd_async_lock);
133 if (!list_empty(&kafsasyncd_async_attnq)) {
134 op = list_entry(kafsasyncd_async_attnq.next,
135 struct afs_async_op, link);
136 list_move_tail(&op->link,
137 &kafsasyncd_async_busyq);
140 spin_unlock(&kafsasyncd_async_lock);
142 _debug("@@@ Operation %p {%p}\n",
143 op, op ? op->ops : NULL);
145 if (op)
146 op->ops->attend(op);
148 _debug("@@@ End Asynchronous Operation");
151 } while(!die);
153 /* need to kill all outstanding asynchronous operations before
154 * exiting */
155 kafsasyncd_task = NULL;
156 spin_lock(&kafsasyncd_async_lock);
158 /* fold the busy and attention queues together */
159 list_splice_init(&kafsasyncd_async_busyq,
160 &kafsasyncd_async_attnq);
162 /* dequeue kafsasyncd from all their wait queues */
163 list_for_each_entry(op, &kafsasyncd_async_attnq, link) {
164 op->call->app_attn_func = kafsasyncd_null_call_attn_func;
165 op->call->app_error_func = kafsasyncd_null_call_error_func;
166 remove_wait_queue(&op->call->waitq, &op->waiter);
169 spin_unlock(&kafsasyncd_async_lock);
171 /* abort all the operations */
172 while (!list_empty(&kafsasyncd_async_attnq)) {
173 op = list_entry(kafsasyncd_async_attnq.next, struct afs_async_op, link);
174 list_del_init(&op->link);
176 rxrpc_call_abort(op->call, -EIO);
177 rxrpc_put_call(op->call);
178 op->call = NULL;
180 op->ops->discard(op);
183 /* and that's all */
184 _leave("");
185 complete_and_exit(&kafsasyncd_dead, 0);
189 * begin an operation
190 * - place operation on busy queue
192 void afs_kafsasyncd_begin_op(struct afs_async_op *op)
194 _enter("");
196 spin_lock(&kafsasyncd_async_lock);
198 init_waitqueue_entry(&op->waiter, kafsasyncd_task);
199 add_wait_queue(&op->call->waitq, &op->waiter);
201 list_move_tail(&op->link, &kafsasyncd_async_busyq);
203 spin_unlock(&kafsasyncd_async_lock);
205 _leave("");
209 * request attention for an operation
210 * - move to attention queue
212 void afs_kafsasyncd_attend_op(struct afs_async_op *op)
214 _enter("");
216 spin_lock(&kafsasyncd_async_lock);
218 list_move_tail(&op->link, &kafsasyncd_async_attnq);
220 spin_unlock(&kafsasyncd_async_lock);
222 wake_up(&kafsasyncd_sleepq);
224 _leave("");
228 * terminate an operation
229 * - remove from either queue
231 void afs_kafsasyncd_terminate_op(struct afs_async_op *op)
233 _enter("");
235 spin_lock(&kafsasyncd_async_lock);
237 if (!list_empty(&op->link)) {
238 list_del_init(&op->link);
239 remove_wait_queue(&op->call->waitq, &op->waiter);
242 spin_unlock(&kafsasyncd_async_lock);
244 wake_up(&kafsasyncd_sleepq);
246 _leave("");