mon.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * linux/fs/lockd/mon.c
  3. *
  4. * The kernel statd client.
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/utsname.h>
  10. #include <linux/kernel.h>
  11. #include <linux/sunrpc/clnt.h>
  12. #include <linux/sunrpc/svc.h>
  13. #include <linux/lockd/lockd.h>
  14. #include <linux/lockd/sm_inter.h>
  15. #define NLMDBG_FACILITY NLMDBG_MONITOR
  16. static struct rpc_clnt * nsm_create(void);
  17. static struct rpc_program nsm_program;
  18. /*
  19. * Local NSM state
  20. */
  21. u32 nsm_local_state;
  22. /*
  23. * Common procedure for SM_MON/SM_UNMON calls
  24. */
  25. static int
  26. nsm_mon_unmon(struct nlm_host *host, u32 proc, struct nsm_res *res)
  27. {
  28. struct rpc_clnt *clnt;
  29. int status;
  30. struct nsm_args args;
  31. struct rpc_message msg = {
  32. .rpc_argp = &args,
  33. .rpc_resp = res,
  34. };
  35. clnt = nsm_create();
  36. if (IS_ERR(clnt)) {
  37. status = PTR_ERR(clnt);
  38. goto out;
  39. }
  40. args.addr = host->h_addr.sin_addr.s_addr;
  41. args.proto= (host->h_proto<<1) | host->h_server;
  42. args.prog = NLM_PROGRAM;
  43. args.vers = host->h_version;
  44. args.proc = NLMPROC_NSM_NOTIFY;
  45. memset(res, 0, sizeof(*res));
  46. msg.rpc_proc = &clnt->cl_procinfo[proc];
  47. status = rpc_call_sync(clnt, &msg, 0);
  48. if (status < 0)
  49. printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
  50. status);
  51. else
  52. status = 0;
  53. out:
  54. return status;
  55. }
  56. /*
  57. * Set up monitoring of a remote host
  58. */
  59. int
  60. nsm_monitor(struct nlm_host *host)
  61. {
  62. struct nsm_res res;
  63. int status;
  64. dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
  65. status = nsm_mon_unmon(host, SM_MON, &res);
  66. if (status < 0 || res.status != 0)
  67. printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
  68. else
  69. host->h_monitored = 1;
  70. return status;
  71. }
  72. /*
  73. * Cease to monitor remote host
  74. */
  75. int
  76. nsm_unmonitor(struct nlm_host *host)
  77. {
  78. struct nsm_res res;
  79. int status;
  80. dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
  81. status = nsm_mon_unmon(host, SM_UNMON, &res);
  82. if (status < 0)
  83. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
  84. else
  85. host->h_monitored = 0;
  86. return status;
  87. }
  88. /*
  89. * Create NSM client for the local host
  90. */
  91. static struct rpc_clnt *
  92. nsm_create(void)
  93. {
  94. struct rpc_xprt *xprt;
  95. struct rpc_clnt *clnt;
  96. struct sockaddr_in sin;
  97. sin.sin_family = AF_INET;
  98. sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  99. sin.sin_port = 0;
  100. xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
  101. if (IS_ERR(xprt))
  102. return (struct rpc_clnt *)xprt;
  103. xprt->resvport = 1; /* NSM requires a reserved port */
  104. clnt = rpc_create_client(xprt, "localhost",
  105. &nsm_program, SM_VERSION,
  106. RPC_AUTH_NULL);
  107. if (IS_ERR(clnt))
  108. goto out_err;
  109. clnt->cl_softrtry = 1;
  110. clnt->cl_oneshot = 1;
  111. return clnt;
  112. out_err:
  113. return clnt;
  114. }
  115. /*
  116. * XDR functions for NSM.
  117. */
  118. static u32 *
  119. xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  120. {
  121. char buffer[20];
  122. /*
  123. * Use the dotted-quad IP address of the remote host as
  124. * identifier. Linux statd always looks up the canonical
  125. * hostname first for whatever remote hostname it receives,
  126. * so this works alright.
  127. */
  128. sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
  129. if (!(p = xdr_encode_string(p, buffer))
  130. || !(p = xdr_encode_string(p, system_utsname.nodename)))
  131. return ERR_PTR(-EIO);
  132. *p++ = htonl(argp->prog);
  133. *p++ = htonl(argp->vers);
  134. *p++ = htonl(argp->proc);
  135. return p;
  136. }
  137. static int
  138. xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  139. {
  140. p = xdr_encode_common(rqstp, p, argp);
  141. if (IS_ERR(p))
  142. return PTR_ERR(p);
  143. *p++ = argp->addr;
  144. *p++ = argp->vers;
  145. *p++ = argp->proto;
  146. *p++ = 0;
  147. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  148. return 0;
  149. }
  150. static int
  151. xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  152. {
  153. p = xdr_encode_common(rqstp, p, argp);
  154. if (IS_ERR(p))
  155. return PTR_ERR(p);
  156. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  157. return 0;
  158. }
  159. static int
  160. xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
  161. {
  162. resp->status = ntohl(*p++);
  163. resp->state = ntohl(*p++);
  164. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  165. resp->status, resp->state);
  166. return 0;
  167. }
  168. static int
  169. xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
  170. {
  171. resp->state = ntohl(*p++);
  172. return 0;
  173. }
  174. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  175. #define SM_my_id_sz (3+1+SM_my_name_sz)
  176. #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
  177. #define SM_mon_sz (SM_mon_id_sz+4)
  178. #define SM_monres_sz 2
  179. #define SM_unmonres_sz 1
  180. #ifndef MAX
  181. # define MAX(a, b) (((a) > (b))? (a) : (b))
  182. #endif
  183. static struct rpc_procinfo nsm_procedures[] = {
  184. [SM_MON] = {
  185. .p_proc = SM_MON,
  186. .p_encode = (kxdrproc_t) xdr_encode_mon,
  187. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  188. .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
  189. .p_statidx = SM_MON,
  190. .p_name = "MONITOR",
  191. },
  192. [SM_UNMON] = {
  193. .p_proc = SM_UNMON,
  194. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  195. .p_decode = (kxdrproc_t) xdr_decode_stat,
  196. .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
  197. .p_statidx = SM_UNMON,
  198. .p_name = "UNMONITOR",
  199. },
  200. };
  201. static struct rpc_version nsm_version1 = {
  202. .number = 1,
  203. .nrprocs = ARRAY_SIZE(nsm_procedures),
  204. .procs = nsm_procedures
  205. };
  206. static struct rpc_version * nsm_version[] = {
  207. [1] = &nsm_version1,
  208. };
  209. static struct rpc_stat nsm_stats;
  210. static struct rpc_program nsm_program = {
  211. .name = "statd",
  212. .number = SM_PROGRAM,
  213. .nrvers = ARRAY_SIZE(nsm_version),
  214. .version = nsm_version,
  215. .stats = &nsm_stats
  216. };