mon.c 13 KB

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