nfs4callback.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <kmsmith@umich.edu>
  6. * Andy Adamson <andros@umich.edu>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  22. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  23. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  28. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <linux/sunrpc/clnt.h>
  34. #include <linux/sunrpc/svc_xprt.h>
  35. #include <linux/slab.h>
  36. #include "nfsd.h"
  37. #include "state.h"
  38. #define NFSDDBG_FACILITY NFSDDBG_PROC
  39. #define NFSPROC4_CB_NULL 0
  40. #define NFSPROC4_CB_COMPOUND 1
  41. #define NFS4_STATEID_SIZE 16
  42. /* Index of predefined Linux callback client operations */
  43. enum {
  44. NFSPROC4_CLNT_CB_NULL = 0,
  45. NFSPROC4_CLNT_CB_RECALL,
  46. NFSPROC4_CLNT_CB_SEQUENCE,
  47. };
  48. enum nfs_cb_opnum4 {
  49. OP_CB_RECALL = 4,
  50. OP_CB_SEQUENCE = 11,
  51. };
  52. #define NFS4_MAXTAGLEN 20
  53. #define NFS4_enc_cb_null_sz 0
  54. #define NFS4_dec_cb_null_sz 0
  55. #define cb_compound_enc_hdr_sz 4
  56. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  57. #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
  58. #define cb_sequence_enc_sz (sessionid_sz + 4 + \
  59. 1 /* no referring calls list yet */)
  60. #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
  61. #define op_enc_sz 1
  62. #define op_dec_sz 2
  63. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  64. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  65. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  66. cb_sequence_enc_sz + \
  67. 1 + enc_stateid_sz + \
  68. enc_nfs4_fh_sz)
  69. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  70. cb_sequence_dec_sz + \
  71. op_dec_sz)
  72. /*
  73. * Generic encode routines from fs/nfs/nfs4xdr.c
  74. */
  75. static inline __be32 *
  76. xdr_writemem(__be32 *p, const void *ptr, int nbytes)
  77. {
  78. int tmp = XDR_QUADLEN(nbytes);
  79. if (!tmp)
  80. return p;
  81. p[tmp-1] = 0;
  82. memcpy(p, ptr, nbytes);
  83. return p + tmp;
  84. }
  85. #define WRITE32(n) *p++ = htonl(n)
  86. #define WRITEMEM(ptr,nbytes) do { \
  87. p = xdr_writemem(p, ptr, nbytes); \
  88. } while (0)
  89. #define RESERVE_SPACE(nbytes) do { \
  90. p = xdr_reserve_space(xdr, nbytes); \
  91. if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
  92. BUG_ON(!p); \
  93. } while (0)
  94. /*
  95. * Generic decode routines from fs/nfs/nfs4xdr.c
  96. */
  97. #define DECODE_TAIL \
  98. status = 0; \
  99. out: \
  100. return status; \
  101. xdr_error: \
  102. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  103. status = -EIO; \
  104. goto out
  105. #define READ32(x) (x) = ntohl(*p++)
  106. #define READ64(x) do { \
  107. (x) = (u64)ntohl(*p++) << 32; \
  108. (x) |= ntohl(*p++); \
  109. } while (0)
  110. #define READTIME(x) do { \
  111. p++; \
  112. (x.tv_sec) = ntohl(*p++); \
  113. (x.tv_nsec) = ntohl(*p++); \
  114. } while (0)
  115. #define READ_BUF(nbytes) do { \
  116. p = xdr_inline_decode(xdr, nbytes); \
  117. if (!p) { \
  118. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  119. __func__, __LINE__); \
  120. return -EIO; \
  121. } \
  122. } while (0)
  123. struct nfs4_cb_compound_hdr {
  124. /* args */
  125. u32 ident; /* minorversion 0 only */
  126. u32 nops;
  127. __be32 *nops_p;
  128. u32 minorversion;
  129. /* res */
  130. int status;
  131. u32 taglen;
  132. char *tag;
  133. };
  134. static struct {
  135. int stat;
  136. int errno;
  137. } nfs_cb_errtbl[] = {
  138. { NFS4_OK, 0 },
  139. { NFS4ERR_PERM, EPERM },
  140. { NFS4ERR_NOENT, ENOENT },
  141. { NFS4ERR_IO, EIO },
  142. { NFS4ERR_NXIO, ENXIO },
  143. { NFS4ERR_ACCESS, EACCES },
  144. { NFS4ERR_EXIST, EEXIST },
  145. { NFS4ERR_XDEV, EXDEV },
  146. { NFS4ERR_NOTDIR, ENOTDIR },
  147. { NFS4ERR_ISDIR, EISDIR },
  148. { NFS4ERR_INVAL, EINVAL },
  149. { NFS4ERR_FBIG, EFBIG },
  150. { NFS4ERR_NOSPC, ENOSPC },
  151. { NFS4ERR_ROFS, EROFS },
  152. { NFS4ERR_MLINK, EMLINK },
  153. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  154. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  155. { NFS4ERR_DQUOT, EDQUOT },
  156. { NFS4ERR_STALE, ESTALE },
  157. { NFS4ERR_BADHANDLE, EBADHANDLE },
  158. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  159. { NFS4ERR_NOTSUPP, ENOTSUPP },
  160. { NFS4ERR_TOOSMALL, ETOOSMALL },
  161. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  162. { NFS4ERR_BADTYPE, EBADTYPE },
  163. { NFS4ERR_LOCKED, EAGAIN },
  164. { NFS4ERR_RESOURCE, EREMOTEIO },
  165. { NFS4ERR_SYMLINK, ELOOP },
  166. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  167. { NFS4ERR_DEADLOCK, EDEADLK },
  168. { -1, EIO }
  169. };
  170. static int
  171. nfs_cb_stat_to_errno(int stat)
  172. {
  173. int i;
  174. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  175. if (nfs_cb_errtbl[i].stat == stat)
  176. return nfs_cb_errtbl[i].errno;
  177. }
  178. /* If we cannot translate the error, the recovery routines should
  179. * handle it.
  180. * Note: remaining NFSv4 error codes have values > 10000, so should
  181. * not conflict with native Linux error codes.
  182. */
  183. return stat;
  184. }
  185. /*
  186. * XDR encode
  187. */
  188. static void
  189. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  190. {
  191. __be32 * p;
  192. RESERVE_SPACE(16);
  193. WRITE32(0); /* tag length is always 0 */
  194. WRITE32(hdr->minorversion);
  195. WRITE32(hdr->ident);
  196. hdr->nops_p = p;
  197. WRITE32(hdr->nops);
  198. }
  199. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  200. {
  201. *hdr->nops_p = htonl(hdr->nops);
  202. }
  203. static void
  204. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
  205. struct nfs4_cb_compound_hdr *hdr)
  206. {
  207. __be32 *p;
  208. int len = dp->dl_fh.fh_size;
  209. RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
  210. WRITE32(OP_CB_RECALL);
  211. WRITE32(dp->dl_stateid.si_generation);
  212. WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
  213. WRITE32(0); /* truncate optimization not implemented */
  214. WRITE32(len);
  215. WRITEMEM(&dp->dl_fh.fh_base, len);
  216. hdr->nops++;
  217. }
  218. static void
  219. encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *args,
  220. struct nfs4_cb_compound_hdr *hdr)
  221. {
  222. __be32 *p;
  223. if (hdr->minorversion == 0)
  224. return;
  225. RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
  226. WRITE32(OP_CB_SEQUENCE);
  227. WRITEMEM(args->cbs_clp->cl_sessionid.data, NFS4_MAX_SESSIONID_LEN);
  228. WRITE32(args->cbs_clp->cl_cb_seq_nr);
  229. WRITE32(0); /* slotid, always 0 */
  230. WRITE32(0); /* highest slotid always 0 */
  231. WRITE32(0); /* cachethis always 0 */
  232. WRITE32(0); /* FIXME: support referring_call_lists */
  233. hdr->nops++;
  234. }
  235. static int
  236. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  237. {
  238. struct xdr_stream xdrs, *xdr = &xdrs;
  239. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  240. RESERVE_SPACE(0);
  241. return 0;
  242. }
  243. static int
  244. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
  245. struct nfs4_rpc_args *rpc_args)
  246. {
  247. struct xdr_stream xdr;
  248. struct nfs4_delegation *args = rpc_args->args_op;
  249. struct nfs4_cb_compound_hdr hdr = {
  250. .ident = args->dl_ident,
  251. .minorversion = rpc_args->args_seq.cbs_minorversion,
  252. };
  253. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  254. encode_cb_compound_hdr(&xdr, &hdr);
  255. encode_cb_sequence(&xdr, &rpc_args->args_seq, &hdr);
  256. encode_cb_recall(&xdr, args, &hdr);
  257. encode_cb_nops(&hdr);
  258. return 0;
  259. }
  260. static int
  261. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  262. __be32 *p;
  263. READ_BUF(8);
  264. READ32(hdr->status);
  265. READ32(hdr->taglen);
  266. READ_BUF(hdr->taglen + 4);
  267. hdr->tag = (char *)p;
  268. p += XDR_QUADLEN(hdr->taglen);
  269. READ32(hdr->nops);
  270. return 0;
  271. }
  272. static int
  273. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  274. {
  275. __be32 *p;
  276. u32 op;
  277. int32_t nfserr;
  278. READ_BUF(8);
  279. READ32(op);
  280. if (op != expected) {
  281. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  282. " operation %d but we issued a request for %d\n",
  283. op, expected);
  284. return -EIO;
  285. }
  286. READ32(nfserr);
  287. if (nfserr != NFS_OK)
  288. return -nfs_cb_stat_to_errno(nfserr);
  289. return 0;
  290. }
  291. /*
  292. * Our current back channel implmentation supports a single backchannel
  293. * with a single slot.
  294. */
  295. static int
  296. decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *res,
  297. struct rpc_rqst *rqstp)
  298. {
  299. struct nfs4_sessionid id;
  300. int status;
  301. u32 dummy;
  302. __be32 *p;
  303. if (res->cbs_minorversion == 0)
  304. return 0;
  305. status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
  306. if (status)
  307. return status;
  308. /*
  309. * If the server returns different values for sessionID, slotID or
  310. * sequence number, the server is looney tunes.
  311. */
  312. status = -ESERVERFAULT;
  313. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  314. memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
  315. p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
  316. if (memcmp(id.data, res->cbs_clp->cl_sessionid.data,
  317. NFS4_MAX_SESSIONID_LEN)) {
  318. dprintk("%s Invalid session id\n", __func__);
  319. goto out;
  320. }
  321. READ32(dummy);
  322. if (dummy != res->cbs_clp->cl_cb_seq_nr) {
  323. dprintk("%s Invalid sequence number\n", __func__);
  324. goto out;
  325. }
  326. READ32(dummy); /* slotid must be 0 */
  327. if (dummy != 0) {
  328. dprintk("%s Invalid slotid\n", __func__);
  329. goto out;
  330. }
  331. /* FIXME: process highest slotid and target highest slotid */
  332. status = 0;
  333. out:
  334. return status;
  335. }
  336. static int
  337. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  338. {
  339. return 0;
  340. }
  341. static int
  342. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
  343. struct nfsd4_cb_sequence *seq)
  344. {
  345. struct xdr_stream xdr;
  346. struct nfs4_cb_compound_hdr hdr;
  347. int status;
  348. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  349. status = decode_cb_compound_hdr(&xdr, &hdr);
  350. if (status)
  351. goto out;
  352. if (seq) {
  353. status = decode_cb_sequence(&xdr, seq, rqstp);
  354. if (status)
  355. goto out;
  356. }
  357. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  358. out:
  359. return status;
  360. }
  361. /*
  362. * RPC procedure tables
  363. */
  364. #define PROC(proc, call, argtype, restype) \
  365. [NFSPROC4_CLNT_##proc] = { \
  366. .p_proc = NFSPROC4_CB_##call, \
  367. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  368. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  369. .p_arglen = NFS4_##argtype##_sz, \
  370. .p_replen = NFS4_##restype##_sz, \
  371. .p_statidx = NFSPROC4_CB_##call, \
  372. .p_name = #proc, \
  373. }
  374. static struct rpc_procinfo nfs4_cb_procedures[] = {
  375. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  376. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  377. };
  378. static struct rpc_version nfs_cb_version4 = {
  379. /*
  380. * Note on the callback rpc program version number: despite language in rfc
  381. * 5661 section 18.36.3 requiring servers to use 4 in this field, the
  382. * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
  383. * in practice that appears to be what implementations use. The section
  384. * 18.36.3 language is expected to be fixed in an erratum.
  385. */
  386. .number = 1,
  387. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  388. .procs = nfs4_cb_procedures
  389. };
  390. static struct rpc_version * nfs_cb_version[] = {
  391. &nfs_cb_version4,
  392. };
  393. static struct rpc_program cb_program;
  394. static struct rpc_stat cb_stats = {
  395. .program = &cb_program
  396. };
  397. #define NFS4_CALLBACK 0x40000000
  398. static struct rpc_program cb_program = {
  399. .name = "nfs4_cb",
  400. .number = NFS4_CALLBACK,
  401. .nrvers = ARRAY_SIZE(nfs_cb_version),
  402. .version = nfs_cb_version,
  403. .stats = &cb_stats,
  404. .pipe_dir_name = "/nfsd4_cb",
  405. };
  406. static int max_cb_time(void)
  407. {
  408. return max(nfsd4_lease/10, (time_t)1) * HZ;
  409. }
  410. /* Reference counting, callback cleanup, etc., all look racy as heck.
  411. * And why is cl_cb_set an atomic? */
  412. int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *cb)
  413. {
  414. struct rpc_timeout timeparms = {
  415. .to_initval = max_cb_time(),
  416. .to_retries = 0,
  417. };
  418. struct rpc_create_args args = {
  419. .protocol = XPRT_TRANSPORT_TCP,
  420. .address = (struct sockaddr *) &cb->cb_addr,
  421. .addrsize = cb->cb_addrlen,
  422. .timeout = &timeparms,
  423. .program = &cb_program,
  424. .prognumber = cb->cb_prog,
  425. .version = 0,
  426. .authflavor = clp->cl_flavor,
  427. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  428. .client_name = clp->cl_principal,
  429. };
  430. struct rpc_clnt *client;
  431. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  432. return -EINVAL;
  433. if (cb->cb_minorversion) {
  434. args.bc_xprt = cb->cb_xprt;
  435. args.protocol = XPRT_TRANSPORT_BC_TCP;
  436. }
  437. /* Create RPC client */
  438. client = rpc_create(&args);
  439. if (IS_ERR(client)) {
  440. dprintk("NFSD: couldn't create callback client: %ld\n",
  441. PTR_ERR(client));
  442. return PTR_ERR(client);
  443. }
  444. nfsd4_set_callback_client(clp, client);
  445. return 0;
  446. }
  447. static void warn_no_callback_path(struct nfs4_client *clp, int reason)
  448. {
  449. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  450. (int)clp->cl_name.len, clp->cl_name.data, reason);
  451. }
  452. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  453. {
  454. struct nfs4_client *clp = calldata;
  455. if (task->tk_status)
  456. warn_no_callback_path(clp, task->tk_status);
  457. else
  458. atomic_set(&clp->cl_cb_set, 1);
  459. }
  460. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  461. .rpc_call_done = nfsd4_cb_probe_done,
  462. };
  463. static struct rpc_cred *callback_cred;
  464. int set_callback_cred(void)
  465. {
  466. if (callback_cred)
  467. return 0;
  468. callback_cred = rpc_lookup_machine_cred();
  469. if (!callback_cred)
  470. return -ENOMEM;
  471. return 0;
  472. }
  473. void do_probe_callback(struct nfs4_client *clp)
  474. {
  475. struct rpc_message msg = {
  476. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  477. .rpc_argp = clp,
  478. .rpc_cred = callback_cred
  479. };
  480. int status;
  481. status = rpc_call_async(clp->cl_cb_client, &msg,
  482. RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  483. &nfsd4_cb_probe_ops, (void *)clp);
  484. if (status)
  485. warn_no_callback_path(clp, status);
  486. }
  487. /*
  488. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  489. */
  490. void nfsd4_probe_callback(struct nfs4_client *clp, struct nfs4_cb_conn *cb)
  491. {
  492. int status;
  493. BUG_ON(atomic_read(&clp->cl_cb_set));
  494. status = setup_callback_client(clp, cb);
  495. if (status) {
  496. warn_no_callback_path(clp, status);
  497. return;
  498. }
  499. do_probe_callback(clp);
  500. }
  501. /*
  502. * There's currently a single callback channel slot.
  503. * If the slot is available, then mark it busy. Otherwise, set the
  504. * thread for sleeping on the callback RPC wait queue.
  505. */
  506. static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
  507. struct rpc_task *task)
  508. {
  509. struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
  510. u32 *ptr = (u32 *)clp->cl_sessionid.data;
  511. int status = 0;
  512. dprintk("%s: %u:%u:%u:%u\n", __func__,
  513. ptr[0], ptr[1], ptr[2], ptr[3]);
  514. if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  515. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  516. dprintk("%s slot is busy\n", __func__);
  517. status = -EAGAIN;
  518. goto out;
  519. }
  520. /*
  521. * We'll need the clp during XDR encoding and decoding,
  522. * and the sequence during decoding to verify the reply
  523. */
  524. args->args_seq.cbs_clp = clp;
  525. task->tk_msg.rpc_resp = &args->args_seq;
  526. out:
  527. dprintk("%s status=%d\n", __func__, status);
  528. return status;
  529. }
  530. /*
  531. * TODO: cb_sequence should support referring call lists, cachethis, multiple
  532. * slots, and mark callback channel down on communication errors.
  533. */
  534. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  535. {
  536. struct nfs4_delegation *dp = calldata;
  537. struct nfs4_client *clp = dp->dl_client;
  538. struct nfs4_rpc_args *args = task->tk_msg.rpc_argp;
  539. u32 minorversion = clp->cl_cb_conn.cb_minorversion;
  540. int status = 0;
  541. args->args_seq.cbs_minorversion = minorversion;
  542. if (minorversion) {
  543. status = nfsd41_cb_setup_sequence(clp, task);
  544. if (status) {
  545. if (status != -EAGAIN) {
  546. /* terminate rpc task */
  547. task->tk_status = status;
  548. task->tk_action = NULL;
  549. }
  550. return;
  551. }
  552. }
  553. rpc_call_start(task);
  554. }
  555. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  556. {
  557. struct nfs4_delegation *dp = calldata;
  558. struct nfs4_client *clp = dp->dl_client;
  559. dprintk("%s: minorversion=%d\n", __func__,
  560. clp->cl_cb_conn.cb_minorversion);
  561. if (clp->cl_cb_conn.cb_minorversion) {
  562. /* No need for lock, access serialized in nfsd4_cb_prepare */
  563. ++clp->cl_cb_seq_nr;
  564. clear_bit(0, &clp->cl_cb_slot_busy);
  565. rpc_wake_up_next(&clp->cl_cb_waitq);
  566. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  567. clp->cl_cb_seq_nr);
  568. /* We're done looking into the sequence information */
  569. task->tk_msg.rpc_resp = NULL;
  570. }
  571. }
  572. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  573. {
  574. struct nfs4_delegation *dp = calldata;
  575. struct nfs4_client *clp = dp->dl_client;
  576. struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
  577. nfsd4_cb_done(task, calldata);
  578. if (current_rpc_client == NULL) {
  579. /* We're shutting down; give up. */
  580. /* XXX: err, or is it ok just to fall through
  581. * and rpc_restart_call? */
  582. return;
  583. }
  584. switch (task->tk_status) {
  585. case -EIO:
  586. /* Network partition? */
  587. atomic_set(&clp->cl_cb_set, 0);
  588. warn_no_callback_path(clp, task->tk_status);
  589. if (current_rpc_client != task->tk_client) {
  590. /* queue a callback on the new connection: */
  591. nfsd4_cb_recall(dp);
  592. return;
  593. }
  594. case -EBADHANDLE:
  595. case -NFS4ERR_BAD_STATEID:
  596. /* Race: client probably got cb_recall
  597. * before open reply granting delegation */
  598. break;
  599. default:
  600. /* success, or error we can't handle */
  601. return;
  602. }
  603. if (dp->dl_retries--) {
  604. rpc_delay(task, 2*HZ);
  605. task->tk_status = 0;
  606. rpc_restart_call(task);
  607. return;
  608. } else {
  609. atomic_set(&clp->cl_cb_set, 0);
  610. warn_no_callback_path(clp, task->tk_status);
  611. }
  612. }
  613. static void nfsd4_cb_recall_release(void *calldata)
  614. {
  615. struct nfs4_delegation *dp = calldata;
  616. nfs4_put_delegation(dp);
  617. }
  618. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  619. .rpc_call_prepare = nfsd4_cb_prepare,
  620. .rpc_call_done = nfsd4_cb_recall_done,
  621. .rpc_release = nfsd4_cb_recall_release,
  622. };
  623. static struct workqueue_struct *callback_wq;
  624. int nfsd4_create_callback_queue(void)
  625. {
  626. callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
  627. if (!callback_wq)
  628. return -ENOMEM;
  629. return 0;
  630. }
  631. void nfsd4_destroy_callback_queue(void)
  632. {
  633. destroy_workqueue(callback_wq);
  634. }
  635. /* must be called under the state lock */
  636. void nfsd4_set_callback_client(struct nfs4_client *clp, struct rpc_clnt *new)
  637. {
  638. struct rpc_clnt *old = clp->cl_cb_client;
  639. clp->cl_cb_client = new;
  640. /*
  641. * After this, any work that saw the old value of cl_cb_client will
  642. * be gone:
  643. */
  644. flush_workqueue(callback_wq);
  645. /* So we can safely shut it down: */
  646. if (old)
  647. rpc_shutdown_client(old);
  648. }
  649. /*
  650. * called with dp->dl_count inc'ed.
  651. */
  652. static void _nfsd4_cb_recall(struct nfs4_delegation *dp)
  653. {
  654. struct nfs4_client *clp = dp->dl_client;
  655. struct rpc_clnt *clnt = clp->cl_cb_client;
  656. struct nfs4_rpc_args *args = &dp->dl_recall.cb_args;
  657. struct rpc_message msg = {
  658. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  659. .rpc_cred = callback_cred
  660. };
  661. int status;
  662. if (clnt == NULL)
  663. return; /* Client is shutting down; give up. */
  664. args->args_op = dp;
  665. msg.rpc_argp = args;
  666. dp->dl_retries = 1;
  667. status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
  668. &nfsd4_cb_recall_ops, dp);
  669. if (status)
  670. nfs4_put_delegation(dp);
  671. }
  672. void nfsd4_do_callback_rpc(struct work_struct *w)
  673. {
  674. /* XXX: for now, just send off delegation recall. */
  675. /* In future, generalize to handle any sort of callback. */
  676. struct nfsd4_callback *c = container_of(w, struct nfsd4_callback, cb_work);
  677. struct nfs4_delegation *dp = container_of(c, struct nfs4_delegation, dl_recall);
  678. _nfsd4_cb_recall(dp);
  679. }
  680. void nfsd4_cb_recall(struct nfs4_delegation *dp)
  681. {
  682. queue_work(callback_wq, &dp->dl_recall.cb_work);
  683. }