mon.c 6.8 KB

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