nfs4callback.c 24 KB

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