nfs4callback.c 19 KB

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