svc.c 12 KB

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