mon.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. u32 nsm_local_state;
  22. /*
  23. * Common procedure for SM_MON/SM_UNMON calls
  24. */
  25. static int
  26. nsm_mon_unmon(struct nlm_host *host, u32 proc, struct nsm_res *res)
  27. {
  28. struct rpc_clnt *clnt;
  29. int status;
  30. struct nsm_args args;
  31. clnt = nsm_create();
  32. if (IS_ERR(clnt)) {
  33. status = PTR_ERR(clnt);
  34. goto out;
  35. }
  36. args.addr = host->h_addr.sin_addr.s_addr;
  37. args.proto= (host->h_proto<<1) | host->h_server;
  38. args.prog = NLM_PROGRAM;
  39. args.vers = host->h_version;
  40. args.proc = NLMPROC_NSM_NOTIFY;
  41. memset(res, 0, sizeof(*res));
  42. status = rpc_call(clnt, proc, &args, res, 0);
  43. if (status < 0)
  44. printk(KERN_DEBUG "nsm_mon_unmon: rpc failed, status=%d\n",
  45. status);
  46. else
  47. status = 0;
  48. out:
  49. return status;
  50. }
  51. /*
  52. * Set up monitoring of a remote host
  53. */
  54. int
  55. nsm_monitor(struct nlm_host *host)
  56. {
  57. struct nsm_res res;
  58. int status;
  59. dprintk("lockd: nsm_monitor(%s)\n", host->h_name);
  60. status = nsm_mon_unmon(host, SM_MON, &res);
  61. if (status < 0 || res.status != 0)
  62. printk(KERN_NOTICE "lockd: cannot monitor %s\n", host->h_name);
  63. else
  64. host->h_monitored = 1;
  65. return status;
  66. }
  67. /*
  68. * Cease to monitor remote host
  69. */
  70. int
  71. nsm_unmonitor(struct nlm_host *host)
  72. {
  73. struct nsm_res res;
  74. int status;
  75. dprintk("lockd: nsm_unmonitor(%s)\n", host->h_name);
  76. status = nsm_mon_unmon(host, SM_UNMON, &res);
  77. if (status < 0)
  78. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n", host->h_name);
  79. else
  80. host->h_monitored = 0;
  81. return status;
  82. }
  83. /*
  84. * Create NSM client for the local host
  85. */
  86. static struct rpc_clnt *
  87. nsm_create(void)
  88. {
  89. struct rpc_xprt *xprt;
  90. struct rpc_clnt *clnt;
  91. struct sockaddr_in sin;
  92. sin.sin_family = AF_INET;
  93. sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  94. sin.sin_port = 0;
  95. xprt = xprt_create_proto(IPPROTO_UDP, &sin, NULL);
  96. if (IS_ERR(xprt))
  97. return (struct rpc_clnt *)xprt;
  98. xprt->resvport = 1; /* NSM requires a reserved port */
  99. clnt = rpc_create_client(xprt, "localhost",
  100. &nsm_program, SM_VERSION,
  101. RPC_AUTH_NULL);
  102. if (IS_ERR(clnt))
  103. goto out_err;
  104. clnt->cl_softrtry = 1;
  105. clnt->cl_oneshot = 1;
  106. return clnt;
  107. out_err:
  108. return clnt;
  109. }
  110. /*
  111. * XDR functions for NSM.
  112. */
  113. static u32 *
  114. xdr_encode_common(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  115. {
  116. char buffer[20];
  117. /*
  118. * Use the dotted-quad IP address of the remote host as
  119. * identifier. Linux statd always looks up the canonical
  120. * hostname first for whatever remote hostname it receives,
  121. * so this works alright.
  122. */
  123. sprintf(buffer, "%u.%u.%u.%u", NIPQUAD(argp->addr));
  124. if (!(p = xdr_encode_string(p, buffer))
  125. || !(p = xdr_encode_string(p, system_utsname.nodename)))
  126. return ERR_PTR(-EIO);
  127. *p++ = htonl(argp->prog);
  128. *p++ = htonl(argp->vers);
  129. *p++ = htonl(argp->proc);
  130. return p;
  131. }
  132. static int
  133. xdr_encode_mon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  134. {
  135. p = xdr_encode_common(rqstp, p, argp);
  136. if (IS_ERR(p))
  137. return PTR_ERR(p);
  138. *p++ = argp->addr;
  139. *p++ = argp->vers;
  140. *p++ = argp->proto;
  141. *p++ = 0;
  142. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  143. return 0;
  144. }
  145. static int
  146. xdr_encode_unmon(struct rpc_rqst *rqstp, u32 *p, struct nsm_args *argp)
  147. {
  148. p = xdr_encode_common(rqstp, p, argp);
  149. if (IS_ERR(p))
  150. return PTR_ERR(p);
  151. rqstp->rq_slen = xdr_adjust_iovec(rqstp->rq_svec, p);
  152. return 0;
  153. }
  154. static int
  155. xdr_decode_stat_res(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
  156. {
  157. resp->status = ntohl(*p++);
  158. resp->state = ntohl(*p++);
  159. dprintk("nsm: xdr_decode_stat_res status %d state %d\n",
  160. resp->status, resp->state);
  161. return 0;
  162. }
  163. static int
  164. xdr_decode_stat(struct rpc_rqst *rqstp, u32 *p, struct nsm_res *resp)
  165. {
  166. resp->state = ntohl(*p++);
  167. return 0;
  168. }
  169. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  170. #define SM_my_id_sz (3+1+SM_my_name_sz)
  171. #define SM_mon_id_sz (1+XDR_QUADLEN(20)+SM_my_id_sz)
  172. #define SM_mon_sz (SM_mon_id_sz+4)
  173. #define SM_monres_sz 2
  174. #define SM_unmonres_sz 1
  175. #ifndef MAX
  176. # define MAX(a, b) (((a) > (b))? (a) : (b))
  177. #endif
  178. static struct rpc_procinfo nsm_procedures[] = {
  179. [SM_MON] = {
  180. .p_proc = SM_MON,
  181. .p_encode = (kxdrproc_t) xdr_encode_mon,
  182. .p_decode = (kxdrproc_t) xdr_decode_stat_res,
  183. .p_bufsiz = MAX(SM_mon_sz, SM_monres_sz) << 2,
  184. },
  185. [SM_UNMON] = {
  186. .p_proc = SM_UNMON,
  187. .p_encode = (kxdrproc_t) xdr_encode_unmon,
  188. .p_decode = (kxdrproc_t) xdr_decode_stat,
  189. .p_bufsiz = MAX(SM_mon_id_sz, SM_unmonres_sz) << 2,
  190. },
  191. };
  192. static struct rpc_version nsm_version1 = {
  193. .number = 1,
  194. .nrprocs = sizeof(nsm_procedures)/sizeof(nsm_procedures[0]),
  195. .procs = nsm_procedures
  196. };
  197. static struct rpc_version * nsm_version[] = {
  198. [1] = &nsm_version1,
  199. };
  200. static struct rpc_stat nsm_stats;
  201. static struct rpc_program nsm_program = {
  202. .name = "statd",
  203. .number = SM_PROGRAM,
  204. .nrvers = sizeof(nsm_version)/sizeof(nsm_version[0]),
  205. .version = nsm_version,
  206. .stats = &nsm_stats
  207. };