nfs4callback.c 26 KB

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