nfs4callback.c 26 KB

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