nfs4callback.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393
  1. /*
  2. * Copyright (c) 2001 The Regents of the University of Michigan.
  3. * All rights reserved.
  4. *
  5. * Kendrick Smith <[email protected]>
  6. * Andy Adamson <[email protected]>
  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 "trace.h"
  41. #include "xdr4cb.h"
  42. #include "xdr4.h"
  43. #define NFSDDBG_FACILITY NFSDDBG_PROC
  44. static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
  45. #define NFSPROC4_CB_NULL 0
  46. #define NFSPROC4_CB_COMPOUND 1
  47. /* Index of predefined Linux callback client operations */
  48. struct nfs4_cb_compound_hdr {
  49. /* args */
  50. u32 ident; /* minorversion 0 only */
  51. u32 nops;
  52. __be32 *nops_p;
  53. u32 minorversion;
  54. /* res */
  55. int status;
  56. };
  57. static __be32 *xdr_encode_empty_array(__be32 *p)
  58. {
  59. *p++ = xdr_zero;
  60. return p;
  61. }
  62. /*
  63. * Encode/decode NFSv4 CB basic data types
  64. *
  65. * Basic NFSv4 callback data types are defined in section 15 of RFC
  66. * 3530: "Network File System (NFS) version 4 Protocol" and section
  67. * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
  68. * 1 Protocol"
  69. */
  70. /*
  71. * nfs_cb_opnum4
  72. *
  73. * enum nfs_cb_opnum4 {
  74. * OP_CB_GETATTR = 3,
  75. * ...
  76. * };
  77. */
  78. enum nfs_cb_opnum4 {
  79. OP_CB_GETATTR = 3,
  80. OP_CB_RECALL = 4,
  81. OP_CB_LAYOUTRECALL = 5,
  82. OP_CB_NOTIFY = 6,
  83. OP_CB_PUSH_DELEG = 7,
  84. OP_CB_RECALL_ANY = 8,
  85. OP_CB_RECALLABLE_OBJ_AVAIL = 9,
  86. OP_CB_RECALL_SLOT = 10,
  87. OP_CB_SEQUENCE = 11,
  88. OP_CB_WANTS_CANCELLED = 12,
  89. OP_CB_NOTIFY_LOCK = 13,
  90. OP_CB_NOTIFY_DEVICEID = 14,
  91. OP_CB_OFFLOAD = 15,
  92. OP_CB_ILLEGAL = 10044
  93. };
  94. static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
  95. {
  96. __be32 *p;
  97. p = xdr_reserve_space(xdr, 4);
  98. *p = cpu_to_be32(op);
  99. }
  100. /*
  101. * nfs_fh4
  102. *
  103. * typedef opaque nfs_fh4<NFS4_FHSIZE>;
  104. */
  105. static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
  106. {
  107. u32 length = fh->fh_size;
  108. __be32 *p;
  109. BUG_ON(length > NFS4_FHSIZE);
  110. p = xdr_reserve_space(xdr, 4 + length);
  111. xdr_encode_opaque(p, &fh->fh_raw, length);
  112. }
  113. /*
  114. * stateid4
  115. *
  116. * struct stateid4 {
  117. * uint32_t seqid;
  118. * opaque other[12];
  119. * };
  120. */
  121. static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
  122. {
  123. __be32 *p;
  124. p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
  125. *p++ = cpu_to_be32(sid->si_generation);
  126. xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
  127. }
  128. /*
  129. * sessionid4
  130. *
  131. * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
  132. */
  133. static void encode_sessionid4(struct xdr_stream *xdr,
  134. const struct nfsd4_session *session)
  135. {
  136. __be32 *p;
  137. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
  138. xdr_encode_opaque_fixed(p, session->se_sessionid.data,
  139. NFS4_MAX_SESSIONID_LEN);
  140. }
  141. /*
  142. * nfsstat4
  143. */
  144. static const struct {
  145. int stat;
  146. int errno;
  147. } nfs_cb_errtbl[] = {
  148. { NFS4_OK, 0 },
  149. { NFS4ERR_PERM, -EPERM },
  150. { NFS4ERR_NOENT, -ENOENT },
  151. { NFS4ERR_IO, -EIO },
  152. { NFS4ERR_NXIO, -ENXIO },
  153. { NFS4ERR_ACCESS, -EACCES },
  154. { NFS4ERR_EXIST, -EEXIST },
  155. { NFS4ERR_XDEV, -EXDEV },
  156. { NFS4ERR_NOTDIR, -ENOTDIR },
  157. { NFS4ERR_ISDIR, -EISDIR },
  158. { NFS4ERR_INVAL, -EINVAL },
  159. { NFS4ERR_FBIG, -EFBIG },
  160. { NFS4ERR_NOSPC, -ENOSPC },
  161. { NFS4ERR_ROFS, -EROFS },
  162. { NFS4ERR_MLINK, -EMLINK },
  163. { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
  164. { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
  165. { NFS4ERR_DQUOT, -EDQUOT },
  166. { NFS4ERR_STALE, -ESTALE },
  167. { NFS4ERR_BADHANDLE, -EBADHANDLE },
  168. { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
  169. { NFS4ERR_NOTSUPP, -ENOTSUPP },
  170. { NFS4ERR_TOOSMALL, -ETOOSMALL },
  171. { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
  172. { NFS4ERR_BADTYPE, -EBADTYPE },
  173. { NFS4ERR_LOCKED, -EAGAIN },
  174. { NFS4ERR_RESOURCE, -EREMOTEIO },
  175. { NFS4ERR_SYMLINK, -ELOOP },
  176. { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
  177. { NFS4ERR_DEADLOCK, -EDEADLK },
  178. { -1, -EIO }
  179. };
  180. /*
  181. * If we cannot translate the error, the recovery routines should
  182. * handle it.
  183. *
  184. * Note: remaining NFSv4 error codes have values > 10000, so should
  185. * not conflict with native Linux error codes.
  186. */
  187. static int nfs_cb_stat_to_errno(int status)
  188. {
  189. int i;
  190. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  191. if (nfs_cb_errtbl[i].stat == status)
  192. return nfs_cb_errtbl[i].errno;
  193. }
  194. dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
  195. return -status;
  196. }
  197. static int decode_cb_op_status(struct xdr_stream *xdr,
  198. enum nfs_cb_opnum4 expected, int *status)
  199. {
  200. __be32 *p;
  201. u32 op;
  202. p = xdr_inline_decode(xdr, 4 + 4);
  203. if (unlikely(p == NULL))
  204. goto out_overflow;
  205. op = be32_to_cpup(p++);
  206. if (unlikely(op != expected))
  207. goto out_unexpected;
  208. *status = nfs_cb_stat_to_errno(be32_to_cpup(p));
  209. return 0;
  210. out_overflow:
  211. return -EIO;
  212. out_unexpected:
  213. dprintk("NFSD: Callback server returned operation %d but "
  214. "we issued a request for %d\n", op, expected);
  215. return -EIO;
  216. }
  217. /*
  218. * CB_COMPOUND4args
  219. *
  220. * struct CB_COMPOUND4args {
  221. * utf8str_cs tag;
  222. * uint32_t minorversion;
  223. * uint32_t callback_ident;
  224. * nfs_cb_argop4 argarray<>;
  225. * };
  226. */
  227. static void encode_cb_compound4args(struct xdr_stream *xdr,
  228. struct nfs4_cb_compound_hdr *hdr)
  229. {
  230. __be32 * p;
  231. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
  232. p = xdr_encode_empty_array(p); /* empty tag */
  233. *p++ = cpu_to_be32(hdr->minorversion);
  234. *p++ = cpu_to_be32(hdr->ident);
  235. hdr->nops_p = p;
  236. *p = cpu_to_be32(hdr->nops); /* argarray element count */
  237. }
  238. /*
  239. * Update argarray element count
  240. */
  241. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  242. {
  243. BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
  244. *hdr->nops_p = cpu_to_be32(hdr->nops);
  245. }
  246. /*
  247. * CB_COMPOUND4res
  248. *
  249. * struct CB_COMPOUND4res {
  250. * nfsstat4 status;
  251. * utf8str_cs tag;
  252. * nfs_cb_resop4 resarray<>;
  253. * };
  254. */
  255. static int decode_cb_compound4res(struct xdr_stream *xdr,
  256. struct nfs4_cb_compound_hdr *hdr)
  257. {
  258. u32 length;
  259. __be32 *p;
  260. p = xdr_inline_decode(xdr, 4 + 4);
  261. if (unlikely(p == NULL))
  262. goto out_overflow;
  263. hdr->status = be32_to_cpup(p++);
  264. /* Ignore the tag */
  265. length = be32_to_cpup(p++);
  266. p = xdr_inline_decode(xdr, length + 4);
  267. if (unlikely(p == NULL))
  268. goto out_overflow;
  269. p += XDR_QUADLEN(length);
  270. hdr->nops = be32_to_cpup(p);
  271. return 0;
  272. out_overflow:
  273. return -EIO;
  274. }
  275. /*
  276. * CB_RECALL4args
  277. *
  278. * struct CB_RECALL4args {
  279. * stateid4 stateid;
  280. * bool truncate;
  281. * nfs_fh4 fh;
  282. * };
  283. */
  284. static void encode_cb_recall4args(struct xdr_stream *xdr,
  285. const struct nfs4_delegation *dp,
  286. struct nfs4_cb_compound_hdr *hdr)
  287. {
  288. __be32 *p;
  289. encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
  290. encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
  291. p = xdr_reserve_space(xdr, 4);
  292. *p++ = xdr_zero; /* truncate */
  293. encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
  294. hdr->nops++;
  295. }
  296. /*
  297. * CB_SEQUENCE4args
  298. *
  299. * struct CB_SEQUENCE4args {
  300. * sessionid4 csa_sessionid;
  301. * sequenceid4 csa_sequenceid;
  302. * slotid4 csa_slotid;
  303. * slotid4 csa_highest_slotid;
  304. * bool csa_cachethis;
  305. * referring_call_list4 csa_referring_call_lists<>;
  306. * };
  307. */
  308. static void encode_cb_sequence4args(struct xdr_stream *xdr,
  309. const struct nfsd4_callback *cb,
  310. struct nfs4_cb_compound_hdr *hdr)
  311. {
  312. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  313. __be32 *p;
  314. if (hdr->minorversion == 0)
  315. return;
  316. encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
  317. encode_sessionid4(xdr, session);
  318. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
  319. *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
  320. *p++ = xdr_zero; /* csa_slotid */
  321. *p++ = xdr_zero; /* csa_highest_slotid */
  322. *p++ = xdr_zero; /* csa_cachethis */
  323. xdr_encode_empty_array(p); /* csa_referring_call_lists */
  324. hdr->nops++;
  325. }
  326. /*
  327. * CB_SEQUENCE4resok
  328. *
  329. * struct CB_SEQUENCE4resok {
  330. * sessionid4 csr_sessionid;
  331. * sequenceid4 csr_sequenceid;
  332. * slotid4 csr_slotid;
  333. * slotid4 csr_highest_slotid;
  334. * slotid4 csr_target_highest_slotid;
  335. * };
  336. *
  337. * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
  338. * case NFS4_OK:
  339. * CB_SEQUENCE4resok csr_resok4;
  340. * default:
  341. * void;
  342. * };
  343. *
  344. * Our current back channel implmentation supports a single backchannel
  345. * with a single slot.
  346. */
  347. static int decode_cb_sequence4resok(struct xdr_stream *xdr,
  348. struct nfsd4_callback *cb)
  349. {
  350. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  351. int status = -ESERVERFAULT;
  352. __be32 *p;
  353. u32 dummy;
  354. /*
  355. * If the server returns different values for sessionID, slotID or
  356. * sequence number, the server is looney tunes.
  357. */
  358. p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
  359. if (unlikely(p == NULL))
  360. goto out_overflow;
  361. if (memcmp(p, session->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
  362. dprintk("NFS: %s Invalid session id\n", __func__);
  363. goto out;
  364. }
  365. p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
  366. dummy = be32_to_cpup(p++);
  367. if (dummy != session->se_cb_seq_nr) {
  368. dprintk("NFS: %s Invalid sequence number\n", __func__);
  369. goto out;
  370. }
  371. dummy = be32_to_cpup(p++);
  372. if (dummy != 0) {
  373. dprintk("NFS: %s Invalid slotid\n", __func__);
  374. goto out;
  375. }
  376. /*
  377. * FIXME: process highest slotid and target highest slotid
  378. */
  379. status = 0;
  380. out:
  381. cb->cb_seq_status = status;
  382. return status;
  383. out_overflow:
  384. status = -EIO;
  385. goto out;
  386. }
  387. static int decode_cb_sequence4res(struct xdr_stream *xdr,
  388. struct nfsd4_callback *cb)
  389. {
  390. int status;
  391. if (cb->cb_clp->cl_minorversion == 0)
  392. return 0;
  393. status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &cb->cb_seq_status);
  394. if (unlikely(status || cb->cb_seq_status))
  395. return status;
  396. return decode_cb_sequence4resok(xdr, cb);
  397. }
  398. /*
  399. * NFSv4.0 and NFSv4.1 XDR encode functions
  400. *
  401. * NFSv4.0 callback argument types are defined in section 15 of RFC
  402. * 3530: "Network File System (NFS) version 4 Protocol" and section 20
  403. * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
  404. * Protocol".
  405. */
  406. /*
  407. * NB: Without this zero space reservation, callbacks over krb5p fail
  408. */
  409. static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
  410. const void *__unused)
  411. {
  412. xdr_reserve_space(xdr, 0);
  413. }
  414. /*
  415. * 20.2. Operation 4: CB_RECALL - Recall a Delegation
  416. */
  417. static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
  418. const void *data)
  419. {
  420. const struct nfsd4_callback *cb = data;
  421. const struct nfs4_delegation *dp = cb_to_delegation(cb);
  422. struct nfs4_cb_compound_hdr hdr = {
  423. .ident = cb->cb_clp->cl_cb_ident,
  424. .minorversion = cb->cb_clp->cl_minorversion,
  425. };
  426. encode_cb_compound4args(xdr, &hdr);
  427. encode_cb_sequence4args(xdr, cb, &hdr);
  428. encode_cb_recall4args(xdr, dp, &hdr);
  429. encode_cb_nops(&hdr);
  430. }
  431. /*
  432. * NFSv4.0 and NFSv4.1 XDR decode functions
  433. *
  434. * NFSv4.0 callback result types are defined in section 15 of RFC
  435. * 3530: "Network File System (NFS) version 4 Protocol" and section 20
  436. * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
  437. * Protocol".
  438. */
  439. static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
  440. void *__unused)
  441. {
  442. return 0;
  443. }
  444. /*
  445. * 20.2. Operation 4: CB_RECALL - Recall a Delegation
  446. */
  447. static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
  448. struct xdr_stream *xdr,
  449. void *data)
  450. {
  451. struct nfsd4_callback *cb = data;
  452. struct nfs4_cb_compound_hdr hdr;
  453. int status;
  454. status = decode_cb_compound4res(xdr, &hdr);
  455. if (unlikely(status))
  456. return status;
  457. status = decode_cb_sequence4res(xdr, cb);
  458. if (unlikely(status || cb->cb_seq_status))
  459. return status;
  460. return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
  461. }
  462. #ifdef CONFIG_NFSD_PNFS
  463. /*
  464. * CB_LAYOUTRECALL4args
  465. *
  466. * struct layoutrecall_file4 {
  467. * nfs_fh4 lor_fh;
  468. * offset4 lor_offset;
  469. * length4 lor_length;
  470. * stateid4 lor_stateid;
  471. * };
  472. *
  473. * union layoutrecall4 switch(layoutrecall_type4 lor_recalltype) {
  474. * case LAYOUTRECALL4_FILE:
  475. * layoutrecall_file4 lor_layout;
  476. * case LAYOUTRECALL4_FSID:
  477. * fsid4 lor_fsid;
  478. * case LAYOUTRECALL4_ALL:
  479. * void;
  480. * };
  481. *
  482. * struct CB_LAYOUTRECALL4args {
  483. * layouttype4 clora_type;
  484. * layoutiomode4 clora_iomode;
  485. * bool clora_changed;
  486. * layoutrecall4 clora_recall;
  487. * };
  488. */
  489. static void encode_cb_layout4args(struct xdr_stream *xdr,
  490. const struct nfs4_layout_stateid *ls,
  491. struct nfs4_cb_compound_hdr *hdr)
  492. {
  493. __be32 *p;
  494. BUG_ON(hdr->minorversion == 0);
  495. p = xdr_reserve_space(xdr, 5 * 4);
  496. *p++ = cpu_to_be32(OP_CB_LAYOUTRECALL);
  497. *p++ = cpu_to_be32(ls->ls_layout_type);
  498. *p++ = cpu_to_be32(IOMODE_ANY);
  499. *p++ = cpu_to_be32(1);
  500. *p = cpu_to_be32(RETURN_FILE);
  501. encode_nfs_fh4(xdr, &ls->ls_stid.sc_file->fi_fhandle);
  502. p = xdr_reserve_space(xdr, 2 * 8);
  503. p = xdr_encode_hyper(p, 0);
  504. xdr_encode_hyper(p, NFS4_MAX_UINT64);
  505. encode_stateid4(xdr, &ls->ls_recall_sid);
  506. hdr->nops++;
  507. }
  508. static void nfs4_xdr_enc_cb_layout(struct rpc_rqst *req,
  509. struct xdr_stream *xdr,
  510. const void *data)
  511. {
  512. const struct nfsd4_callback *cb = data;
  513. const struct nfs4_layout_stateid *ls =
  514. container_of(cb, struct nfs4_layout_stateid, ls_recall);
  515. struct nfs4_cb_compound_hdr hdr = {
  516. .ident = 0,
  517. .minorversion = cb->cb_clp->cl_minorversion,
  518. };
  519. encode_cb_compound4args(xdr, &hdr);
  520. encode_cb_sequence4args(xdr, cb, &hdr);
  521. encode_cb_layout4args(xdr, ls, &hdr);
  522. encode_cb_nops(&hdr);
  523. }
  524. static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
  525. struct xdr_stream *xdr,
  526. void *data)
  527. {
  528. struct nfsd4_callback *cb = data;
  529. struct nfs4_cb_compound_hdr hdr;
  530. int status;
  531. status = decode_cb_compound4res(xdr, &hdr);
  532. if (unlikely(status))
  533. return status;
  534. status = decode_cb_sequence4res(xdr, cb);
  535. if (unlikely(status || cb->cb_seq_status))
  536. return status;
  537. return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
  538. }
  539. #endif /* CONFIG_NFSD_PNFS */
  540. static void encode_stateowner(struct xdr_stream *xdr, struct nfs4_stateowner *so)
  541. {
  542. __be32 *p;
  543. p = xdr_reserve_space(xdr, 8 + 4 + so->so_owner.len);
  544. p = xdr_encode_opaque_fixed(p, &so->so_client->cl_clientid, 8);
  545. xdr_encode_opaque(p, so->so_owner.data, so->so_owner.len);
  546. }
  547. static void nfs4_xdr_enc_cb_notify_lock(struct rpc_rqst *req,
  548. struct xdr_stream *xdr,
  549. const void *data)
  550. {
  551. const struct nfsd4_callback *cb = data;
  552. const struct nfsd4_blocked_lock *nbl =
  553. container_of(cb, struct nfsd4_blocked_lock, nbl_cb);
  554. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)nbl->nbl_lock.fl_owner;
  555. struct nfs4_cb_compound_hdr hdr = {
  556. .ident = 0,
  557. .minorversion = cb->cb_clp->cl_minorversion,
  558. };
  559. __be32 *p;
  560. BUG_ON(hdr.minorversion == 0);
  561. encode_cb_compound4args(xdr, &hdr);
  562. encode_cb_sequence4args(xdr, cb, &hdr);
  563. p = xdr_reserve_space(xdr, 4);
  564. *p = cpu_to_be32(OP_CB_NOTIFY_LOCK);
  565. encode_nfs_fh4(xdr, &nbl->nbl_fh);
  566. encode_stateowner(xdr, &lo->lo_owner);
  567. hdr.nops++;
  568. encode_cb_nops(&hdr);
  569. }
  570. static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
  571. struct xdr_stream *xdr,
  572. void *data)
  573. {
  574. struct nfsd4_callback *cb = data;
  575. struct nfs4_cb_compound_hdr hdr;
  576. int status;
  577. status = decode_cb_compound4res(xdr, &hdr);
  578. if (unlikely(status))
  579. return status;
  580. status = decode_cb_sequence4res(xdr, cb);
  581. if (unlikely(status || cb->cb_seq_status))
  582. return status;
  583. return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
  584. }
  585. /*
  586. * struct write_response4 {
  587. * stateid4 wr_callback_id<1>;
  588. * length4 wr_count;
  589. * stable_how4 wr_committed;
  590. * verifier4 wr_writeverf;
  591. * };
  592. * union offload_info4 switch (nfsstat4 coa_status) {
  593. * case NFS4_OK:
  594. * write_response4 coa_resok4;
  595. * default:
  596. * length4 coa_bytes_copied;
  597. * };
  598. * struct CB_OFFLOAD4args {
  599. * nfs_fh4 coa_fh;
  600. * stateid4 coa_stateid;
  601. * offload_info4 coa_offload_info;
  602. * };
  603. */
  604. static void encode_offload_info4(struct xdr_stream *xdr,
  605. const struct nfsd4_cb_offload *cbo)
  606. {
  607. __be32 *p;
  608. p = xdr_reserve_space(xdr, 4);
  609. *p = cbo->co_nfserr;
  610. switch (cbo->co_nfserr) {
  611. case nfs_ok:
  612. p = xdr_reserve_space(xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE);
  613. p = xdr_encode_empty_array(p);
  614. p = xdr_encode_hyper(p, cbo->co_res.wr_bytes_written);
  615. *p++ = cpu_to_be32(cbo->co_res.wr_stable_how);
  616. p = xdr_encode_opaque_fixed(p, cbo->co_res.wr_verifier.data,
  617. NFS4_VERIFIER_SIZE);
  618. break;
  619. default:
  620. p = xdr_reserve_space(xdr, 8);
  621. /* We always return success if bytes were written */
  622. p = xdr_encode_hyper(p, 0);
  623. }
  624. }
  625. static void encode_cb_offload4args(struct xdr_stream *xdr,
  626. const struct nfsd4_cb_offload *cbo,
  627. struct nfs4_cb_compound_hdr *hdr)
  628. {
  629. __be32 *p;
  630. p = xdr_reserve_space(xdr, 4);
  631. *p = cpu_to_be32(OP_CB_OFFLOAD);
  632. encode_nfs_fh4(xdr, &cbo->co_fh);
  633. encode_stateid4(xdr, &cbo->co_res.cb_stateid);
  634. encode_offload_info4(xdr, cbo);
  635. hdr->nops++;
  636. }
  637. static void nfs4_xdr_enc_cb_offload(struct rpc_rqst *req,
  638. struct xdr_stream *xdr,
  639. const void *data)
  640. {
  641. const struct nfsd4_callback *cb = data;
  642. const struct nfsd4_cb_offload *cbo =
  643. container_of(cb, struct nfsd4_cb_offload, co_cb);
  644. struct nfs4_cb_compound_hdr hdr = {
  645. .ident = 0,
  646. .minorversion = cb->cb_clp->cl_minorversion,
  647. };
  648. encode_cb_compound4args(xdr, &hdr);
  649. encode_cb_sequence4args(xdr, cb, &hdr);
  650. encode_cb_offload4args(xdr, cbo, &hdr);
  651. encode_cb_nops(&hdr);
  652. }
  653. static int nfs4_xdr_dec_cb_offload(struct rpc_rqst *rqstp,
  654. struct xdr_stream *xdr,
  655. void *data)
  656. {
  657. struct nfsd4_callback *cb = data;
  658. struct nfs4_cb_compound_hdr hdr;
  659. int status;
  660. status = decode_cb_compound4res(xdr, &hdr);
  661. if (unlikely(status))
  662. return status;
  663. status = decode_cb_sequence4res(xdr, cb);
  664. if (unlikely(status || cb->cb_seq_status))
  665. return status;
  666. return decode_cb_op_status(xdr, OP_CB_OFFLOAD, &cb->cb_status);
  667. }
  668. /*
  669. * RPC procedure tables
  670. */
  671. #define PROC(proc, call, argtype, restype) \
  672. [NFSPROC4_CLNT_##proc] = { \
  673. .p_proc = NFSPROC4_CB_##call, \
  674. .p_encode = nfs4_xdr_enc_##argtype, \
  675. .p_decode = nfs4_xdr_dec_##restype, \
  676. .p_arglen = NFS4_enc_##argtype##_sz, \
  677. .p_replen = NFS4_dec_##restype##_sz, \
  678. .p_statidx = NFSPROC4_CB_##call, \
  679. .p_name = #proc, \
  680. }
  681. static const struct rpc_procinfo nfs4_cb_procedures[] = {
  682. PROC(CB_NULL, NULL, cb_null, cb_null),
  683. PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
  684. #ifdef CONFIG_NFSD_PNFS
  685. PROC(CB_LAYOUT, COMPOUND, cb_layout, cb_layout),
  686. #endif
  687. PROC(CB_NOTIFY_LOCK, COMPOUND, cb_notify_lock, cb_notify_lock),
  688. PROC(CB_OFFLOAD, COMPOUND, cb_offload, cb_offload),
  689. };
  690. static unsigned int nfs4_cb_counts[ARRAY_SIZE(nfs4_cb_procedures)];
  691. static const struct rpc_version nfs_cb_version4 = {
  692. /*
  693. * Note on the callback rpc program version number: despite language in rfc
  694. * 5661 section 18.36.3 requiring servers to use 4 in this field, the
  695. * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
  696. * in practice that appears to be what implementations use. The section
  697. * 18.36.3 language is expected to be fixed in an erratum.
  698. */
  699. .number = 1,
  700. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  701. .procs = nfs4_cb_procedures,
  702. .counts = nfs4_cb_counts,
  703. };
  704. static const struct rpc_version *nfs_cb_version[2] = {
  705. [1] = &nfs_cb_version4,
  706. };
  707. static const struct rpc_program cb_program;
  708. static struct rpc_stat cb_stats = {
  709. .program = &cb_program
  710. };
  711. #define NFS4_CALLBACK 0x40000000
  712. static const struct rpc_program cb_program = {
  713. .name = "nfs4_cb",
  714. .number = NFS4_CALLBACK,
  715. .nrvers = ARRAY_SIZE(nfs_cb_version),
  716. .version = nfs_cb_version,
  717. .stats = &cb_stats,
  718. .pipe_dir_name = "nfsd4_cb",
  719. };
  720. static int max_cb_time(struct net *net)
  721. {
  722. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  723. /*
  724. * nfsd4_lease is set to at most one hour in __nfsd4_write_time,
  725. * so we can use 32-bit math on it. Warn if that assumption
  726. * ever stops being true.
  727. */
  728. if (WARN_ON_ONCE(nn->nfsd4_lease > 3600))
  729. return 360 * HZ;
  730. return max(((u32)nn->nfsd4_lease)/10, 1u) * HZ;
  731. }
  732. static struct workqueue_struct *callback_wq;
  733. static bool nfsd4_queue_cb(struct nfsd4_callback *cb)
  734. {
  735. return queue_work(callback_wq, &cb->cb_work);
  736. }
  737. static void nfsd41_cb_inflight_begin(struct nfs4_client *clp)
  738. {
  739. atomic_inc(&clp->cl_cb_inflight);
  740. }
  741. static void nfsd41_cb_inflight_end(struct nfs4_client *clp)
  742. {
  743. if (atomic_dec_and_test(&clp->cl_cb_inflight))
  744. wake_up_var(&clp->cl_cb_inflight);
  745. }
  746. static void nfsd41_cb_inflight_wait_complete(struct nfs4_client *clp)
  747. {
  748. wait_var_event(&clp->cl_cb_inflight,
  749. !atomic_read(&clp->cl_cb_inflight));
  750. }
  751. static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
  752. {
  753. if (clp->cl_minorversion == 0) {
  754. client->cl_principal = clp->cl_cred.cr_targ_princ ?
  755. clp->cl_cred.cr_targ_princ : "nfs";
  756. return get_cred(rpc_machine_cred());
  757. } else {
  758. struct cred *kcred;
  759. kcred = prepare_kernel_cred(NULL);
  760. if (!kcred)
  761. return NULL;
  762. kcred->fsuid = ses->se_cb_sec.uid;
  763. kcred->fsgid = ses->se_cb_sec.gid;
  764. return kcred;
  765. }
  766. }
  767. static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
  768. {
  769. int maxtime = max_cb_time(clp->net);
  770. struct rpc_timeout timeparms = {
  771. .to_initval = maxtime,
  772. .to_retries = 0,
  773. .to_maxval = maxtime,
  774. };
  775. struct rpc_create_args args = {
  776. .net = clp->net,
  777. .address = (struct sockaddr *) &conn->cb_addr,
  778. .addrsize = conn->cb_addrlen,
  779. .saddress = (struct sockaddr *) &conn->cb_saddr,
  780. .timeout = &timeparms,
  781. .program = &cb_program,
  782. .version = 1,
  783. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  784. .cred = current_cred(),
  785. };
  786. struct rpc_clnt *client;
  787. const struct cred *cred;
  788. if (clp->cl_minorversion == 0) {
  789. if (!clp->cl_cred.cr_principal &&
  790. (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5)) {
  791. trace_nfsd_cb_setup_err(clp, -EINVAL);
  792. return -EINVAL;
  793. }
  794. args.client_name = clp->cl_cred.cr_principal;
  795. args.prognumber = conn->cb_prog;
  796. args.protocol = XPRT_TRANSPORT_TCP;
  797. args.authflavor = clp->cl_cred.cr_flavor;
  798. clp->cl_cb_ident = conn->cb_ident;
  799. } else {
  800. if (!conn->cb_xprt)
  801. return -EINVAL;
  802. clp->cl_cb_session = ses;
  803. args.bc_xprt = conn->cb_xprt;
  804. args.prognumber = clp->cl_cb_session->se_cb_prog;
  805. args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
  806. XPRT_TRANSPORT_BC;
  807. args.authflavor = ses->se_cb_sec.flavor;
  808. }
  809. /* Create RPC client */
  810. client = rpc_create(&args);
  811. if (IS_ERR(client)) {
  812. trace_nfsd_cb_setup_err(clp, PTR_ERR(client));
  813. return PTR_ERR(client);
  814. }
  815. cred = get_backchannel_cred(clp, client, ses);
  816. if (!cred) {
  817. trace_nfsd_cb_setup_err(clp, -ENOMEM);
  818. rpc_shutdown_client(client);
  819. return -ENOMEM;
  820. }
  821. if (clp->cl_minorversion != 0)
  822. clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
  823. clp->cl_cb_client = client;
  824. clp->cl_cb_cred = cred;
  825. rcu_read_lock();
  826. trace_nfsd_cb_setup(clp, rpc_peeraddr2str(client, RPC_DISPLAY_NETID),
  827. args.authflavor);
  828. rcu_read_unlock();
  829. return 0;
  830. }
  831. static void nfsd4_mark_cb_state(struct nfs4_client *clp, int newstate)
  832. {
  833. if (clp->cl_cb_state != newstate) {
  834. clp->cl_cb_state = newstate;
  835. trace_nfsd_cb_state(clp);
  836. }
  837. }
  838. static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
  839. {
  840. if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
  841. return;
  842. nfsd4_mark_cb_state(clp, NFSD4_CB_DOWN);
  843. }
  844. static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
  845. {
  846. if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
  847. return;
  848. nfsd4_mark_cb_state(clp, NFSD4_CB_FAULT);
  849. }
  850. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  851. {
  852. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  853. if (task->tk_status)
  854. nfsd4_mark_cb_down(clp, task->tk_status);
  855. else
  856. nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
  857. }
  858. static void nfsd4_cb_probe_release(void *calldata)
  859. {
  860. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  861. nfsd41_cb_inflight_end(clp);
  862. }
  863. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  864. /* XXX: release method to ensure we set the cb channel down if
  865. * necessary on early failure? */
  866. .rpc_call_done = nfsd4_cb_probe_done,
  867. .rpc_release = nfsd4_cb_probe_release,
  868. };
  869. /*
  870. * Poke the callback thread to process any updates to the callback
  871. * parameters, and send a null probe.
  872. */
  873. void nfsd4_probe_callback(struct nfs4_client *clp)
  874. {
  875. trace_nfsd_cb_probe(clp);
  876. nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
  877. set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  878. nfsd4_run_cb(&clp->cl_cb_null);
  879. }
  880. void nfsd4_probe_callback_sync(struct nfs4_client *clp)
  881. {
  882. nfsd4_probe_callback(clp);
  883. flush_workqueue(callback_wq);
  884. }
  885. void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  886. {
  887. nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
  888. spin_lock(&clp->cl_lock);
  889. memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
  890. spin_unlock(&clp->cl_lock);
  891. }
  892. /*
  893. * There's currently a single callback channel slot.
  894. * If the slot is available, then mark it busy. Otherwise, set the
  895. * thread for sleeping on the callback RPC wait queue.
  896. */
  897. static bool nfsd41_cb_get_slot(struct nfsd4_callback *cb, struct rpc_task *task)
  898. {
  899. struct nfs4_client *clp = cb->cb_clp;
  900. if (!cb->cb_holds_slot &&
  901. test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  902. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  903. /* Race breaker */
  904. if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  905. dprintk("%s slot is busy\n", __func__);
  906. return false;
  907. }
  908. rpc_wake_up_queued_task(&clp->cl_cb_waitq, task);
  909. }
  910. cb->cb_holds_slot = true;
  911. return true;
  912. }
  913. static void nfsd41_cb_release_slot(struct nfsd4_callback *cb)
  914. {
  915. struct nfs4_client *clp = cb->cb_clp;
  916. if (cb->cb_holds_slot) {
  917. cb->cb_holds_slot = false;
  918. clear_bit(0, &clp->cl_cb_slot_busy);
  919. rpc_wake_up_next(&clp->cl_cb_waitq);
  920. }
  921. }
  922. static void nfsd41_destroy_cb(struct nfsd4_callback *cb)
  923. {
  924. struct nfs4_client *clp = cb->cb_clp;
  925. nfsd41_cb_release_slot(cb);
  926. if (cb->cb_ops && cb->cb_ops->release)
  927. cb->cb_ops->release(cb);
  928. nfsd41_cb_inflight_end(clp);
  929. }
  930. /*
  931. * TODO: cb_sequence should support referring call lists, cachethis, multiple
  932. * slots, and mark callback channel down on communication errors.
  933. */
  934. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  935. {
  936. struct nfsd4_callback *cb = calldata;
  937. struct nfs4_client *clp = cb->cb_clp;
  938. u32 minorversion = clp->cl_minorversion;
  939. /*
  940. * cb_seq_status is only set in decode_cb_sequence4res,
  941. * and so will remain 1 if an rpc level failure occurs.
  942. */
  943. cb->cb_seq_status = 1;
  944. cb->cb_status = 0;
  945. if (minorversion && !nfsd41_cb_get_slot(cb, task))
  946. return;
  947. rpc_call_start(task);
  948. }
  949. static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
  950. {
  951. struct nfs4_client *clp = cb->cb_clp;
  952. struct nfsd4_session *session = clp->cl_cb_session;
  953. bool ret = true;
  954. if (!clp->cl_minorversion) {
  955. /*
  956. * If the backchannel connection was shut down while this
  957. * task was queued, we need to resubmit it after setting up
  958. * a new backchannel connection.
  959. *
  960. * Note that if we lost our callback connection permanently
  961. * the submission code will error out, so we don't need to
  962. * handle that case here.
  963. */
  964. if (RPC_SIGNALLED(task))
  965. goto need_restart;
  966. return true;
  967. }
  968. if (!cb->cb_holds_slot)
  969. goto need_restart;
  970. switch (cb->cb_seq_status) {
  971. case 0:
  972. /*
  973. * No need for lock, access serialized in nfsd4_cb_prepare
  974. *
  975. * RFC5661 20.9.3
  976. * If CB_SEQUENCE returns an error, then the state of the slot
  977. * (sequence ID, cached reply) MUST NOT change.
  978. */
  979. ++session->se_cb_seq_nr;
  980. break;
  981. case -ESERVERFAULT:
  982. ++session->se_cb_seq_nr;
  983. fallthrough;
  984. case 1:
  985. case -NFS4ERR_BADSESSION:
  986. nfsd4_mark_cb_fault(cb->cb_clp, cb->cb_seq_status);
  987. ret = false;
  988. break;
  989. case -NFS4ERR_DELAY:
  990. if (!rpc_restart_call(task))
  991. goto out;
  992. rpc_delay(task, 2 * HZ);
  993. return false;
  994. case -NFS4ERR_BADSLOT:
  995. goto retry_nowait;
  996. case -NFS4ERR_SEQ_MISORDERED:
  997. if (session->se_cb_seq_nr != 1) {
  998. session->se_cb_seq_nr = 1;
  999. goto retry_nowait;
  1000. }
  1001. break;
  1002. default:
  1003. nfsd4_mark_cb_fault(cb->cb_clp, cb->cb_seq_status);
  1004. dprintk("%s: unprocessed error %d\n", __func__,
  1005. cb->cb_seq_status);
  1006. }
  1007. nfsd41_cb_release_slot(cb);
  1008. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  1009. clp->cl_cb_session->se_cb_seq_nr);
  1010. if (RPC_SIGNALLED(task))
  1011. goto need_restart;
  1012. out:
  1013. return ret;
  1014. retry_nowait:
  1015. if (rpc_restart_call_prepare(task))
  1016. ret = false;
  1017. goto out;
  1018. need_restart:
  1019. if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
  1020. task->tk_status = 0;
  1021. cb->cb_need_restart = true;
  1022. }
  1023. return false;
  1024. }
  1025. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  1026. {
  1027. struct nfsd4_callback *cb = calldata;
  1028. struct nfs4_client *clp = cb->cb_clp;
  1029. if (!nfsd4_cb_sequence_done(task, cb))
  1030. return;
  1031. if (cb->cb_status) {
  1032. WARN_ON_ONCE(task->tk_status);
  1033. task->tk_status = cb->cb_status;
  1034. }
  1035. switch (cb->cb_ops->done(cb, task)) {
  1036. case 0:
  1037. task->tk_status = 0;
  1038. rpc_restart_call_prepare(task);
  1039. return;
  1040. case 1:
  1041. switch (task->tk_status) {
  1042. case -EIO:
  1043. case -ETIMEDOUT:
  1044. case -EACCES:
  1045. nfsd4_mark_cb_down(clp, task->tk_status);
  1046. }
  1047. break;
  1048. default:
  1049. BUG();
  1050. }
  1051. }
  1052. static void nfsd4_cb_release(void *calldata)
  1053. {
  1054. struct nfsd4_callback *cb = calldata;
  1055. if (cb->cb_need_restart)
  1056. nfsd4_queue_cb(cb);
  1057. else
  1058. nfsd41_destroy_cb(cb);
  1059. }
  1060. static const struct rpc_call_ops nfsd4_cb_ops = {
  1061. .rpc_call_prepare = nfsd4_cb_prepare,
  1062. .rpc_call_done = nfsd4_cb_done,
  1063. .rpc_release = nfsd4_cb_release,
  1064. };
  1065. int nfsd4_create_callback_queue(void)
  1066. {
  1067. callback_wq = alloc_ordered_workqueue("nfsd4_callbacks", 0);
  1068. if (!callback_wq)
  1069. return -ENOMEM;
  1070. return 0;
  1071. }
  1072. void nfsd4_destroy_callback_queue(void)
  1073. {
  1074. destroy_workqueue(callback_wq);
  1075. }
  1076. /* must be called under the state lock */
  1077. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  1078. {
  1079. if (clp->cl_cb_state != NFSD4_CB_UNKNOWN)
  1080. trace_nfsd_cb_shutdown(clp);
  1081. set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
  1082. /*
  1083. * Note this won't actually result in a null callback;
  1084. * instead, nfsd4_run_cb_null() will detect the killed
  1085. * client, destroy the rpc client, and stop:
  1086. */
  1087. nfsd4_run_cb(&clp->cl_cb_null);
  1088. flush_workqueue(callback_wq);
  1089. nfsd41_cb_inflight_wait_complete(clp);
  1090. }
  1091. /* requires cl_lock: */
  1092. static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
  1093. {
  1094. struct nfsd4_session *s;
  1095. struct nfsd4_conn *c;
  1096. list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
  1097. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1098. if (c->cn_flags & NFS4_CDFC4_BACK)
  1099. return c;
  1100. }
  1101. }
  1102. return NULL;
  1103. }
  1104. /*
  1105. * Note there isn't a lot of locking in this code; instead we depend on
  1106. * the fact that it is run from the callback_wq, which won't run two
  1107. * work items at once. So, for example, callback_wq handles all access
  1108. * of cl_cb_client and all calls to rpc_create or rpc_shutdown_client.
  1109. */
  1110. static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  1111. {
  1112. struct nfs4_cb_conn conn;
  1113. struct nfs4_client *clp = cb->cb_clp;
  1114. struct nfsd4_session *ses = NULL;
  1115. struct nfsd4_conn *c;
  1116. int err;
  1117. /*
  1118. * This is either an update, or the client dying; in either case,
  1119. * kill the old client:
  1120. */
  1121. if (clp->cl_cb_client) {
  1122. rpc_shutdown_client(clp->cl_cb_client);
  1123. clp->cl_cb_client = NULL;
  1124. put_cred(clp->cl_cb_cred);
  1125. clp->cl_cb_cred = NULL;
  1126. }
  1127. if (clp->cl_cb_conn.cb_xprt) {
  1128. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  1129. clp->cl_cb_conn.cb_xprt = NULL;
  1130. }
  1131. if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
  1132. return;
  1133. spin_lock(&clp->cl_lock);
  1134. /*
  1135. * Only serialized callback code is allowed to clear these
  1136. * flags; main nfsd code can only set them:
  1137. */
  1138. BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
  1139. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  1140. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  1141. c = __nfsd4_find_backchannel(clp);
  1142. if (c) {
  1143. svc_xprt_get(c->cn_xprt);
  1144. conn.cb_xprt = c->cn_xprt;
  1145. ses = c->cn_session;
  1146. }
  1147. spin_unlock(&clp->cl_lock);
  1148. err = setup_callback_client(clp, &conn, ses);
  1149. if (err) {
  1150. nfsd4_mark_cb_down(clp, err);
  1151. if (c)
  1152. svc_xprt_put(c->cn_xprt);
  1153. return;
  1154. }
  1155. }
  1156. static void
  1157. nfsd4_run_cb_work(struct work_struct *work)
  1158. {
  1159. struct nfsd4_callback *cb =
  1160. container_of(work, struct nfsd4_callback, cb_work);
  1161. struct nfs4_client *clp = cb->cb_clp;
  1162. struct rpc_clnt *clnt;
  1163. int flags;
  1164. if (cb->cb_need_restart) {
  1165. cb->cb_need_restart = false;
  1166. } else {
  1167. if (cb->cb_ops && cb->cb_ops->prepare)
  1168. cb->cb_ops->prepare(cb);
  1169. }
  1170. if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
  1171. nfsd4_process_cb_update(cb);
  1172. clnt = clp->cl_cb_client;
  1173. if (!clnt) {
  1174. /* Callback channel broken, or client killed; give up: */
  1175. nfsd41_destroy_cb(cb);
  1176. return;
  1177. }
  1178. /*
  1179. * Don't send probe messages for 4.1 or later.
  1180. */
  1181. if (!cb->cb_ops && clp->cl_minorversion) {
  1182. nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
  1183. nfsd41_destroy_cb(cb);
  1184. return;
  1185. }
  1186. cb->cb_msg.rpc_cred = clp->cl_cb_cred;
  1187. flags = clp->cl_minorversion ? RPC_TASK_NOCONNECT : RPC_TASK_SOFTCONN;
  1188. rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | flags,
  1189. cb->cb_ops ? &nfsd4_cb_ops : &nfsd4_cb_probe_ops, cb);
  1190. }
  1191. void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
  1192. const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op)
  1193. {
  1194. cb->cb_clp = clp;
  1195. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
  1196. cb->cb_msg.rpc_argp = cb;
  1197. cb->cb_msg.rpc_resp = cb;
  1198. cb->cb_ops = ops;
  1199. INIT_WORK(&cb->cb_work, nfsd4_run_cb_work);
  1200. cb->cb_seq_status = 1;
  1201. cb->cb_status = 0;
  1202. cb->cb_need_restart = false;
  1203. cb->cb_holds_slot = false;
  1204. }
  1205. /**
  1206. * nfsd4_run_cb - queue up a callback job to run
  1207. * @cb: callback to queue
  1208. *
  1209. * Kick off a callback to do its thing. Returns false if it was already
  1210. * on a queue, true otherwise.
  1211. */
  1212. bool nfsd4_run_cb(struct nfsd4_callback *cb)
  1213. {
  1214. struct nfs4_client *clp = cb->cb_clp;
  1215. bool queued;
  1216. nfsd41_cb_inflight_begin(clp);
  1217. queued = nfsd4_queue_cb(cb);
  1218. if (!queued)
  1219. nfsd41_cb_inflight_end(clp);
  1220. return queued;
  1221. }