nfs4callback.c 18 KB

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