nfs4callback.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  724. struct nfs4_client *clp = dp->dl_stid.sc_client;
  725. u32 minorversion = clp->cl_minorversion;
  726. cb->cb_minorversion = minorversion;
  727. if (minorversion) {
  728. if (!nfsd41_cb_get_slot(clp, task))
  729. return;
  730. }
  731. spin_lock(&clp->cl_lock);
  732. if (list_empty(&cb->cb_per_client)) {
  733. /* This is the first call, not a restart */
  734. cb->cb_done = false;
  735. list_add(&cb->cb_per_client, &clp->cl_callbacks);
  736. }
  737. spin_unlock(&clp->cl_lock);
  738. rpc_call_start(task);
  739. }
  740. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  741. {
  742. struct nfsd4_callback *cb = calldata;
  743. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  744. struct nfs4_client *clp = dp->dl_stid.sc_client;
  745. dprintk("%s: minorversion=%d\n", __func__,
  746. clp->cl_minorversion);
  747. if (clp->cl_minorversion) {
  748. /* No need for lock, access serialized in nfsd4_cb_prepare */
  749. ++clp->cl_cb_session->se_cb_seq_nr;
  750. clear_bit(0, &clp->cl_cb_slot_busy);
  751. rpc_wake_up_next(&clp->cl_cb_waitq);
  752. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  753. clp->cl_cb_session->se_cb_seq_nr);
  754. /* We're done looking into the sequence information */
  755. task->tk_msg.rpc_resp = NULL;
  756. }
  757. }
  758. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  759. {
  760. struct nfsd4_callback *cb = calldata;
  761. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  762. struct nfs4_client *clp = dp->dl_stid.sc_client;
  763. struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
  764. nfsd4_cb_done(task, calldata);
  765. if (current_rpc_client != task->tk_client) {
  766. /* We're shutting down or changing cl_cb_client; leave
  767. * it to nfsd4_process_cb_update to restart the call if
  768. * necessary. */
  769. return;
  770. }
  771. if (cb->cb_done)
  772. return;
  773. switch (task->tk_status) {
  774. case 0:
  775. cb->cb_done = true;
  776. return;
  777. case -EBADHANDLE:
  778. case -NFS4ERR_BAD_STATEID:
  779. /* Race: client probably got cb_recall
  780. * before open reply granting delegation */
  781. break;
  782. default:
  783. /* Network partition? */
  784. nfsd4_mark_cb_down(clp, task->tk_status);
  785. }
  786. if (dp->dl_retries--) {
  787. rpc_delay(task, 2*HZ);
  788. task->tk_status = 0;
  789. rpc_restart_call_prepare(task);
  790. return;
  791. }
  792. nfsd4_mark_cb_down(clp, task->tk_status);
  793. cb->cb_done = true;
  794. }
  795. static void nfsd4_cb_recall_release(void *calldata)
  796. {
  797. struct nfsd4_callback *cb = calldata;
  798. struct nfs4_client *clp = cb->cb_clp;
  799. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  800. if (cb->cb_done) {
  801. spin_lock(&clp->cl_lock);
  802. list_del(&cb->cb_per_client);
  803. spin_unlock(&clp->cl_lock);
  804. nfs4_put_delegation(dp);
  805. }
  806. }
  807. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  808. .rpc_call_prepare = nfsd4_cb_prepare,
  809. .rpc_call_done = nfsd4_cb_recall_done,
  810. .rpc_release = nfsd4_cb_recall_release,
  811. };
  812. int nfsd4_create_callback_queue(void)
  813. {
  814. callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
  815. if (!callback_wq)
  816. return -ENOMEM;
  817. return 0;
  818. }
  819. void nfsd4_destroy_callback_queue(void)
  820. {
  821. destroy_workqueue(callback_wq);
  822. }
  823. /* must be called under the state lock */
  824. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  825. {
  826. set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
  827. /*
  828. * Note this won't actually result in a null callback;
  829. * instead, nfsd4_do_callback_rpc() will detect the killed
  830. * client, destroy the rpc client, and stop:
  831. */
  832. do_probe_callback(clp);
  833. flush_workqueue(callback_wq);
  834. }
  835. static void nfsd4_release_cb(struct nfsd4_callback *cb)
  836. {
  837. if (cb->cb_ops->rpc_release)
  838. cb->cb_ops->rpc_release(cb);
  839. }
  840. /* requires cl_lock: */
  841. static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
  842. {
  843. struct nfsd4_session *s;
  844. struct nfsd4_conn *c;
  845. list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
  846. list_for_each_entry(c, &s->se_conns, cn_persession) {
  847. if (c->cn_flags & NFS4_CDFC4_BACK)
  848. return c;
  849. }
  850. }
  851. return NULL;
  852. }
  853. static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  854. {
  855. struct nfs4_cb_conn conn;
  856. struct nfs4_client *clp = cb->cb_clp;
  857. struct nfsd4_session *ses = NULL;
  858. struct nfsd4_conn *c;
  859. int err;
  860. /*
  861. * This is either an update, or the client dying; in either case,
  862. * kill the old client:
  863. */
  864. if (clp->cl_cb_client) {
  865. rpc_shutdown_client(clp->cl_cb_client);
  866. clp->cl_cb_client = NULL;
  867. put_rpccred(clp->cl_cb_cred);
  868. clp->cl_cb_cred = NULL;
  869. }
  870. if (clp->cl_cb_conn.cb_xprt) {
  871. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  872. clp->cl_cb_conn.cb_xprt = NULL;
  873. }
  874. if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
  875. return;
  876. spin_lock(&clp->cl_lock);
  877. /*
  878. * Only serialized callback code is allowed to clear these
  879. * flags; main nfsd code can only set them:
  880. */
  881. BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
  882. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  883. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  884. c = __nfsd4_find_backchannel(clp);
  885. if (c) {
  886. svc_xprt_get(c->cn_xprt);
  887. conn.cb_xprt = c->cn_xprt;
  888. ses = c->cn_session;
  889. }
  890. spin_unlock(&clp->cl_lock);
  891. err = setup_callback_client(clp, &conn, ses);
  892. if (err) {
  893. nfsd4_mark_cb_down(clp, err);
  894. return;
  895. }
  896. /* Yay, the callback channel's back! Restart any callbacks: */
  897. list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
  898. run_nfsd4_cb(cb);
  899. }
  900. static void nfsd4_do_callback_rpc(struct work_struct *w)
  901. {
  902. struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
  903. struct nfs4_client *clp = cb->cb_clp;
  904. struct rpc_clnt *clnt;
  905. if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
  906. nfsd4_process_cb_update(cb);
  907. clnt = clp->cl_cb_client;
  908. if (!clnt) {
  909. /* Callback channel broken, or client killed; give up: */
  910. nfsd4_release_cb(cb);
  911. return;
  912. }
  913. cb->cb_msg.rpc_cred = clp->cl_cb_cred;
  914. rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  915. cb->cb_ops, cb);
  916. }
  917. void nfsd4_init_callback(struct nfsd4_callback *cb)
  918. {
  919. INIT_WORK(&cb->cb_work, nfsd4_do_callback_rpc);
  920. }
  921. void nfsd4_cb_recall(struct nfs4_delegation *dp)
  922. {
  923. struct nfsd4_callback *cb = &dp->dl_recall;
  924. struct nfs4_client *clp = dp->dl_stid.sc_client;
  925. dp->dl_retries = 1;
  926. cb->cb_op = dp;
  927. cb->cb_clp = clp;
  928. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
  929. cb->cb_msg.rpc_argp = cb;
  930. cb->cb_msg.rpc_resp = cb;
  931. cb->cb_ops = &nfsd4_cb_recall_ops;
  932. INIT_LIST_HEAD(&cb->cb_per_client);
  933. cb->cb_done = true;
  934. run_nfsd4_cb(&dp->dl_recall);
  935. }