mon.c 7.5 KB

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