mon.c 7.7 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * linux/fs/lockd/mon.c
 *
 * The kernel statd client.
 *
 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
 */

#include <linux/types.h>
#include <linux/utsname.h>
#include <linux/kernel.h>
#include <linux/sunrpc/clnt.h>
13
#include <linux/sunrpc/xprtsock.h>
L
Linus Torvalds 已提交
14 15 16 17 18 19
#include <linux/sunrpc/svc.h>
#include <linux/lockd/lockd.h>
#include <linux/lockd/sm_inter.h>


#define NLMDBG_FACILITY		NLMDBG_MONITOR
20 21 22 23 24 25 26 27 28 29 30 31
#define NSM_PROGRAM		100024
#define NSM_VERSION		1

enum {
	NSMPROC_NULL,
	NSMPROC_STAT,
	NSMPROC_MON,
	NSMPROC_UNMON,
	NSMPROC_UNMON_ALL,
	NSMPROC_SIMU_CRASH,
	NSMPROC_NOTIFY,
};
L
Linus Torvalds 已提交
32

33 34 35 36 37 38 39 40 41 42 43 44 45 46
struct nsm_args {
	__be32			addr;		/* remote address */
	u32			prog;		/* RPC callback info */
	u32			vers;
	u32			proc;

	char			*mon_name;
};

struct nsm_res {
	u32			status;
	u32			state;
};

L
Linus Torvalds 已提交
47 48 49 50 51 52 53
static struct rpc_clnt *	nsm_create(void);

static struct rpc_program	nsm_program;

/*
 * Local NSM state
 */
54
int				nsm_local_state;
L
Linus Torvalds 已提交
55 56

/*
57
 * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
L
Linus Torvalds 已提交
58 59
 */
static int
60
nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
L
Linus Torvalds 已提交
61 62 63
{
	struct rpc_clnt	*clnt;
	int		status;
64 65 66 67 68
	struct nsm_args args = {
		.addr		= nsm_addr_in(nsm)->sin_addr.s_addr,
		.prog		= NLM_PROGRAM,
		.vers		= 3,
		.proc		= NLMPROC_NSM_NOTIFY,
69
		.mon_name	= nsm->sm_mon_name,
70
	};
C
Chuck Lever 已提交
71 72 73 74
	struct rpc_message msg = {
		.rpc_argp	= &args,
		.rpc_resp	= res,
	};
L
Linus Torvalds 已提交
75 76 77 78

	clnt = nsm_create();
	if (IS_ERR(clnt)) {
		status = PTR_ERR(clnt);
79 80
		dprintk("lockd: failed to create NSM upcall transport, "
				"status=%d\n", status);
L
Linus Torvalds 已提交
81 82 83 84 85
		goto out;
	}

	memset(res, 0, sizeof(*res));

C
Chuck Lever 已提交
86 87
	msg.rpc_proc = &clnt->cl_procinfo[proc];
	status = rpc_call_sync(clnt, &msg, 0);
L
Linus Torvalds 已提交
88
	if (status < 0)
89 90
		dprintk("lockd: NSM upcall RPC failed, status=%d\n",
				status);
L
Linus Torvalds 已提交
91 92
	else
		status = 0;
93
	rpc_shutdown_client(clnt);
L
Linus Torvalds 已提交
94 95 96 97
 out:
	return status;
}

98 99 100 101 102 103 104 105 106 107
/**
 * nsm_monitor - Notify a peer in case we reboot
 * @host: pointer to nlm_host of peer to notify
 *
 * If this peer is not already monitored, this function sends an
 * upcall to the local rpc.statd to record the name/address of
 * the peer to notify in case we reboot.
 *
 * Returns zero if the peer is monitored by the local rpc.statd;
 * otherwise a negative errno value is returned.
L
Linus Torvalds 已提交
108
 */
