svc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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/signal.h>
  19. #include <linux/errno.h>
  20. #include <linux/in.h>
  21. #include <linux/uio.h>
  22. #include <linux/smp.h>
  23. #include <linux/mutex.h>
  24. #include <linux/kthread.h>
  25. #include <linux/freezer.h>
  26. #include <linux/inetdevice.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 <linux/sunrpc/svc_xprt.h>
  33. #include <net/ip.h>
  34. #include <net/addrconf.h>
  35. #include <net/ipv6.h>
  36. #include <linux/lockd/lockd.h>
  37. #include <linux/nfs.h>
  38. #include "netns.h"
  39. #include "procfs.h"
  40. #define NLMDBG_FACILITY NLMDBG_SVC
  41. #define LOCKD_BUFSIZE (1024 + NLMSVC_XDRSIZE)
  42. #define ALLOWED_SIGS (sigmask(SIGKILL))
  43. static struct svc_program nlmsvc_program;
  44. const struct nlmsvc_binding *nlmsvc_ops;
  45. EXPORT_SYMBOL_GPL(nlmsvc_ops);
  46. static DEFINE_MUTEX(nlmsvc_mutex);
  47. static unsigned int nlmsvc_users;
  48. static struct task_struct *nlmsvc_task;
  49. static struct svc_rqst *nlmsvc_rqst;
  50. unsigned long nlmsvc_timeout;
  51. static atomic_t nlm_ntf_refcnt = ATOMIC_INIT(0);
  52. static DECLARE_WAIT_QUEUE_HEAD(nlm_ntf_wq);
  53. unsigned int lockd_net_id;
  54. /*
  55. * These can be set at insmod time (useful for NFS as root filesystem),
  56. * and also changed through the sysctl interface. -- Jamie Lokier, Aug 2003
  57. */
  58. static unsigned long nlm_grace_period;
  59. static unsigned long nlm_timeout = LOCKD_DFLT_TIMEO;
  60. static int nlm_udpport, nlm_tcpport;
  61. /* RLIM_NOFILE defaults to 1024. That seems like a reasonable default here. */
  62. static unsigned int nlm_max_connections = 1024;
  63. /*
  64. * Constants needed for the sysctl interface.
  65. */
  66. static const unsigned long nlm_grace_period_min = 0;
  67. static const unsigned long nlm_grace_period_max = 240;
  68. static const unsigned long nlm_timeout_min = 3;
  69. static const unsigned long nlm_timeout_max = 20;
  70. static const int nlm_port_min = 0, nlm_port_max = 65535;
  71. #ifdef CONFIG_SYSCTL
  72. static struct ctl_table_header * nlm_sysctl_table;
  73. #endif
  74. static unsigned long get_lockd_grace_period(void)
  75. {
  76. /* Note: nlm_timeout should always be nonzero */
  77. if (nlm_grace_period)
  78. return roundup(nlm_grace_period, nlm_timeout) * HZ;
  79. else
  80. return nlm_timeout * 5 * HZ;
  81. }
  82. static void grace_ender(struct work_struct *grace)
  83. {
  84. struct delayed_work *dwork = to_delayed_work(grace);
  85. struct lockd_net *ln = container_of(dwork, struct lockd_net,
  86. grace_period_end);
  87. locks_end_grace(&ln->lockd_manager);
  88. }
  89. static void set_grace_period(struct net *net)
  90. {
  91. unsigned long grace_period = get_lockd_grace_period();
  92. struct lockd_net *ln = net_generic(net, lockd_net_id);
  93. locks_start_grace(net, &ln->lockd_manager);
  94. cancel_delayed_work_sync(&ln->grace_period_end);
  95. schedule_delayed_work(&ln->grace_period_end, grace_period);
  96. }
  97. static void restart_grace(void)
  98. {
  99. if (nlmsvc_ops) {
  100. struct net *net = &init_net;
  101. struct lockd_net *ln = net_generic(net, lockd_net_id);
  102. cancel_delayed_work_sync(&ln->grace_period_end);
  103. locks_end_grace(&ln->lockd_manager);
  104. nlmsvc_invalidate_all();
  105. set_grace_period(net);
  106. }
  107. }
  108. /*
  109. * This is the lockd kernel thread
  110. */
  111. static int
  112. lockd(void *vrqstp)
  113. {
  114. int err = 0;
  115. struct svc_rqst *rqstp = vrqstp;
  116. struct net *net = &init_net;
  117. struct lockd_net *ln = net_generic(net, lockd_net_id);
  118. /* try_to_freeze() is called from svc_recv() */
  119. set_freezable();
  120. /* Allow SIGKILL to tell lockd to drop all of its locks */
  121. allow_signal(SIGKILL);
  122. dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
  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. continue;
  145. dprintk("lockd: request from %s\n",
  146. svc_print_addr(rqstp, buf, sizeof(buf)));
  147. svc_process(rqstp);
  148. }
  149. flush_signals(current);
  150. if (nlmsvc_ops)
  151. nlmsvc_invalidate_all();
  152. nlm_shutdown_hosts();
  153. cancel_delayed_work_sync(&ln->grace_period_end);
  154. locks_end_grace(&ln->lockd_manager);
  155. return 0;
  156. }
  157. static int create_lockd_listener(struct svc_serv *serv, const char *name,
  158. struct net *net, const int family,
  159. const unsigned short port)
  160. {
  161. struct svc_xprt *xprt;
  162. xprt = svc_find_xprt(serv, name, net, family, 0);
  163. if (xprt == NULL)
  164. return svc_create_xprt(serv, name, net, family, port,
  165. SVC_SOCK_DEFAULTS);
  166. svc_xprt_put(xprt);
  167. return 0;
  168. }
  169. static int create_lockd_family(struct svc_serv *serv, struct net *net,
  170. const int family)
  171. {
  172. int err;
  173. err = create_lockd_listener(serv, "udp", net, family, nlm_udpport);
  174. if (err < 0)
  175. return err;
  176. return create_lockd_listener(serv, "tcp", net, family, nlm_tcpport);
  177. }
  178. /*
  179. * Ensure there are active UDP and TCP listeners for lockd.
  180. *
  181. * Even if we have only TCP NFS mounts and/or TCP NFSDs, some
  182. * local services (such as rpc.statd) still require UDP, and
  183. * some NFS servers do not yet support NLM over TCP.
  184. *
  185. * Returns zero if all listeners are available; otherwise a
  186. * negative errno value is returned.
  187. */
  188. static int make_socks(struct svc_serv *serv, struct net *net)
  189. {
  190. static int warned;
  191. int err;
  192. err = create_lockd_family(serv, net, PF_INET);
  193. if (err < 0)
  194. goto out_err;
  195. err = create_lockd_family(serv, net, PF_INET6);
  196. if (err < 0 && err != -EAFNOSUPPORT)
  197. goto out_err;
  198. warned = 0;
  199. return 0;
  200. out_err:
  201. if (warned++ == 0)
  202. printk(KERN_WARNING
  203. "lockd_up: makesock failed, error=%d\n", err);
  204. svc_shutdown_net(serv, net);
  205. return err;
  206. }
  207. static int lockd_up_net(struct svc_serv *serv, struct net *net)
  208. {
  209. struct lockd_net *ln = net_generic(net, lockd_net_id);
  210. int error;
  211. if (ln->nlmsvc_users++)
  212. return 0;
  213. error = svc_bind(serv, net);
  214. if (error)
  215. goto err_bind;
  216. error = make_socks(serv, net);
  217. if (error < 0)
  218. goto err_bind;
  219. set_grace_period(net);
  220. dprintk("%s: per-net data created; net=%x\n", __func__, net->ns.inum);
  221. return 0;
  222. err_bind:
  223. ln->nlmsvc_users--;
  224. return error;
  225. }
  226. static void lockd_down_net(struct svc_serv *serv, struct net *net)
  227. {
  228. struct lockd_net *ln = net_generic(net, lockd_net_id);
  229. if (ln->nlmsvc_users) {
  230. if (--ln->nlmsvc_users == 0) {
  231. nlm_shutdown_hosts_net(net);
  232. cancel_delayed_work_sync(&ln->grace_period_end);
  233. locks_end_grace(&ln->lockd_manager);
  234. svc_shutdown_net(serv, net);
  235. dprintk("%s: per-net data destroyed; net=%x\n",
  236. __func__, net->ns.inum);
  237. }
  238. } else {
  239. pr_err("%s: no users! task=%p, net=%x\n",
  240. __func__, nlmsvc_task, net->ns.inum);
  241. BUG();
  242. }
  243. }
  244. static int lockd_inetaddr_event(struct notifier_block *this,
  245. unsigned long event, void *ptr)
  246. {
  247. struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
  248. struct sockaddr_in sin;
  249. if ((event != NETDEV_DOWN) ||
  250. !atomic_inc_not_zero(&nlm_ntf_refcnt))
  251. goto out;
  252. if (nlmsvc_rqst) {
  253. dprintk("lockd_inetaddr_event: removed %pI4\n",
  254. &ifa->ifa_local);
  255. sin.sin_family = AF_INET;
  256. sin.sin_addr.s_addr = ifa->ifa_local;
  257. svc_age_temp_xprts_now(nlmsvc_rqst->rq_server,
  258. (struct sockaddr *)&sin);
  259. }
  260. atomic_dec(&nlm_ntf_refcnt);
  261. wake_up(&nlm_ntf_wq);
  262. out:
  263. return NOTIFY_DONE;
  264. }
  265. static struct notifier_block lockd_inetaddr_notifier = {
  266. .notifier_call = lockd_inetaddr_event,
  267. };
  268. #if IS_ENABLED(CONFIG_IPV6)
  269. static int lockd_inet6addr_event(struct notifier_block *this,
  270. unsigned long event, void *ptr)
  271. {
  272. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  273. struct sockaddr_in6 sin6;
  274. if ((event != NETDEV_DOWN) ||
  275. !atomic_inc_not_zero(&nlm_ntf_refcnt))
  276. goto out;
  277. if (nlmsvc_rqst) {
  278. dprintk("lockd_inet6addr_event: removed %pI6\n", &ifa->addr);
  279. sin6.sin6_family = AF_INET6;
  280. sin6.sin6_addr = ifa->addr;
  281. if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  282. sin6.sin6_scope_id = ifa->idev->dev->ifindex;
  283. svc_age_temp_xprts_now(nlmsvc_rqst->rq_server,
  284. (struct sockaddr *)&sin6);
  285. }
  286. atomic_dec(&nlm_ntf_refcnt);
  287. wake_up(&nlm_ntf_wq);
  288. out:
  289. return NOTIFY_DONE;
  290. }
  291. static struct notifier_block lockd_inet6addr_notifier = {
  292. .notifier_call = lockd_inet6addr_event,
  293. };
  294. #endif
  295. static void lockd_unregister_notifiers(void)
  296. {
  297. unregister_inetaddr_notifier(&lockd_inetaddr_notifier);
  298. #if IS_ENABLED(CONFIG_IPV6)
  299. unregister_inet6addr_notifier(&lockd_inet6addr_notifier);
  300. #endif
  301. wait_event(nlm_ntf_wq, atomic_read(&nlm_ntf_refcnt) == 0);
  302. }
  303. static void lockd_svc_exit_thread(void)
  304. {
  305. atomic_dec(&nlm_ntf_refcnt);
  306. lockd_unregister_notifiers();
  307. svc_exit_thread(nlmsvc_rqst);
  308. }
  309. static int lockd_start_svc(struct svc_serv *serv)
  310. {
  311. int error;
  312. if (nlmsvc_rqst)
  313. return 0;
  314. /*
  315. * Create the kernel thread and wait for it to start.
  316. */
  317. nlmsvc_rqst = svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
  318. if (IS_ERR(nlmsvc_rqst)) {
  319. error = PTR_ERR(nlmsvc_rqst);
  320. printk(KERN_WARNING
  321. "lockd_up: svc_rqst allocation failed, error=%d\n",
  322. error);
  323. lockd_unregister_notifiers();
  324. goto out_rqst;
  325. }
  326. atomic_inc(&nlm_ntf_refcnt);
  327. svc_sock_update_bufs(serv);
  328. serv->sv_maxconn = nlm_max_connections;
  329. nlmsvc_task = kthread_create(lockd, nlmsvc_rqst, "%s", serv->sv_name);
  330. if (IS_ERR(nlmsvc_task)) {
  331. error = PTR_ERR(nlmsvc_task);
  332. printk(KERN_WARNING
  333. "lockd_up: kthread_run failed, error=%d\n", error);
  334. goto out_task;
  335. }
  336. nlmsvc_rqst->rq_task = nlmsvc_task;
  337. wake_up_process(nlmsvc_task);
  338. dprintk("lockd_up: service started\n");
  339. return 0;
  340. out_task:
  341. lockd_svc_exit_thread();
  342. nlmsvc_task = NULL;
  343. out_rqst:
  344. nlmsvc_rqst = NULL;
  345. return error;
  346. }
  347. static const struct svc_serv_ops lockd_sv_ops = {
  348. .svo_shutdown = svc_rpcb_cleanup,
  349. .svo_enqueue_xprt = svc_xprt_do_enqueue,
  350. };
  351. static struct svc_serv *lockd_create_svc(void)
  352. {
  353. struct svc_serv *serv;
  354. /*
  355. * Check whether we're already up and running.
  356. */
  357. if (nlmsvc_rqst) {
  358. /*
  359. * Note: increase service usage, because later in case of error
  360. * svc_destroy() will be called.
  361. */
  362. svc_get(nlmsvc_rqst->rq_server);
  363. return nlmsvc_rqst->rq_server;
  364. }
  365. /*
  366. * Sanity check: if there's no pid,
  367. * we should be the first user ...
  368. */
  369. if (nlmsvc_users)
  370. printk(KERN_WARNING
  371. "lockd_up: no pid, %d users??\n", nlmsvc_users);
  372. if (!nlm_timeout)
  373. nlm_timeout = LOCKD_DFLT_TIMEO;
  374. nlmsvc_timeout = nlm_timeout * HZ;
  375. serv = svc_create(&nlmsvc_program, LOCKD_BUFSIZE, &lockd_sv_ops);
  376. if (!serv) {
  377. printk(KERN_WARNING "lockd_up: create service failed\n");
  378. return ERR_PTR(-ENOMEM);
  379. }
  380. register_inetaddr_notifier(&lockd_inetaddr_notifier);
  381. #if IS_ENABLED(CONFIG_IPV6)
  382. register_inet6addr_notifier(&lockd_inet6addr_notifier);
  383. #endif
  384. dprintk("lockd_up: service created\n");
  385. return serv;
  386. }
  387. /*
  388. * Bring up the lockd process if it's not already up.
  389. */
  390. int lockd_up(struct net *net)
  391. {
  392. struct svc_serv *serv;
  393. int error;
  394. mutex_lock(&nlmsvc_mutex);
  395. serv = lockd_create_svc();
  396. if (IS_ERR(serv)) {
  397. error = PTR_ERR(serv);
  398. goto err_create;
  399. }
  400. error = lockd_up_net(serv, net);
  401. if (error < 0) {
  402. lockd_unregister_notifiers();
  403. goto err_put;
  404. }
  405. error = lockd_start_svc(serv);
  406. if (error < 0) {
  407. lockd_down_net(serv, net);
  408. goto err_put;
  409. }
  410. nlmsvc_users++;
  411. /*
  412. * Note: svc_serv structures have an initial use count of 1,
  413. * so we exit through here on both success and failure.
  414. */
  415. err_put:
  416. svc_destroy(serv);
  417. err_create:
  418. mutex_unlock(&nlmsvc_mutex);
  419. return error;
  420. }
  421. EXPORT_SYMBOL_GPL(lockd_up);
  422. /*
  423. * Decrement the user count and bring down lockd if we're the last.
  424. */
  425. void
  426. lockd_down(struct net *net)
  427. {
  428. mutex_lock(&nlmsvc_mutex);
  429. lockd_down_net(nlmsvc_rqst->rq_server, net);
  430. if (nlmsvc_users) {
  431. if (--nlmsvc_users)
  432. goto out;
  433. } else {
  434. printk(KERN_ERR "lockd_down: no users! task=%p\n",
  435. nlmsvc_task);
  436. BUG();
  437. }
  438. if (!nlmsvc_task) {
  439. printk(KERN_ERR "lockd_down: no lockd running.\n");
  440. BUG();
  441. }
  442. kthread_stop(nlmsvc_task);
  443. dprintk("lockd_down: service stopped\n");
  444. lockd_svc_exit_thread();
  445. dprintk("lockd_down: service destroyed\n");
  446. nlmsvc_task = NULL;
  447. nlmsvc_rqst = NULL;
  448. out:
  449. mutex_unlock(&nlmsvc_mutex);
  450. }
  451. EXPORT_SYMBOL_GPL(lockd_down);
  452. #ifdef CONFIG_SYSCTL
  453. /*
  454. * Sysctl parameters (same as module parameters, different interface).
  455. */
  456. static struct ctl_table nlm_sysctls[] = {
  457. {
  458. .procname = "nlm_grace_period",
  459. .data = &nlm_grace_period,
  460. .maxlen = sizeof(unsigned long),
  461. .mode = 0644,
  462. .proc_handler = proc_doulongvec_minmax,
  463. .extra1 = (unsigned long *) &nlm_grace_period_min,
  464. .extra2 = (unsigned long *) &nlm_grace_period_max,
  465. },
  466. {
  467. .procname = "nlm_timeout",
  468. .data = &nlm_timeout,
  469. .maxlen = sizeof(unsigned long),
  470. .mode = 0644,
  471. .proc_handler = proc_doulongvec_minmax,
  472. .extra1 = (unsigned long *) &nlm_timeout_min,
  473. .extra2 = (unsigned long *) &nlm_timeout_max,
  474. },
  475. {
  476. .procname = "nlm_udpport",
  477. .data = &nlm_udpport,
  478. .maxlen = sizeof(int),
  479. .mode = 0644,
  480. .proc_handler = proc_dointvec_minmax,
  481. .extra1 = (int *) &nlm_port_min,
  482. .extra2 = (int *) &nlm_port_max,
  483. },
  484. {
  485. .procname = "nlm_tcpport",
  486. .data = &nlm_tcpport,
  487. .maxlen = sizeof(int),
  488. .mode = 0644,
  489. .proc_handler = proc_dointvec_minmax,
  490. .extra1 = (int *) &nlm_port_min,
  491. .extra2 = (int *) &nlm_port_max,
  492. },
  493. {
  494. .procname = "nsm_use_hostnames",
  495. .data = &nsm_use_hostnames,
  496. .maxlen = sizeof(int),
  497. .mode = 0644,
  498. .proc_handler = proc_dointvec,
  499. },
  500. {
  501. .procname = "nsm_local_state",
  502. .data = &nsm_local_state,
  503. .maxlen = sizeof(int),
  504. .mode = 0644,
  505. .proc_handler = proc_dointvec,
  506. },
  507. { }
  508. };
  509. static struct ctl_table nlm_sysctl_dir[] = {
  510. {
  511. .procname = "nfs",
  512. .mode = 0555,
  513. .child = nlm_sysctls,
  514. },
  515. { }
  516. };
  517. static struct ctl_table nlm_sysctl_root[] = {
  518. {
  519. .procname = "fs",
  520. .mode = 0555,
  521. .child = nlm_sysctl_dir,
  522. },
  523. { }
  524. };
  525. #endif /* CONFIG_SYSCTL */
  526. /*
  527. * Module (and sysfs) parameters.
  528. */
  529. #define param_set_min_max(name, type, which_strtol, min, max) \
  530. static int param_set_##name(const char *val, const struct kernel_param *kp) \
  531. { \
  532. char *endp; \
  533. __typeof__(type) num = which_strtol(val, &endp, 0); \
  534. if (endp == val || *endp || num < (min) || num > (max)) \
  535. return -EINVAL; \
  536. *((type *) kp->arg) = num; \
  537. return 0; \
  538. }
  539. static inline int is_callback(u32 proc)
  540. {
  541. return proc == NLMPROC_GRANTED
  542. || proc == NLMPROC_GRANTED_MSG
  543. || proc == NLMPROC_TEST_RES
  544. || proc == NLMPROC_LOCK_RES
  545. || proc == NLMPROC_CANCEL_RES
  546. || proc == NLMPROC_UNLOCK_RES
  547. || proc == NLMPROC_NSM_NOTIFY;
  548. }
  549. static int lockd_authenticate(struct svc_rqst *rqstp)
  550. {
  551. rqstp->rq_client = NULL;
  552. switch (rqstp->rq_authop->flavour) {
  553. case RPC_AUTH_NULL:
  554. case RPC_AUTH_UNIX:
  555. if (rqstp->rq_proc == 0)
  556. return SVC_OK;
  557. if (is_callback(rqstp->rq_proc)) {
  558. /* Leave it to individual procedures to
  559. * call nlmsvc_lookup_host(rqstp)
  560. */
  561. return SVC_OK;
  562. }
  563. return svc_set_client(rqstp);
  564. }
  565. return SVC_DENIED;
  566. }
  567. param_set_min_max(port, int, simple_strtol, 0, 65535)
  568. param_set_min_max(grace_period, unsigned long, simple_strtoul,
  569. nlm_grace_period_min, nlm_grace_period_max)
  570. param_set_min_max(timeout, unsigned long, simple_strtoul,
  571. nlm_timeout_min, nlm_timeout_max)
  572. MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
  573. MODULE_DESCRIPTION("NFS file locking service version " LOCKD_VERSION ".");
  574. MODULE_LICENSE("GPL");
  575. module_param_call(nlm_grace_period, param_set_grace_period, param_get_ulong,
  576. &nlm_grace_period, 0644);
  577. module_param_call(nlm_timeout, param_set_timeout, param_get_ulong,
  578. &nlm_timeout, 0644);
  579. module_param_call(nlm_udpport, param_set_port, param_get_int,
  580. &nlm_udpport, 0644);
  581. module_param_call(nlm_tcpport, param_set_port, param_get_int,
  582. &nlm_tcpport, 0644);
  583. module_param(nsm_use_hostnames, bool, 0644);
  584. module_param(nlm_max_connections, uint, 0644);
  585. static int lockd_init_net(struct net *net)
  586. {
  587. struct lockd_net *ln = net_generic(net, lockd_net_id);
  588. INIT_DELAYED_WORK(&ln->grace_period_end, grace_ender);
  589. INIT_LIST_HEAD(&ln->lockd_manager.list);
  590. ln->lockd_manager.block_opens = false;
  591. INIT_LIST_HEAD(&ln->nsm_handles);
  592. return 0;
  593. }
  594. static void lockd_exit_net(struct net *net)
  595. {
  596. struct lockd_net *ln = net_generic(net, lockd_net_id);
  597. WARN_ONCE(!list_empty(&ln->lockd_manager.list),
  598. "net %x %s: lockd_manager.list is not empty\n",
  599. net->ns.inum, __func__);
  600. WARN_ONCE(!list_empty(&ln->nsm_handles),
  601. "net %x %s: nsm_handles list is not empty\n",
  602. net->ns.inum, __func__);
  603. WARN_ONCE(delayed_work_pending(&ln->grace_period_end),
  604. "net %x %s: grace_period_end was not cancelled\n",
  605. net->ns.inum, __func__);
  606. }
  607. static struct pernet_operations lockd_net_ops = {
  608. .init = lockd_init_net,
  609. .exit = lockd_exit_net,
  610. .id = &lockd_net_id,
  611. .size = sizeof(struct lockd_net),
  612. };
  613. /*
  614. * Initialising and terminating the module.
  615. */
  616. static int __init init_nlm(void)
  617. {
  618. int err;
  619. #ifdef CONFIG_SYSCTL
  620. err = -ENOMEM;
  621. nlm_sysctl_table = register_sysctl_table(nlm_sysctl_root);
  622. if (nlm_sysctl_table == NULL)
  623. goto err_sysctl;
  624. #endif
  625. err = register_pernet_subsys(&lockd_net_ops);
  626. if (err)
  627. goto err_pernet;
  628. err = lockd_create_procfs();
  629. if (err)
  630. goto err_procfs;
  631. return 0;
  632. err_procfs:
  633. unregister_pernet_subsys(&lockd_net_ops);
  634. err_pernet:
  635. #ifdef CONFIG_SYSCTL
  636. unregister_sysctl_table(nlm_sysctl_table);
  637. err_sysctl:
  638. #endif
  639. return err;
  640. }
  641. static void __exit exit_nlm(void)
  642. {
  643. /* FIXME: delete all NLM clients */
  644. nlm_shutdown_hosts();
  645. lockd_remove_procfs();
  646. unregister_pernet_subsys(&lockd_net_ops);
  647. #ifdef CONFIG_SYSCTL
  648. unregister_sysctl_table(nlm_sysctl_table);
  649. #endif
  650. }
  651. module_init(init_nlm);
  652. module_exit(exit_nlm);
  653. /*
  654. * Define NLM program and procedures
  655. */
  656. static unsigned int nlmsvc_version1_count[17];
  657. static const struct svc_version nlmsvc_version1 = {
  658. .vs_vers = 1,
  659. .vs_nproc = 17,
  660. .vs_proc = nlmsvc_procedures,
  661. .vs_count = nlmsvc_version1_count,
  662. .vs_xdrsize = NLMSVC_XDRSIZE,
  663. };
  664. static unsigned int nlmsvc_version3_count[24];
  665. static const struct svc_version nlmsvc_version3 = {
  666. .vs_vers = 3,
  667. .vs_nproc = 24,
  668. .vs_proc = nlmsvc_procedures,
  669. .vs_count = nlmsvc_version3_count,
  670. .vs_xdrsize = NLMSVC_XDRSIZE,
  671. };
  672. #ifdef CONFIG_LOCKD_V4
  673. static unsigned int nlmsvc_version4_count[24];
  674. static const struct svc_version nlmsvc_version4 = {
  675. .vs_vers = 4,
  676. .vs_nproc = 24,
  677. .vs_proc = nlmsvc_procedures4,
  678. .vs_count = nlmsvc_version4_count,
  679. .vs_xdrsize = NLMSVC_XDRSIZE,
  680. };
  681. #endif
  682. static const struct svc_version *nlmsvc_version[] = {
  683. [1] = &nlmsvc_version1,
  684. [3] = &nlmsvc_version3,
  685. #ifdef CONFIG_LOCKD_V4
  686. [4] = &nlmsvc_version4,
  687. #endif
  688. };
  689. static struct svc_stat nlmsvc_stats;
  690. #define NLM_NRVERS ARRAY_SIZE(nlmsvc_version)
  691. static struct svc_program nlmsvc_program = {
  692. .pg_prog = NLM_PROGRAM, /* program number */
  693. .pg_nvers = NLM_NRVERS, /* number of entries in nlmsvc_version */
  694. .pg_vers = nlmsvc_version, /* version table */
  695. .pg_name = "lockd", /* service name */
  696. .pg_class = "nfsd", /* share authentication with nfsd */
  697. .pg_stats = &nlmsvc_stats, /* stats table */
  698. .pg_authenticate = &lockd_authenticate /* export authentication */
  699. };