mon.c 7.3 KB

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