nfs4callback.c 25 KB

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