svc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*
  2. * linux/fs/lockd/svc.c
  3. *
  4. * This is the central lockd service.
  5. *
  6. * FIXME: Separate the lockd NFS server functionality from the lockd NFS
  7. * client functionality. Oh why didn't Sun create two separate
  8. * services in the first place?
  9. *
  10. * Authors: Olaf Kirch (okir@monad.swb.de)
  11. *
  12. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/sysctl.h>
  17. #include <linux/moduleparam.h>
  18. #include <linux/sched.h>
  19. #include <linux/errno.h>
  20. #include <linux/in.h>
  21. #include <linux/uio.h>
  22. #include <linux/smp.h>
  23. #include <linux/mutex.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/sunrpc/types.h>
  27. #include <linux/sunrpc/stats.h>
  28. #include <linux/sunrpc/clnt.h>
  29. #include <linux/sunrpc/svc.h>
  30. #include <linux/sunrpc/svcsock.h>
  31. #include <net/ip.h>
  32. #include <linux/lockd/lockd.h>
  33. #include <linux/nfs.h>
  34. #include "netns.h"
  35. #define NLMDBG_FACILITY NLMDBG_SVC
  36. #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
  37. #define ALLOWED_SIGS (sigmask(SIGKILL))
  38. static struct svc_program nlmsvc_program;
  39. struct nlmsvc_binding * nlmsvc_ops;
  40. EXPORT_SYMBOL_GPL(nlmsvc_ops);
  41. static DEFINE_MUTEX(nlmsvc_mutex);
  42. static unsigned int nlmsvc_users;
  43. static struct task_struct *nlmsvc_task;
  44. static struct svc_rqst *nlmsvc_rqst;
  45. unsigned long nlmsvc_timeout;
  46. int lockd_net_id;
  47. /*
  48. * These can be set at insmod time (useful for NFS as root filesystem),
  49. * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
  50. */
  51. static unsigned long nlm_grace_period;
  52. static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
  53. static int nlm_udpport, nlm_tcpport;
  54. /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
  55. static unsigned int nlm_max_connections = 1024;
  56. /*
  57. * Constants needed for the sysctl interface.
  58. */
  59. static const unsigned long nlm_grace_period_min = 0;
  60. static const unsigned long nlm_grace_period_max = 240;
  61. static const unsigned long nlm_timeout_min = 3;
  62. static const unsigned long nlm_timeout_max = 20;
  63. static const int nlm_port_min = 0, nlm_port_max = 65535;
  64. #ifdef CONFIG_SYSCTL
  65. static struct ctl_table_header * nlm_sysctl_table;
  66. #endif
  67. static unsigned long get_lockd_grace_period(void)
  68. {
  69. /* Note: nlm_timeout should always be nonzero */
  70. if (nlm_grace_period)
  71. return roundup(nlm_grace_period, nlm_timeout) * HZ;
  72. else
  73. return nlm_timeout * 5 * HZ;
  74. }
  75. static struct lock_manager lockd_manager = {
  76. };
  77. static void grace_ender(struct work_struct *not_used)
  78. {
  79. locks_end_grace(&lockd_manager);
  80. }
  81. static DECLARE_DELAYED_WORK(grace_period_end, grace_ender);
  82. static void set_grace_period(void)
  83. {
  84. unsigned long grace_period = get_lockd_grace_period();
  85. locks_start_grace(&lockd_manager);
  86. cancel_delayed_work_sync(&grace_period_end);
  87. schedule_delayed_work(&grace_period_end, grace_period);
  88. }
  89. static void restart_grace(void)
  90. {
  91. if (nlmsvc_ops) {
  92. cancel_delayed_work_sync(&grace_period_end);
  93. locks_end_grace(&lockd_manager);
  94. nlmsvc_invalidate_all();
  95. set_grace_period();
  96. }
  97. }
  98. /*
  99. * This is the lockd kernel thread
  100. */
  101. static int
  102. lockd(void *vrqstp)
  103. {
  104. int err = 0, preverr = 0;
  105. struct svc_rqst *rqstp = vrqstp;
  106. /* try_to_freeze() is called from svc_recv() */
  107. set_freezable();
  108. /* Allow SIGKILL to tell lockd to drop all of its locks */
  109. allow_signal(SIGKILL);
  110. dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
  111. if (!nlm_timeout)
  112. nlm_timeout = LOCKD_DFLT_TIMEO;
  113. nlmsvc_timeout = nlm_timeout * HZ;
  114. set_grace_period();
  115. /*
  116. * The main request loop. We don't terminate until the last
  117. * NFS mount or NFS daemon has gone away.
  118. */
  119. while (!kthread_should_stop()) {
  120. long timeout = MAX_SCHEDULE_TIMEOUT;
  121. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  122. /* update sv_maxconn if it has changed */
  123. rqstp->rq_server->sv_maxconn = nlm_max_connections;
  124. if (signalled()) {
  125. flush_signals(current);
  126. restart_grace();
  127. continue;
  128. }
  129. timeout = nlmsvc_retry_blocked();
  130. /*
  131. * Find a socket with data available and call its
  132. * recvfrom routine.
  133. */
  134. err = svc_recv(rqstp, timeout);
  135. if (err == -EAGAIN || err == -EINTR) {
  136. preverr = err;
  137. continue;
  138. }
  139. if (err < 0) {
  140. if (err != preverr) {
  141. printk(KERN_WARNING "%s: unexpected error "
  142. "from svc_recv (%d)\n", __func__, err);
  143. preverr = err;
  144. }
  145. schedule_timeout_interruptible(HZ);
  146. continue;
  147. }
  148. preverr = err;
  149. dprintk("lockd: request from %s\n",
  150. svc_print_addr(rqstp, buf, sizeof(buf)));
  151. svc_process(rqstp);
  152. }
  153. flush_signals(current);
  154. cancel_delayed_work_sync(&grace_period_end);
  155. locks_end_grace(&lockd_manager);
  156. if (nlmsvc_ops)
  157. nlmsvc_invalidate_all();
  158. nlm_shutdown_hosts();
  159. return 0;
  160. }
  161. static int create_lockd_listener(struct svc_serv *serv, const char *name,
  162. struct net *net, const int family,
  163. const unsigned short port)
  164. {
  165. struct svc_xprt *xprt;
  166. xprt = svc_find_xprt(serv, name, net, family, 0);
  167. if (xprt == NULL)
  168. return svc_create_xprt(serv, name, net, family, port,
  169. SVC_SOCK_DEFAULTS);
  170. svc_xprt_put(xprt);
  171. return 0;
  172. }
  173. static int create_lockd_family(struct svc_serv *serv, struct net *net,
  174. const int family)
  175. {
  176. int err;
  177. err = create_lockd_listener(serv, "udp", net, family, nlm_udpport);
  178. if (err < 0)
  179. return err;
  180. return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport);
  181. }
  182. /*
  183. * Ensure there are active UDP and TCP listeners for lockd.
  184. *
  185. * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
  186. * local services (such as rpc.statd) still require UDP, and
  187. * some NFS servers do not yet support NLM over TCP.
  188. *
  189. * Returns zero if all listeners are available; otherwise a
  190. * negative errno value is returned.
  191. */
  192. static int make_socks(struct svc_serv *serv, struct net *net)
  193. {
  194. static int warned;
  195. int err;
  196. err = create_lockd_family(serv, net, PF_INET);
  197. if (err < 0)
  198. goto out_err;
  199. err = create_lockd_family(serv, net, PF_INET6);
  200. if (err < 0 && err != -EAFNOSUPPORT)
  201. goto out_err;
  202. warned = 0;
  203. return 0;
  204. out_err:
  205. if (warned++ == 0)
  206. printk(KERN_WARNING
  207. "lockd_up: makesock failed, error=%d\n", err);
  208. return err;
  209. }
  210. static int lockd_up_net(struct net *net)
  211. {
  212. struct lockd_net *ln = net_generic(net, lockd_net_id);
  213. struct svc_serv *serv = nlmsvc_rqst->rq_server;
  214. int error;
  215. if (ln->nlmsvc_users)
  216. return 0;
  217. error = svc_rpcb_setup(serv, net);
  218. if (error)
  219. goto err_rpcb;
  220. error = make_socks(serv, net);
  221. if (error < 0)
  222. goto err_socks;
  223. return 0;
  224. err_socks:
  225. svc_rpcb_cleanup(serv, net);
  226. err_rpcb:
  227. return error;
  228. }
  229. static void lockd_down_net(struct net *net)
  230. {
  231. struct lockd_net *ln = net_generic(net, lockd_net_id);
  232. struct svc_serv *serv = nlmsvc_rqst->rq_server;
  233. if (ln->nlmsvc_users) {
  234. if (--ln->nlmsvc_users == 0) {
  235. nlm_shutdown_hosts_net(net);
  236. svc_shutdown_net(serv, net);
  237. }
  238. } else {
  239. printk(KERN_ERR "lockd_down_net: no users! task=%p, net=%p\n",
  240. nlmsvc_task, net);
  241. BUG();
  242. }
  243. }
  244. /*
  245. * Bring up the lockd process if it's not already up.
  246. */
  247. int lockd_up(void)
  248. {
  249. struct svc_serv *serv;
  250. int error = 0;
  251. struct net *net = current->nsproxy->net_ns;
  252. mutex_lock(&nlmsvc_mutex);
  253. /*
  254. * Check whether we're already up and running.
  255. */
  256. if (nlmsvc_rqst) {
  257. error = lockd_up_net(net);
  258. goto out;
  259. }
  260. /*
  261. * Sanity check: if there's no pid,
  262. * we should be the first user ...
  263. */
  264. if (nlmsvc_users)
  265. printk(KERN_WARNING
  266. "lockd_up: no pid, %d users??\n", nlmsvc_users);
  267. error = -ENOMEM;
  268. serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, NULL);
  269. if (!serv) {
  270. printk(KERN_WARNING "lockd_up: create service failed\n");
  271. goto out;
  272. }
  273. error = make_socks(serv, net);
  274. if (error < 0)
  275. goto destroy_and_out;
  276. /*
  277. * Create the kernel thread and wait for it to start.
  278. */
  279. nlmsvc_rqst = svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
  280. if (IS_ERR(nlmsvc_rqst)) {
  281. error = PTR_ERR(nlmsvc_rqst);
  282. nlmsvc_rqst = NULL;
  283. printk(KERN_WARNING
  284. "lockd_up: svc_rqst allocation failed, error=%d\n",
  285. error);
  286. goto destroy_and_out;
  287. }
  288. svc_sock_update_bufs(serv);
  289. serv->sv_maxconn = nlm_max_connections;
  290. nlmsvc_task = kthread_run(lockd, nlmsvc_rqst, serv->sv_name);
  291. if (IS_ERR(nlmsvc_task)) {
  292. error = PTR_ERR(nlmsvc_task);
  293. svc_exit_thread(nlmsvc_rqst);
  294. nlmsvc_task = NULL;
  295. nlmsvc_rqst = NULL;
  296. printk(KERN_WARNING
  297. "lockd_up: kthread_run failed, error=%d\n", error);
  298. goto destroy_and_out;
  299. }
  300. /*
  301. * Note: svc_serv structures have an initial use count of 1,
  302. * so we exit through here on both success and failure.
  303. */
  304. destroy_and_out:
  305. svc_destroy(serv);
  306. out:
  307. if (!error) {
  308. struct lockd_net *ln = net_generic(net, lockd_net_id);
  309. ln->nlmsvc_users++;
  310. nlmsvc_users++;
  311. }
  312. mutex_unlock(&nlmsvc_mutex);
  313. return error;
  314. }
  315. EXPORT_SYMBOL_GPL(lockd_up);
  316. /*
  317. * Decrement the user count and bring down lockd if we're the last.
  318. */
  319. void
  320. lockd_down(void)
  321. {
  322. mutex_lock(&nlmsvc_mutex);
  323. if (nlmsvc_users) {
  324. if (--nlmsvc_users) {
  325. lockd_down_net(current->nsproxy->net_ns);
  326. goto out;
  327. }
  328. } else {
  329. printk(KERN_ERR "lockd_down: no users! task=%p\n",
  330. nlmsvc_task);
  331. BUG();
  332. }
  333. if (!nlmsvc_task) {
  334. printk(KERN_ERR "lockd_down: no lockd running.\n");
  335. BUG();
  336. }
  337. kthread_stop(nlmsvc_task);
  338. svc_exit_thread(nlmsvc_rqst);
  339. nlmsvc_task = NULL;
  340. nlmsvc_rqst = NULL;
  341. out:
  342. mutex_unlock(&nlmsvc_mutex);
  343. }
  344. EXPORT_SYMBOL_GPL(lockd_down);
  345. #ifdef CONFIG_SYSCTL
  346. /*
  347. * Sysctl parameters (same as module parameters, different interface).
  348. */
  349. static ctl_table nlm_sysctls[] = {
  350. {
  351. .procname = "nlm_grace_period",
  352. .data = &nlm_grace_period,
  353. .maxlen = sizeof(unsigned long),
  354. .mode = 0644,
  355. .proc_handler = proc_doulongvec_minmax,
  356. .extra1 = (unsigned long *) &nlm_grace_period_min,
  357. .extra2 = (unsigned long *) &nlm_grace_period_max,
  358. },
  359. {
  360. .procname = "nlm_timeout",
  361. .data = &nlm_timeout,
  362. .maxlen = sizeof(unsigned long),
  363. .mode = 0644,
  364. .proc_handler = proc_doulongvec_minmax,
  365. .extra1 = (unsigned long *) &nlm_timeout_min,
  366. .extra2 = (unsigned long *) &nlm_timeout_max,
  367. },
  368. {
  369. .procname = "nlm_udpport",
  370. .data = &nlm_udpport,
  371. .maxlen = sizeof(int),
  372. .mode = 0644,
  373. .proc_handler = proc_dointvec_minmax,
  374. .extra1 = (int *) &nlm_port_min,
  375. .extra2 = (int *) &nlm_port_max,
  376. },
  377. {
  378. .procname = "nlm_tcpport",
  379. .data = &nlm_tcpport,
  380. .maxlen = sizeof(int),
  381. .mode = 0644,
  382. .proc_handler = proc_dointvec_minmax,
  383. .extra1 = (int *) &nlm_port_min,
  384. .extra2 = (int *) &nlm_port_max,
  385. },
  386. {
  387. .procname = "nsm_use_hostnames",
  388. .data = &nsm_use_hostnames,
  389. .maxlen = sizeof(int),
  390. .mode = 0644,
  391. .proc_handler = proc_dointvec,
  392. },
  393. {
  394. .procname = "nsm_local_state",
  395. .data = &nsm_local_state,
  396. .maxlen = sizeof(int),
  397. .mode = 0644,
  398. .proc_handler = proc_dointvec,
  399. },
  400. { }
  401. };
  402. static ctl_table nlm_sysctl_dir[] = {
  403. {
  404. .procname = "nfs",
  405. .mode = 0555,
  406. .child = nlm_sysctls,
  407. },
  408. { }
  409. };
  410. static ctl_table nlm_sysctl_root[] = {
  411. {
  412. .procname = "fs",
  413. .mode = 0555,
  414. .child = nlm_sysctl_dir,
  415. },
  416. { }
  417. };
  418. #endif /* CONFIG_SYSCTL */
  419. /*
  420. * Module (and sysfs) parameters.
  421. */
  422. #define param_set_min_max(name, type, which_strtol, min, max) \
  423. static int param_set_##name(const char *val, struct kernel_param *kp) \
  424. { \
  425. char *endp; \
  426. __typeof__(type) num = which_strtol(val, &endp, 0); \
  427. if (endp == val || *endp || num < (min) || num > (max)) \
  428. return -EINVAL; \
  429. *((type *) kp->arg) = num; \
  430. return 0; \
  431. }
  432. static inline int is_callback(u32 proc)
  433. {
  434. return proc == NLMPROC_GRANTED
  435. || proc == NLMPROC_GRANTED_MSG
  436. || proc == NLMPROC_TEST_RES
  437. || proc == NLMPROC_LOCK_RES
  438. || proc == NLMPROC_CANCEL_RES
  439. || proc == NLMPROC_UNLOCK_RES
  440. || proc == NLMPROC_NSM_NOTIFY;
  441. }
  442. static int lockd_authenticate(struct svc_rqst *rqstp)
  443. {
  444. rqstp->rq_client = NULL;
  445. switch (rqstp->rq_authop->flavour) {
  446. case RPC_AUTH_NULL:
  447. case RPC_AUTH_UNIX:
  448. if (rqstp->rq_proc == 0)
  449. return SVC_OK;
  450. if (is_callback(rqstp->rq_proc)) {
  451. /* Leave it to individual procedures to
  452. * call nlmsvc_lookup_host(rqstp)
  453. */
  454. return SVC_OK;
  455. }
  456. return svc_set_client(rqstp);
  457. }
  458. return SVC_DENIED;
  459. }
  460. param_set_min_max(port, int, simple_strtol, 0, 65535)
  461. param_set_min_max(grace_period, unsigned long, simple_strtoul,
  462. nlm_grace_period_min, nlm_grace_period_max)
  463. param_set_min_max(timeout, unsigned long, simple_strtoul,
  464. nlm_timeout_min, nlm_timeout_max)
  465. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  466. MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
  467. MODULE_LICENSE("GPL");
  468. module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
  469. &nlm_grace_period, 0644);
  470. module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
  471. &nlm_timeout, 0644);
  472. module_param_call(nlm_udpport, param_set_port, param_get_int,
  473. &nlm_udpport, 0644);
  474. module_param_call(nlm_tcpport, param_set_port, param_get_int,
  475. &nlm_tcpport, 0644);
  476. module_param(nsm_use_hostnames, bool, 0644);
  477. module_param(nlm_max_connections, uint, 0644);
  478. static int lockd_init_net(struct net *net)
  479. {
  480. return 0;
  481. }
  482. static void lockd_exit_net(struct net *net)
  483. {
  484. }
  485. static struct pernet_operations lockd_net_ops = {
  486. .init = lockd_init_net,
  487. .exit = lockd_exit_net,
  488. .id = &lockd_net_id,
  489. .size = sizeof(struct lockd_net),
  490. };
  491. /*
  492. * Initialising and terminating the module.
  493. */
  494. static int __init init_nlm(void)
  495. {
  496. int err;
  497. #ifdef CONFIG_SYSCTL
  498. err = -ENOMEM;
  499. nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
  500. if (nlm_sysctl_table == NULL)
  501. goto err_sysctl;
  502. #endif
  503. err = register_pernet_subsys(&lockd_net_ops);
  504. if (err)
  505. goto err_pernet;
  506. return 0;
  507. err_pernet:
  508. #ifdef CONFIG_SYSCTL
  509. unregister_sysctl_table(nlm_sysctl_table);
  510. #endif
  511. err_sysctl:
  512. return err;
  513. }
  514. static void __exit exit_nlm(void)
  515. {
  516. /* FIXME: delete all NLM clients */
  517. nlm_shutdown_hosts();
  518. unregister_pernet_subsys(&lockd_net_ops);
  519. #ifdef CONFIG_SYSCTL
  520. unregister_sysctl_table(nlm_sysctl_table);
  521. #endif
  522. }
  523. module_init(init_nlm);
  524. module_exit(exit_nlm);
  525. /*
  526. * Define NLM program and procedures
  527. */
  528. static struct svc_version nlmsvc_version1 = {
  529. .vs_vers = 1,
  530. .vs_nproc = 17,
  531. .vs_proc = nlmsvc_procedures,
  532. .vs_xdrsize = NLMSVC_XDRSIZE,
  533. };
  534. static struct svc_version nlmsvc_version3 = {
  535. .vs_vers = 3,
  536. .vs_nproc = 24,
  537. .vs_proc = nlmsvc_procedures,
  538. .vs_xdrsize = NLMSVC_XDRSIZE,
  539. };
  540. #ifdef CONFIG_LOCKD_V4
  541. static struct svc_version nlmsvc_version4 = {
  542. .vs_vers = 4,
  543. .vs_nproc = 24,
  544. .vs_proc = nlmsvc_procedures4,
  545. .vs_xdrsize = NLMSVC_XDRSIZE,
  546. };
  547. #endif
  548. static struct svc_version * nlmsvc_version[] = {
  549. [1] = &nlmsvc_version1,
  550. [3] = &nlmsvc_version3,
  551. #ifdef CONFIG_LOCKD_V4
  552. [4] = &nlmsvc_version4,
  553. #endif
  554. };
  555. static struct svc_stat nlmsvc_stats;
  556. #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
  557. static struct svc_program nlmsvc_program = {
  558. .pg_prog = NLM_PROGRAM, /* program number */
  559. .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
  560. .pg_vers = nlmsvc_version, /* version table */
  561. .pg_name = "lockd", /* service name */
  562. .pg_class = "nfsd", /* share authentication with nfsd */
  563. .pg_stats = &nlmsvc_stats, /* stats table */
  564. .pg_authenticate = &lockd_authenticate /* export authentication */
  565. };