aboutsummaryrefslogtreecommitdiffstats
path: root/net/rxrpc/krxiod.c
blob: 49effd92144e2908ea95ce6ecdff1fb4be7a6940 (plain) (blame)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* krxiod.c: Rx I/O 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.
 */

#include <linux/sched.h>
#include <linux/completion.h>
#include <linux/spinlock.h>
#include <linux/init.h>
#include <linux/freezer.h>
#include <rxrpc/krxiod.h>
#include <rxrpc/transport.h>
#include <rxrpc/peer.h>
#include <rxrpc/call.h>
#include "internal.h"

static DECLARE_WAIT_QUEUE_HEAD(rxrpc_krxiod_sleepq);
static DECLARE_COMPLETION(rxrpc_krxiod_dead);

static atomic_t rxrpc_krxiod_qcount = ATOMIC_INIT(0);

static LIST_HEAD(rxrpc_krxiod_transportq);
static DEFINE_SPINLOCK(rxrpc_krxiod_transportq_lock);

static LIST_HEAD(rxrpc_krxiod_callq);
static DEFINE_SPINLOCK(rxrpc_krxiod_callq_lock);

static volatile int rxrpc_krxiod_die;

/*****************************************************************************/
/*
 * Rx I/O daemon
 */
static int rxrpc_krxiod(void *arg)
{
	DECLARE_WAITQUEUE(krxiod,current);

	printk("Started krxiod %d\n",current->pid);

	daemonize("krxiod");

	/* loop around waiting for work to do */
	do {
		/* wait for work or to be told to exit */
		_debug("### Begin Wait");
		if (!atomic_read(&rxrpc_krxiod_qcount)) {
			set_current_state(TASK_INTERRUPTIBLE);

			add_wait_queue(&rxrpc_krxiod_sleepq, &krxiod);

			for (;;) {
				set_current_state(TASK_INTERRUPTIBLE);
				if (atomic_read(&rxrpc_krxiod_qcount) ||
				    rxrpc_krxiod_die ||
				    signal_pending(current))
					break;

				schedule();
			}

			remove_wait_queue(&rxrpc_krxiod_sleepq, &krxiod);
			set_current_state(TASK_RUNNING);
		}
		_debug("### End Wait");

		/* do work if been given some to do */
		_debug("### Begin Work");

		/* see if there's a transport in need of attention */
		if (!list_empty(&rxrpc_krxiod_transportq)) {
			struct rxrpc_transport *trans = NULL;

			spin_lock_irq(&rxrpc_krxiod_transportq_lock);

			if (!list_empty(&rxrpc_krxiod_transportq)) {
				trans = list_entry(
					rxrpc_krxiod_transportq.next,
					struct rxrpc_transport,
					krxiodq_link);

				list_del_init(&trans->krxiodq_link);
				atomic_dec(&rxrpc_krxiod_qcount);

				/* make sure it hasn't gone away and doesn't go
				 * away */
				if (atomic_read(&trans->usage)>0)
					rxrpc_get_transport(trans);
				else
					trans = NULL;
			}

			spin_unlock_irq(&rxrpc_krxiod_transportq_lock);

			if (trans) {
				rxrpc_trans_receive_packet(trans);
				rxrpc_put_transport(trans);
			}
		}

		/* see if there's a call in need of attention */
		if (!list_empty(&rxrpc_krxiod_callq)) {
			struct rxrpc_call *call = NULL;

			spin_lock_irq(&rxrpc_krxiod_callq_lock);

			if (!list_empty(&rxrpc_krxiod_callq)) {
				call = list_entry(rxrpc_krxiod_callq.next,
						  struct rxrpc_call,
						  rcv_krxiodq_lk);
				list_del_init(&call->rcv_krxiodq_lk);
				atomic_dec(&rxrpc_krxiod_qcount);

				/* make sure it hasn't gone away and doesn't go
				 * away */
				if (atomic_read(&call->usage) > 0) {
					_debug("@@@ KRXIOD"
					       " Begin Attend Call %p", call);
					rxrpc_get_call(call);
				}
				else {
					call = NULL;
				}
			}

			spin_unlock_irq(&rxrpc_krxiod_callq_lock);

			if (call) {
				rxrpc_call_do_stuff(call);
				rxrpc_put_call(call);
				_debug("@@@ KRXIOD End Attend Call %p", call);
			}
		}

		_debug("### End Work");

		try_to_freeze();

                /* discard pending signals */
		rxrpc_discard_my_signals();

	} while (!rxrpc_krxiod_die);

	/* and that's all */
	complete_and_exit(&rxrpc_krxiod_dead, 0);

} /* end rxrpc_krxiod() */

