aboutsummaryrefslogtreecommitdiff
path: root/fs/afs/kafsasyncd.c
blob: 8ca01c2360137ec9a901229410aed310a72e458a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/* AFS asynchronous operation daemon
 *
 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 *
 * The AFS async daemon is used to the following:
 * - probe "dead" servers to see whether they've come back to life yet.
 * - probe "live" servers that we haven't talked to for a while to see if they are better
 *   candidates for serving than what we're currently using
 * - poll volume location servers to keep up to date volume location lists
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/completion.h>
#include <linux/freezer.h>
#include "cell.h"
#include "server.h"
#include "volume.h"
#include "kafsasyncd.h"
#include "kafstimod.h"
#include <rxrpc/call.h>
#include <asm/errno.h>
#include "internal.h"

static DECLARE_COMPLETION(kafsasyncd_alive);
static DECLARE_COMPLETION(kafsasyncd_dead);
static DECLARE_WAIT_QUEUE_HEAD(kafsasyncd_sleepq);
static struct task_struct *kafsasyncd_task;
static int kafsasyncd_die;

static int kafsasyncd(void *arg);

static LIST_HEAD(kafsasyncd_async_attnq);
static LIST_HEAD(kafsasyncd_async_busyq);
static DEFINE_SPINLOCK(kafsasyncd_async_lock);

static void kafsasyncd_null_call_attn_func(struct rxrpc_call *call)
{
}

static void kafsasyncd_null_call_error_func(struct rxrpc_call *call)
{
}

/*
 * start the async daemon
 */
int afs_kafsasyncd_start(void)
{
	int ret;

	ret = kernel_thread(kafsasyncd, NULL, 0);
	if (ret < 0)
		return ret;

	wait_for_completion(&kafsasyncd_alive);

	return ret;
}

/*
 * stop the async daemon
 */
void afs_kafsasyncd_stop(void)
{
	/* get rid of my daemon */
	kafsasyncd_die = 1;
	wake_up(&kafsasyncd_sleepq);
	wait_for_completion(&kafsasyncd_dead);
}

/*
 * probing daemon
 */
static int kafsasyncd(void *arg)
{
	struct afs_async_op *op;
	int die;

	DECLARE_WAITQUEUE(myself, current);

	kafsasyncd_task = current;

	printk("kAFS: Started kafsasyncd %d\n", current->pid);

	daemonize("kafsasyncd");

	complete(&kafsasyncd_alive);

	/* loop around looking for things to attend to */
	do {
		set_current_state(TASK_INTERRUPTIBLE);
		add_wait_queue(&kafsasyncd_sleepq, &myself);

		for (;;) {
			if (!list_empty(&kafsasyncd_async_attnq) ||
			    signal_pending(current) ||
			    kafsasyncd_die)
				break;

			schedule();
			set_current_state(TASK_INTERRUPTIBLE);
		}

		remove_wait_queue(&kafsasyncd_sleepq, &myself);
		set_current_state(TASK_RUNNING);

		try_to_freeze();

		/* discard pending signals */
		afs_discard_my_signals();

		die = kafsasyncd_die;

		/* deal with the next asynchronous operation requiring
		 * attention */
		if (!list_empty(&kafsasyncd_async_attnq)) {
			struct afs_async_op *op;

			_debug("@@@ Begin Asynchronous Operation");

			op = NULL;
			spin_lock(&kafsasyncd_async_lock);

			if (!list_empty(&kafsasyncd_async_attnq)) {
				op = list_entry(kafsasyncd_async_attnq.next,
						struct afs_async_op, link);
				list_move_tail(&op->link,
					      &kafsasyncd_async_busyq);
			}

			spin_unlock(&kafsasyncd_async_lock);

			_debug("@@@ Operation %p {%p}\n",
			       op, op ? op->ops : NULL);

			if (op)
				op->ops->attend(op);

			_debug("@@@ End Asynchronous Operation");
		}

	} while(!die);

	/* need to kill all outstanding asynchronous operations before
	 * exiting */
	kafsasyncd_task = NULL;
	spin_lock(&kafsasyncd_async_lock);

	/* fold the busy and attention queues together */
	list_splice_init(&kafsasyncd_async_busyq,
			 &kafsasyncd_async_attnq);

	/* dequeue kafsasyncd from all their wait queues */
	list_for_each_entry(op, &kafsasyncd_async_attnq, link) {
		op->call->app_attn_func = kafsasyncd_null_call_attn_func;
		op->call->app_error_func = kafsasyncd_null_call_error_func;
		remove_wait_queue(&op->call->waitq, &op->waiter);
	}

	spin_unlock(&kafsasyncd_async_lock);

	/* abort all the operations */
	while (!list_empty(&kafsasyncd_async_attnq)) {
		op = list_entry(kafsasyncd_async_attnq.next, struct afs_async_op, link);
		list_del_init(&op->link);

		rxrpc_call_abort(op->call, -EIO);
		rxrpc_put_call(op->call);
		op->call = NULL;

		op->ops->discard(op);
	}

	/* and that's all */
	_leave("");
	complete_and_exit(&kafsasyncd_dead, 0);
}

/*
 * begin an operation
 * - place operation on busy queue
 */
void afs_kafsasyncd_begin_op(struct afs_async_op *op)
{
	_enter("");

	spin_lock(&kafsasyncd_async_lock);

	init_waitqueue_entry(&op->waiter, kafsasyncd_task);
	add_wait_queue(&op->call->waitq, &op->waiter);

	list_move_tail(&op->link, &kafsasyncd_async_busyq);

	spin_unlock(&kafsasyncd_async_lock);

	_leave("");
}

/*
 * request attention for an operation
 * - move to attention queue
 */
void afs_kafsasyncd_attend_op(struct afs_async_op *op)
{
	_enter("");

	spin_lock(&kafsasyncd_async_lock);

	list_move_tail(&op->link, &kafsasyncd_async_attnq);

	spin_unlock(&kafsasyncd_async_lock);

	wake_up(&kafsasyncd_sleepq);

	_leave("");
}

/*
 * terminate an operation
 * - remove from either queue
 */
void afs_kafsasyncd_terminate_op(struct afs_async_op *op)
{
	_enter("");

	spin_lock(&kafsasyncd_async_lock);

	if (!list_empty(&op->link)) {
		list_del_init(&op->link);
		remove_wait_queue(&op->call->waitq, &op->waiter);
	}

	spin_unlock(&kafsasyncd_async_lock);

	wake_up(&kafsasyncd_sleepq);

	_leave("");
}