svc.c 13 KB

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