mon.c 14 KB

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