callback.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * linux/fs/nfs/callback.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback handling
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/ip.h>
  10. #include <linux/module.h>
  11. #include <linux/sunrpc/svc.h>
  12. #include <linux/sunrpc/svcsock.h>
  13. #include <linux/nfs_fs.h>
  14. #include <linux/mutex.h>
  15. #include <linux/freezer.h>
  16. #include <linux/kthread.h>
  17. #include <linux/sunrpc/svcauth_gss.h>
  18. #include <linux/sunrpc/bc_xprt.h>
  19. #include <net/inet_sock.h>
  20. #include "nfs4_fs.h"
  21. #include "callback.h"
  22. #include "internal.h"
  23. #include "netns.h"
  24. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  25. struct nfs_callback_data {
  26. unsigned int users;
  27. struct svc_serv *serv;
  28. struct svc_rqst *rqst;
  29. struct task_struct *task;
  30. };
  31. static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
  32. static DEFINE_MUTEX(nfs_callback_mutex);
  33. static struct svc_program nfs4_callback_program;
  34. static int nfs4_callback_up_net(struct svc_serv *serv, struct net *net)
  35. {
  36. int ret;
  37. struct nfs_net *nn = net_generic(net, nfs_net_id);
  38. ret = svc_create_xprt(serv, "tcp", net, PF_INET,
  39. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  40. if (ret <= 0)
  41. goto out_err;
  42. nn->nfs_callback_tcpport = ret;
  43. dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
  44. nn->nfs_callback_tcpport, PF_INET, net);
  45. ret = svc_create_xprt(serv, "tcp", net, PF_INET6,
  46. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  47. if (ret > 0) {
  48. nn->nfs_callback_tcpport6 = ret;
  49. dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
  50. nn->nfs_callback_tcpport6, PF_INET6, net);
  51. } else if (ret != -EAFNOSUPPORT)
  52. goto out_err;
  53. return 0;
  54. out_err:
  55. return (ret) ? ret : -ENOMEM;
  56. }
  57. /*
  58. * This is the NFSv4 callback kernel thread.
  59. */
  60. static int
  61. nfs4_callback_svc(void *vrqstp)
  62. {
  63. int err, preverr = 0;
  64. struct svc_rqst *rqstp = vrqstp;
  65. set_freezable();
  66. while (!kthread_should_stop()) {
  67. /*
  68. * Listen for a request on the socket
  69. */
  70. err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
  71. if (err == -EAGAIN || err == -EINTR) {
  72. preverr = err;
  73. continue;
  74. }
  75. if (err < 0) {
  76. if (err != preverr) {
  77. printk(KERN_WARNING "NFS: %s: unexpected error "
  78. "from svc_recv (%d)\n", __func__, err);
  79. preverr = err;
  80. }
  81. schedule_timeout_uninterruptible(HZ);
  82. continue;
  83. }
  84. preverr = err;
  85. svc_process(rqstp);
  86. }
  87. return 0;
  88. }
  89. /*
  90. * Prepare to bring up the NFSv4 callback service
  91. */
  92. static struct svc_rqst *
  93. nfs4_callback_up(struct svc_serv *serv)
  94. {
  95. return svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
  96. }
  97. #if defined(CONFIG_NFS_V4_1)
  98. static int nfs41_callback_up_net(struct svc_serv *serv, struct net *net)
  99. {
  100. /*
  101. * Create an svc_sock for the back channel service that shares the
  102. * fore channel connection.
  103. * Returns the input port (0) and sets the svc_serv bc_xprt on success
  104. */
  105. return svc_create_xprt(serv, "tcp-bc", net, PF_INET, 0,
  106. SVC_SOCK_ANONYMOUS);
  107. }
  108. /*
  109. * The callback service for NFSv4.1 callbacks
  110. */
  111. static int
  112. nfs41_callback_svc(void *vrqstp)
  113. {
  114. struct svc_rqst *rqstp = vrqstp;
  115. struct svc_serv *serv = rqstp->rq_server;
  116. struct rpc_rqst *req;
  117. int error;
  118. DEFINE_WAIT(wq);
  119. set_freezable();
  120. while (!kthread_should_stop()) {
  121. prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
  122. spin_lock_bh(&serv->sv_cb_lock);
  123. if (!list_empty(&serv->sv_cb_list)) {
  124. req = list_first_entry(&serv->sv_cb_list,
  125. struct rpc_rqst, rq_bc_list);
  126. list_del(&req->rq_bc_list);
  127. spin_unlock_bh(&serv->sv_cb_lock);
  128. dprintk("Invoking bc_svc_process()\n");
  129. error = bc_svc_process(serv, req, rqstp);
  130. dprintk("bc_svc_process() returned w/ error code= %d\n",
  131. error);
  132. } else {
  133. spin_unlock_bh(&serv->sv_cb_lock);
  134. schedule();
  135. }
  136. finish_wait(&serv->sv_cb_waitq, &wq);
  137. }
  138. return 0;
  139. }
  140. /*
  141. * Bring up the NFSv4.1 callback service
  142. */
  143. static struct svc_rqst *
  144. nfs41_callback_up(struct svc_serv *serv)
  145. {
  146. struct svc_rqst *rqstp;
  147. INIT_LIST_HEAD(&serv->sv_cb_list);
  148. spin_lock_init(&serv->sv_cb_lock);
  149. init_waitqueue_head(&serv->sv_cb_waitq);
  150. rqstp = svc_prepare_thread(serv, &serv->sv_pools[0], NUMA_NO_NODE);
  151. if (IS_ERR(rqstp)) {
  152. svc_xprt_put(serv->sv_bc_xprt);
  153. serv->sv_bc_xprt = NULL;
  154. }
  155. dprintk("--> %s return %ld\n", __func__,
  156. IS_ERR(rqstp) ? PTR_ERR(rqstp) : 0);
  157. return rqstp;
  158. }
  159. static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
  160. struct svc_serv *serv,
  161. struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
  162. {
  163. if (minorversion) {
  164. *rqstpp = nfs41_callback_up(serv);
  165. *callback_svc = nfs41_callback_svc;
  166. }
  167. return minorversion;
  168. }
  169. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  170. struct svc_serv *serv)
  171. {
  172. if (minorversion)
  173. /*
  174. * Save the svc_serv in the transport so that it can
  175. * be referenced when the session backchannel is initialized
  176. */
  177. xprt->bc_serv = serv;
  178. }
  179. #else
  180. static int nfs41_callback_up_net(struct svc_serv *serv, struct net *net)
  181. {
  182. return 0;
  183. }
  184. static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
  185. struct svc_serv *serv,
  186. struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
  187. {
  188. return 0;
  189. }
  190. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  191. struct svc_serv *serv)
  192. {
  193. }
  194. #endif /* CONFIG_NFS_V4_1 */
  195. static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
  196. struct svc_serv *serv)
  197. {
  198. struct svc_rqst *rqstp;
  199. int (*callback_svc)(void *vrqstp);
  200. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  201. char svc_name[12];
  202. int ret;
  203. int minorversion_setup;
  204. nfs_callback_bc_serv(minorversion, xprt, serv);
  205. if (cb_info->task)
  206. return 0;
  207. minorversion_setup = nfs_minorversion_callback_svc_setup(minorversion,
  208. serv, &rqstp, &callback_svc);
  209. if (!minorversion_setup) {
  210. /* v4.0 callback setup */
  211. rqstp = nfs4_callback_up(serv);
  212. callback_svc = nfs4_callback_svc;
  213. }
  214. if (IS_ERR(rqstp))
  215. return PTR_ERR(rqstp);
  216. svc_sock_update_bufs(serv);
  217. sprintf(svc_name, "nfsv4.%u-svc", minorversion);
  218. cb_info->serv = serv;
  219. cb_info->rqst = rqstp;
  220. cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name);
  221. if (IS_ERR(cb_info->task)) {
  222. ret = PTR_ERR(cb_info->task);
  223. svc_exit_thread(cb_info->rqst);
  224. cb_info->rqst = NULL;
  225. cb_info->task = NULL;
  226. return PTR_ERR(cb_info->task);
  227. }
  228. dprintk("nfs_callback_up: service started\n");
  229. return 0;
  230. }
  231. static void nfs_callback_down_net(u32 minorversion, struct svc_serv *serv, struct net *net)
  232. {
  233. struct nfs_net *nn = net_generic(net, nfs_net_id);
  234. if (--nn->cb_users[minorversion])
  235. return;
  236. dprintk("NFS: destroy per-net callback data; net=%p\n", net);
  237. svc_shutdown_net(serv, net);
  238. }
  239. static int nfs_callback_up_net(int minorversion, struct svc_serv *serv, struct net *net)
  240. {
  241. struct nfs_net *nn = net_generic(net, nfs_net_id);
  242. int ret;
  243. if (nn->cb_users[minorversion]++)
  244. return 0;
  245. dprintk("NFS: create per-net callback data; net=%p\n", net);
  246. ret = svc_bind(serv, net);
  247. if (ret < 0) {
  248. printk(KERN_WARNING "NFS: bind callback service failed\n");
  249. goto err_bind;
  250. }
  251. switch (minorversion) {
  252. case 0:
  253. ret = nfs4_callback_up_net(serv, net);
  254. break;
  255. case 1:
  256. ret = nfs41_callback_up_net(serv, net);
  257. break;
  258. default:
  259. printk(KERN_ERR "NFS: unknown callback version: %d\n",
  260. minorversion);
  261. ret = -EINVAL;
  262. break;
  263. }
  264. if (ret < 0) {
  265. printk(KERN_ERR "NFS: callback service start failed\n");
  266. goto err_socks;
  267. }
  268. return 0;
  269. err_socks:
  270. svc_rpcb_cleanup(serv, net);
  271. err_bind:
  272. dprintk("NFS: Couldn't create callback socket: err = %d; "
  273. "net = %p\n", ret, net);
  274. return ret;
  275. }
  276. static struct svc_serv *nfs_callback_create_svc(int minorversion)
  277. {
  278. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  279. struct svc_serv *serv;
  280. /*
  281. * Check whether we're already up and running.
  282. */
  283. if (cb_info->task) {
  284. /*
  285. * Note: increase service usage, because later in case of error
  286. * svc_destroy() will be called.
  287. */
  288. svc_get(cb_info->serv);
  289. return cb_info->serv;
  290. }
  291. /*
  292. * Sanity check: if there's no task,
  293. * we should be the first user ...
  294. */
  295. if (cb_info->users)
  296. printk(KERN_WARNING "nfs_callback_create_svc: no kthread, %d users??\n",
  297. cb_info->users);
  298. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
  299. if (!serv) {
  300. printk(KERN_ERR "nfs_callback_create_svc: create service failed\n");
  301. return ERR_PTR(-ENOMEM);
  302. }
  303. /* As there is only one thread we need to over-ride the
  304. * default maximum of 80 connections
  305. */
  306. serv->sv_maxconn = 1024;
  307. dprintk("nfs_callback_create_svc: service created\n");
  308. return serv;
  309. }
  310. /*
  311. * Bring up the callback thread if it is not already up.
  312. */
  313. int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
  314. {
  315. struct svc_serv *serv;
  316. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  317. int ret;
  318. struct net *net = xprt->xprt_net;
  319. mutex_lock(&nfs_callback_mutex);
  320. serv = nfs_callback_create_svc(minorversion);
  321. if (IS_ERR(serv)) {
  322. ret = PTR_ERR(serv);
  323. goto err_create;
  324. }
  325. ret = nfs_callback_up_net(minorversion, serv, net);
  326. if (ret < 0)
  327. goto err_net;
  328. ret = nfs_callback_start_svc(minorversion, xprt, serv);
  329. if (ret < 0)
  330. goto err_start;
  331. cb_info->users++;
  332. /*
  333. * svc_create creates the svc_serv with sv_nrthreads == 1, and then
  334. * svc_prepare_thread increments that. So we need to call svc_destroy
  335. * on both success and failure so that the refcount is 1 when the
  336. * thread exits.
  337. */
  338. err_net:
  339. svc_destroy(serv);
  340. err_create:
  341. mutex_unlock(&nfs_callback_mutex);
  342. return ret;
  343. err_start:
  344. nfs_callback_down_net(minorversion, serv, net);
  345. dprintk("NFS: Couldn't create server thread; err = %d\n", ret);
  346. goto err_net;
  347. }
  348. /*
  349. * Kill the callback thread if it's no longer being used.
  350. */
  351. void nfs_callback_down(int minorversion, struct net *net)
  352. {
  353. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  354. mutex_lock(&nfs_callback_mutex);
  355. nfs_callback_down_net(minorversion, cb_info->serv, net);
  356. cb_info->users--;
  357. if (cb_info->users == 0 && cb_info->task != NULL) {
  358. kthread_stop(cb_info->task);
  359. svc_exit_thread(cb_info->rqst);
  360. cb_info->serv = NULL;
  361. cb_info->rqst = NULL;
  362. cb_info->task = NULL;
  363. }
  364. mutex_unlock(&nfs_callback_mutex);
  365. }
  366. /* Boolean check of RPC_AUTH_GSS principal */
  367. int
  368. check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
  369. {
  370. char *p = rqstp->rq_cred.cr_principal;
  371. if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
  372. return 1;
  373. /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
  374. if (clp->cl_minorversion != 0)
  375. return 0;
  376. /*
  377. * It might just be a normal user principal, in which case
  378. * userspace won't bother to tell us the name at all.
  379. */
  380. if (p == NULL)
  381. return 0;
  382. /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
  383. if (memcmp(p, "nfs@", 4) != 0)
  384. return 0;
  385. p += 4;
  386. if (strcmp(p, clp->cl_hostname) != 0)
  387. return 0;
  388. return 1;
  389. }
  390. /*
  391. * pg_authenticate method for nfsv4 callback threads.
  392. *
  393. * The authflavor has been negotiated, so an incorrect flavor is a server
  394. * bug. Drop packets with incorrect authflavor.
  395. *
  396. * All other checking done after NFS decoding where the nfs_client can be
  397. * found in nfs4_callback_compound
  398. */
  399. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  400. {
  401. switch (rqstp->rq_authop->flavour) {
  402. case RPC_AUTH_NULL:
  403. if (rqstp->rq_proc != CB_NULL)
  404. return SVC_DROP;
  405. break;
  406. case RPC_AUTH_GSS:
  407. /* No RPC_AUTH_GSS support yet in NFSv4.1 */
  408. if (svc_is_backchannel(rqstp))
  409. return SVC_DROP;
  410. }
  411. return SVC_OK;
  412. }
  413. /*
  414. * Define NFS4 callback program
  415. */
  416. static struct svc_version *nfs4_callback_version[] = {
  417. [1] = &nfs4_callback_version1,
  418. [4] = &nfs4_callback_version4,
  419. };
  420. static struct svc_stat nfs4_callback_stats;
  421. static struct svc_program nfs4_callback_program = {
  422. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  423. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  424. .pg_vers = nfs4_callback_version, /* version table */
  425. .pg_name = "NFSv4 callback", /* service name */
  426. .pg_class = "nfs", /* authentication class */
  427. .pg_stats = &nfs4_callback_stats,
  428. .pg_authenticate = nfs_callback_authenticate,
  429. };