mon.c 7.0 KB

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