nfs4callback.c 26 KB

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