svc.c 14 KB

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