svc.c 14 KB

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