/kern_oII/fs/lockd/svc.c

http://omnia2droid.googlecode.com/ · C · 590 lines · 426 code · 75 blank · 89 comment · 56 complexity · ee83735b259b231480d486ec5d0d6a8e MD5 · raw file

  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/kthread.h>
  27. #include <linux/freezer.h>
  28. #include <linux/sunrpc/types.h>
  29. #include <linux/sunrpc/stats.h>
  30. #include <linux/sunrpc/clnt.h>
  31. #include <linux/sunrpc/svc.h>
  32. #include <linux/sunrpc/svcsock.h>
  33. #include <net/ip.h>
  34. #include <linux/lockd/lockd.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_GPL(nlmsvc_ops);
  42. static DEFINE_MUTEX(nlmsvc_mutex);
  43. static unsigned int nlmsvc_users;
  44. static struct task_struct *nlmsvc_task;
  45. static struct svc_rqst *nlmsvc_rqst;
  46. unsigned long nlmsvc_timeout;
  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. /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
  55. static unsigned int nlm_max_connections = 1024;
  56. /*
  57. * Constants needed for the sysctl interface.
  58. */
  59. static const unsigned long nlm_grace_period_min = 0;
  60. static const unsigned long nlm_grace_period_max = 240;
  61. static const unsigned long nlm_timeout_min = 3;
  62. static const unsigned long nlm_timeout_max = 20;
  63. static const int nlm_port_min = 0, nlm_port_max = 65535;
  64. #ifdef CONFIG_SYSCTL
  65. static struct ctl_table_header * nlm_sysctl_table;
  66. #endif
  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. static struct lock_manager lockd_manager = {
  76. };
  77. static void grace_ender(struct work_struct *not_used)
  78. {
  79. locks_end_grace(&lockd_manager);
  80. }
  81. static DECLARE_DELAYED_WORK(grace_period_end, grace_ender);
  82. static void set_grace_period(void)
  83. {
  84. unsigned long grace_period = get_lockd_grace_period();
  85. locks_start_grace(&lockd_manager);
  86. cancel_delayed_work_sync(&grace_period_end);
  87. schedule_delayed_work(&grace_period_end, grace_period);
  88. }
  89. static void restart_grace(void)
  90. {
  91. if (nlmsvc_ops) {
  92. cancel_delayed_work_sync(&grace_period_end);
  93. locks_end_grace(&lockd_manager);
  94. nlmsvc_invalidate_all();
  95. set_grace_period();
  96. }
  97. }
  98. /*
  99. * This is the lockd kernel thread
  100. */
  101. static int
  102. lockd(void *vrqstp)
  103. {
  104. int err = 0, preverr = 0;
  105. struct svc_rqst *rqstp = vrqstp;
  106. /* try_to_freeze() is called from svc_recv() */
  107. set_freezable();
  108. /* Allow SIGKILL to tell lockd to drop all of its locks */
  109. allow_signal(SIGKILL);
  110. dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
  111. /*
  112. * FIXME: it would be nice if lockd didn't spend its entire life
  113. * running under the BKL. At the very least, it would be good to
  114. * have someone clarify what it's intended to protect here. I've
  115. * seen some handwavy posts about posix locking needing to be
  116. * done under the BKL, but it's far from clear.
  117. */
  118. lock_kernel();
  119. if (!nlm_timeout)
  120. nlm_timeout = LOCKD_DFLT_TIMEO;
  121. nlmsvc_timeout = nlm_timeout * HZ;
  122. 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.
  126. */
  127. while (!kthread_should_stop()) {
  128. long timeout = MAX_SCHEDULE_TIMEOUT;
  129. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  130. /* update sv_maxconn if it has changed */
  131. rqstp->rq_server->sv_maxconn = nlm_max_connections;
  132. if (signalled()) {
  133. flush_signals(current);
  134. restart_grace();
  135. continue;
  136. }
  137. timeout = nlmsvc_retry_blocked();
  138. /*
  139. * Find a socket with data available and call its
  140. * recvfrom routine.
  141. */
  142. err = svc_recv(rqstp, timeout);
  143. if (err == -EAGAIN || err == -EINTR) {
  144. preverr = err;
  145. continue;
  146. }
  147. if (err < 0) {
  148. if (err != preverr) {
  149. printk(KERN_WARNING "%s: unexpected error "
  150. "from svc_recv (%d)\n", __func__, err);
  151. preverr = err;
  152. }
  153. schedule_timeout_interruptible(HZ);
  154. continue;
  155. }
  156. preverr = err;
  157. dprintk("lockd: request from %s\n",
  158. svc_print_addr(rqstp, buf, sizeof(buf)));
  159. svc_process(rqstp);
  160. }
  161. flush_signals(current);
  162. cancel_delayed_work_sync(&grace_period_end);
  163. locks_end_grace(&lockd_manager);
  164. if (nlmsvc_ops)
  165. nlmsvc_invalidate_all();
  166. nlm_shutdown_hosts();
  167. unlock_kernel();
  168. return 0;
  169. }
  170. static int create_lockd_listener(struct svc_serv *serv, const char *name,
  171. const int family, const unsigned short port)
  172. {
  173. struct svc_xprt *xprt;
  174. xprt = svc_find_xprt(serv, name, family, 0);
  175. if (xprt == NULL)
  176. return svc_create_xprt(serv, name, family, port,
  177. SVC_SOCK_DEFAULTS);
  178. svc_xprt_put(xprt);
  179. return 0;
  180. }
  181. static int create_lockd_family(struct svc_serv *serv, const int family)
  182. {
  183. int err;
  184. err = create_lockd_listener(serv, "udp", family, nlm_udpport);
  185. if (err < 0)
  186. return err;
  187. return create_lockd_listener(serv, "tcp", family, nlm_tcpport);
  188. }
  189. /*
  190. * Ensure there are active UDP and TCP listeners for lockd.
  191. *
  192. * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
  193. * local services (such as rpc.statd) still require UDP, and
  194. * some NFS servers do not yet support NLM over TCP.
  195. *
  196. * Returns zero if all listeners are available; otherwise a
  197. * negative errno value is returned.
  198. */
  199. static int make_socks(struct svc_serv *serv)
  200. {
  201. static int warned;
  202. int err;
  203. err = create_lockd_family(serv, PF_INET);
  204. if (err < 0)
  205. goto out_err;
  206. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  207. err = create_lockd_family(serv, PF_INET6);
  208. if (err < 0 && err != -EAFNOSUPPORT)
  209. goto out_err;
  210. #endif /* CONFIG_IPV6 || CONFIG_IPV6_MODULE */
  211. warned = 0;
  212. return 0;
  213. out_err:
  214. if (warned++ == 0)
  215. printk(KERN_WARNING
  216. "lockd_up: makesock failed, error=%d\n", err);
  217. return err;
  218. }
  219. /*
  220. * Bring up the lockd process if it's not already up.
  221. */
  222. int lockd_up(void)
  223. {
  224. struct svc_serv *serv;
  225. int error = 0;
  226. mutex_lock(&nlmsvc_mutex);
  227. /*
  228. * Check whether we're already up and running.
  229. */
  230. if (nlmsvc_rqst)
  231. goto out;
  232. /*
  233. * Sanity check: if there's no pid,
  234. * we should be the first user ...
  235. */
  236. if (nlmsvc_users)
  237. printk(KERN_WARNING
  238. "lockd_up: no pid, %d users??\n", nlmsvc_users);
  239. error = -ENOMEM;
  240. serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, NULL);
  241. if (!serv) {
  242. printk(KERN_WARNING "lockd_up: create service failed\n");
  243. goto out;
  244. }
  245. error = make_socks(serv);
  246. if (error < 0)
  247. goto destroy_and_out;
  248. /*
  249. * Create the kernel thread and wait for it to start.
  250. */
  251. nlmsvc_rqst = svc_prepare_thread(serv, &serv->sv_pools[0]);
  252. if (IS_ERR(nlmsvc_rqst)) {
  253. error = PTR_ERR(nlmsvc_rqst);
  254. nlmsvc_rqst = NULL;
  255. printk(KERN_WARNING
  256. "lockd_up: svc_rqst allocation failed, error=%d\n",
  257. error);
  258. goto destroy_and_out;
  259. }
  260. svc_sock_update_bufs(serv);
  261. serv->sv_maxconn = nlm_max_connections;
  262. nlmsvc_task = kthread_run(lockd, nlmsvc_rqst, serv->sv_name);
  263. if (IS_ERR(nlmsvc_task)) {
  264. error = PTR_ERR(nlmsvc_task);
  265. svc_exit_thread(nlmsvc_rqst);
  266. nlmsvc_task = NULL;
  267. nlmsvc_rqst = NULL;
  268. printk(KERN_WARNING
  269. "lockd_up: kthread_run failed, error=%d\n", error);
  270. goto destroy_and_out;
  271. }
  272. /*
  273. * Note: svc_serv structures have an initial use count of 1,
  274. * so we exit through here on both success and failure.
  275. */
  276. destroy_and_out:
  277. svc_destroy(serv);
  278. out:
  279. if (!error)
  280. nlmsvc_users++;
  281. mutex_unlock(&nlmsvc_mutex);
  282. return error;
  283. }
  284. EXPORT_SYMBOL_GPL(lockd_up);
  285. /*
  286. * Decrement the user count and bring down lockd if we're the last.
  287. */
  288. void
  289. lockd_down(void)
  290. {
  291. mutex_lock(&nlmsvc_mutex);
  292. if (nlmsvc_users) {
  293. if (--nlmsvc_users)
  294. goto out;
  295. } else {
  296. printk(KERN_ERR "lockd_down: no users! task=%p\n",
  297. nlmsvc_task);
  298. BUG();
  299. }
  300. if (!nlmsvc_task) {
  301. printk(KERN_ERR "lockd_down: no lockd running.\n");
  302. BUG();
  303. }
  304. kthread_stop(nlmsvc_task);
  305. svc_exit_thread(nlmsvc_rqst);
  306. nlmsvc_task = NULL;
  307. nlmsvc_rqst = NULL;
  308. out:
  309. mutex_unlock(&nlmsvc_mutex);
  310. }
  311. EXPORT_SYMBOL_GPL(lockd_down);
  312. #ifdef CONFIG_SYSCTL
  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. #endif /* CONFIG_SYSCTL */
  394. /*
  395. * Module (and sysfs) parameters.
  396. */
  397. #define param_set_min_max(name, type, which_strtol, min, max) \
  398. static int param_set_##name(const char *val, struct kernel_param *kp) \
  399. { \
  400. char *endp; \
  401. __typeof__(type) num = which_strtol(val, &endp, 0); \
  402. if (endp == val || *endp || num < (min) || num > (max)) \
  403. return -EINVAL; \
  404. *((int *) kp->arg) = num; \
  405. return 0; \
  406. }
  407. static inline int is_callback(u32 proc)
  408. {
  409. return proc == NLMPROC_GRANTED
  410. || proc == NLMPROC_GRANTED_MSG
  411. || proc == NLMPROC_TEST_RES
  412. || proc == NLMPROC_LOCK_RES
  413. || proc == NLMPROC_CANCEL_RES
  414. || proc == NLMPROC_UNLOCK_RES
  415. || proc == NLMPROC_NSM_NOTIFY;
  416. }
  417. static int lockd_authenticate(struct svc_rqst *rqstp)
  418. {
  419. rqstp->rq_client = NULL;
  420. switch (rqstp->rq_authop->flavour) {
  421. case RPC_AUTH_NULL:
  422. case RPC_AUTH_UNIX:
  423. if (rqstp->rq_proc == 0)
  424. return SVC_OK;
  425. if (is_callback(rqstp->rq_proc)) {
  426. /* Leave it to individual procedures to
  427. * call nlmsvc_lookup_host(rqstp)
  428. */
  429. return SVC_OK;
  430. }
  431. return svc_set_client(rqstp);
  432. }
  433. return SVC_DENIED;
  434. }
  435. param_set_min_max(port, int, simple_strtol, 0, 65535)
  436. param_set_min_max(grace_period, unsigned long, simple_strtoul,
  437. nlm_grace_period_min, nlm_grace_period_max)
  438. param_set_min_max(timeout, unsigned long, simple_strtoul,
  439. nlm_timeout_min, nlm_timeout_max)
  440. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  441. MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
  442. MODULE_LICENSE("GPL");
  443. module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
  444. &nlm_grace_period, 0644);
  445. module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
  446. &nlm_timeout, 0644);
  447. module_param_call(nlm_udpport, param_set_port, param_get_int,
  448. &nlm_udpport, 0644);
  449. module_param_call(nlm_tcpport, param_set_port, param_get_int,
  450. &nlm_tcpport, 0644);
  451. module_param(nsm_use_hostnames, bool, 0644);
  452. module_param(nlm_max_connections, uint, 0644);
  453. /*
  454. * Initialising and terminating the module.
  455. */
  456. static int __init init_nlm(void)
  457. {
  458. #ifdef CONFIG_SYSCTL
  459. nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
  460. return nlm_sysctl_table ? 0 : -ENOMEM;
  461. #else
  462. return 0;
  463. #endif
  464. }
  465. static void __exit exit_nlm(void)
  466. {
  467. /* FIXME: delete all NLM clients */
  468. nlm_shutdown_hosts();
  469. #ifdef CONFIG_SYSCTL
  470. unregister_sysctl_table(nlm_sysctl_table);
  471. #endif
  472. }
  473. module_init(init_nlm);
  474. module_exit(exit_nlm);
  475. /*
  476. * Define NLM program and procedures
  477. */
  478. static struct svc_version nlmsvc_version1 = {
  479. .vs_vers = 1,
  480. .vs_nproc = 17,
  481. .vs_proc = nlmsvc_procedures,
  482. .vs_xdrsize = NLMSVC_XDRSIZE,
  483. };
  484. static struct svc_version nlmsvc_version3 = {
  485. .vs_vers = 3,
  486. .vs_nproc = 24,
  487. .vs_proc = nlmsvc_procedures,
  488. .vs_xdrsize = NLMSVC_XDRSIZE,
  489. };
  490. #ifdef CONFIG_LOCKD_V4
  491. static struct svc_version nlmsvc_version4 = {
  492. .vs_vers = 4,
  493. .vs_nproc = 24,
  494. .vs_proc = nlmsvc_procedures4,
  495. .vs_xdrsize = NLMSVC_XDRSIZE,
  496. };
  497. #endif
  498. static struct svc_version * nlmsvc_version[] = {
  499. [1] = &nlmsvc_version1,
  500. [3] = &nlmsvc_version3,
  501. #ifdef CONFIG_LOCKD_V4
  502. [4] = &nlmsvc_version4,
  503. #endif
  504. };
  505. static struct svc_stat nlmsvc_stats;
  506. #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
  507. static struct svc_program nlmsvc_program = {
  508. .pg_prog = NLM_PROGRAM, /* program number */
  509. .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
  510. .pg_vers = nlmsvc_version, /* version table */
  511. .pg_name = "lockd", /* service name */
  512. .pg_class = "nfsd", /* share authentication with nfsd */
  513. .pg_stats = &nlmsvc_stats, /* stats table */
  514. .pg_authenticate = &lockd_authenticate /* export authentication */
  515. };