svc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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/lockd/sm_inter.h>
  36. #include <linux/nfs.h>
  37. #define NLMDBG_FACILITY NLMDBG_SVC
  38. #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
  39. #define ALLOWED_SIGS (sigmask(SIGKILL))
  40. static struct svc_program nlmsvc_program;
  41. struct nlmsvc_binding * nlmsvc_ops;
  42. EXPORT_SYMBOL(nlmsvc_ops);
  43. static DEFINE_MUTEX(nlmsvc_mutex);
  44. static unsigned int nlmsvc_users;
  45. static struct task_struct *nlmsvc_task;
  46. static struct svc_serv *nlmsvc_serv;
  47. int nlmsvc_grace_period;
  48. unsigned long nlmsvc_timeout;
  49. /*
  50. * These can be set at insmod time (useful for NFS as root filesystem),
  51. * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
  52. */
  53. static unsigned long nlm_grace_period;
  54. static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
  55. static int nlm_udpport, nlm_tcpport;
  56. int nsm_use_hostnames = 0;
  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. unsigned long get_nfs_grace_period(void)
  77. {
  78. unsigned long lockdgrace = get_lockd_grace_period();
  79. unsigned long nfsdgrace = 0;
  80. if (nlmsvc_ops)
  81. nfsdgrace = nlmsvc_ops->get_grace_period();
  82. return max(lockdgrace, nfsdgrace);
  83. }
  84. EXPORT_SYMBOL(get_nfs_grace_period);
  85. static unsigned long set_grace_period(void)
  86. {
  87. nlmsvc_grace_period = 1;
  88. return get_nfs_grace_period() + jiffies;
  89. }
  90. static inline void clear_grace_period(void)
  91. {
  92. nlmsvc_grace_period = 0;
  93. }
  94. /*
  95. * This is the lockd kernel thread
  96. */
  97. static int
  98. lockd(void *vrqstp)
  99. {
  100. int err = 0, preverr = 0;
  101. struct svc_rqst *rqstp = vrqstp;
  102. unsigned long grace_period_expire;
  103. /* try_to_freeze() is called from svc_recv() */
  104. set_freezable();
  105. /* Allow SIGKILL to tell lockd to drop all of its locks */
  106. allow_signal(SIGKILL);
  107. dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
  108. /*
  109. * FIXME: it would be nice if lockd didn't spend its entire life
  110. * running under the BKL. At the very least, it would be good to
  111. * have someone clarify what it's intended to protect here. I've
  112. * seen some handwavy posts about posix locking needing to be
  113. * done under the BKL, but it's far from clear.
  114. */
  115. lock_kernel();
  116. if (!nlm_timeout)
  117. nlm_timeout = LOCKD_DFLT_TIMEO;
  118. nlmsvc_timeout = nlm_timeout * HZ;
  119. grace_period_expire = set_grace_period();
  120. /*
  121. * The main request loop. We don't terminate until the last
  122. * NFS mount or NFS daemon has gone away.
  123. */
  124. while (!kthread_should_stop()) {
  125. long timeout = MAX_SCHEDULE_TIMEOUT;
  126. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  127. if (signalled()) {
  128. flush_signals(current);
  129. if (nlmsvc_ops) {
  130. nlmsvc_invalidate_all();
  131. grace_period_expire = set_grace_period();
  132. }
  133. continue;
  134. }
  135. /*
  136. * Retry any blocked locks that have been notified by
  137. * the VFS. Don't do this during grace period.
  138. * (Theoretically, there shouldn't even be blocked locks
  139. * during grace period).
  140. */
  141. if (!nlmsvc_grace_period) {
  142. timeout = nlmsvc_retry_blocked();
  143. } else if (time_before(grace_period_expire, jiffies))
  144. clear_grace_period();
  145. /*
  146. * Find a socket with data available and call its
  147. * recvfrom routine.
  148. */
  149. err = svc_recv(rqstp, timeout);
  150. if (err == -EAGAIN || err == -EINTR) {
  151. preverr = err;
  152. continue;
  153. }
  154. if (err < 0) {
  155. if (err != preverr) {
  156. printk(KERN_WARNING "%s: unexpected error "
  157. "from svc_recv (%d)\n", __func__, err);
  158. preverr = err;
  159. }
  160. schedule_timeout_interruptible(HZ);
  161. continue;
  162. }
  163. preverr = err;
  164. dprintk("lockd: request from %s\n",
  165. svc_print_addr(rqstp, buf, sizeof(buf)));
  166. svc_process(rqstp);
  167. }
  168. flush_signals(current);
  169. if (nlmsvc_ops)
  170. nlmsvc_invalidate_all();
  171. nlm_shutdown_hosts();
  172. unlock_kernel();
  173. nlmsvc_task = NULL;
  174. nlmsvc_serv = NULL;
  175. /* Exit the RPC thread */
  176. svc_exit_thread(rqstp);
  177. return 0;
  178. }
  179. /*
  180. * Make any sockets that are needed but not present.
  181. * If nlm_udpport or nlm_tcpport were set as module
  182. * options, make those sockets unconditionally
  183. */
  184. static int make_socks(struct svc_serv *serv, int proto)
  185. {
  186. static int warned;
  187. struct svc_xprt *xprt;
  188. int err = 0;
  189. if (proto == IPPROTO_UDP || nlm_udpport) {
  190. xprt = svc_find_xprt(serv, "udp", 0, 0);
  191. if (!xprt)
  192. err = svc_create_xprt(serv, "udp", nlm_udpport,
  193. SVC_SOCK_DEFAULTS);
  194. else
  195. svc_xprt_put(xprt);
  196. }
  197. if (err >= 0 && (proto == IPPROTO_TCP || nlm_tcpport)) {
  198. xprt = svc_find_xprt(serv, "tcp", 0, 0);
  199. if (!xprt)
  200. err = svc_create_xprt(serv, "tcp", nlm_tcpport,
  201. SVC_SOCK_DEFAULTS);
  202. else
  203. svc_xprt_put(xprt);
  204. }
  205. if (err >= 0) {
  206. warned = 0;
  207. err = 0;
  208. } else if (warned++ == 0)
  209. printk(KERN_WARNING
  210. "lockd_up: makesock failed, error=%d\n", err);
  211. return err;
  212. }
  213. /*
  214. * Bring up the lockd process if it's not already up.
  215. */
  216. int
  217. lockd_up(int proto) /* Maybe add a 'family' option when IPv6 is supported ?? */
  218. {
  219. struct svc_serv *serv;
  220. struct svc_rqst *rqstp;
  221. int error = 0;
  222. mutex_lock(&nlmsvc_mutex);
  223. /*
  224. * Check whether we're already up and running.
  225. */
  226. if (nlmsvc_serv) {
  227. if (proto)
  228. error = make_socks(nlmsvc_serv, proto);
  229. goto out;
  230. }
  231. /*
  232. * Sanity check: if there's no pid,
  233. * we should be the first user ...
  234. */
  235. if (nlmsvc_users)
  236. printk(KERN_WARNING
  237. "lockd_up: no pid, %d users??\n", nlmsvc_users);
  238. error = -ENOMEM;
  239. serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, NULL);
  240. if (!serv) {
  241. printk(KERN_WARNING "lockd_up: create service failed\n");
  242. goto out;
  243. }
  244. if ((error = make_socks(serv, proto)) < 0)
  245. goto destroy_and_out;
  246. /*
  247. * Create the kernel thread and wait for it to start.
  248. */
  249. rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
  250. if (IS_ERR(rqstp)) {
  251. error = PTR_ERR(rqstp);
  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. nlmsvc_serv = rqstp->rq_server;
  259. nlmsvc_task = kthread_run(lockd, rqstp, serv->sv_name);
  260. if (IS_ERR(nlmsvc_task)) {
  261. error = PTR_ERR(nlmsvc_task);
  262. nlmsvc_task = NULL;
  263. nlmsvc_serv = NULL;
  264. printk(KERN_WARNING
  265. "lockd_up: kthread_run failed, error=%d\n", error);
  266. svc_exit_thread(rqstp);
  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(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. out:
  303. mutex_unlock(&nlmsvc_mutex);
  304. }
  305. EXPORT_SYMBOL(lockd_down);
  306. #ifdef CONFIG_SYSCTL
  307. /*
  308. * Sysctl parameters (same as module parameters, different interface).
  309. */
  310. static ctl_table nlm_sysctls[] = {
  311. {
  312. .ctl_name = CTL_UNNUMBERED,
  313. .procname = "nlm_grace_period",
  314. .data = &nlm_grace_period,
  315. .maxlen = sizeof(unsigned long),
  316. .mode = 0644,
  317. .proc_handler = &proc_doulongvec_minmax,
  318. .extra1 = (unsigned long *) &nlm_grace_period_min,
  319. .extra2 = (unsigned long *) &nlm_grace_period_max,
  320. },
  321. {
  322. .ctl_name = CTL_UNNUMBERED,
  323. .procname = "nlm_timeout",
  324. .data = &nlm_timeout,
  325. .maxlen = sizeof(unsigned long),
  326. .mode = 0644,
  327. .proc_handler = &proc_doulongvec_minmax,
  328. .extra1 = (unsigned long *) &nlm_timeout_min,
  329. .extra2 = (unsigned long *) &nlm_timeout_max,
  330. },
  331. {
  332. .ctl_name = CTL_UNNUMBERED,
  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. .ctl_name = CTL_UNNUMBERED,
  343. .procname = "nlm_tcpport",
  344. .data = &nlm_tcpport,
  345. .maxlen = sizeof(int),
  346. .mode = 0644,
  347. .proc_handler = &proc_dointvec_minmax,
  348. .extra1 = (int *) &nlm_port_min,
  349. .extra2 = (int *) &nlm_port_max,
  350. },
  351. {
  352. .ctl_name = CTL_UNNUMBERED,
  353. .procname = "nsm_use_hostnames",
  354. .data = &nsm_use_hostnames,
  355. .maxlen = sizeof(int),
  356. .mode = 0644,
  357. .proc_handler = &proc_dointvec,
  358. },
  359. {
  360. .ctl_name = CTL_UNNUMBERED,
  361. .procname = "nsm_local_state",
  362. .data = &nsm_local_state,
  363. .maxlen = sizeof(int),
  364. .mode = 0644,
  365. .proc_handler = &proc_dointvec,
  366. },
  367. { .ctl_name = 0 }
  368. };
  369. static ctl_table nlm_sysctl_dir[] = {
  370. {
  371. .ctl_name = CTL_UNNUMBERED,
  372. .procname = "nfs",
  373. .mode = 0555,
  374. .child = nlm_sysctls,
  375. },
  376. { .ctl_name = 0 }
  377. };
  378. static ctl_table nlm_sysctl_root[] = {
  379. {
  380. .ctl_name = CTL_FS,
  381. .procname = "fs",
  382. .mode = 0555,
  383. .child = nlm_sysctl_dir,
  384. },
  385. { .ctl_name = 0 }
  386. };
  387. #endif /* CONFIG_SYSCTL */
  388. /*
  389. * Module (and sysfs) parameters.
  390. */
  391. #define param_set_min_max(name, type, which_strtol, min, max) \
  392. static int param_set_##name(const char *val, struct kernel_param *kp) \
  393. { \
  394. char *endp; \
  395. __typeof__(type) num = which_strtol(val, &endp, 0); \
  396. if (endp == val || *endp || num < (min) || num > (max)) \
  397. return -EINVAL; \
  398. *((int *) kp->arg) = num; \
  399. return 0; \
  400. }
  401. static inline int is_callback(u32 proc)
  402. {
  403. return proc == NLMPROC_GRANTED
  404. || proc == NLMPROC_GRANTED_MSG
  405. || proc == NLMPROC_TEST_RES
  406. || proc == NLMPROC_LOCK_RES
  407. || proc == NLMPROC_CANCEL_RES
  408. || proc == NLMPROC_UNLOCK_RES
  409. || proc == NLMPROC_NSM_NOTIFY;
  410. }
  411. static int lockd_authenticate(struct svc_rqst *rqstp)
  412. {
  413. rqstp->rq_client = NULL;
  414. switch (rqstp->rq_authop->flavour) {
  415. case RPC_AUTH_NULL:
  416. case RPC_AUTH_UNIX:
  417. if (rqstp->rq_proc == 0)
  418. return SVC_OK;
  419. if (is_callback(rqstp->rq_proc)) {
  420. /* Leave it to individual procedures to
  421. * call nlmsvc_lookup_host(rqstp)
  422. */
  423. return SVC_OK;
  424. }
  425. return svc_set_client(rqstp);
  426. }
  427. return SVC_DENIED;
  428. }
  429. param_set_min_max(port, int, simple_strtol, 0, 65535)
  430. param_set_min_max(grace_period, unsigned long, simple_strtoul,
  431. nlm_grace_period_min, nlm_grace_period_max)
  432. param_set_min_max(timeout, unsigned long, simple_strtoul,
  433. nlm_timeout_min, nlm_timeout_max)
  434. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  435. MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
  436. MODULE_LICENSE("GPL");
  437. module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
  438. &nlm_grace_period, 0644);
  439. module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
  440. &nlm_timeout, 0644);
  441. module_param_call(nlm_udpport, param_set_port, param_get_int,
  442. &nlm_udpport, 0644);
  443. module_param_call(nlm_tcpport, param_set_port, param_get_int,
  444. &nlm_tcpport, 0644);
  445. module_param(nsm_use_hostnames, bool, 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. };