svc.c 13 KB

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