nfs4callback.c 26 KB

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