109
int nsm_monitor(const struct nlm_host *host)
L
Linus Torvalds 已提交
110
{
111
	struct nsm_handle *nsm = host->h_nsmhandle;
L
Linus Torvalds 已提交
112 113 114
	struct nsm_res	res;
	int		status;

115
	dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
116 117

	if (nsm->sm_monitored)
118
		return 0;
L
Linus Torvalds 已提交
119

120 121 122 123 124 125
	/*
	 * Choose whether to record the caller_name or IP address of
	 * this peer in the local rpc.statd's database.
	 */
	nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;

126
	status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
127 128 129
	if (res.status != 0)
		status = -EIO;
	if (status < 0)
130
		printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
L
Linus Torvalds 已提交
131
	else
132
		nsm->sm_monitored = 1;
L
Linus Torvalds 已提交
133 134 135
	return status;
}

136 137 138 139 140 141 142
/**
 * nsm_unmonitor - Unregister peer notification
 * @host: pointer to nlm_host of peer to stop monitoring
 *
 * If this peer is monitored, this function sends an upcall to
 * tell the local rpc.statd not to send this peer a notification
 * when we reboot.
L
Linus Torvalds 已提交
143
 */
144
void nsm_unmonitor(const struct nlm_host *host)
L
Linus Torvalds 已提交
145
{
146
	struct nsm_handle *nsm = host->h_nsmhandle;
L
Linus Torvalds 已提交
147
	struct nsm_res	res;
148
	int status;
L
Linus Torvalds 已提交
149

150 151
	if (atomic_read(&nsm->sm_count) == 1
	 && nsm->sm_monitored && !nsm->sm_sticky) {
152
		dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
153

154
		status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
155 156
		if (res.status != 0)
			status = -EIO;
157
		if (status < 0)
158
			printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
159
					nsm->sm_name);
160 161
		else
			nsm->sm_monitored = 0;
162
	}
L
Linus Torvalds 已提交
163 164 165 166 167 168 169 170
}

/*
 * Create NSM client for the local host
 */
static struct rpc_clnt *
nsm_create(void)
{
171 172 173 174 175 176
	struct sockaddr_in	sin = {
		.sin_family	= AF_INET,
		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
		.sin_port	= 0,
	};
	struct rpc_create_args args = {
177
		.protocol	= XPRT_TRANSPORT_UDP,
178 179 180 181
		.address	= (struct sockaddr *)&sin,
		.addrsize	= sizeof(sin),
		.servername	= "localhost",
		.program	= &nsm_program,
182
		.version	= NSM_VERSION,
183 184 185 186
		.authflavor	= RPC_AUTH_NULL,
	};

	return rpc_create(&args);
L
Linus Torvalds 已提交
187 188 189 190
}

/*
 * XDR functions for NSM.
191 192 193
 *
 * See http://www.opengroup.org/ for details on the Network
 * Status Monitor wire protocol.
L
Linus Torvalds 已提交
194 195
 */

196 197 198 199 200 201 202 203 204
static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
{
	size_t len = strlen(string);

	if (len > SM_MAXSTRLEN)
		len = SM_MAXSTRLEN;
	return xdr_encode_opaque(p, string, len);
}

205 206 207 208 209
/*
 * "mon_name" specifies the host to be monitored.
 */
static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
{
210
	return xdr_encode_nsm_string(p, argp->mon_name);
211 212
}

213 214 215
/*
 * The "my_id" argument specifies the hostname and RPC procedure
 * to be called when the status manager receives notification
216
 * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
 * has changed.
 */
static __be32 *xdr_encode_my_id(__be32 *p, struct nsm_args *argp)
{
	p = xdr_encode_nsm_string(p, utsname()->nodename);
	if (!p)
		return ERR_PTR(-EIO);

	*p++ = htonl(argp->prog);
	*p++ = htonl(argp->vers);
	*p++ = htonl(argp->proc);

	return p;
}

232 233
/*
 * The "mon_id" argument specifies the non-private arguments
234
 * of an NSMPROC_MON or NSMPROC_UNMON call.
235 236 237 238 239 240 241 242 243 244
 */
static __be32 *xdr_encode_mon_id(__be32 *p, struct nsm_args *argp)
{
	p = xdr_encode_mon_name(p, argp);
	if (!p)
		return ERR_PTR(-EIO);

	return xdr_encode_my_id(p, argp);
}

245 246
/*
 * The "priv" argument may contain private information required
247 248
 * by the NSMPROC_MON call. This information will be supplied in the
 * NLMPROC_SM_NOTIFY call.
249 250 251 252 253 254 255 256 257 258 259 260 261 262
 *
 * Linux provides the raw IP address of the monitored host,
 * left in network byte order.
 */
