nfs4callback.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 "nfsd.h"
  35. #include "state.h"
  36. #define NFSDDBG_FACILITY NFSDDBG_PROC
  37. #define NFSPROC4_CB_NULL 0
  38. #define NFSPROC4_CB_COMPOUND 1
  39. #define NFS4_STATEID_SIZE 16
  40. /* Index of predefined Linux callback client operations */
  41. enum {
  42. NFSPROC4_CLNT_CB_NULL = 0,
  43. NFSPROC4_CLNT_CB_RECALL,
  44. NFSPROC4_CLNT_CB_SEQUENCE,
  45. };
  46. enum nfs_cb_opnum4 {
  47. OP_CB_RECALL = 4,
  48. OP_CB_SEQUENCE = 11,
  49. };
  50. #define NFS4_MAXTAGLEN 20
  51. #define NFS4_enc_cb_null_sz 0
  52. #define NFS4_dec_cb_null_sz 0
  53. #define cb_compound_enc_hdr_sz 4
  54. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  55. #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
  56. #define cb_sequence_enc_sz (sessionid_sz + 4 + \
  57. 1 /* no referring calls list yet */)
  58. #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
  59. #define op_enc_sz 1
  60. #define op_dec_sz 2
  61. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  62. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  63. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  64. cb_sequence_enc_sz + \
  65. 1 + enc_stateid_sz + \
  66. enc_nfs4_fh_sz)
  67. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  68. cb_sequence_dec_sz + \
  69. op_dec_sz)
  70. struct nfs4_rpc_args {
  71. void *args_op;
  72. struct nfsd4_cb_sequence args_seq;
  73. };
  74. /*
  75. * Generic encode routines from fs/nfs/nfs4xdr.c
  76. */
  77. static inline __be32 *
  78. xdr_writemem(__be32 *p, const void *ptr, int nbytes)
  79. {
  80. int tmp = XDR_QUADLEN(nbytes);
  81. if (!tmp)
  82. return p;
  83. p[tmp-1] = 0;
  84. memcpy(p, ptr, nbytes);
  85. return p + tmp;
  86. }
  87. #define WRITE32(n) *p++ = htonl(n)
  88. #define WRITEMEM(ptr,nbytes) do { \
  89. p = xdr_writemem(p, ptr, nbytes); \
  90. } while (0)
  91. #define RESERVE_SPACE(nbytes) do { \
  92. p = xdr_reserve_space(xdr, nbytes); \
  93. if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
  94. BUG_ON(!p); \
  95. } while (0)
  96. /*
  97. * Generic decode routines from fs/nfs/nfs4xdr.c
  98. */
  99. #define DECODE_TAIL \
  100. status = 0; \
  101. out: \
  102. return status; \
  103. xdr_error: \
  104. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  105. status = -EIO; \
  106. goto out
  107. #define READ32(x) (x) = ntohl(*p++)
  108. #define READ64(x) do { \
  109. (x) = (u64)ntohl(*p++) << 32; \
  110. (x) |= ntohl(*p++); \
  111. } while (0)
  112. #define READTIME(x) do { \
  113. p++; \
  114. (x.tv_sec) = ntohl(*p++); \
  115. (x.tv_nsec) = ntohl(*p++); \
  116. } while (0)
  117. #define READ_BUF(nbytes) do { \
  118. p = xdr_inline_decode(xdr, nbytes); \
  119. if (!p) { \
  120. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  121. __func__, __LINE__); \
  122. return -EIO; \
  123. } \
  124. } while (0)
  125. struct nfs4_cb_compound_hdr {
  126. /* args */
  127. u32 ident; /* minorversion 0 only */
  128. u32 nops;
  129. __be32 *nops_p;
  130. u32 minorversion;
  131. /* res */
  132. int status;
  133. u32 taglen;
  134. char *tag;
  135. };
  136. static struct {
  137. int stat;
  138. int errno;
  139. } nfs_cb_errtbl[] = {
  140. { NFS4_OK, 0 },
  141. { NFS4ERR_PERM, EPERM },
  142. { NFS4ERR_NOENT, ENOENT },
  143. { NFS4ERR_IO, EIO },
  144. { NFS4ERR_NXIO, ENXIO },
  145. { NFS4ERR_ACCESS, EACCES },
  146. { NFS4ERR_EXIST, EEXIST },
  147. { NFS4ERR_XDEV, EXDEV },
  148. { NFS4ERR_NOTDIR, ENOTDIR },
  149. { NFS4ERR_ISDIR, EISDIR },
  150. { NFS4ERR_INVAL, EINVAL },
  151. { NFS4ERR_FBIG, EFBIG },
  152. { NFS4ERR_NOSPC, ENOSPC },
  153. { NFS4ERR_ROFS, EROFS },
  154. { NFS4ERR_MLINK, EMLINK },
  155. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  156. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  157. { NFS4ERR_DQUOT, EDQUOT },
  158. { NFS4ERR_STALE, ESTALE },
  159. { NFS4ERR_BADHANDLE, EBADHANDLE },
  160. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  161. { NFS4ERR_NOTSUPP, ENOTSUPP },
  162. { NFS4ERR_TOOSMALL, ETOOSMALL },
  163. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  164. { NFS4ERR_BADTYPE, EBADTYPE },
  165. { NFS4ERR_LOCKED, EAGAIN },
  166. { NFS4ERR_RESOURCE, EREMOTEIO },
  167. { NFS4ERR_SYMLINK, ELOOP },
  168. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  169. { NFS4ERR_DEADLOCK, EDEADLK },
  170. { -1, EIO }
  171. };
  172. static int
  173. nfs_cb_stat_to_errno(int stat)
  174. {
  175. int i;
  176. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  177. if (nfs_cb_errtbl[i].stat == stat)
  178. return nfs_cb_errtbl[i].errno;
  179. }
  180. /* If we cannot translate the error, the recovery routines should
  181. * handle it.
  182. * Note: remaining NFSv4 error codes have values > 10000, so should
  183. * not conflict with native Linux error codes.
  184. */
  185. return stat;
  186. }
  187. /*
  188. * XDR encode
  189. */
  190. static void
  191. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  192. {
  193. __be32 * p;
  194. RESERVE_SPACE(16);
  195. WRITE32(0); /* tag length is always 0 */
  196. WRITE32(hdr->minorversion);
  197. WRITE32(hdr->ident);
  198. hdr->nops_p = p;
  199. WRITE32(hdr->nops);
  200. }
  201. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  202. {
  203. *hdr->nops_p = htonl(hdr->nops);
  204. }
  205. static void
  206. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
  207. struct nfs4_cb_compound_hdr *hdr)
  208. {
  209. __be32 *p;
  210. int len = dp->dl_fh.fh_size;
  211. RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
  212. WRITE32(OP_CB_RECALL);
  213. WRITE32(dp->dl_stateid.si_generation);
  214. WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
  215. WRITE32(0); /* truncate optimization not implemented */
  216. WRITE32(len);
  217. WRITEMEM(&dp->dl_fh.fh_base, len);
  218. hdr->nops++;
  219. }
  220. static void
  221. encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *args,
  222. struct nfs4_cb_compound_hdr *hdr)
  223. {
  224. __be32 *p;
  225. if (hdr->minorversion == 0)
  226. return;
  227. RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
  228. WRITE32(OP_CB_SEQUENCE);
  229. WRITEMEM(args->cbs_clp->cl_sessionid.data, NFS4_MAX_SESSIONID_LEN);
  230. WRITE32(args->cbs_clp->cl_cb_seq_nr);
  231. WRITE32(0); /* slotid, always 0 */
  232. WRITE32(0); /* highest slotid always 0 */
  233. WRITE32(0); /* cachethis always 0 */
  234. WRITE32(0); /* FIXME: support referring_call_lists */
  235. hdr->nops++;
  236. }
  237. static int
  238. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  239. {
  240. struct xdr_stream xdrs, *xdr = &xdrs;
  241. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  242. RESERVE_SPACE(0);
  243. return 0;
  244. }
  245. static int
  246. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
  247. struct nfs4_rpc_args *rpc_args)
  248. {
  249. struct xdr_stream xdr;
  250. struct nfs4_delegation *args = rpc_args->args_op;
  251. struct nfs4_cb_compound_hdr hdr = {
  252. .ident = args->dl_ident,
  253. .minorversion = rpc_args->args_seq.cbs_minorversion,
  254. };
  255. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  256. encode_cb_compound_hdr(&xdr, &hdr);
  257. encode_cb_sequence(&xdr, &rpc_args->args_seq, &hdr);
  258. encode_cb_recall(&xdr, args, &hdr);
  259. encode_cb_nops(&hdr);
  260. return 0;
  261. }
  262. static int
  263. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  264. __be32 *p;
  265. READ_BUF(8);
  266. READ32(hdr->status);
  267. READ32(hdr->taglen);
  268. READ_BUF(hdr->taglen + 4);
  269. hdr->tag = (char *)p;
  270. p += XDR_QUADLEN(hdr->taglen);
  271. READ32(hdr->nops);
  272. return 0;
  273. }
  274. static int
  275. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  276. {
  277. __be32 *p;
  278. u32 op;
  279. int32_t nfserr;
  280. READ_BUF(8);
  281. READ32(op);
  282. if (op != expected) {
  283. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  284. " operation %d but we issued a request for %d\n",
  285. op, expected);
  286. return -EIO;
  287. }
  288. READ32(nfserr);
  289. if (nfserr != NFS_OK)
  290. return -nfs_cb_stat_to_errno(nfserr);
  291. return 0;
  292. }
  293. /*
  294. * Our current back channel implmentation supports a single backchannel
  295. * with a single slot.
  296. */
  297. static int
  298. decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_cb_sequence *res,
  299. struct rpc_rqst *rqstp)
  300. {
  301. struct nfs4_sessionid id;
  302. int status;
  303. u32 dummy;
  304. __be32 *p;
  305. if (res->cbs_minorversion == 0)
  306. return 0;
  307. status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
  308. if (status)
  309. return status;
  310. /*
  311. * If the server returns different values for sessionID, slotID or
  312. * sequence number, the server is looney tunes.
  313. */
  314. status = -ESERVERFAULT;
  315. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  316. memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
  317. p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
  318. if (memcmp(id.data, res->cbs_clp->cl_sessionid.data,
  319. NFS4_MAX_SESSIONID_LEN)) {
  320. dprintk("%s Invalid session id\n", __func__);
  321. goto out;
  322. }
  323. READ32(dummy);
  324. if (dummy != res->cbs_clp->cl_cb_seq_nr) {
  325. dprintk("%s Invalid sequence number\n", __func__);
  326. goto out;
  327. }
  328. READ32(dummy); /* slotid must be 0 */
  329. if (dummy != 0) {
  330. dprintk("%s Invalid slotid\n", __func__);
  331. goto out;
  332. }
  333. /* FIXME: process highest slotid and target highest slotid */
  334. status = 0;
  335. out:
  336. return status;
  337. }
  338. static int
  339. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  340. {
  341. return 0;
  342. }
  343. static int
  344. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
  345. struct nfsd4_cb_sequence *seq)
  346. {
  347. struct xdr_stream xdr;
  348. struct nfs4_cb_compound_hdr hdr;
  349. int status;
  350. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  351. status = decode_cb_compound_hdr(&xdr, &hdr);
  352. if (status)
  353. goto out;
  354. if (seq) {
  355. status = decode_cb_sequence(&xdr, seq, rqstp);
  356. if (status)
  357. goto out;
  358. }
  359. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  360. out:
  361. return status;
  362. }
  363. /*
  364. * RPC procedure tables
  365. */
  366. #define PROC(proc, call, argtype, restype) \
  367. [NFSPROC4_CLNT_##proc] = { \
  368. .p_proc = NFSPROC4_CB_##call, \
  369. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  370. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  371. .p_arglen = NFS4_##argtype##_sz, \
  372. .p_replen = NFS4_##restype##_sz, \
  373. .p_statidx = NFSPROC4_CB_##call, \
  374. .p_name = #proc, \
  375. }
  376. static struct rpc_procinfo nfs4_cb_procedures[] = {
  377. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  378. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  379. };
  380. static struct rpc_version nfs_cb_version4 = {
  381. .number = 1,
  382. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  383. .procs = nfs4_cb_procedures
  384. };
  385. static struct rpc_version * nfs_cb_version[] = {
  386. NULL,
  387. &nfs_cb_version4,
  388. };
  389. static struct rpc_program cb_program;
  390. static struct rpc_stat cb_stats = {
  391. .program = &cb_program
  392. };
  393. #define NFS4_CALLBACK 0x40000000
  394. static struct rpc_program cb_program = {
  395. .name = "nfs4_cb",
  396. .number = NFS4_CALLBACK,
  397. .nrvers = ARRAY_SIZE(nfs_cb_version),
  398. .version = nfs_cb_version,
  399. .stats = &cb_stats,
  400. .pipe_dir_name = "/nfsd4_cb",
  401. };
  402. static int max_cb_time(void)
  403. {
  404. return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
  405. }
  406. /* Reference counting, callback cleanup, etc., all look racy as heck.
  407. * And why is cb_set an atomic? */
  408. int setup_callback_client(struct nfs4_client *clp)
  409. {
  410. struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
  411. struct rpc_timeout timeparms = {
  412. .to_initval = max_cb_time(),
  413. .to_retries = 0,
  414. };
  415. struct rpc_create_args args = {
  416. .protocol = XPRT_TRANSPORT_TCP,
  417. .address = (struct sockaddr *) &cb->cb_addr,
  418. .addrsize = cb->cb_addrlen,
  419. .timeout = &timeparms,
  420. .program = &cb_program,
  421. .prognumber = cb->cb_prog,
  422. .version = nfs_cb_version[1]->number,
  423. .authflavor = clp->cl_flavor,
  424. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  425. .client_name = clp->cl_principal,
  426. };
  427. struct rpc_clnt *client;
  428. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  429. return -EINVAL;
  430. if (cb->cb_minorversion) {
  431. args.bc_xprt = clp->cl_cb_xprt;
  432. args.protocol = XPRT_TRANSPORT_BC_TCP;
  433. }
  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_done(struct rpc_task *task, void *calldata)
  557. {
  558. struct nfs4_delegation *dp = calldata;
  559. struct nfs4_client *clp = dp->dl_client;
  560. dprintk("%s: minorversion=%d\n", __func__,
  561. clp->cl_cb_conn.cb_minorversion);
  562. if (clp->cl_cb_conn.cb_minorversion) {
  563. /* No need for lock, access serialized in nfsd4_cb_prepare */
  564. ++clp->cl_cb_seq_nr;
  565. clear_bit(0, &clp->cl_cb_slot_busy);
  566. rpc_wake_up_next(&clp->cl_cb_waitq);
  567. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  568. clp->cl_cb_seq_nr);
  569. /* We're done looking into the sequence information */
  570. task->tk_msg.rpc_resp = NULL;
  571. }
  572. }
  573. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  574. {
  575. struct nfs4_delegation *dp = calldata;
  576. struct nfs4_client *clp = dp->dl_client;
  577. nfsd4_cb_done(task, calldata);
  578. switch (task->tk_status) {
  579. case -EIO:
  580. /* Network partition? */
  581. atomic_set(&clp->cl_cb_conn.cb_set, 0);
  582. warn_no_callback_path(clp, task->tk_status);
  583. case -EBADHANDLE:
  584. case -NFS4ERR_BAD_STATEID:
  585. /* Race: client probably got cb_recall
  586. * before open reply granting delegation */
  587. break;
  588. default:
  589. /* success, or error we can't handle */
  590. goto done;
  591. }
  592. if (dp->dl_retries--) {
  593. rpc_delay(task, 2*HZ);
  594. task->tk_status = 0;
  595. rpc_restart_call(task);
  596. return;
  597. } else {
  598. atomic_set(&clp->cl_cb_conn.cb_set, 0);
  599. warn_no_callback_path(clp, task->tk_status);
  600. }
  601. done:
  602. kfree(task->tk_msg.rpc_argp);
  603. }
  604. static void nfsd4_cb_recall_release(void *calldata)
  605. {
  606. struct nfs4_delegation *dp = calldata;
  607. struct nfs4_client *clp = dp->dl_client;
  608. nfs4_put_delegation(dp);
  609. put_nfs4_client(clp);
  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. /*
  617. * called with dp->dl_count inc'ed.
  618. */
  619. void
  620. nfsd4_cb_recall(struct nfs4_delegation *dp)
  621. {
  622. struct nfs4_client *clp = dp->dl_client;
  623. struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
  624. struct nfs4_rpc_args *args;
  625. struct rpc_message msg = {
  626. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  627. .rpc_cred = callback_cred
  628. };
  629. int status = -ENOMEM;
  630. args = kzalloc(sizeof(*args), GFP_KERNEL);
  631. if (!args)
  632. goto out;
  633. args->args_op = dp;
  634. msg.rpc_argp = args;
  635. dp->dl_retries = 1;
  636. status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
  637. &nfsd4_cb_recall_ops, dp);
  638. out:
  639. if (status) {
  640. kfree(args);
  641. put_nfs4_client(clp);
  642. nfs4_put_delegation(dp);
  643. }
  644. }