svc.c 14 KB

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