svc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/config.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/sysctl.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/sched.h>
  20. #include <linux/errno.h>
  21. #include <linux/in.h>
  22. #include <linux/uio.h>
  23. #include <linux/slab.h>
  24. #include <linux/smp.h>
  25. #include <linux/smp_lock.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 <linux/lockd/lockd.h>
  32. #include <linux/nfs.h>
  33. #define NLMDBG_FACILITY NLMDBG_SVC
  34. #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
  35. #define ALLOWED_SIGS (sigmask(SIGKILL))
  36. static struct svc_program nlmsvc_program;
  37. struct nlmsvc_binding * nlmsvc_ops;
  38. EXPORT_SYMBOL(nlmsvc_ops);
  39. static DECLARE_MUTEX(nlmsvc_sema);
  40. static unsigned int nlmsvc_users;
  41. static pid_t nlmsvc_pid;
  42. int nlmsvc_grace_period;
  43. unsigned long nlmsvc_timeout;
  44. static DECLARE_MUTEX_LOCKED(lockd_start);
  45. static DECLARE_WAIT_QUEUE_HEAD(lockd_exit);
  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. /*
  54. * Constants needed for the sysctl interface.
  55. */
  56. static const unsigned long nlm_grace_period_min = 0;
  57. static const unsigned long nlm_grace_period_max = 240;
  58. static const unsigned long nlm_timeout_min = 3;
  59. static const unsigned long nlm_timeout_max = 20;
  60. static const int nlm_port_min = 0, nlm_port_max = 65535;
  61. static struct ctl_table_header * nlm_sysctl_table;
  62. static unsigned long set_grace_period(void)
  63. {
  64. unsigned long grace_period;
  65. /* Note: nlm_timeout should always be nonzero */
  66. if (nlm_grace_period)
  67. grace_period = ((nlm_grace_period + nlm_timeout - 1)
  68. / nlm_timeout) * nlm_timeout * HZ;
  69. else
  70. grace_period = nlm_timeout * 5 * HZ;
  71. nlmsvc_grace_period = 1;
  72. return grace_period + jiffies;
  73. }
  74. static inline void clear_grace_period(void)
  75. {
  76. nlmsvc_grace_period = 0;
  77. }
  78. /*
  79. * This is the lockd kernel thread
  80. */
  81. static void
  82. lockd(struct svc_rqst *rqstp)
  83. {
  84. struct svc_serv *serv = rqstp->rq_server;
  85. int err = 0;
  86. unsigned long grace_period_expire;
  87. /* Lock module and set up kernel thread */
  88. /* lockd_up is waiting for us to startup, so will
  89. * be holding a reference to this module, so it
  90. * is safe to just claim another reference
  91. */
  92. __module_get(THIS_MODULE);
  93. lock_kernel();
  94. /*
  95. * Let our maker know we're running.
  96. */
  97. nlmsvc_pid = current->pid;
  98. up(&lockd_start);
  99. daemonize("lockd");
  100. /* Process request with signals blocked, but allow SIGKILL. */
  101. allow_signal(SIGKILL);
  102. /* kick rpciod */
  103. rpciod_up();
  104. dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
  105. if (!nlm_timeout)
  106. nlm_timeout = LOCKD_DFLT_TIMEO;
  107. nlmsvc_timeout = nlm_timeout * HZ;
  108. grace_period_expire = set_grace_period();
  109. /*
  110. * The main request loop. We don't terminate until the last
  111. * NFS mount or NFS daemon has gone away, and we've been sent a
  112. * signal, or else another process has taken over our job.
  113. */
  114. while ((nlmsvc_users || !signalled()) && nlmsvc_pid == current->pid) {
  115. long timeout = MAX_SCHEDULE_TIMEOUT;
  116. if (signalled()) {
  117. flush_signals(current);
  118. if (nlmsvc_ops) {
  119. nlmsvc_invalidate_all();
  120. grace_period_expire = set_grace_period();
  121. }
  122. }
  123. /*
  124. * Retry any blocked locks that have been notified by
  125. * the VFS. Don't do this during grace period.
  126. * (Theoretically, there shouldn't even be blocked locks
  127. * during grace period).
  128. */
  129. if (!nlmsvc_grace_period) {
  130. timeout = nlmsvc_retry_blocked();
  131. } else if (time_before(grace_period_expire, jiffies))
  132. clear_grace_period();
  133. /*
  134. * Find a socket with data available and call its
  135. * recvfrom routine.
  136. */
  137. err = svc_recv(serv, rqstp, timeout);
  138. if (err == -EAGAIN || err == -EINTR)
  139. continue;
  140. if (err < 0) {
  141. printk(KERN_WARNING
  142. "lockd: terminating on error %d\n",
  143. -err);
  144. break;
  145. }
  146. dprintk("lockd: request from %08x\n",
  147. (unsigned)ntohl(rqstp->rq_addr.sin_addr.s_addr));
  148. svc_process(serv, rqstp);
  149. }
  150. /*
  151. * Check whether there's a new lockd process before
  152. * shutting down the hosts and clearing the slot.
  153. */
  154. if (!nlmsvc_pid || current->pid == nlmsvc_pid) {
  155. if (nlmsvc_ops)
  156. nlmsvc_invalidate_all();
  157. nlm_shutdown_hosts();
  158. nlmsvc_pid = 0;
  159. } else
  160. printk(KERN_DEBUG
  161. "lockd: new process, skipping host shutdown\n");
  162. wake_up(&lockd_exit);
  163. flush_signals(current);
  164. /* Exit the RPC thread */
  165. svc_exit_thread(rqstp);
  166. /* release rpciod */
  167. rpciod_down();
  168. /* Release module */
  169. unlock_kernel();
  170. module_put_and_exit(0);
  171. }
  172. /*
  173. * Bring up the lockd process if it's not already up.
  174. */
  175. int
  176. lockd_up(void)
  177. {
  178. static int warned;
  179. struct svc_serv * serv;
  180. int error = 0;
  181. down(&nlmsvc_sema);
  182. /*
  183. * Unconditionally increment the user count ... this is
  184. * the number of clients who _want_ a lockd process.
  185. */
  186. nlmsvc_users++;
  187. /*
  188. * Check whether we're already up and running.
  189. */
  190. if (nlmsvc_pid)
  191. goto out;
  192. /*
  193. * Sanity check: if there's no pid,
  194. * we should be the first user ...
  195. */
  196. if (nlmsvc_users > 1)
  197. printk(KERN_WARNING
  198. "lockd_up: no pid, %d users??\n", nlmsvc_users);
  199. error = -ENOMEM;
  200. serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE);
  201. if (!serv) {
  202. printk(KERN_WARNING "lockd_up: create service failed\n");
  203. goto out;
  204. }
  205. if ((error = svc_makesock(serv, IPPROTO_UDP, nlm_udpport)) < 0
  206. #ifdef CONFIG_NFSD_TCP
  207. || (error = svc_makesock(serv, IPPROTO_TCP, nlm_tcpport)) < 0
  208. #endif
  209. ) {
  210. if (warned++ == 0)
  211. printk(KERN_WARNING
  212. "lockd_up: makesock failed, error=%d\n", error);
  213. goto destroy_and_out;
  214. }
  215. warned = 0;
  216. /*
  217. * Create the kernel thread and wait for it to start.
  218. */
  219. error = svc_create_thread(lockd, serv);
  220. if (error) {
  221. printk(KERN_WARNING
  222. "lockd_up: create thread failed, error=%d\n", error);
  223. goto destroy_and_out;
  224. }
  225. down(&lockd_start);
  226. /*
  227. * Note: svc_serv structures have an initial use count of 1,
  228. * so we exit through here on both success and failure.
  229. */
  230. destroy_and_out:
  231. svc_destroy(serv);
  232. out:
  233. up(&nlmsvc_sema);
  234. return error;
  235. }
  236. EXPORT_SYMBOL(lockd_up);
  237. /*
  238. * Decrement the user count and bring down lockd if we're the last.
  239. */
  240. void
  241. lockd_down(void)
  242. {
  243. static int warned;
  244. down(&nlmsvc_sema);
  245. if (nlmsvc_users) {
  246. if (--nlmsvc_users)
  247. goto out;
  248. } else
  249. printk(KERN_WARNING "lockd_down: no users! pid=%d\n", nlmsvc_pid);
  250. if (!nlmsvc_pid) {
  251. if (warned++ == 0)
  252. printk(KERN_WARNING "lockd_down: no lockd running.\n");
  253. goto out;
  254. }
  255. warned = 0;
  256. kill_proc(nlmsvc_pid, SIGKILL, 1);
  257. /*
  258. * Wait for the lockd process to exit, but since we're holding
  259. * the lockd semaphore, we can't wait around forever ...
  260. */
  261. clear_thread_flag(TIF_SIGPENDING);
  262. interruptible_sleep_on_timeout(&lockd_exit, HZ);
  263. if (nlmsvc_pid) {
  264. printk(KERN_WARNING
  265. "lockd_down: lockd failed to exit, clearing pid\n");
  266. nlmsvc_pid = 0;
  267. }
  268. spin_lock_irq(&current->sighand->siglock);
  269. recalc_sigpending();
  270. spin_unlock_irq(&current->sighand->siglock);
  271. out:
  272. up(&nlmsvc_sema);
  273. }
  274. EXPORT_SYMBOL(lockd_down);
  275. /*
  276. * Sysctl parameters (same as module parameters, different interface).
  277. */
  278. /* Something that isn't CTL_ANY, CTL_NONE or a value that may clash. */
  279. #define CTL_UNNUMBERED -2
  280. static ctl_table nlm_sysctls[] = {
  281. {
  282. .ctl_name = CTL_UNNUMBERED,
  283. .procname = "nlm_grace_period",
  284. .data = &nlm_grace_period,
  285. .maxlen = sizeof(unsigned long),
  286. .mode = 0644,
  287. .proc_handler = &proc_doulongvec_minmax,
  288. .extra1 = (unsigned long *) &nlm_grace_period_min,
  289. .extra2 = (unsigned long *) &nlm_grace_period_max,
  290. },
  291. {
  292. .ctl_name = CTL_UNNUMBERED,
  293. .procname = "nlm_timeout",
  294. .data = &nlm_timeout,
  295. .maxlen = sizeof(unsigned long),
  296. .mode = 0644,
  297. .proc_handler = &proc_doulongvec_minmax,
  298. .extra1 = (unsigned long *) &nlm_timeout_min,
  299. .extra2 = (unsigned long *) &nlm_timeout_max,
  300. },
  301. {
  302. .ctl_name = CTL_UNNUMBERED,
  303. .procname = "nlm_udpport",
  304. .data = &nlm_udpport,
  305. .maxlen = sizeof(int),
  306. .mode = 0644,
  307. .proc_handler = &proc_dointvec_minmax,
  308. .extra1 = (int *) &nlm_port_min,
  309. .extra2 = (int *) &nlm_port_max,
  310. },
  311. {
  312. .ctl_name = CTL_UNNUMBERED,
  313. .procname = "nlm_tcpport",
  314. .data = &nlm_tcpport,
  315. .maxlen = sizeof(int),
  316. .mode = 0644,
  317. .proc_handler = &proc_dointvec_minmax,
  318. .extra1 = (int *) &nlm_port_min,
  319. .extra2 = (int *) &nlm_port_max,
  320. },
  321. { .ctl_name = 0 }
  322. };
  323. static ctl_table nlm_sysctl_dir[] = {
  324. {
  325. .ctl_name = CTL_UNNUMBERED,
  326. .procname = "nfs",
  327. .mode = 0555,
  328. .child = nlm_sysctls,
  329. },
  330. { .ctl_name = 0 }
  331. };
  332. static ctl_table nlm_sysctl_root[] = {
  333. {
  334. .ctl_name = CTL_FS,
  335. .procname = "fs",
  336. .mode = 0555,
  337. .child = nlm_sysctl_dir,
  338. },
  339. { .ctl_name = 0 }
  340. };
  341. /*
  342. * Module (and driverfs) parameters.
  343. */
  344. #define param_set_min_max(name, type, which_strtol, min, max) \
  345. static int param_set_##name(const char *val, struct kernel_param *kp) \
  346. { \
  347. char *endp; \
  348. __typeof__(type) num = which_strtol(val, &endp, 0); \
  349. if (endp == val || *endp || num < (min) || num > (max)) \
  350. return -EINVAL; \
  351. *((int *) kp->arg) = num; \
  352. return 0; \
  353. }
  354. static inline int is_callback(u32 proc)
  355. {
  356. return proc == NLMPROC_GRANTED
  357. || proc == NLMPROC_GRANTED_MSG
  358. || proc == NLMPROC_TEST_RES
  359. || proc == NLMPROC_LOCK_RES
  360. || proc == NLMPROC_CANCEL_RES
  361. || proc == NLMPROC_UNLOCK_RES
  362. || proc == NLMPROC_NSM_NOTIFY;
  363. }
  364. static int lockd_authenticate(struct svc_rqst *rqstp)
  365. {
  366. rqstp->rq_client = NULL;
  367. switch (rqstp->rq_authop->flavour) {
  368. case RPC_AUTH_NULL:
  369. case RPC_AUTH_UNIX:
  370. if (rqstp->rq_proc == 0)
  371. return SVC_OK;
  372. if (is_callback(rqstp->rq_proc)) {
  373. /* Leave it to individual procedures to
  374. * call nlmsvc_lookup_host(rqstp)
  375. */
  376. return SVC_OK;
  377. }
  378. return svc_set_client(rqstp);
  379. }
  380. return SVC_DENIED;
  381. }
  382. param_set_min_max(port, int, simple_strtol, 0, 65535)
  383. param_set_min_max(grace_period, unsigned long, simple_strtoul,
  384. nlm_grace_period_min, nlm_grace_period_max)
  385. param_set_min_max(timeout, unsigned long, simple_strtoul,
  386. nlm_timeout_min, nlm_timeout_max)
  387. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  388. MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
  389. MODULE_LICENSE("GPL");
  390. module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
  391. &nlm_grace_period, 0644);
  392. module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
  393. &nlm_timeout, 0644);
  394. module_param_call(nlm_udpport, param_set_port, param_get_int,
  395. &nlm_udpport, 0644);
  396. module_param_call(nlm_tcpport, param_set_port, param_get_int,
  397. &nlm_tcpport, 0644);
  398. /*
  399. * Initialising and terminating the module.
  400. */
  401. static int __init init_nlm(void)
  402. {
  403. nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root, 0);
  404. return nlm_sysctl_table ? 0 : -ENOMEM;
  405. }
  406. static void __exit exit_nlm(void)
  407. {
  408. /* FIXME: delete all NLM clients */
  409. nlm_shutdown_hosts();
  410. unregister_sysctl_table(nlm_sysctl_table);
  411. }
  412. module_init(init_nlm);
  413. module_exit(exit_nlm);
  414. /*
  415. * Define NLM program and procedures
  416. */
  417. static struct svc_version nlmsvc_version1 = {
  418. .vs_vers = 1,
  419. .vs_nproc = 17,
  420. .vs_proc = nlmsvc_procedures,
  421. .vs_xdrsize = NLMSVC_XDRSIZE,
  422. };
  423. static struct svc_version nlmsvc_version3 = {
  424. .vs_vers = 3,
  425. .vs_nproc = 24,
  426. .vs_proc = nlmsvc_procedures,
  427. .vs_xdrsize = NLMSVC_XDRSIZE,
  428. };
  429. #ifdef CONFIG_LOCKD_V4
  430. static struct svc_version nlmsvc_version4 = {
  431. .vs_vers = 4,
  432. .vs_nproc = 24,
  433. .vs_proc = nlmsvc_procedures4,
  434. .vs_xdrsize = NLMSVC_XDRSIZE,
  435. };
  436. #endif
  437. static struct svc_version * nlmsvc_version[] = {
  438. [1] = &nlmsvc_version1,
  439. [3] = &nlmsvc_version3,
  440. #ifdef CONFIG_LOCKD_V4
  441. [4] = &nlmsvc_version4,
  442. #endif
  443. };
  444. static struct svc_stat nlmsvc_stats;
  445. #define NLM_NRVERS (sizeof(nlmsvc_version)/sizeof(nlmsvc_version[0]))
  446. static struct svc_program nlmsvc_program = {
  447. .pg_prog = NLM_PROGRAM, /* program number */
  448. .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
  449. .pg_vers = nlmsvc_version, /* version table */
  450. .pg_name = "lockd", /* service name */
  451. .pg_class = "nfsd", /* share authentication with nfsd */
  452. .pg_stats = &nlmsvc_stats, /* stats table */
  453. .pg_authenticate = &lockd_authenticate /* export authentication */
  454. };