nfs4callback.c 26 KB

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