svc.c 13 KB

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