mon.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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->sm_addr.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. static __be32 *xdr_encode_nsm_string(__be32 *p, char *string)
  131. {
  132. size_t len = strlen(string);
  133. if (len > SM_MAXSTRLEN)
  134. len = SM_MAXSTRLEN;
  135. return xdr_encode_opaque(p, string, len);
  136. }
  137. /*
  138. * "mon_name" specifies the host to be monitored.
  139. *
  140. * Linux uses a text version of the IP address of the remote
  141. * host as the host identifier (the "mon_name" argument).
  142. *
  143. * Linux statd always looks up the canonical hostname first for
  144. * whatever remote hostname it receives, so this works alright.
  145. */
  146. static __be32 *xdr_encode_mon_name(__be32 *p, struct nsm_args *argp)
  147. {
  148. char buffer[XDR_ADDRBUF_LEN + 1];
  149. char *name = argp->mon_name;
  150. if (!nsm_use_hostnames) {
  151. snprintf(buffer, XDR_ADDRBUF_LEN,
  152. NIPQUAD_FMT, NIPQUAD(argp->addr));
  153. name = buffer;
  154. }
  155. return xdr_encode_nsm_string(p, 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. static __be32 *
  185. xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  186. {
  187. char buffer[20], *name;
  188. /*
  189. * Use the dotted-quad IP address of the remote host as
  190. * identifier. Linux statd always looks up the canonical
  191. * hostname first for whatever remote hostname it receives,
  192. * so this works alright.
  193. */
  194. if (nsm_use_hostnames) {
  195. name = argp->mon_name;
  196. } else {
  197. sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
  198. name = buffer;
  199. }
  200. if (!(p = xdr_encode_string(p, name))
  201. || !(p = xdr_encode_string(p, utsname()->nodename)))
  202. return ERR_PTR(-EIO);
  203. *p++ = htonl(argp->prog);
  204. *p++ = htonl(argp->vers);
  205. *p++ = htonl(argp->proc);
  206. return p;
  207. }
  208. static int
  209. xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  210. {
  211. p = xdr_encode_common(rqstp, p, argp);
  212. if (IS_ERR(p))
  213. return PTR_ERR(p);
  214. /* Surprise - there may even be room for an IPv6 address now */
  215. *p++ = argp->addr;
  216. *p++ = 0;
  217. *p++ = 0;
  218. *p++ = 0;
  219. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  220. return 0;
  221. }
  222. static int
  223. xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  224. {
  225. p = xdr_encode_common(rqstp, p, argp);
  226. if (IS_ERR(p))
  227. return PTR_ERR(p);
  228. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  229. return 0;
  230. }
  231. static int
  232. xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  233. {
  234. resp->status = ntohl(*p++);
  235. resp->state = ntohl(*p++);
  236. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  237. resp->status, resp->state);
  238. return 0;
  239. }
  240. static int
  241. xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  242. {
  243. resp->state = ntohl(*p++);
  244. return 0;
  245. }
  246. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  247. #define SM_my_id_sz (3+1+SM_my_name_sz)
  248. #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
  249. #define SM_mon_sz (SM_mon_id_sz+4)
  250. #define SM_monres_sz 2
  251. #define SM_unmonres_sz 1
  252. static struct rpc_procinfo nsm_procedures[] = {
  253. [SM_MON] = {
  254. .p_proc = SM_MON,
  255. .p_encode = (kxdrproc_t) xdr_encode_mon,
  256. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  257. .p_arglen = SM_mon_sz,
  258. .p_replen = SM_monres_sz,
  259. .p_statidx = SM_MON,
  260. .p_name = "MONITOR",
  261. },
  262. [SM_UNMON] = {
  263. .p_proc = SM_UNMON,
  264. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  265. .p_decode = (kxdrproc_t) xdr_decode_stat,
  266. .p_arglen = SM_mon_id_sz,
  267. .p_replen = SM_unmonres_sz,
  268. .p_statidx = SM_UNMON,
  269. .p_name = "UNMONITOR",
  270. },
  271. };
  272. static struct rpc_version nsm_version1 = {
  273. .number = 1,
  274. .nrprocs = ARRAY_SIZE(nsm_procedures),
  275. .procs = nsm_procedures
  276. };
  277. static struct rpc_version * nsm_version[] = {
  278. [1] = &nsm_version1,
  279. };
  280. static struct rpc_stat nsm_stats;
  281. static struct rpc_program nsm_program = {
  282. .name = "statd",
  283. .number = SM_PROGRAM,
  284. .nrvers = ARRAY_SIZE(nsm_version),
  285. .version = nsm_version,
  286. .stats = &nsm_stats
  287. };