mon.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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/xprtsock.h>
  13. #include <linux/sunrpc/svc.h>
  14. #include <linux/lockd/lockd.h>
  15. #include <linux/lockd/sm_inter.h>
  16. #define NLMDBG_FACILITY NLMDBG_MONITOR
  17. #define XDR_ADDRBUF_LEN (20)
  18. static struct rpc_clnt * nsm_create(void);
  19. static struct rpc_program nsm_program;
  20. /*
  21. * Local NSM state
  22. */
  23. int nsm_local_state;
  24. /*
  25. * Common procedure for SM_MON/SM_UNMON calls
  26. */
  27. static int
  28. nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
  29. {
  30. struct rpc_clnt *clnt;
  31. int status;
  32. struct nsm_args args;
  33. struct rpc_message msg = {
  34. .rpc_argp = &args,
  35. .rpc_resp = res,
  36. };
  37. clnt = nsm_create();
  38. if (IS_ERR(clnt)) {
  39. status = PTR_ERR(clnt);
  40. goto out;
  41. }
  42. memset(&args, 0, sizeof(args));
  43. args.mon_name = nsm->sm_name;
  44. args.addr = nsm_addr_in(nsm)->sin_addr.s_addr;
  45. args.prog = NLM_PROGRAM;
  46. args.vers = 3;
  47. args.proc = NLMPROC_NSM_NOTIFY;
  48. memset(res, 0, sizeof(*res));
  49. msg.rpc_proc = &clnt->cl_procinfo[proc];
  50. status = rpc_call_sync(clnt, &msg, 0);
  51. if (status < 0)
  52. printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
  53. status);
  54. else
  55. status = 0;
  56. rpc_shutdown_client(clnt);
  57. out:
  58. return status;
  59. }
  60. /*
  61. * Set up monitoring of a remote host
  62. */
  63. int
  64. nsm_monitor(struct nlm_host *host)
  65. {
  66. struct nsm_handle *nsm = host->h_nsmhandle;
  67. struct nsm_res res;
  68. int status;
  69. dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
  70. BUG_ON(nsm == NULL);
  71. if (nsm->sm_monitored)
  72. return 0;
  73. status = nsm_mon_unmon(nsm, SM_MON, &res);
  74. if (status < 0 || res.status != 0)
  75. printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
  76. else
  77. nsm->sm_monitored = 1;
  78. return status;
  79. }
  80. /*
  81. * Cease to monitor remote host
  82. */
  83. int
  84. nsm_unmonitor(struct nlm_host *host)
  85. {
  86. struct nsm_handle *nsm = host->h_nsmhandle;
  87. struct nsm_res res;
  88. int status = 0;
  89. if (nsm == NULL)
  90. return 0;
  91. host->h_nsmhandle = NULL;
  92. if (atomic_read(&nsm->sm_count) == 1
  93. && nsm->sm_monitored && !nsm->sm_sticky) {
  94. dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
  95. status = nsm_mon_unmon(nsm, SM_UNMON, &res);
  96. if (status < 0)
  97. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
  98. host->h_name);
  99. else
  100. nsm->sm_monitored = 0;
  101. }
  102. nsm_release(nsm);
  103. return status;
  104. }
  105. /*
  106. * Create NSM client for the local host
  107. */
  108. static struct rpc_clnt *
  109. nsm_create(void)
  110. {
  111. struct sockaddr_in sin = {
  112. .sin_family = AF_INET,
  113. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  114. .sin_port = 0,
  115. };
  116. struct rpc_create_args args = {
  117. .protocol = XPRT_TRANSPORT_UDP,
  118. .address = (struct sockaddr *)&sin,
  119. .addrsize = sizeof(sin),
  120. .servername = "localhost",
  121. .program = &nsm_program,
  122. .version = SM_VERSION,
  123. .authflavor = RPC_AUTH_NULL,
  124. };
  125. return rpc_create(&args);
  126. }
  127. /*
  128. * XDR functions for NSM.
  129. *
  130. * See http://www.opengroup.org/ for details on the Network
  131. * Status Monitor wire protocol.
  132. */
  133. static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
  134. {
  135. size_t len = strlen(string);
  136. if (len > SM_MAXSTRLEN)
  137. len = SM_MAXSTRLEN;
  138. return xdr_encode_opaque(p, string, len);
  139. }
  140. /*
  141. * "mon_name" specifies the host to be monitored.
  142. *
  143. * Linux uses a text version of the IP address of the remote
  144. * host as the host identifier (the "mon_name" argument).
  145. *
  146. * Linux statd always looks up the canonical hostname first for
  147. * whatever remote hostname it receives, so this works alright.
  148. */
  149. static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
  150. {
  151. char buffer[XDR_ADDRBUF_LEN + 1];
  152. char *name = argp->mon_name;
  153. if (!nsm_use_hostnames) {
  154. snprintf(buffer, XDR_ADDRBUF_LEN,
  155. NIPQUAD_FMT, NIPQUAD(argp->addr));
  156. name = buffer;
  157. }
  158. return xdr_encode_nsm_string(p, name);
  159. }
  160. /*
  161. * The "my_id" argument specifies the hostname and RPC procedure
  162. * to be called when the status manager receives notification
  163. * (via the SM_NOTIFY call) that the state of host "mon_name"
  164. * has changed.
  165. */
  166. static __be32 *xdr_encode_my_id(__be32 *p, struct nsm_args *argp)
  167. {
  168. p = xdr_encode_nsm_string(p, utsname()->nodename);
  169. if (!p)
  170. return ERR_PTR(-EIO);
  171. *p++ = htonl(argp->prog);
  172. *p++ = htonl(argp->vers);
  173. *p++ = htonl(argp->proc);
  174. return p;
  175. }
  176. /*
  177. * The "mon_id" argument specifies the non-private arguments
  178. * of an SM_MON or SM_UNMON call.
  179. */
  180. static __be32 *xdr_encode_mon_id(__be32 *p, struct nsm_args *argp)
  181. {
  182. p = xdr_encode_mon_name(p, argp);
  183. if (!p)
  184. return ERR_PTR(-EIO);
  185. return xdr_encode_my_id(p, argp);
  186. }
  187. /*
  188. * The "priv" argument may contain private information required
  189. * by the SM_MON call. This information will be supplied in the
  190. * SM_NOTIFY call.
  191. *
  192. * Linux provides the raw IP address of the monitored host,
  193. * left in network byte order.
  194. */
  195. static __be32 *xdr_encode_priv(__be32 *p, struct nsm_args *argp)
  196. {
  197. *p++ = argp->addr;
  198. *p++ = 0;
  199. *p++ = 0;
  200. *p++ = 0;
  201. return p;
  202. }
  203. static int
  204. xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  205. {
  206. p = xdr_encode_mon_id(p, argp);
  207. if (IS_ERR(p))
  208. return PTR_ERR(p);
  209. p = xdr_encode_priv(p, argp);
  210. if (IS_ERR(p))
  211. return PTR_ERR(p);
  212. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  213. return 0;
  214. }
  215. static int
  216. xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  217. {
  218. p = xdr_encode_mon_id(p, argp);
  219. if (IS_ERR(p))
  220. return PTR_ERR(p);
  221. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  222. return 0;
  223. }
  224. static int
  225. xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  226. {
  227. resp->status = ntohl(*p++);
  228. resp->state = ntohl(*p++);
  229. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  230. resp->status, resp->state);
  231. return 0;
  232. }
  233. static int
  234. xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  235. {
  236. resp->state = ntohl(*p++);
  237. return 0;
  238. }
  239. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  240. #define SM_my_id_sz (SM_my_name_sz+3)
  241. #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  242. #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
  243. #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
  244. #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
  245. #define SM_monres_sz 2
  246. #define SM_unmonres_sz 1
  247. static struct rpc_procinfo nsm_procedures[] = {
  248. [SM_MON] = {
  249. .p_proc = SM_MON,
  250. .p_encode = (kxdrproc_t) xdr_encode_mon,
  251. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  252. .p_arglen = SM_mon_sz,
  253. .p_replen = SM_monres_sz,
  254. .p_statidx = SM_MON,
  255. .p_name = "MONITOR",
  256. },
  257. [SM_UNMON] = {
  258. .p_proc = SM_UNMON,
  259. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  260. .p_decode = (kxdrproc_t) xdr_decode_stat,
  261. .p_arglen = SM_mon_id_sz,
  262. .p_replen = SM_unmonres_sz,
  263. .p_statidx = SM_UNMON,
  264. .p_name = "UNMONITOR",
  265. },
  266. };
  267. static struct rpc_version nsm_version1 = {
  268. .number = 1,
  269. .nrprocs = ARRAY_SIZE(nsm_procedures),
  270. .procs = nsm_procedures
  271. };
  272. static struct rpc_version * nsm_version[] = {
  273. [1] = &nsm_version1,
  274. };
  275. static struct rpc_stat nsm_stats;
  276. static struct rpc_program nsm_program = {
  277. .name = "statd",
  278. .number = SM_PROGRAM,
  279. .nrvers = ARRAY_SIZE(nsm_version),
  280. .version = nsm_version,
  281. .stats = &nsm_stats
  282. };