nfs4callback.c 26 KB

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