nfs4callback.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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/sunrpc/svc_xprt.h>
  35. #include <linux/slab.h>
  36. #include "nfsd.h"
  37. #include "state.h"
  38. #define NFSDDBG_FACILITY NFSDDBG_PROC
  39. #define NFSPROC4_CB_NULL 0
  40. #define NFSPROC4_CB_COMPOUND 1
  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. #define NFS4_MAXTAGLEN 20
  48. #define NFS4_enc_cb_null_sz 0
  49. #define NFS4_dec_cb_null_sz 0
  50. #define cb_compound_enc_hdr_sz 4
  51. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  52. #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
  53. #define cb_sequence_enc_sz (sessionid_sz + 4 + \
  54. 1 /* no referring calls list yet */)
  55. #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
  56. #define op_enc_sz 1
  57. #define op_dec_sz 2
  58. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  59. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  60. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  61. cb_sequence_enc_sz + \
  62. 1 + enc_stateid_sz + \
  63. enc_nfs4_fh_sz)
  64. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  65. cb_sequence_dec_sz + \
  66. op_dec_sz)
  67. /*
  68. * Generic decode routines from fs/nfs/nfs4xdr.c
  69. */
  70. #define DECODE_TAIL \
  71. status = 0; \
  72. out: \
  73. return status; \
  74. xdr_error: \
  75. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  76. status = -EIO; \
  77. goto out
  78. #define READ32(x) (x) = ntohl(*p++)
  79. #define READ64(x) do { \
  80. (x) = (u64)ntohl(*p++) << 32; \
  81. (x) |= ntohl(*p++); \
  82. } while (0)
  83. #define READTIME(x) do { \
  84. p++; \
  85. (x.tv_sec) = ntohl(*p++); \
  86. (x.tv_nsec) = ntohl(*p++); \
  87. } while (0)
  88. #define READ_BUF(nbytes) do { \
  89. p = xdr_inline_decode(xdr, nbytes); \
  90. if (!p) { \
  91. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  92. __func__, __LINE__); \
  93. return -EIO; \
  94. } \
  95. } while (0)
  96. struct nfs4_cb_compound_hdr {
  97. /* args */
  98. u32 ident; /* minorversion 0 only */
  99. u32 nops;
  100. __be32 *nops_p;
  101. u32 minorversion;
  102. /* res */
  103. int status;
  104. };
  105. static struct {
  106. int stat;
  107. int errno;
  108. } nfs_cb_errtbl[] = {
  109. { NFS4_OK, 0 },
  110. { NFS4ERR_PERM, EPERM },
  111. { NFS4ERR_NOENT, ENOENT },
  112. { NFS4ERR_IO, EIO },
  113. { NFS4ERR_NXIO, ENXIO },
  114. { NFS4ERR_ACCESS, EACCES },
  115. { NFS4ERR_EXIST, EEXIST },
  116. { NFS4ERR_XDEV, EXDEV },
  117. { NFS4ERR_NOTDIR, ENOTDIR },
  118. { NFS4ERR_ISDIR, EISDIR },
  119. { NFS4ERR_INVAL, EINVAL },
  120. { NFS4ERR_FBIG, EFBIG },
  121. { NFS4ERR_NOSPC, ENOSPC },
  122. { NFS4ERR_ROFS, EROFS },
  123. { NFS4ERR_MLINK, EMLINK },
  124. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  125. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  126. { NFS4ERR_DQUOT, EDQUOT },
  127. { NFS4ERR_STALE, ESTALE },
  128. { NFS4ERR_BADHANDLE, EBADHANDLE },
  129. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  130. { NFS4ERR_NOTSUPP, ENOTSUPP },
  131. { NFS4ERR_TOOSMALL, ETOOSMALL },
  132. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  133. { NFS4ERR_BADTYPE, EBADTYPE },
  134. { NFS4ERR_LOCKED, EAGAIN },
  135. { NFS4ERR_RESOURCE, EREMOTEIO },
  136. { NFS4ERR_SYMLINK, ELOOP },
  137. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  138. { NFS4ERR_DEADLOCK, EDEADLK },
  139. { -1, EIO }
  140. };
  141. static int
  142. nfs_cb_stat_to_errno(int stat)
  143. {
  144. int i;
  145. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  146. if (nfs_cb_errtbl[i].stat == stat)
  147. return nfs_cb_errtbl[i].errno;
  148. }
  149. /* If we cannot translate the error, the recovery routines should
  150. * handle it.
  151. * Note: remaining NFSv4 error codes have values > 10000, so should
  152. * not conflict with native Linux error codes.
  153. */
  154. return stat;
  155. }
  156. static __be32 *xdr_encode_empty_array(__be32 *p)
  157. {
  158. *p++ = xdr_zero;
  159. return p;
  160. }
  161. /*
  162. * Encode/decode NFSv4 CB basic data types
  163. *
  164. * Basic NFSv4 callback data types are defined in section 15 of RFC
  165. * 3530: "Network File System (NFS) version 4 Protocol" and section
  166. * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
  167. * 1 Protocol"
  168. */
  169. /*
  170. * nfs_cb_opnum4
  171. *
  172. * enum nfs_cb_opnum4 {
  173. * OP_CB_GETATTR = 3,
  174. * ...
  175. * };
  176. */
  177. enum nfs_cb_opnum4 {
  178. OP_CB_GETATTR = 3,
  179. OP_CB_RECALL = 4,
  180. OP_CB_LAYOUTRECALL = 5,
  181. OP_CB_NOTIFY = 6,
  182. OP_CB_PUSH_DELEG = 7,
  183. OP_CB_RECALL_ANY = 8,
  184. OP_CB_RECALLABLE_OBJ_AVAIL = 9,
  185. OP_CB_RECALL_SLOT = 10,
  186. OP_CB_SEQUENCE = 11,
  187. OP_CB_WANTS_CANCELLED = 12,
  188. OP_CB_NOTIFY_LOCK = 13,
  189. OP_CB_NOTIFY_DEVICEID = 14,
  190. OP_CB_ILLEGAL = 10044
  191. };
  192. static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
  193. {
  194. __be32 *p;
  195. p = xdr_reserve_space(xdr, 4);
  196. *p = cpu_to_be32(op);
  197. }
  198. /*
  199. * nfs_fh4
  200. *
  201. * typedef opaque nfs_fh4<NFS4_FHSIZE>;
  202. */
  203. static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
  204. {
  205. u32 length = fh->fh_size;
  206. __be32 *p;
  207. BUG_ON(length > NFS4_FHSIZE);
  208. p = xdr_reserve_space(xdr, 4 + length);
  209. xdr_encode_opaque(p, &fh->fh_base, length);
  210. }
  211. /*
  212. * stateid4
  213. *
  214. * struct stateid4 {
  215. * uint32_t seqid;
  216. * opaque other[12];
  217. * };
  218. */
  219. static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
  220. {
  221. __be32 *p;
  222. p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
  223. *p++ = cpu_to_be32(sid->si_generation);
  224. xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
  225. }
  226. /*
  227. * sessionid4
  228. *
  229. * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
  230. */
  231. static void encode_sessionid4(struct xdr_stream *xdr,
  232. const struct nfsd4_session *session)
  233. {
  234. __be32 *p;
  235. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
  236. xdr_encode_opaque_fixed(p, session->se_sessionid.data,
  237. NFS4_MAX_SESSIONID_LEN);
  238. }
  239. /*
  240. * CB_COMPOUND4args
  241. *
  242. * struct CB_COMPOUND4args {
  243. * utf8str_cs tag;
  244. * uint32_t minorversion;
  245. * uint32_t callback_ident;
  246. * nfs_cb_argop4 argarray<>;
  247. * };
  248. */
  249. static void encode_cb_compound4args(struct xdr_stream *xdr,
  250. struct nfs4_cb_compound_hdr *hdr)
  251. {
  252. __be32 * p;
  253. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
  254. p = xdr_encode_empty_array(p); /* empty tag */
  255. *p++ = cpu_to_be32(hdr->minorversion);
  256. *p++ = cpu_to_be32(hdr->ident);
  257. hdr->nops_p = p;
  258. *p = cpu_to_be32(hdr->nops); /* argarray element count */
  259. }
  260. /*
  261. * Update argarray element count
  262. */
  263. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  264. {
  265. BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
  266. *hdr->nops_p = cpu_to_be32(hdr->nops);
  267. }
  268. /*
  269. * CB_RECALL4args
  270. *
  271. * struct CB_RECALL4args {
  272. * stateid4 stateid;
  273. * bool truncate;
  274. * nfs_fh4 fh;
  275. * };
  276. */
  277. static void encode_cb_recall4args(struct xdr_stream *xdr,
  278. const struct nfs4_delegation *dp,
  279. struct nfs4_cb_compound_hdr *hdr)
  280. {
  281. __be32 *p;
  282. encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
  283. encode_stateid4(xdr, &dp->dl_stateid);
  284. p = xdr_reserve_space(xdr, 4);
  285. *p++ = xdr_zero; /* truncate */
  286. encode_nfs_fh4(xdr, &dp->dl_fh);
  287. hdr->nops++;
  288. }
  289. /*
  290. * CB_SEQUENCE4args
  291. *
  292. * struct CB_SEQUENCE4args {
  293. * sessionid4 csa_sessionid;
  294. * sequenceid4 csa_sequenceid;
  295. * slotid4 csa_slotid;
  296. * slotid4 csa_highest_slotid;
  297. * bool csa_cachethis;
  298. * referring_call_list4 csa_referring_call_lists<>;
  299. * };
  300. */
  301. static void encode_cb_sequence4args(struct xdr_stream *xdr,
  302. const struct nfsd4_callback *cb,
  303. struct nfs4_cb_compound_hdr *hdr)
  304. {
  305. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  306. __be32 *p;
  307. if (hdr->minorversion == 0)
  308. return;
  309. encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
  310. encode_sessionid4(xdr, session);
  311. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
  312. *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
  313. *p++ = xdr_zero; /* csa_slotid */
  314. *p++ = xdr_zero; /* csa_highest_slotid */
  315. *p++ = xdr_zero; /* csa_cachethis */
  316. xdr_encode_empty_array(p); /* csa_referring_call_lists */
  317. hdr->nops++;
  318. }
  319. /*
  320. * NFSv4.0 and NFSv4.1 XDR encode functions
  321. *
  322. * NFSv4.0 callback argument types are defined in section 15 of RFC
  323. * 3530: "Network File System (NFS) version 4 Protocol" and section 20
  324. * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
  325. * Protocol".
  326. */
  327. /*
  328. * NB: Without this zero space reservation, callbacks over krb5p fail
  329. */
  330. static int nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p, void *__unused)
  331. {
  332. struct xdr_stream xdrs, *xdr = &xdrs;
  333. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  334. xdr_reserve_space(xdr, 0);
  335. return 0;
  336. }
  337. /*
  338. * 20.2. Operation 4: CB_RECALL - Recall a Delegation
  339. */
  340. static int nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
  341. const struct nfsd4_callback *cb)
  342. {
  343. struct xdr_stream xdr;
  344. const struct nfs4_delegation *args = cb->cb_op;
  345. struct nfs4_cb_compound_hdr hdr = {
  346. .ident = cb->cb_clp->cl_cb_ident,
  347. .minorversion = cb->cb_minorversion,
  348. };
  349. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  350. encode_cb_compound4args(&xdr, &hdr);
  351. encode_cb_sequence4args(&xdr, cb, &hdr);
  352. encode_cb_recall4args(&xdr, args, &hdr);
  353. encode_cb_nops(&hdr);
  354. return 0;
  355. }
  356. static int
  357. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  358. __be32 *p;
  359. u32 taglen;
  360. READ_BUF(8);
  361. READ32(hdr->status);
  362. /* We've got no use for the tag; ignore it: */
  363. READ32(taglen);
  364. READ_BUF(taglen + 4);
  365. p += XDR_QUADLEN(taglen);
  366. READ32(hdr->nops);
  367. return 0;
  368. }
  369. static int
  370. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  371. {
  372. __be32 *p;
  373. u32 op;
  374. int32_t nfserr;
  375. READ_BUF(8);
  376. READ32(op);
  377. if (op != expected) {
  378. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  379. " operation %d but we issued a request for %d\n",
  380. op, expected);
  381. return -EIO;
  382. }
  383. READ32(nfserr);
  384. if (nfserr != NFS_OK)
  385. return -nfs_cb_stat_to_errno(nfserr);
  386. return 0;
  387. }
  388. /*
  389. * Our current back channel implmentation supports a single backchannel
  390. * with a single slot.
  391. */
  392. static int
  393. decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_callback *cb,
  394. struct rpc_rqst *rqstp)
  395. {
  396. struct nfsd4_session *ses = cb->cb_clp->cl_cb_session;
  397. struct nfs4_sessionid id;
  398. int status;
  399. u32 dummy;
  400. __be32 *p;
  401. if (cb->cb_minorversion == 0)
  402. return 0;
  403. status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
  404. if (status)
  405. return status;
  406. /*
  407. * If the server returns different values for sessionID, slotID or
  408. * sequence number, the server is looney tunes.
  409. */
  410. status = -ESERVERFAULT;
  411. READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
  412. memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
  413. p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
  414. if (memcmp(id.data, ses->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
  415. dprintk("%s Invalid session id\n", __func__);
  416. goto out;
  417. }
  418. READ32(dummy);
  419. if (dummy != ses->se_cb_seq_nr) {
  420. dprintk("%s Invalid sequence number\n", __func__);
  421. goto out;
  422. }
  423. READ32(dummy); /* slotid must be 0 */
  424. if (dummy != 0) {
  425. dprintk("%s Invalid slotid\n", __func__);
  426. goto out;
  427. }
  428. /* FIXME: process highest slotid and target highest slotid */
  429. status = 0;
  430. out:
  431. return status;
  432. }
  433. static int
  434. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  435. {
  436. return 0;
  437. }
  438. static int
  439. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
  440. struct nfsd4_callback *cb)
  441. {
  442. struct xdr_stream xdr;
  443. struct nfs4_cb_compound_hdr hdr;
  444. int status;
  445. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  446. status = decode_cb_compound_hdr(&xdr, &hdr);
  447. if (status)
  448. goto out;
  449. if (cb) {
  450. status = decode_cb_sequence(&xdr, cb, rqstp);
  451. if (status)
  452. goto out;
  453. }
  454. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  455. out:
  456. return status;
  457. }
  458. /*
  459. * RPC procedure tables
  460. */
  461. #define PROC(proc, call, argtype, restype) \
  462. [NFSPROC4_CLNT_##proc] = { \
  463. .p_proc = NFSPROC4_CB_##call, \
  464. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  465. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  466. .p_arglen = NFS4_##argtype##_sz, \
  467. .p_replen = NFS4_##restype##_sz, \
  468. .p_statidx = NFSPROC4_CB_##call, \
  469. .p_name = #proc, \
  470. }
  471. static struct rpc_procinfo nfs4_cb_procedures[] = {
  472. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  473. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  474. };
  475. static struct rpc_version nfs_cb_version4 = {
  476. /*
  477. * Note on the callback rpc program version number: despite language in rfc
  478. * 5661 section 18.36.3 requiring servers to use 4 in this field, the
  479. * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
  480. * in practice that appears to be what implementations use. The section
  481. * 18.36.3 language is expected to be fixed in an erratum.
  482. */
  483. .number = 1,
  484. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  485. .procs = nfs4_cb_procedures
  486. };
  487. static struct rpc_version * nfs_cb_version[] = {
  488. &nfs_cb_version4,
  489. };
  490. static struct rpc_program cb_program;
  491. static struct rpc_stat cb_stats = {
  492. .program = &cb_program
  493. };
  494. #define NFS4_CALLBACK 0x40000000
  495. static struct rpc_program cb_program = {
  496. .name = "nfs4_cb",
  497. .number = NFS4_CALLBACK,
  498. .nrvers = ARRAY_SIZE(nfs_cb_version),
  499. .version = nfs_cb_version,
  500. .stats = &cb_stats,
  501. .pipe_dir_name = "/nfsd4_cb",
  502. };
  503. static int max_cb_time(void)
  504. {
  505. return max(nfsd4_lease/10, (time_t)1) * HZ;
  506. }
  507. /* Reference counting, callback cleanup, etc., all look racy as heck.
  508. * And why is cl_cb_set an atomic? */
  509. int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  510. {
  511. struct rpc_timeout timeparms = {
  512. .to_initval = max_cb_time(),
  513. .to_retries = 0,
  514. };
  515. struct rpc_create_args args = {
  516. .net = &init_net,
  517. .address = (struct sockaddr *) &conn->cb_addr,
  518. .addrsize = conn->cb_addrlen,
  519. .timeout = &timeparms,
  520. .program = &cb_program,
  521. .version = 0,
  522. .authflavor = clp->cl_flavor,
  523. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  524. };
  525. struct rpc_clnt *client;
  526. if (clp->cl_minorversion == 0) {
  527. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  528. return -EINVAL;
  529. args.client_name = clp->cl_principal;
  530. args.prognumber = conn->cb_prog,
  531. args.protocol = XPRT_TRANSPORT_TCP;
  532. clp->cl_cb_ident = conn->cb_ident;
  533. } else {
  534. args.bc_xprt = conn->cb_xprt;
  535. args.prognumber = clp->cl_cb_session->se_cb_prog;
  536. args.protocol = XPRT_TRANSPORT_BC_TCP;
  537. }
  538. /* Create RPC client */
  539. client = rpc_create(&args);
  540. if (IS_ERR(client)) {
  541. dprintk("NFSD: couldn't create callback client: %ld\n",
  542. PTR_ERR(client));
  543. return PTR_ERR(client);
  544. }
  545. clp->cl_cb_client = client;
  546. return 0;
  547. }
  548. static void warn_no_callback_path(struct nfs4_client *clp, int reason)
  549. {
  550. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  551. (int)clp->cl_name.len, clp->cl_name.data, reason);
  552. }
  553. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  554. {
  555. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  556. if (task->tk_status)
  557. warn_no_callback_path(clp, task->tk_status);
  558. else
  559. atomic_set(&clp->cl_cb_set, 1);
  560. }
  561. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  562. /* XXX: release method to ensure we set the cb channel down if
  563. * necessary on early failure? */
  564. .rpc_call_done = nfsd4_cb_probe_done,
  565. };
  566. static struct rpc_cred *callback_cred;
  567. int set_callback_cred(void)
  568. {
  569. if (callback_cred)
  570. return 0;
  571. callback_cred = rpc_lookup_machine_cred();
  572. if (!callback_cred)
  573. return -ENOMEM;
  574. return 0;
  575. }
  576. static struct workqueue_struct *callback_wq;
  577. static void do_probe_callback(struct nfs4_client *clp)
  578. {
  579. struct nfsd4_callback *cb = &clp->cl_cb_null;
  580. cb->cb_op = NULL;
  581. cb->cb_clp = clp;
  582. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
  583. cb->cb_msg.rpc_argp = NULL;
  584. cb->cb_msg.rpc_resp = NULL;
  585. cb->cb_msg.rpc_cred = callback_cred;
  586. cb->cb_ops = &nfsd4_cb_probe_ops;
  587. queue_work(callback_wq, &cb->cb_work);
  588. }
  589. /*
  590. * Poke the callback thread to process any updates to the callback
  591. * parameters, and send a null probe.
  592. */
  593. void nfsd4_probe_callback(struct nfs4_client *clp)
  594. {
  595. set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
  596. do_probe_callback(clp);
  597. }
  598. void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  599. {
  600. BUG_ON(atomic_read(&clp->cl_cb_set));
  601. spin_lock(&clp->cl_lock);
  602. memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
  603. spin_unlock(&clp->cl_lock);
  604. }
  605. /*
  606. * There's currently a single callback channel slot.
  607. * If the slot is available, then mark it busy. Otherwise, set the
  608. * thread for sleeping on the callback RPC wait queue.
  609. */
  610. static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
  611. struct rpc_task *task)
  612. {
  613. u32 *ptr = (u32 *)clp->cl_cb_session->se_sessionid.data;
  614. int status = 0;
  615. dprintk("%s: %u:%u:%u:%u\n", __func__,
  616. ptr[0], ptr[1], ptr[2], ptr[3]);
  617. if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  618. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  619. dprintk("%s slot is busy\n", __func__);
  620. status = -EAGAIN;
  621. goto out;
  622. }
  623. out:
  624. dprintk("%s status=%d\n", __func__, status);
  625. return status;
  626. }
  627. /*
  628. * TODO: cb_sequence should support referring call lists, cachethis, multiple
  629. * slots, and mark callback channel down on communication errors.
  630. */
  631. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  632. {
  633. struct nfsd4_callback *cb = calldata;
  634. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  635. struct nfs4_client *clp = dp->dl_client;
  636. u32 minorversion = clp->cl_minorversion;
  637. int status = 0;
  638. cb->cb_minorversion = minorversion;
  639. if (minorversion) {
  640. status = nfsd41_cb_setup_sequence(clp, task);
  641. if (status) {
  642. if (status != -EAGAIN) {
  643. /* terminate rpc task */
  644. task->tk_status = status;
  645. task->tk_action = NULL;
  646. }
  647. return;
  648. }
  649. }
  650. rpc_call_start(task);
  651. }
  652. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  653. {
  654. struct nfsd4_callback *cb = calldata;
  655. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  656. struct nfs4_client *clp = dp->dl_client;
  657. dprintk("%s: minorversion=%d\n", __func__,
  658. clp->cl_minorversion);
  659. if (clp->cl_minorversion) {
  660. /* No need for lock, access serialized in nfsd4_cb_prepare */
  661. ++clp->cl_cb_session->se_cb_seq_nr;
  662. clear_bit(0, &clp->cl_cb_slot_busy);
  663. rpc_wake_up_next(&clp->cl_cb_waitq);
  664. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  665. clp->cl_cb_session->se_cb_seq_nr);
  666. /* We're done looking into the sequence information */
  667. task->tk_msg.rpc_resp = NULL;
  668. }
  669. }
  670. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  671. {
  672. struct nfsd4_callback *cb = calldata;
  673. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  674. struct nfs4_client *clp = dp->dl_client;
  675. struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
  676. nfsd4_cb_done(task, calldata);
  677. if (current_rpc_client == NULL) {
  678. /* We're shutting down; give up. */
  679. /* XXX: err, or is it ok just to fall through
  680. * and rpc_restart_call? */
  681. return;
  682. }
  683. switch (task->tk_status) {
  684. case 0:
  685. return;
  686. case -EBADHANDLE:
  687. case -NFS4ERR_BAD_STATEID:
  688. /* Race: client probably got cb_recall
  689. * before open reply granting delegation */
  690. break;
  691. default:
  692. /* Network partition? */
  693. atomic_set(&clp->cl_cb_set, 0);
  694. warn_no_callback_path(clp, task->tk_status);
  695. if (current_rpc_client != task->tk_client) {
  696. /* queue a callback on the new connection: */
  697. atomic_inc(&dp->dl_count);
  698. nfsd4_cb_recall(dp);
  699. return;
  700. }
  701. }
  702. if (dp->dl_retries--) {
  703. rpc_delay(task, 2*HZ);
  704. task->tk_status = 0;
  705. rpc_restart_call_prepare(task);
  706. return;
  707. } else {
  708. atomic_set(&clp->cl_cb_set, 0);
  709. warn_no_callback_path(clp, task->tk_status);
  710. }
  711. }
  712. static void nfsd4_cb_recall_release(void *calldata)
  713. {
  714. struct nfsd4_callback *cb = calldata;
  715. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  716. nfs4_put_delegation(dp);
  717. }
  718. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  719. .rpc_call_prepare = nfsd4_cb_prepare,
  720. .rpc_call_done = nfsd4_cb_recall_done,
  721. .rpc_release = nfsd4_cb_recall_release,
  722. };
  723. int nfsd4_create_callback_queue(void)
  724. {
  725. callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
  726. if (!callback_wq)
  727. return -ENOMEM;
  728. return 0;
  729. }
  730. void nfsd4_destroy_callback_queue(void)
  731. {
  732. destroy_workqueue(callback_wq);
  733. }
  734. /* must be called under the state lock */
  735. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  736. {
  737. set_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags);
  738. /*
  739. * Note this won't actually result in a null callback;
  740. * instead, nfsd4_do_callback_rpc() will detect the killed
  741. * client, destroy the rpc client, and stop:
  742. */
  743. do_probe_callback(clp);
  744. flush_workqueue(callback_wq);
  745. }
  746. void nfsd4_release_cb(struct nfsd4_callback *cb)
  747. {
  748. if (cb->cb_ops->rpc_release)
  749. cb->cb_ops->rpc_release(cb);
  750. }
  751. void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  752. {
  753. struct nfs4_cb_conn conn;
  754. struct nfs4_client *clp = cb->cb_clp;
  755. int err;
  756. /*
  757. * This is either an update, or the client dying; in either case,
  758. * kill the old client:
  759. */
  760. if (clp->cl_cb_client) {
  761. rpc_shutdown_client(clp->cl_cb_client);
  762. clp->cl_cb_client = NULL;
  763. }
  764. if (test_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags))
  765. return;
  766. spin_lock(&clp->cl_lock);
  767. /*
  768. * Only serialized callback code is allowed to clear these
  769. * flags; main nfsd code can only set them:
  770. */
  771. BUG_ON(!clp->cl_cb_flags);
  772. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
  773. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  774. spin_unlock(&clp->cl_lock);
  775. err = setup_callback_client(clp, &conn);
  776. if (err)
  777. warn_no_callback_path(clp, err);
  778. }
  779. void nfsd4_do_callback_rpc(struct work_struct *w)
  780. {
  781. struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
  782. struct nfs4_client *clp = cb->cb_clp;
  783. struct rpc_clnt *clnt;
  784. if (clp->cl_cb_flags)
  785. nfsd4_process_cb_update(cb);
  786. clnt = clp->cl_cb_client;
  787. if (!clnt) {
  788. /* Callback channel broken, or client killed; give up: */
  789. nfsd4_release_cb(cb);
  790. return;
  791. }
  792. rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  793. cb->cb_ops, cb);
  794. }
  795. void nfsd4_cb_recall(struct nfs4_delegation *dp)
  796. {
  797. struct nfsd4_callback *cb = &dp->dl_recall;
  798. dp->dl_retries = 1;
  799. cb->cb_op = dp;
  800. cb->cb_clp = dp->dl_client;
  801. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
  802. cb->cb_msg.rpc_argp = cb;
  803. cb->cb_msg.rpc_resp = cb;
  804. cb->cb_msg.rpc_cred = callback_cred;
  805. cb->cb_ops = &nfsd4_cb_recall_ops;
  806. dp->dl_retries = 1;
  807. queue_work(callback_wq, &dp->dl_recall.cb_work);
  808. }