cmservice.c 7.5 KB
Newer Older
D
David Howells 已提交
1
/* AFS Cache Manager Service
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
 *
 * 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/module.h>
#include <linux/init.h>
#include <linux/sched.h>
15
#include <linux/ip.h>
L
Linus Torvalds 已提交
16
#include "internal.h"
17
#include "afs_cm.h"
L
Linus Torvalds 已提交
18

19
struct workqueue_struct *afs_cm_workqueue;
L
Linus Torvalds 已提交
20

21 22 23 24 25
static int afs_deliver_cb_init_call_back_state(struct afs_call *,
					       struct sk_buff *, bool);
static int afs_deliver_cb_probe(struct afs_call *, struct sk_buff *, bool);
static int afs_deliver_cb_callback(struct afs_call *, struct sk_buff *, bool);
static void afs_cm_destructor(struct afs_call *);
L
Linus Torvalds 已提交
26 27

/*
28
 * CB.CallBack operation type
L
Linus Torvalds 已提交
29
 */
30
static const struct afs_call_type afs_SRXCBCallBack = {
D
David Howells 已提交
31
	.name		= "CB.CallBack",
32 33 34 35
	.deliver	= afs_deliver_cb_callback,
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
};
L
Linus Torvalds 已提交
36 37

/*
38
 * CB.InitCallBackState operation type
L
Linus Torvalds 已提交
39
 */
40
static const struct afs_call_type afs_SRXCBInitCallBackState = {
D
David Howells 已提交
41
	.name		= "CB.InitCallBackState",
42 43 44 45
	.deliver	= afs_deliver_cb_init_call_back_state,
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
};
L
Linus Torvalds 已提交
46 47

/*
48
 * CB.Probe operation type
L
Linus Torvalds 已提交
49
 */
50
static const struct afs_call_type afs_SRXCBProbe = {
D
David Howells 已提交
51
	.name		= "CB.Probe",
52 53 54 55
	.deliver	= afs_deliver_cb_probe,
	.abort_to_error	= afs_abort_to_error,
	.destructor	= afs_cm_destructor,
};
L
Linus Torvalds 已提交
56 57

/*
58 59
 * route an incoming cache manager call
 * - return T if supported, F if not
L
Linus Torvalds 已提交
60
 */
61
bool afs_cm_incoming_call(struct afs_call *call)
L
Linus Torvalds 已提交
62
{
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	u32 operation_id = ntohl(call->operation_ID);

	_enter("{CB.OP %u}", operation_id);

	switch (operation_id) {
	case CBCallBack:
		call->type = &afs_SRXCBCallBack;
		return true;
	case CBInitCallBackState:
		call->type = &afs_SRXCBInitCallBackState;
		return true;
	case CBProbe:
		call->type = &afs_SRXCBProbe;
		return true;
	default:
		return false;
L
Linus Torvalds 已提交
79
	}
D
David Howells 已提交
80
}
L
Linus Torvalds 已提交
81 82

/*
83
 * clean up a cache manager call
L
Linus Torvalds 已提交
84
 */
85
static void afs_cm_destructor(struct afs_call *call)
L
Linus Torvalds 已提交
86
{
87 88 89 90 91 92
	_enter("");

	afs_put_server(call->server);
	call->server = NULL;
	kfree(call->buffer);
	call->buffer = NULL;
D
David Howells 已提交
93
}
L
Linus Torvalds 已提交
94 95

/*
96
 * allow the fileserver to see if the cache manager is still alive
L
Linus Torvalds 已提交
97
 */
98
static void SRXAFSCB_CallBack(struct work_struct *work)
L
Linus Torvalds 已提交
99
{
100
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
101

102
	_enter("");
L
Linus Torvalds 已提交
103

104 105 106 107 108 109
	/* be sure to send the reply *before* attempting to spam the AFS server
	 * with FSFetchStatus requests on the vnodes with broken callbacks lest
	 * the AFS server get into a vicious cycle of trying to break further
	 * callbacks because it hadn't received completion of the CBCallBack op
	 * yet */
	afs_send_empty_reply(call);
L
Linus Torvalds 已提交
110

111 112
	afs_break_callbacks(call->server, call->count, call->request);
	_leave("");
D
David Howells 已提交
113
}
L
Linus Torvalds 已提交
114 115

/*
116
 * deliver request data to a CB.CallBack call
L
Linus Torvalds 已提交
117
 */
118 119
static int afs_deliver_cb_callback(struct afs_call *call, struct sk_buff *skb,
				   bool last)
L
Linus Torvalds 已提交
120
{
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	struct afs_callback *cb;
	struct afs_server *server;
	struct in_addr addr;
	__be32 *bp;
	u32 tmp;
	int ret, loop;

	_enter("{%u},{%u},%d", call->unmarshall, skb->len, last);

	switch (call->unmarshall) {
	case 0:
		call->offset = 0;
		call->unmarshall++;

		/* extract the FID array and its count in two steps */
	case 1:
		_debug("extract FID count");
		ret = afs_extract_data(call, skb, last, &call->tmp, 4);
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
L
Linus Torvalds 已提交
143 144
		}

145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		call->count = ntohl(call->tmp);
		_debug("FID count: %u", call->count);
		if (call->count > AFSCBMAX)
			return -EBADMSG;

		call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
		if (!call->buffer)
			return -ENOMEM;
		call->offset = 0;
		call->unmarshall++;

	case 2:
		_debug("extract FID array");
		ret = afs_extract_data(call, skb, last, call->buffer,
				       call->count * 3 * 4);
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
L
Linus Torvalds 已提交
164 165
		}