static __be32 *xdr_encode_priv(__be32 *p, struct nsm_args *argp)
{
	*p++ = argp->addr;
	*p++ = 0;
	*p++ = 0;
	*p++ = 0;

	return p;
}

L
Linus Torvalds 已提交
263
static int
A
Al Viro 已提交
264
xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
L
Linus Torvalds 已提交
265
{
266
	p = xdr_encode_mon_id(p, argp);
L
Linus Torvalds 已提交
267 268
	if (IS_ERR(p))
		return PTR_ERR(p);
269

270 271 272 273
	p = xdr_encode_priv(p, argp);
	if (IS_ERR(p))
		return PTR_ERR(p);

L
Linus Torvalds 已提交
274 275 276 277 278
	rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
	return 0;
}

static int
A
Al Viro 已提交
279
xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
L
Linus Torvalds 已提交
280
{
281
	p = xdr_encode_mon_id(p, argp);
L
Linus Torvalds 已提交
282 283 284 285 286 287 288
	if (IS_ERR(p))
		return PTR_ERR(p);
	rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
	return 0;
}

static int
A
Al Viro 已提交
289
xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
L
Linus Torvalds 已提交
290 291 292 293 294 295 296 297 298
{
	resp->status = ntohl(*p++);
	resp->state = ntohl(*p++);
	dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
			resp->status, resp->state);
	return 0;
}

static int
A
Al Viro 已提交
299
xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
L
Linus Torvalds 已提交
300 301 302 303 304 305
{
	resp->state = ntohl(*p++);
	return 0;
}

#define SM_my_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
306 307 308
#define SM_my_id_sz	(SM_my_name_sz+3)
#define SM_mon_name_sz	(1+XDR_QUADLEN(SM_MAXSTRLEN))
#define SM_mon_id_sz	(SM_mon_name_sz+SM_my_id_sz)
309 310
#define SM_priv_sz	(XDR_QUADLEN(SM_PRIV_SIZE))
#define SM_mon_sz	(SM_mon_id_sz+SM_priv_sz)
L
Linus Torvalds 已提交
311 312 313 314
#define SM_monres_sz	2
#define SM_unmonres_sz	1

static struct rpc_procinfo	nsm_procedures[] = {
315 316
[NSMPROC_MON] = {
		.p_proc		= NSMPROC_MON,
L
Linus Torvalds 已提交
317 318
		.p_encode	= (kxdrproc_t) xdr_encode_mon,
		.p_decode	= (kxdrproc_t) xdr_decode_stat_res,
319 320
		.p_arglen	= SM_mon_sz,
		.p_replen	= SM_monres_sz,
321
		.p_statidx	= NSMPROC_MON,
322
		.p_name		= "MONITOR",
L
Linus Torvalds 已提交
323
	},
324 325
[NSMPROC_UNMON] = {
		.p_proc		= NSMPROC_UNMON,
L
Linus Torvalds 已提交
326 327
		.p_encode	= (kxdrproc_t) xdr_encode_unmon,
		.p_decode	= (kxdrproc_t) xdr_decode_stat,
328 329
		.p_arglen	= SM_mon_id_sz,
		.p_replen	= SM_unmonres_sz,
330
		.p_statidx	= NSMPROC_UNMON,
331
		.p_name		= "UNMONITOR",
L
Linus Torvalds 已提交
332 333 334 335
	},
};

static struct rpc_version	nsm_version1 = {
336 337
		.number		= 1,
		.nrprocs	= ARRAY_SIZE(nsm_procedures),
L
Linus Torvalds 已提交
338 339 340 341 342 343 344 345 346 347 348
		.procs		= nsm_procedures
};

static struct rpc_version *	nsm_version[] = {
	[1] = &nsm_version1,
};

static struct rpc_stat		nsm_stats;

static struct rpc_program	nsm_program = {
		.name		= "statd",
349
		.number		= NSM_PROGRAM,
350
		.nrvers		= ARRAY_SIZE(nsm_version),
L
Linus Torvalds 已提交
351 352 353
		.version	= nsm_version,
		.stats		= &nsm_stats
};