mon.c 14 KB

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