mon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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 NSM_PROGRAM 100024
  18. #define NSM_VERSION 1
  19. enum {
  20. NSMPROC_NULL,
  21. NSMPROC_STAT,
  22. NSMPROC_MON,
  23. NSMPROC_UNMON,
  24. NSMPROC_UNMON_ALL,
  25. NSMPROC_SIMU_CRASH,
  26. NSMPROC_NOTIFY,
  27. };
  28. struct nsm_args {
  29. struct nsm_private *priv;
  30. u32 prog; /* RPC callback info */
  31. u32 vers;
  32. u32 proc;
  33. char *mon_name;
  34. };
  35. struct nsm_res {
  36. u32 status;
  37. u32 state;
  38. };
  39. static struct rpc_clnt * nsm_create(void);
  40. static struct rpc_program nsm_program;
  41. static LIST_HEAD(nsm_handles);
  42. static DEFINE_SPINLOCK(nsm_lock);
  43. /*
  44. * Local NSM state
  45. */
  46. int nsm_local_state;
  47. static void nsm_display_ipv4_address(const struct sockaddr *sap, char *buf,
  48. const size_t len)
  49. {
  50. const struct sockaddr_in *sin = (struct sockaddr_in *)sap;
  51. snprintf(buf, len, "%pI4", &sin->sin_addr.s_addr);
  52. }
  53. static void nsm_display_ipv6_address(const struct sockaddr *sap, char *buf,
  54. const size_t len)
  55. {
  56. const struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sap;
  57. if (ipv6_addr_v4mapped(&sin6->sin6_addr))
  58. snprintf(buf, len, "%pI4", &sin6->sin6_addr.s6_addr32[3]);
  59. else if (sin6->sin6_scope_id != 0)
  60. snprintf(buf, len, "%pI6%%%u", &sin6->sin6_addr,
  61. sin6->sin6_scope_id);
  62. else
  63. snprintf(buf, len, "%pI6", &sin6->sin6_addr);
  64. }
  65. static void nsm_display_address(const struct sockaddr *sap,
  66. char *buf, const size_t len)
  67. {
  68. switch (sap->sa_family) {
  69. case AF_INET:
  70. nsm_display_ipv4_address(sap, buf, len);
  71. break;
  72. case AF_INET6:
  73. nsm_display_ipv6_address(sap, buf, len);
  74. break;
  75. default:
  76. snprintf(buf, len, "unsupported address family");
  77. break;
  78. }
  79. }
  80. /*
  81. * Common procedure for NSMPROC_MON/NSMPROC_UNMON calls
  82. */
  83. static int
  84. nsm_mon_unmon(struct nsm_handle *nsm, u32 proc, struct nsm_res *res)
  85. {
  86. struct rpc_clnt *clnt;
  87. int status;
  88. struct nsm_args args = {
  89. .priv = &nsm->sm_priv,
  90. .prog = NLM_PROGRAM,
  91. .vers = 3,
  92. .proc = NLMPROC_NSM_NOTIFY,
  93. .mon_name = nsm->sm_mon_name,
  94. };
  95. struct rpc_message msg = {
  96. .rpc_argp = &args,
  97. .rpc_resp = res,
  98. };
  99. clnt = nsm_create();
  100. if (IS_ERR(clnt)) {
  101. status = PTR_ERR(clnt);
  102. dprintk("lockd: failed to create NSM upcall transport, "
  103. "status=%d\n", status);
  104. goto out;
  105. }
  106. memset(res, 0, sizeof(*res));
  107. msg.rpc_proc = &clnt->cl_procinfo[proc];
  108. status = rpc_call_sync(clnt, &msg, 0);
  109. if (status < 0)
  110. dprintk("lockd: NSM upcall RPC failed, status=%d\n",
  111. status);
  112. else
  113. status = 0;
  114. rpc_shutdown_client(clnt);
  115. out:
  116. return status;
  117. }
  118. /**
  119. * nsm_monitor - Notify a peer in case we reboot
  120. * @host: pointer to nlm_host of peer to notify
  121. *
  122. * If this peer is not already monitored, this function sends an
  123. * upcall to the local rpc.statd to record the name/address of
  124. * the peer to notify in case we reboot.
  125. *
  126. * Returns zero if the peer is monitored by the local rpc.statd;
  127. * otherwise a negative errno value is returned.
  128. */
  129. int nsm_monitor(const struct nlm_host *host)
  130. {
  131. struct nsm_handle *nsm = host->h_nsmhandle;
  132. struct nsm_res res;
  133. int status;
  134. dprintk("lockd: nsm_monitor(%s)\n", nsm->sm_name);
  135. if (nsm->sm_monitored)
  136. return 0;
  137. /*
  138. * Choose whether to record the caller_name or IP address of
  139. * this peer in the local rpc.statd's database.
  140. */
  141. nsm->sm_mon_name = nsm_use_hostnames ? nsm->sm_name : nsm->sm_addrbuf;
  142. status = nsm_mon_unmon(nsm, NSMPROC_MON, &res);
  143. if (res.status != 0)
  144. status = -EIO;
  145. if (status < 0)
  146. printk(KERN_NOTICE "lockd: cannot monitor %s\n", nsm->sm_name);
  147. else
  148. nsm->sm_monitored = 1;
  149. return status;
  150. }
  151. /**
  152. * nsm_unmonitor - Unregister peer notification
  153. * @host: pointer to nlm_host of peer to stop monitoring
  154. *
  155. * If this peer is monitored, this function sends an upcall to
  156. * tell the local rpc.statd not to send this peer a notification
  157. * when we reboot.
  158. */
  159. void nsm_unmonitor(const struct nlm_host *host)
  160. {
  161. struct nsm_handle *nsm = host->h_nsmhandle;
  162. struct nsm_res res;
  163. int status;
  164. if (atomic_read(&nsm->sm_count) == 1
  165. && nsm->sm_monitored && !nsm->sm_sticky) {
  166. dprintk("lockd: nsm_unmonitor(%s)\n", nsm->sm_name);
  167. status = nsm_mon_unmon(nsm, NSMPROC_UNMON, &res);
  168. if (res.status != 0)
  169. status = -EIO;
  170. if (status < 0)
  171. printk(KERN_NOTICE "lockd: cannot unmonitor %s\n",
  172. nsm->sm_name);
  173. else
  174. nsm->sm_monitored = 0;
  175. }
  176. }
  177. static struct nsm_handle *nsm_lookup_hostname(const char *hostname,
  178. const size_t len)
  179. {
  180. struct nsm_handle *nsm;
  181. list_for_each_entry(nsm, &nsm_handles, sm_link)
  182. if (strlen(nsm->sm_name) == len &&
  183. memcmp(nsm->sm_name, hostname, len) == 0)
  184. return nsm;
  185. return NULL;
  186. }
  187. static struct nsm_handle *nsm_lookup_priv(const struct nsm_private *priv)
  188. {
  189. struct nsm_handle *nsm;
  190. list_for_each_entry(nsm, &nsm_handles, sm_link)
  191. if (memcmp(nsm->sm_priv.data, priv->data,
  192. sizeof(priv->data)) == 0)
  193. return nsm;
  194. return NULL;
  195. }
  196. /*
  197. * Construct a unique cookie to match this nsm_handle to this monitored
  198. * host. It is passed to the local rpc.statd via NSMPROC_MON, and
  199. * returned via NLMPROC_SM_NOTIFY, in the "priv" field of these
  200. * requests.
  201. *
  202. * Linux provides the raw IP address of the monitored host,
  203. * left in network byte order.
  204. */
  205. static void nsm_init_private(struct nsm_handle *nsm)
  206. {
  207. __be32 *p = (__be32 *)&nsm->sm_priv.data;
  208. *p = nsm_addr_in(nsm)->sin_addr.s_addr;
  209. }
  210. /**
  211. * nsm_find - Find or create a cached nsm_handle
  212. * @sap: pointer to socket address of handle to find
  213. * @salen: length of socket address
  214. * @hostname: pointer to C string containing hostname to find
  215. * @hostname_len: length of C string
  216. * @create: one means create new handle if not found in cache
  217. *
  218. * Behavior is modulated by the global nsm_use_hostnames variable
  219. * and by the @create argument.
  220. *
  221. * Returns a cached nsm_handle after bumping its ref count, or if
  222. * @create is set, returns a fresh nsm_handle if a handle that
  223. * matches @sap and/or @hostname cannot be found in the handle cache.
  224. * Returns NULL if an error occurs.
  225. */
  226. struct nsm_handle *nsm_find(const struct sockaddr *sap, const size_t salen,
  227. const char *hostname, const size_t hostname_len,
  228. const int create)
  229. {
  230. struct nsm_handle *nsm = NULL;
  231. struct nsm_handle *pos;
  232. if (hostname && memchr(hostname, '/', hostname_len) != NULL) {
  233. if (printk_ratelimit()) {
  234. printk(KERN_WARNING "Invalid hostname \"%.*s\" "
  235. "in NFS lock request\n",
  236. (int)hostname_len, hostname);
  237. }
  238. return NULL;
  239. }
  240. retry:
  241. spin_lock(&nsm_lock);
  242. list_for_each_entry(pos, &nsm_handles, sm_link) {
  243. if (hostname && nsm_use_hostnames) {
  244. if (strlen(pos->sm_name) != hostname_len
  245. || memcmp(pos->sm_name, hostname, hostname_len))
  246. continue;
  247. } else if (!nlm_cmp_addr(nsm_addr(pos), sap))
  248. continue;
  249. atomic_inc(&pos->sm_count);
  250. kfree(nsm);
  251. nsm = pos;
  252. dprintk("lockd: found nsm_handle for %s (%s), cnt %d\n",
  253. pos->sm_name, pos->sm_addrbuf,
  254. atomic_read(&pos->sm_count));
  255. goto found;
  256. }
  257. if (nsm) {
  258. list_add(&nsm->sm_link, &nsm_handles);
  259. dprintk("lockd: created nsm_handle for %s (%s)\n",
  260. nsm->sm_name, nsm->sm_addrbuf);
  261. goto found;
  262. }
  263. spin_unlock(&nsm_lock);
  264. if (!create)
  265. return NULL;
  266. nsm = kzalloc(sizeof(*nsm) + hostname_len + 1, GFP_KERNEL);
  267. if (nsm == NULL)
  268. return NULL;
  269. memcpy(nsm_addr(nsm), sap, salen);
  270. nsm->sm_addrlen = salen;
  271. nsm->sm_name = (char *) (nsm + 1);
  272. memcpy(nsm->sm_name, hostname, hostname_len);
  273. nsm->sm_name[hostname_len] = '\0';
  274. nsm_init_private(nsm);
  275. nsm_display_address((struct sockaddr *)&nsm->sm_addr,
  276. nsm->sm_addrbuf, sizeof(nsm->sm_addrbuf));
  277. atomic_set(&nsm->sm_count, 1);
  278. goto retry;
  279. found:
  280. spin_unlock(&nsm_lock);
  281. return nsm;
  282. }
  283. /**
  284. * nsm_reboot_lookup - match NLMPROC_SM_NOTIFY arguments to an nsm_handle
  285. * @info: pointer to NLMPROC_SM_NOTIFY arguments
  286. *
  287. * Returns a matching nsm_handle if found in the nsm cache; the returned
  288. * nsm_handle's reference count is bumped and sm_monitored is cleared.
  289. * Otherwise returns NULL if some error occurred.
  290. */
  291. struct nsm_handle *nsm_reboot_lookup(const struct nlm_reboot *info)
  292. {
  293. struct nsm_handle *cached;
  294. spin_lock(&nsm_lock);
  295. if (nsm_use_hostnames && info->mon != NULL)
  296. cached = nsm_lookup_hostname(info->mon, info->len);
  297. else
  298. cached = nsm_lookup_priv(&info->priv);
  299. if (unlikely(cached == NULL)) {
  300. spin_unlock(&nsm_lock);
  301. dprintk("lockd: never saw rebooted peer '%.*s' before\n",
  302. info->len, info->mon);
  303. return cached;
  304. }
  305. atomic_inc(&cached->sm_count);
  306. spin_unlock(&nsm_lock);
  307. /*
  308. * During subsequent lock activity, force a fresh
  309. * notification to be set up for this host.
  310. */
  311. cached->sm_monitored = 0;
  312. dprintk("lockd: host %s (%s) rebooted, cnt %d\n",
  313. cached->sm_name, cached->sm_addrbuf,
  314. atomic_read(&cached->sm_count));
  315. return cached;
  316. }
  317. /**
  318. * nsm_release - Release an NSM handle
  319. * @nsm: pointer to handle to be released
  320. *
  321. */
  322. void nsm_release(struct nsm_handle *nsm)
  323. {
  324. if (atomic_dec_and_lock(&nsm->sm_count, &nsm_lock)) {
  325. list_del(&nsm->sm_link);
  326. spin_unlock(&nsm_lock);
  327. dprintk("lockd: destroyed nsm_handle for %s (%s)\n",
  328. nsm->sm_name, nsm->sm_addrbuf);
  329. kfree(nsm);
  330. }
  331. }
  332. /*
  333. * Create NSM client for the local host
  334. */
  335. static struct rpc_clnt *
  336. nsm_create(void)
  337. {
  338. struct sockaddr_in sin = {
  339. .sin_family = AF_INET,
  340. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  341. .sin_port = 0,
  342. };
  343. struct rpc_create_args args = {
  344. .protocol = XPRT_TRANSPORT_UDP,
  345. .address = (struct sockaddr *)&sin,
  346. .addrsize = sizeof(sin),
  347. .servername = "localhost",
  348. .program = &nsm_program,
  349. .version = NSM_VERSION,
  350. .authflavor = RPC_AUTH_NULL,
  351. };
  352. return rpc_create(&args);
  353. }
  354. /*
  355. * XDR functions for NSM.
  356. *
  357. * See http://www.opengroup.org/ for details on the Network
  358. * Status Monitor wire protocol.
  359. */
  360. static int encode_nsm_string(struct xdr_stream *xdr, const char *string)
  361. {
  362. const u32 len = strlen(string);
  363. __be32 *p;
  364. if (unlikely(len > SM_MAXSTRLEN))
  365. return -EIO;
  366. p = xdr_reserve_space(xdr, sizeof(u32) + len);
  367. if (unlikely(p == NULL))
  368. return -EIO;
  369. xdr_encode_opaque(p, string, len);
  370. return 0;
  371. }
  372. /*
  373. * "mon_name" specifies the host to be monitored.
  374. */
  375. static int encode_mon_name(struct xdr_stream *xdr, const struct nsm_args *argp)
  376. {
  377. return encode_nsm_string(xdr, argp->mon_name);
  378. }
  379. /*
  380. * The "my_id" argument specifies the hostname and RPC procedure
  381. * to be called when the status manager receives notification
  382. * (via the NLMPROC_SM_NOTIFY call) that the state of host "mon_name"
  383. * has changed.
  384. */
  385. static int encode_my_id(struct xdr_stream *xdr, const struct nsm_args *argp)
  386. {
  387. int status;
  388. __be32 *p;
  389. status = encode_nsm_string(xdr, utsname()->nodename);
  390. if (unlikely(status != 0))
  391. return status;
  392. p = xdr_reserve_space(xdr, 3 * sizeof(u32));
  393. if (unlikely(p == NULL))
  394. return -EIO;
  395. *p++ = htonl(argp->prog);
  396. *p++ = htonl(argp->vers);
  397. *p++ = htonl(argp->proc);
  398. return 0;
  399. }
  400. /*
  401. * The "mon_id" argument specifies the non-private arguments
  402. * of an NSMPROC_MON or NSMPROC_UNMON call.
  403. */
  404. static int encode_mon_id(struct xdr_stream *xdr, const struct nsm_args *argp)
  405. {
  406. int status;
  407. status = encode_mon_name(xdr, argp);
  408. if (unlikely(status != 0))
  409. return status;
  410. return encode_my_id(xdr, argp);
  411. }
  412. /*
  413. * The "priv" argument may contain private information required
  414. * by the NSMPROC_MON call. This information will be supplied in the
  415. * NLMPROC_SM_NOTIFY call.
  416. */
  417. static int encode_priv(struct xdr_stream *xdr, const struct nsm_args *argp)
  418. {
  419. __be32 *p;
  420. p = xdr_reserve_space(xdr, SM_PRIV_SIZE);
  421. if (unlikely(p == NULL))
  422. return -EIO;
  423. xdr_encode_opaque_fixed(p, argp->priv->data, SM_PRIV_SIZE);
  424. return 0;
  425. }
  426. static int xdr_enc_mon(struct rpc_rqst *req, __be32 *p,
  427. const struct nsm_args *argp)
  428. {
  429. struct xdr_stream xdr;
  430. int status;
  431. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  432. status = encode_mon_id(&xdr, argp);
  433. if (unlikely(status))
  434. return status;
  435. return encode_priv(&xdr, argp);
  436. }
  437. static int xdr_enc_unmon(struct rpc_rqst *req, __be32 *p,
  438. const struct nsm_args *argp)
  439. {
  440. struct xdr_stream xdr;
  441. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  442. return encode_mon_id(&xdr, argp);
  443. }
  444. static int xdr_dec_stat_res(struct rpc_rqst *rqstp, __be32 *p,
  445. struct nsm_res *resp)
  446. {
  447. struct xdr_stream xdr;
  448. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  449. p = xdr_inline_decode(&xdr, 2 * sizeof(u32));
  450. if (unlikely(p == NULL))
  451. return -EIO;
  452. resp->status = ntohl(*p++);
  453. resp->state = ntohl(*p);
  454. dprintk("lockd: xdr_dec_stat_res status %d state %d\n",
  455. resp->status, resp->state);
  456. return 0;
  457. }
  458. static int xdr_dec_stat(struct rpc_rqst *rqstp, __be32 *p,
  459. struct nsm_res *resp)
  460. {
  461. struct xdr_stream xdr;
  462. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  463. p = xdr_inline_decode(&xdr, sizeof(u32));
  464. if (unlikely(p == NULL))
  465. return -EIO;
  466. resp->state = ntohl(*p);
  467. dprintk("lockd: xdr_dec_stat state %d\n", resp->state);
  468. return 0;
  469. }
  470. #define SM_my_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  471. #define SM_my_id_sz (SM_my_name_sz+3)
  472. #define SM_mon_name_sz (1+XDR_QUADLEN(SM_MAXSTRLEN))
  473. #define SM_mon_id_sz (SM_mon_name_sz+SM_my_id_sz)
  474. #define SM_priv_sz (XDR_QUADLEN(SM_PRIV_SIZE))
  475. #define SM_mon_sz (SM_mon_id_sz+SM_priv_sz)
  476. #define SM_monres_sz 2
  477. #define SM_unmonres_sz 1
  478. static struct rpc_procinfo nsm_procedures[] = {
  479. [NSMPROC_MON] = {
  480. .p_proc = NSMPROC_MON,
  481. .p_encode = (kxdrproc_t)xdr_enc_mon,
  482. .p_decode = (kxdrproc_t)xdr_dec_stat_res,
  483. .p_arglen = SM_mon_sz,
  484. .p_replen = SM_monres_sz,
  485. .p_statidx = NSMPROC_MON,
  486. .p_name = "MONITOR",
  487. },
  488. [NSMPROC_UNMON] = {
  489. .p_proc = NSMPROC_UNMON,
  490. .p_encode = (kxdrproc_t)xdr_enc_unmon,
  491. .p_decode = (kxdrproc_t)xdr_dec_stat,
  492. .p_arglen = SM_mon_id_sz,
  493. .p_replen = SM_unmonres_sz,
  494. .p_statidx = NSMPROC_UNMON,
  495. .p_name = "UNMONITOR",
  496. },
  497. };
  498. static struct rpc_version nsm_version1 = {
  499. .number = 1,
  500. .nrprocs = ARRAY_SIZE(nsm_procedures),
  501. .procs = nsm_procedures
  502. };
  503. static struct rpc_version * nsm_version[] = {
  504. [1] = &nsm_version1,
  505. };
  506. static struct rpc_stat nsm_stats;
  507. static struct rpc_program nsm_program = {
  508. .name = "statd",
  509. .number = NSM_PROGRAM,
  510. .nrvers = ARRAY_SIZE(nsm_version),
  511. .version = nsm_version,
  512. .stats = &nsm_stats
  513. };