mon.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. static __be32 *
  158. xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  159. {
  160. char buffer[20], *name;
  161. /*
  162. * Use the dotted-quad IP address of the remote host as
  163. * identifier. Linux statd always looks up the canonical
  164. * hostname first for whatever remote hostname it receives,
  165. * so this works alright.
  166. */
  167. if (nsm_use_hostnames) {
  168. name = argp->mon_name;
  169. } else {
  170. sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
  171. name = buffer;
  172. }
  173. if (!(p = xdr_encode_string(p, name))
  174. || !(p = xdr_encode_string(p, utsname()->nodename)))
  175. return ERR_PTR(-EIO);
  176. *p++ = htonl(argp->prog);
  177. *p++ = htonl(argp->vers);
  178. *p++ = htonl(argp->proc);
  179. return p;
  180. }
  181. static int
  182. xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  183. {
  184. p = xdr_encode_common(rqstp, p, argp);
  185. if (IS_ERR(p))
  186. return PTR_ERR(p);
  187. /* Surprise - there may even be room for an IPv6 address now */
  188. *p++ = argp->addr;
  189. *p++ = 0;
  190. *p++ = 0;
  191. *p++ = 0;
  192. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  193. return 0;
  194. }
  195. static int
  196. xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  197. {
  198. p = xdr_encode_common(rqstp, p, argp);
  199. if (IS_ERR(p))
  200. return PTR_ERR(p);
  201. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  202. return 0;
  203. }
  204. static int
  205. xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  206. {
  207. resp->status = ntohl(*p++);
  208. resp->state = ntohl(*p++);
  209. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  210. resp->status, resp->state);
  211. return 0;
  212. }
  213. static int
  214. xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  215. {
  216. resp->state = ntohl(*p++);
  217. return 0;
  218. }
  219. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  220. #define SM_my_id_sz (3+1+SM_my_name_sz)
  221. #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
  222. #define SM_mon_sz (SM_mon_id_sz+4)
  223. #define SM_monres_sz 2
  224. #define SM_unmonres_sz 1
  225. static struct rpc_procinfo nsm_procedures[] = {
  226. [SM_MON] = {
  227. .p_proc = SM_MON,
  228. .p_encode = (kxdrproc_t) xdr_encode_mon,
  229. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  230. .p_arglen = SM_mon_sz,
  231. .p_replen = SM_monres_sz,
  232. .p_statidx = SM_MON,
  233. .p_name = "MONITOR",
  234. },
  235. [SM_UNMON] = {
  236. .p_proc = SM_UNMON,
  237. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  238. .p_decode = (kxdrproc_t) xdr_decode_stat,
  239. .p_arglen = SM_mon_id_sz,
  240. .p_replen = SM_unmonres_sz,
  241. .p_statidx = SM_UNMON,
  242. .p_name = "UNMONITOR",
  243. },
  244. };
  245. static struct rpc_version nsm_version1 = {
  246. .number = 1,
  247. .nrprocs = ARRAY_SIZE(nsm_procedures),
  248. .procs = nsm_procedures
  249. };
  250. static struct rpc_version * nsm_version[] = {
  251. [1] = &nsm_version1,
  252. };
  253. static struct rpc_stat nsm_stats;
  254. static struct rpc_program nsm_program = {
  255. .name = "statd",
  256. .number = SM_PROGRAM,
  257. .nrvers = ARRAY_SIZE(nsm_version),
  258. .version = nsm_version,
  259. .stats = &nsm_stats
  260. };