nfs4callback.c 20 KB

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