nfs4callback.c 20 KB

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