/*****************************************************************************/
/*
 * start up a krxiod daemon
 */
int __init rxrpc_krxiod_init(void)
{
	return kernel_thread(rxrpc_krxiod, NULL, 0);

} /* end rxrpc_krxiod_init() */

/*****************************************************************************/
/*
 * kill the krxiod daemon and wait for it to complete
 */
void rxrpc_krxiod_kill(void)
{
	rxrpc_krxiod_die = 1;
	wake_up_all(&rxrpc_krxiod_sleepq);
	wait_for_completion(&rxrpc_krxiod_dead);

} /* end rxrpc_krxiod_kill() */

/*****************************************************************************/
/*
 * queue a transport for attention by krxiod
 */
void rxrpc_krxiod_queue_transport(struct rxrpc_transport *trans)
{
	unsigned long flags;

	_enter("");

	if (list_empty(&trans->krxiodq_link)) {
		spin_lock_irqsave(&rxrpc_krxiod_transportq_lock, flags);

		if (list_empty(&trans->krxiodq_link)) {
			if (atomic_read(&trans->usage) > 0) {
				list_add_tail(&trans->krxiodq_link,
					      &rxrpc_krxiod_transportq);
				atomic_inc(&rxrpc_krxiod_qcount);
			}
		}

		spin_unlock_irqrestore(&rxrpc_krxiod_transportq_lock, flags);
		wake_up_all(&rxrpc_krxiod_sleepq);
	}

	_leave("");

} /* end rxrpc_krxiod_queue_transport() */

/*****************************************************************************/
/*
 * dequeue a transport from krxiod's attention queue
 */
void rxrpc_krxiod_dequeue_transport(struct rxrpc_transport *trans)
{
	unsigned long flags;

	_enter("");

	spin_lock_irqsave(&rxrpc_krxiod_transportq_lock, flags);
	if (!list_empty(&trans->krxiodq_link)) {
		list_del_init(&trans->krxiodq_link);
		atomic_dec(&rxrpc_krxiod_qcount);
	}
	spin_unlock_irqrestore(&rxrpc_krxiod_transportq_lock, flags);

	_leave("");

} /* end rxrpc_krxiod_dequeue_transport() */

/*****************************************************************************/
/*
 * queue a call for attention by krxiod
 */
void rxrpc_krxiod_queue_call(struct rxrpc_call *call)
{
	unsigned long flags;

	if (list_empty(&call->rcv_krxiodq_lk)) {
		spin_lock_irqsave(&rxrpc_krxiod_callq_lock, flags);
		if (atomic_read(&call->usage) > 0) {
			list_add_tail(&call->rcv_krxiodq_lk,
				      &rxrpc_krxiod_callq);
			atomic_inc(&rxrpc_krxiod_qcount);
		}
		spin_unlock_irqrestore(&rxrpc_krxiod_callq_lock, flags);
	}
	wake_up_all(&rxrpc_krxiod_sleepq);

} /* end rxrpc_krxiod_queue_call() */

/*****************************************************************************/
/*
 * dequeue a call from krxiod's attention queue
 */
void rxrpc_krxiod_dequeue_call(struct rxrpc_call *call)
{
	unsigned long flags;

	spin_lock_irqsave(&rxrpc_krxiod_callq_lock, flags);
	if (!list_empty(&call->rcv_krxiodq_lk)) {
		list_del_init(&call->rcv_krxiodq_lk);
		atomic_dec(&rxrpc_krxiod_qcount);
	}
	spin_unlock_irqrestore(&rxrpc_krxiod_callq_lock, flags);

} /* end rxrpc_krxiod_dequeue_call() */