nfs4callback.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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_fh);
  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. struct rpc_timeout timeparms = {
  580. .to_initval = max_cb_time(clp->net),
  581. .to_retries = 0,
  582. };
  583. struct rpc_create_args args = {
  584. .net = clp->net,
  585. .address = (struct sockaddr *) &conn->cb_addr,
  586. .addrsize = conn->cb_addrlen,
  587. .saddress = (struct sockaddr *) &conn->cb_saddr,
  588. .timeout = &timeparms,
  589. .program = &cb_program,
  590. .version = 0,
  591. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  592. };
  593. struct rpc_clnt *client;
  594. struct rpc_cred *cred;
  595. if (clp->cl_minorversion == 0) {
  596. if (!clp->cl_cred.cr_principal &&
  597. (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
  598. return -EINVAL;
  599. args.client_name = clp->cl_cred.cr_principal;
  600. args.prognumber = conn->cb_prog,
  601. args.protocol = XPRT_TRANSPORT_TCP;
  602. args.authflavor = clp->cl_cred.cr_flavor;
  603. clp->cl_cb_ident = conn->cb_ident;
  604. } else {
  605. if (!conn->cb_xprt)
  606. return -EINVAL;
  607. clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
  608. clp->cl_cb_session = ses;
  609. args.bc_xprt = conn->cb_xprt;
  610. args.prognumber = clp->cl_cb_session->se_cb_prog;
  611. args.protocol = XPRT_TRANSPORT_BC_TCP;
  612. args.authflavor = ses->se_cb_sec.flavor;
  613. }
  614. /* Create RPC client */
  615. client = create_backchannel_client(&args);
  616. if (IS_ERR(client)) {
  617. dprintk("NFSD: couldn't create callback client: %ld\n",
  618. PTR_ERR(client));
  619. return PTR_ERR(client);
  620. }
  621. cred = get_backchannel_cred(clp, client, ses);
  622. if (IS_ERR(cred)) {
  623. rpc_shutdown_client(client);
  624. return PTR_ERR(cred);
  625. }
  626. clp->cl_cb_client = client;
  627. clp->cl_cb_cred = cred;
  628. return 0;
  629. }
  630. static void warn_no_callback_path(struct nfs4_client *clp, int reason)
  631. {
  632. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  633. (int)clp->cl_name.len, clp->cl_name.data, reason);
  634. }
  635. static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
  636. {
  637. clp->cl_cb_state = NFSD4_CB_DOWN;
  638. warn_no_callback_path(clp, reason);
  639. }
  640. static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
  641. {
  642. clp->cl_cb_state = NFSD4_CB_FAULT;
  643. warn_no_callback_path(clp, reason);
  644. }
  645. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  646. {
  647. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  648. if (task->tk_status)
  649. nfsd4_mark_cb_down(clp, task->tk_status);
  650. else
  651. clp->cl_cb_state = NFSD4_CB_UP;
  652. }
  653. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  654. /* XXX: release method to ensure we set the cb channel down if
  655. * necessary on early failure? */
  656. .rpc_call_done = nfsd4_cb_probe_done,
  657. };
  658. static struct workqueue_struct *callback_wq;
  659. static void run_nfsd4_cb(struct nfsd4_callback *cb)
  660. {
  661. queue_work(callback_wq, &cb->cb_work);
  662. }
  663. static void do_probe_callback(struct nfs4_client *clp)
  664. {
  665. struct nfsd4_callback *cb = &clp->cl_cb_null;
  666. cb->cb_op = NULL;
  667. cb->cb_clp = clp;
  668. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
  669. cb->cb_msg.rpc_argp = NULL;
  670. cb->cb_msg.rpc_resp = NULL;
  671. cb->cb_ops = &nfsd4_cb_probe_ops;
  672. run_nfsd4_cb(cb);
  673. }
  674. /*
  675. * Poke the callback thread to process any updates to the callback
  676. * parameters, and send a null probe.
  677. */
  678. void nfsd4_probe_callback(struct nfs4_client *clp)
  679. {
  680. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  681. set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  682. do_probe_callback(clp);
  683. }
  684. void nfsd4_probe_callback_sync(struct nfs4_client *clp)
  685. {
  686. nfsd4_probe_callback(clp);
  687. flush_workqueue(callback_wq);
  688. }
  689. void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  690. {
  691. clp->cl_cb_state = NFSD4_CB_UNKNOWN;
  692. spin_lock(&clp->cl_lock);
  693. memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
  694. spin_unlock(&clp->cl_lock);
  695. }
  696. /*
  697. * There's currently a single callback channel slot.
  698. * If the slot is available, then mark it busy. Otherwise, set the
  699. * thread for sleeping on the callback RPC wait queue.
  700. */
  701. static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
  702. {
  703. if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  704. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  705. dprintk("%s slot is busy\n", __func__);
  706. return false;
  707. }
  708. return true;
  709. }
  710. /*
  711. * TODO: cb_sequence should support referring call lists, cachethis, multiple
  712. * slots, and mark callback channel down on communication errors.
  713. */
  714. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  715. {
  716. struct nfsd4_callback *cb = calldata;
  717. struct nfs4_client *clp = cb->cb_clp;
  718. u32 minorversion = clp->cl_minorversion;
  719. cb->cb_minorversion = minorversion;
  720. if (minorversion) {
  721. if (!nfsd41_cb_get_slot(clp, task))
  722. return;
  723. }
  724. spin_lock(&clp->cl_lock);
  725. if (list_empty(&cb->cb_per_client)) {
  726. /* This is the first call, not a restart */
  727. cb->cb_done = false;
  728. list_add(&cb->cb_per_client, &clp->cl_callbacks);
  729. }
  730. spin_unlock(&clp->cl_lock);
  731. rpc_call_start(task);
  732. }
  733. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  734. {
  735. struct nfsd4_callback *cb = calldata;
  736. struct nfs4_client *clp = cb->cb_clp;
  737. dprintk("%s: minorversion=%d\n", __func__,
  738. clp->cl_minorversion);
  739. if (clp->cl_minorversion) {
  740. /* No need for lock, access serialized in nfsd4_cb_prepare */
  741. ++clp->cl_cb_session->se_cb_seq_nr;
  742. clear_bit(0, &clp->cl_cb_slot_busy);
  743. rpc_wake_up_next(&clp->cl_cb_waitq);
  744. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  745. clp->cl_cb_session->se_cb_seq_nr);
  746. /* We're done looking into the sequence information */
  747. task->tk_msg.rpc_resp = NULL;
  748. }
  749. }
  750. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  751. {
  752. struct nfsd4_callback *cb = calldata;
  753. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  754. struct nfs4_client *clp = cb->cb_clp;
  755. struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
  756. nfsd4_cb_done(task, calldata);
  757. if (current_rpc_client != task->tk_client) {
  758. /* We're shutting down or changing cl_cb_client; leave
  759. * it to nfsd4_process_cb_update to restart the call if
  760. * necessary. */
  761. return;
  762. }
  763. if (cb->cb_done)
  764. return;
  765. switch (task->tk_status) {
  766. case 0:
  767. cb->cb_done = true;
  768. return;
  769. case -EBADHANDLE:
  770. case -NFS4ERR_BAD_STATEID:
  771. /* Race: client probably got cb_recall
  772. * before open reply granting delegation */
  773. break;
  774. default:
  775. /* Network partition? */
  776. nfsd4_mark_cb_down(clp, task->tk_status);
  777. }
  778. if (dp->dl_retries--) {
  779. rpc_delay(task, 2*HZ);
  780. task->tk_status = 0;
  781. rpc_restart_call_prepare(task);
  782. return;
  783. }
  784. nfsd4_mark_cb_down(clp, task->tk_status);
  785. cb->cb_done = true;
  786. }
  787. static void nfsd4_cb_recall_release(void *calldata)
  788. {
  789. struct nfsd4_callback *cb = calldata;
  790. struct nfs4_client *clp = cb->cb_clp;
  791. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  792. if (cb->cb_done) {
  793. spin_lock(&clp->cl_lock);
  794. list_del(&cb->cb_per_client);
  795. spin_unlock(&clp->cl_lock);
  796. nfs4_put_delegation(dp);
  797. }
  798. }
  799. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  800. .rpc_call_prepare = nfsd4_cb_prepare,
  801. .rpc_call_done = nfsd4_cb_recall_done,
  802. .rpc_release = nfsd4_cb_recall_release,
  803. };
  804. int nfsd4_create_callback_queue(void)
  805. {
  806. callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
  807. if (!callback_wq)
  808. return -ENOMEM;
  809. return 0;
  810. }
  811. void nfsd4_destroy_callback_queue(void)
  812. {
  813. destroy_workqueue(callback_wq);
  814. }
  815. /* must be called under the state lock */
  816. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  817. {
  818. set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
  819. /*
  820. * Note this won't actually result in a null callback;
  821. * instead, nfsd4_do_callback_rpc() will detect the killed
  822. * client, destroy the rpc client, and stop:
  823. */
  824. do_probe_callback(clp);
  825. flush_workqueue(callback_wq);
  826. }
  827. static void nfsd4_release_cb(struct nfsd4_callback *cb)
  828. {
  829. if (cb->cb_ops->rpc_release)
  830. cb->cb_ops->rpc_release(cb);
  831. }
  832. /* requires cl_lock: */
  833. static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
  834. {
  835. struct nfsd4_session *s;
  836. struct nfsd4_conn *c;
  837. list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
  838. list_for_each_entry(c, &s->se_conns, cn_persession) {
  839. if (c->cn_flags & NFS4_CDFC4_BACK)
  840. return c;
  841. }
  842. }
  843. return NULL;
  844. }
  845. static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  846. {
  847. struct nfs4_cb_conn conn;
  848. struct nfs4_client *clp = cb->cb_clp;
  849. struct nfsd4_session *ses = NULL;
  850. struct nfsd4_conn *c;
  851. int err;
  852. /*
  853. * This is either an update, or the client dying; in either case,
  854. * kill the old client:
  855. */
  856. if (clp->cl_cb_client) {
  857. rpc_shutdown_client(clp->cl_cb_client);
  858. clp->cl_cb_client = NULL;
  859. put_rpccred(clp->cl_cb_cred);
  860. clp->cl_cb_cred = NULL;
  861. }
  862. if (clp->cl_cb_conn.cb_xprt) {
  863. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  864. clp->cl_cb_conn.cb_xprt = NULL;
  865. }
  866. if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
  867. return;
  868. spin_lock(&clp->cl_lock);
  869. /*
  870. * Only serialized callback code is allowed to clear these
  871. * flags; main nfsd code can only set them:
  872. */
  873. BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
  874. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  875. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  876. c = __nfsd4_find_backchannel(clp);
  877. if (c) {
  878. svc_xprt_get(c->cn_xprt);
  879. conn.cb_xprt = c->cn_xprt;
  880. ses = c->cn_session;
  881. }
  882. spin_unlock(&clp->cl_lock);
  883. err = setup_callback_client(clp, &conn, ses);
  884. if (err) {
  885. nfsd4_mark_cb_down(clp, err);
  886. return;
  887. }
  888. /* Yay, the callback channel's back! Restart any callbacks: */
  889. list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
  890. run_nfsd4_cb(cb);
  891. }
  892. static void nfsd4_do_callback_rpc(struct work_struct *w)
  893. {
  894. struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
  895. struct nfs4_client *clp = cb->cb_clp;
  896. struct rpc_clnt *clnt;
  897. if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
  898. nfsd4_process_cb_update(cb);
  899. clnt = clp->cl_cb_client;
  900. if (!clnt) {
  901. /* Callback channel broken, or client killed; give up: */
  902. nfsd4_release_cb(cb);
  903. return;
  904. }
  905. cb->cb_msg.rpc_cred = clp->cl_cb_cred;
  906. rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  907. cb->cb_ops, cb);
  908. }
  909. void nfsd4_init_callback(struct nfsd4_callback *cb)
  910. {
  911. INIT_WORK(&cb->cb_work, nfsd4_do_callback_rpc);
  912. }
  913. void nfsd4_cb_recall(struct nfs4_delegation *dp)
  914. {
  915. struct nfsd4_callback *cb = &dp->dl_recall;
  916. struct nfs4_client *clp = dp->dl_stid.sc_client;
  917. dp->dl_retries = 1;
  918. cb->cb_op = dp;
  919. cb->cb_clp = clp;
  920. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
  921. cb->cb_msg.rpc_argp = cb;
  922. cb->cb_msg.rpc_resp = cb;
  923. cb->cb_ops = &nfsd4_cb_recall_ops;
  924. INIT_LIST_HEAD(&cb->cb_per_client);
  925. cb->cb_done = true;
  926. run_nfsd4_cb(&dp->dl_recall);
  927. }