9 #include <linux/types.h>
10 #include <linux/kernel.h>
12 #include <linux/slab.h>
19 #include <asm/unaligned.h>
23 #define NLMDBG_FACILITY NLMDBG_MONITOR
24 #define NSM_PROGRAM 100024
62 static inline struct sockaddr *nsm_addr(
const struct nsm_handle *nsm)
64 return (
struct sockaddr *)&nsm->sm_addr;
73 struct rpc_create_args
args = {
75 .protocol = XPRT_TRANSPORT_TCP,
77 .addrsize =
sizeof(sin),
78 .servername =
"rpc.statd",
79 .program = &nsm_program,
81 .authflavor = RPC_AUTH_NULL,
82 .flags = RPC_CLNT_CREATE_NOPING,
104 static struct rpc_clnt *nsm_client_get(
struct net *net)
109 clnt = nsm_client_set(ln,
NULL);
113 clnt =
new = nsm_create(net);
117 clnt = nsm_client_set(ln,
new);
124 static void nsm_client_put(
struct net *net)
140 static int nsm_mon_unmon(
struct nsm_handle *nsm,
u32 proc,
struct nsm_res *
res,
145 .
priv = &nsm->sm_priv,
149 .mon_name = nsm->sm_mon_name,
159 memset(res, 0,
sizeof(*res));
164 dprintk(
"lockd: NSM upcall RPC failed, status=%d\n",
184 struct nsm_handle *nsm = host->h_nsmhandle;
189 dprintk(
"lockd: nsm_monitor(%s)\n", nsm->sm_name);
191 if (nsm->sm_monitored)
200 clnt = nsm_client_get(host->net);
202 status = PTR_ERR(clnt);
203 dprintk(
"lockd: failed to create NSM upcall transport, "
204 "status=%d, net=%p\n", status, host->net);
208 status = nsm_mon_unmon(nsm,
NSMPROC_MON, &res, clnt);
216 nsm->sm_monitored = 1;
234 struct nsm_handle *nsm = host->h_nsmhandle;
239 && nsm->sm_monitored && !nsm->sm_sticky) {
242 dprintk(
"lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
251 nsm->sm_monitored = 0;
253 nsm_client_put(host->net);
257 static struct nsm_handle *nsm_lookup_hostname(
const char *hostname,
260 struct nsm_handle *nsm;
264 memcmp(nsm->sm_name, hostname, len) == 0)
271 struct nsm_handle *nsm;
274 if (rpc_cmp_addr(nsm_addr(nsm), sap))
281 struct nsm_handle *nsm;
307 static
void nsm_init_private(
struct nsm_handle *nsm)
309 u64 *
p = (
u64 *)&nsm->sm_priv.data;
314 ns = timespec_to_ns(&
ts);
319 static struct nsm_handle *nsm_create_handle(
const struct sockaddr *sap,
321 const char *hostname,
324 struct nsm_handle *
new;
326 new = kzalloc(
sizeof(*
new) + hostname_len + 1,
GFP_KERNEL);
331 new->sm_name = (
char *)(
new + 1);
332 memcpy(nsm_addr(
new), sap, salen);
333 new->sm_addrlen = salen;
334 nsm_init_private(
new);
336 if (
rpc_ntop(nsm_addr(
new), new->sm_addrbuf,
337 sizeof(new->sm_addrbuf)) == 0)
338 (
void)
snprintf(new->sm_addrbuf,
sizeof(new->sm_addrbuf),
339 "unsupported address family");
340 memcpy(new->sm_name, hostname, hostname_len);
361 const size_t salen,
const char *hostname,
362 const size_t hostname_len)
366 if (hostname &&
memchr(hostname,
'/', hostname_len) != NULL) {
367 if (printk_ratelimit()) {
369 "in NFS lock request\n",
370 (
int)hostname_len, hostname);
376 spin_lock(&nsm_lock);
379 cached = nsm_lookup_hostname(hostname, hostname_len);
381 cached = nsm_lookup_addr(sap);
383 if (cached != NULL) {
385 spin_unlock(&nsm_lock);
387 dprintk(
"lockd: found nsm_handle for %s (%s), "
388 "cnt %d\n", cached->sm_name,
395 list_add(&new->sm_link, &nsm_handles);
396 spin_unlock(&nsm_lock);
397 dprintk(
"lockd: created nsm_handle for %s (%s)\n",
398 new->sm_name, new->sm_addrbuf);
402 spin_unlock(&nsm_lock);
404 new = nsm_create_handle(sap, salen, hostname, hostname_len);
420 struct nsm_handle *
cached;
422 spin_lock(&nsm_lock);
424 cached = nsm_lookup_priv(&info->
priv);
426 spin_unlock(&nsm_lock);
427 dprintk(
"lockd: never saw rebooted peer '%.*s' before\n",
433 spin_unlock(&nsm_lock);
435 dprintk(
"lockd: host %s (%s) rebooted, cnt %d\n",
436 cached->sm_name, cached->sm_addrbuf,
450 spin_unlock(&nsm_lock);
451 dprintk(
"lockd: destroyed nsm_handle for %s (%s)\n",
452 nsm->sm_name, nsm->sm_addrbuf);
464 static void encode_nsm_string(
struct xdr_stream *xdr,
const char *
string)
477 static void encode_mon_name(
struct xdr_stream *xdr,
const struct nsm_args *
argp)
479 encode_nsm_string(xdr, argp->
mon_name);
488 static void encode_my_id(
struct xdr_stream *xdr,
const struct nsm_args *
argp)
492 encode_nsm_string(xdr, argp->
nodename);
503 static void encode_mon_id(
struct xdr_stream *xdr,
const struct nsm_args *
argp)
505 encode_mon_name(xdr, argp);
506 encode_my_id(xdr, argp);
514 static void encode_priv(
struct xdr_stream *xdr,
const struct nsm_args *
argp)
522 static void nsm_xdr_enc_mon(
struct rpc_rqst *
req,
struct xdr_stream *xdr,
525 encode_mon_id(xdr, argp);
526 encode_priv(xdr, argp);
529 static void nsm_xdr_enc_unmon(
struct rpc_rqst *
req,
struct xdr_stream *xdr,
532 encode_mon_id(xdr, argp);
535 static int nsm_xdr_dec_stat_res(
struct rpc_rqst *rqstp,
536 struct xdr_stream *xdr,
547 dprintk(
"lockd: %s status %d state %d\n",
552 static int nsm_xdr_dec_stat(
struct rpc_rqst *rqstp,
553 struct xdr_stream *xdr,
567 #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
568 #define SM_my_id_sz (SM_my_name_sz+3)
569 #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
570 #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
571 #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
572 #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
573 #define SM_monres_sz 2
574 #define SM_unmonres_sz 1
579 .p_encode = (kxdreproc_t)nsm_xdr_enc_mon,
580 .
p_decode = (kxdrdproc_t)nsm_xdr_dec_stat_res,
588 .p_encode = (kxdreproc_t)nsm_xdr_enc_unmon,
589 .
p_decode = (kxdrdproc_t)nsm_xdr_dec_stat,
593 .p_name =
"UNMONITOR",
600 .procs = nsm_procedures
613 .version = nsm_version,