svc.c 13 KB

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