svc.c 13 KB

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