nfs4callback.c 19 KB

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