PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src-rt/linux/linux-2.6/fs/lockd/svc.c

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