nfs4callback.c 26 KB

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