166 167 168 169 170 171 172 173 174 175 176 177 178 179
		_debug("unmarshall FID array");
		call->request = kcalloc(call->count,
					sizeof(struct afs_callback),
					GFP_KERNEL);
		if (!call->request)
			return -ENOMEM;

		cb = call->request;
		bp = call->buffer;
		for (loop = call->count; loop > 0; loop--, cb++) {
			cb->fid.vid	= ntohl(*bp++);
			cb->fid.vnode	= ntohl(*bp++);
			cb->fid.unique	= ntohl(*bp++);
			cb->type	= AFSCM_CB_UNTYPED;
L
Linus Torvalds 已提交
180 181
		}

182 183 184 185 186 187 188 189 190 191 192 193
		call->offset = 0;
		call->unmarshall++;

		/* extract the callback array and its count in two steps */
	case 3:
		_debug("extract CB count");
		ret = afs_extract_data(call, skb, last, &call->tmp, 4);
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
		}
L
Linus Torvalds 已提交
194

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
		tmp = ntohl(call->tmp);
		_debug("CB count: %u", tmp);
		if (tmp != call->count && tmp != 0)
			return -EBADMSG;
		call->offset = 0;
		call->unmarshall++;
		if (tmp == 0)
			goto empty_cb_array;

	case 4:
		_debug("extract CB array");
		ret = afs_extract_data(call, skb, last, call->request,
				       call->count * 3 * 4);
		switch (ret) {
		case 0:		break;
		case -EAGAIN:	return 0;
		default:	return ret;
L
Linus Torvalds 已提交
212 213
		}

214 215 216 217 218 219 220 221
		_debug("unmarshall CB array");
		cb = call->request;
		bp = call->buffer;
		for (loop = call->count; loop > 0; loop--, cb++) {
			cb->version	= ntohl(*bp++);
			cb->expiry	= ntohl(*bp++);
			cb->type	= ntohl(*bp++);
		}
L
Linus Torvalds 已提交
222

223 224 225
	empty_cb_array:
		call->offset = 0;
		call->unmarshall++;
L
Linus Torvalds 已提交
226

227 228 229 230
	case 5:
		_debug("trailer");
		if (skb->len != 0)
			return -EBADMSG;
L
Linus Torvalds 已提交
231 232 233
		break;
	}

234 235
	if (!last)
		return 0;
L
Linus Torvalds 已提交
236

237
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
238

239 240 241 242 243 244 245 246 247 248 249
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
	memcpy(&addr, &ip_hdr(skb)->saddr, 4);
	server = afs_find_server(&addr);
	if (!server)
		return -ENOTCONN;
	call->server = server;

	INIT_WORK(&call->work, SRXAFSCB_CallBack);
	schedule_work(&call->work);
	return 0;
D
David Howells 已提交
250
}
L
Linus Torvalds 已提交
251 252

/*
253
 * allow the fileserver to request callback state (re-)initialisation
L
Linus Torvalds 已提交
254
 */
255
static void SRXAFSCB_InitCallBackState(struct work_struct *work)
L
Linus Torvalds 已提交
256
{
257
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
258

259
	_enter("{%p}", call->server);
L
Linus Torvalds 已提交
260

261 262 263
	afs_init_callback_state(call->server);
	afs_send_empty_reply(call);
	_leave("");
D
David Howells 已提交
264
}
L
Linus Torvalds 已提交
265 266

/*
267
 * deliver request data to a CB.InitCallBackState call
L
Linus Torvalds 已提交
268
 */
269 270 271
static int afs_deliver_cb_init_call_back_state(struct afs_call *call,
					       struct sk_buff *skb,
					       bool last)
L
Linus Torvalds 已提交
272 273
{
	struct afs_server *server;
274
	struct in_addr addr;
L
Linus Torvalds 已提交
275

276
	_enter(",{%u},%d", skb->len, last);
L
Linus Torvalds 已提交
277

278 279 280 281
	if (skb->len > 0)
		return -EBADMSG;
	if (!last)
		return 0;
L
Linus Torvalds 已提交
282

283 284
	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
285

286 287 288 289 290 291 292
	/* we'll need the file server record as that tells us which set of
	 * vnodes to operate upon */
	memcpy(&addr, &ip_hdr(skb)->saddr, 4);
	server = afs_find_server(&addr);
	if (!server)
		return -ENOTCONN;
	call->server = server;
L
Linus Torvalds 已提交
293

294 295 296 297
	INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
	schedule_work(&call->work);
	return 0;
}
L
Linus Torvalds 已提交
298

299 300 301 302 303 304
/*
 * allow the fileserver to see if the cache manager is still alive
 */
static void SRXAFSCB_Probe(struct work_struct *work)
{
	struct afs_call *call = container_of(work, struct afs_call, work);
L
Linus Torvalds 已提交
305

306 307 308 309
	_enter("");
	afs_send_empty_reply(call);
	_leave("");
}
L
Linus Torvalds 已提交
310

311 312 313 314 315 316 317
/*
 * deliver request data to a CB.Probe call
 */
static int afs_deliver_cb_probe(struct afs_call *call, struct sk_buff *skb,
				bool last)
{
	_enter(",{%u},%d", skb->len, last);
L
Linus Torvalds 已提交
318

319 320 321 322
	if (skb->len > 0)
		return -EBADMSG;
	if (!last)
		return 0;
L
Linus Torvalds 已提交
323

324 325
	/* no unmarshalling required */
	call->state = AFS_CALL_REPLYING;
L
Linus Torvalds 已提交
326

327 328 329
	INIT_WORK(&call->work, SRXAFSCB_Probe);
	schedule_work(&call->work);
	return 0;
D
David Howells 已提交
330
}