mon.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/svc.h>
  13. #include <linux/lockd/lockd.h>
  14. #include <linux/lockd/sm_inter.h>
  15. #define NLMDBG_FACILITY NLMDBG_MONITOR
  16. static struct rpc_clnt * nsm_create(void);
  17. static struct rpc_program nsm_program;
  18. /*
  19. * Local NSM state
  20. */
  21. int nsm_local_state;
  22. /*
  23. * Common procedure for SM_MON/SM_UNMON calls
  24. */
  25. static int
  26. nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
  27. {
  28. struct rpc_clnt *clnt;
  29. int status;
  30. struct nsm_args args;
  31. struct rpc_message msg = {
  32. .rpc_argp = &args,
  33. .rpc_resp = res,
  34. };
  35. clnt = nsm_create();
  36. if (IS_ERR(clnt)) {
  37. status = PTR_ERR(clnt);
  38. goto out;
  39. }
  40. memset(&args, 0, sizeof(args));
  41. args.mon_name = nsm->sm_name;
  42. args.addr = nsm->sm_addr.sin_addr.s_addr;
  43. args.prog = NLM_PROGRAM;
  44. args.vers = 3;
  45. args.proc = NLMPROC_NSM_NOTIFY;
  46. memset(res, 0, sizeof(*res));
  47. msg.rpc_proc = &clnt->cl_procinfo[proc];
  48. status = rpc_call_sync(clnt, &msg, 0);
  49. if (status < 0)
  50. printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
  51. status);
  52. else
  53. status = 0;
  54. out:
  55. return status;
  56. }
  57. /*
  58. * Set up monitoring of a remote host
  59. */
  60. int
  61. nsm_monitor(struct nlm_host *host)
  62. {
  63. struct nsm_handle *nsm = host->h_nsmhandle;
  64. struct nsm_res res;
  65. int status;
  66. dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
  67. BUG_ON(nsm == NULL);
  68. if (nsm->sm_monitored)
  69. return 0;
  70. status = nsm_mon_unmon(nsm, SM_MON, &res);
  71. if (status < 0 || res.status != 0)
  72. printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
  73. else
  74. nsm->sm_monitored = 1;
  75. return status;
  76. }
  77. /*
  78. * Cease to monitor remote host
  79. */
  80. int
  81. nsm_unmonitor(struct nlm_host *host)
  82. {
  83. struct nsm_handle *nsm = host->h_nsmhandle;
  84. struct nsm_res res;
  85. int status = 0;
  86. if (nsm == NULL)
  87. return 0;
  88. host->h_nsmhandle = NULL;
  89. if (atomic_read(&nsm->sm_count) == 1
  90. && nsm->sm_monitored && !nsm->sm_sticky) {
  91. dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
  92. status = nsm_mon_unmon(nsm, SM_UNMON, &res);
  93. if (status < 0)
  94. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
  95. host->h_name);
  96. else
  97. nsm->sm_monitored = 0;
  98. }
  99. nsm_release(nsm);
  100. return status;
  101. }
  102. /*
  103. * Create NSM client for the local host
  104. */
  105. static struct rpc_clnt *
  106. nsm_create(void)
  107. {
  108. struct sockaddr_in sin = {
  109. .sin_family = AF_INET,
  110. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  111. .sin_port = 0,
  112. };
  113. struct rpc_create_args args = {
  114. .protocol = IPPROTO_UDP,
  115. .address = (struct sockaddr *)&sin,
  116. .addrsize = sizeof(sin),
  117. .servername = "localhost",
  118. .program = &nsm_program,
  119. .version = SM_VERSION,
  120. .authflavor = RPC_AUTH_NULL,
  121. .flags = (RPC_CLNT_CREATE_ONESHOT),
  122. };
  123. return rpc_create(&args);
  124. }
  125. /*
  126. * XDR functions for NSM.
  127. */
  128. static __be32 *
  129. xdr_encode_common(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  130. {
  131. char buffer[20], *name;
  132. /*
  133. * Use the dotted-quad IP address of the remote host as
  134. * identifier. Linux statd always looks up the canonical
  135. * hostname first for whatever remote hostname it receives,
  136. * so this works alright.
  137. */
  138. if (nsm_use_hostnames) {
  139. name = argp->mon_name;
  140. } else {
  141. sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
  142. name = buffer;
  143. }
  144. if (!(p = xdr_encode_string(p, name))
  145. || !(p = xdr_encode_string(p, utsname()->nodename)))
  146. return ERR_PTR(-EIO);
  147. *p++ = htonl(argp->prog);
  148. *p++ = htonl(argp->vers);
  149. *p++ = htonl(argp->proc);
  150. return p;
  151. }
  152. static int
  153. xdr_encode_mon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  154. {
  155. p = xdr_encode_common(rqstp, p, argp);
  156. if (IS_ERR(p))
  157. return PTR_ERR(p);
  158. /* Surprise - there may even be room for an IPv6 address now */
  159. *p++ = argp->addr;
  160. *p++ = 0;
  161. *p++ = 0;
  162. *p++ = 0;
  163. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  164. return 0;
  165. }
  166. static int
  167. xdr_encode_unmon(struct rpc_rqst *rqstp, __be32 *p, struct nsm_args *argp)
  168. {
  169. p = xdr_encode_common(rqstp, p, argp);
  170. if (IS_ERR(p))
  171. return PTR_ERR(p);
  172. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  173. return 0;
  174. }
  175. static int
  176. xdr_decode_stat_res(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  177. {
  178. resp->status = ntohl(*p++);
  179. resp->state = ntohl(*p++);
  180. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  181. resp->status, resp->state);
  182. return 0;
  183. }
  184. static int
  185. xdr_decode_stat(struct rpc_rqst *rqstp, __be32 *p, struct nsm_res *resp)
  186. {
  187. resp->state = ntohl(*p++);
  188. return 0;
  189. }
  190. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  191. #define SM_my_id_sz (3+1+SM_my_name_sz)
  192. #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
  193. #define SM_mon_sz (SM_mon_id_sz+4)
  194. #define SM_monres_sz 2
  195. #define SM_unmonres_sz 1
  196. #ifndef MAX
  197. # define MAX(a, b) (((a) > (b))? (a) : (b))
  198. #endif
  199. static struct rpc_procinfo nsm_procedures[] = {
  200. [SM_MON] = {
  201. .p_proc = SM_MON,
  202. .p_encode = (kxdrproc_t) xdr_encode_mon,
  203. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  204. .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
  205. .p_statidx = SM_MON,
  206. .p_name = "MONITOR",
  207. },
  208. [SM_UNMON] = {
  209. .p_proc = SM_UNMON,
  210. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  211. .p_decode = (kxdrproc_t) xdr_decode_stat,
  212. .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
  213. .p_statidx = SM_UNMON,
  214. .p_name = "UNMONITOR",
  215. },
  216. };
  217. static struct rpc_version nsm_version1 = {
  218. .number = 1,
  219. .nrprocs = ARRAY_SIZE(nsm_procedures),
  220. .procs = nsm_procedures
  221. };
  222. static struct rpc_version * nsm_version[] = {
  223. [1] = &nsm_version1,
  224. };
  225. static struct rpc_stat nsm_stats;
  226. static struct rpc_program nsm_program = {
  227. .name = "statd",
  228. .number = SM_PROGRAM,
  229. .nrvers = ARRAY_SIZE(nsm_version),
  230. .version = nsm_version,
  231. .stats = &nsm_stats
  232. };