svc.c 14 KB

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