nfs4callback.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788
  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/nfs4.h>
  34. #include <linux/sunrpc/clnt.h>
  35. #include <linux/sunrpc/xprt.h>
  36. #include <linux/sunrpc/svc_xprt.h>
  37. #include <linux/slab.h>
  38. #include "nfsd.h"
  39. #include "state.h"
  40. #include "netns.h"
  41. #include "trace.h"
  42. #include "xdr4cb.h"
  43. #include "xdr4.h"
  44. #include "nfs4xdr_gen.h"
  45. #define NFSDDBG_FACILITY NFSDDBG_PROC
  46. #define NFSPROC4_CB_NULL 0
  47. #define NFSPROC4_CB_COMPOUND 1
  48. /* Index of predefined Linux callback client operations */
  49. struct nfs4_cb_compound_hdr {
  50. /* args */
  51. u32 ident; /* minorversion 0 only */
  52. u32 nops;
  53. __be32 *nops_p;
  54. u32 minorversion;
  55. /* res */
  56. int status;
  57. };
  58. static __be32 *xdr_encode_empty_array(__be32 *p)
  59. {
  60. *p++ = xdr_zero;
  61. return p;
  62. }
  63. /*
  64. * Encode/decode NFSv4 CB basic data types
  65. *
  66. * Basic NFSv4 callback data types are defined in section 15 of RFC
  67. * 3530: "Network File System (NFS) version 4 Protocol" and section
  68. * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
  69. * 1 Protocol"
  70. */
  71. static void encode_uint32(struct xdr_stream *xdr, u32 n)
  72. {
  73. WARN_ON_ONCE(xdr_stream_encode_u32(xdr, n) < 0);
  74. }
  75. static void encode_bitmap4(struct xdr_stream *xdr, const __u32 *bitmap,
  76. size_t len)
  77. {
  78. xdr_stream_encode_uint32_array(xdr, bitmap, len);
  79. }
  80. static int decode_cb_fattr4(struct xdr_stream *xdr, uint32_t *bitmap,
  81. struct nfs4_cb_fattr *fattr)
  82. {
  83. fattr->ncf_cb_change = 0;
  84. fattr->ncf_cb_fsize = 0;
  85. fattr->ncf_cb_atime.tv_sec = 0;
  86. fattr->ncf_cb_atime.tv_nsec = 0;
  87. fattr->ncf_cb_mtime.tv_sec = 0;
  88. fattr->ncf_cb_mtime.tv_nsec = 0;
  89. if (bitmap[0] & FATTR4_WORD0_CHANGE)
  90. if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_change) < 0)
  91. return -EIO;
  92. if (bitmap[0] & FATTR4_WORD0_SIZE)
  93. if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_fsize) < 0)
  94. return -EIO;
  95. if (bitmap[2] & FATTR4_WORD2_TIME_DELEG_ACCESS) {
  96. fattr4_time_deleg_access access;
  97. if (!xdrgen_decode_fattr4_time_deleg_access(xdr, &access))
  98. return -EIO;
  99. fattr->ncf_cb_atime.tv_sec = access.seconds;
  100. fattr->ncf_cb_atime.tv_nsec = access.nseconds;
  101. }
  102. if (bitmap[2] & FATTR4_WORD2_TIME_DELEG_MODIFY) {
  103. fattr4_time_deleg_modify modify;
  104. if (!xdrgen_decode_fattr4_time_deleg_modify(xdr, &modify))
  105. return -EIO;
  106. fattr->ncf_cb_mtime.tv_sec = modify.seconds;
  107. fattr->ncf_cb_mtime.tv_nsec = modify.nseconds;
  108. }
  109. return 0;
  110. }
  111. static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
  112. {
  113. __be32 *p;
  114. p = xdr_reserve_space(xdr, 4);
  115. *p = cpu_to_be32(op);
  116. }
  117. /*
  118. * nfs_fh4
  119. *
  120. * typedef opaque nfs_fh4<NFS4_FHSIZE>;
  121. */
  122. static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
  123. {
  124. u32 length = fh->fh_size;
  125. __be32 *p;
  126. BUG_ON(length > NFS4_FHSIZE);
  127. p = xdr_reserve_space(xdr, 4 + length);
  128. xdr_encode_opaque(p, &fh->fh_raw, length);
  129. }
  130. /*
  131. * stateid4
  132. *
  133. * struct stateid4 {
  134. * uint32_t seqid;
  135. * opaque other[12];
  136. * };
  137. */
  138. static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
  139. {
  140. __be32 *p;
  141. p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
  142. *p++ = cpu_to_be32(sid->si_generation);
  143. xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
  144. }
  145. /*
  146. * sessionid4
  147. *
  148. * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
  149. */
  150. static void encode_sessionid4(struct xdr_stream *xdr,
  151. const struct nfsd4_session *session)
  152. {
  153. __be32 *p;
  154. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
  155. xdr_encode_opaque_fixed(p, session->se_sessionid.data,
  156. NFS4_MAX_SESSIONID_LEN);
  157. }
  158. /*
  159. * nfsstat4
  160. */
  161. static const struct {
  162. int stat;
  163. int errno;
  164. } nfs_cb_errtbl[] = {
  165. { NFS4_OK, 0 },
  166. { NFS4ERR_PERM, -EPERM },
  167. { NFS4ERR_NOENT, -ENOENT },
  168. { NFS4ERR_IO, -EIO },
  169. { NFS4ERR_NXIO, -ENXIO },
  170. { NFS4ERR_ACCESS, -EACCES },
  171. { NFS4ERR_EXIST, -EEXIST },
  172. { NFS4ERR_XDEV, -EXDEV },
  173. { NFS4ERR_NOTDIR, -ENOTDIR },
  174. { NFS4ERR_ISDIR, -EISDIR },
  175. { NFS4ERR_INVAL, -EINVAL },
  176. { NFS4ERR_FBIG, -EFBIG },
  177. { NFS4ERR_NOSPC, -ENOSPC },
  178. { NFS4ERR_ROFS, -EROFS },
  179. { NFS4ERR_MLINK, -EMLINK },
  180. { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
  181. { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
  182. { NFS4ERR_DQUOT, -EDQUOT },
  183. { NFS4ERR_STALE, -ESTALE },
  184. { NFS4ERR_BADHANDLE, -EBADHANDLE },
  185. { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
  186. { NFS4ERR_NOTSUPP, -ENOTSUPP },
  187. { NFS4ERR_TOOSMALL, -ETOOSMALL },
  188. { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
  189. { NFS4ERR_BADTYPE, -EBADTYPE },
  190. { NFS4ERR_LOCKED, -EAGAIN },
  191. { NFS4ERR_RESOURCE, -EREMOTEIO },
  192. { NFS4ERR_SYMLINK, -ELOOP },
  193. { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
  194. { NFS4ERR_DEADLOCK, -EDEADLK },
  195. { -1, -EIO }
  196. };
  197. /*
  198. * If we cannot translate the error, the recovery routines should
  199. * handle it.
  200. *
  201. * Note: remaining NFSv4 error codes have values > 10000, so should
  202. * not conflict with native Linux error codes.
  203. */
  204. static int nfs_cb_stat_to_errno(int status)
  205. {
  206. int i;
  207. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  208. if (nfs_cb_errtbl[i].stat == status)
  209. return nfs_cb_errtbl[i].errno;
  210. }
  211. dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
  212. return -status;
  213. }
  214. static int decode_cb_op_status(struct xdr_stream *xdr,
  215. enum nfs_cb_opnum4 expected, int *status)
  216. {
  217. __be32 *p;
  218. u32 op;
  219. p = xdr_inline_decode(xdr, 4 + 4);
  220. if (unlikely(p == NULL))
  221. goto out_overflow;
  222. op = be32_to_cpup(p++);
  223. if (unlikely(op != expected))
  224. goto out_unexpected;
  225. *status = nfs_cb_stat_to_errno(be32_to_cpup(p));
  226. return 0;
  227. out_overflow:
  228. return -EIO;
  229. out_unexpected:
  230. dprintk("NFSD: Callback server returned operation %d but "
  231. "we issued a request for %d\n", op, expected);
  232. return -EIO;
  233. }
  234. /*
  235. * CB_COMPOUND4args
  236. *
  237. * struct CB_COMPOUND4args {
  238. * utf8str_cs tag;
  239. * uint32_t minorversion;
  240. * uint32_t callback_ident;
  241. * nfs_cb_argop4 argarray<>;
  242. * };
  243. */
  244. static void encode_cb_compound4args(struct xdr_stream *xdr,
  245. struct nfs4_cb_compound_hdr *hdr)
  246. {
  247. __be32 * p;
  248. p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
  249. p = xdr_encode_empty_array(p); /* empty tag */
  250. *p++ = cpu_to_be32(hdr->minorversion);
  251. *p++ = cpu_to_be32(hdr->ident);
  252. hdr->nops_p = p;
  253. *p = cpu_to_be32(hdr->nops); /* argarray element count */
  254. }
  255. /*
  256. * Update argarray element count
  257. */
  258. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  259. {
  260. BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
  261. *hdr->nops_p = cpu_to_be32(hdr->nops);
  262. }
  263. /*
  264. * CB_COMPOUND4res
  265. *
  266. * struct CB_COMPOUND4res {
  267. * nfsstat4 status;
  268. * utf8str_cs tag;
  269. * nfs_cb_resop4 resarray<>;
  270. * };
  271. */
  272. static int decode_cb_compound4res(struct xdr_stream *xdr,
  273. struct nfs4_cb_compound_hdr *hdr)
  274. {
  275. u32 length;
  276. __be32 *p;
  277. p = xdr_inline_decode(xdr, XDR_UNIT);
  278. if (unlikely(p == NULL))
  279. goto out_overflow;
  280. hdr->status = be32_to_cpup(p);
  281. /* Ignore the tag */
  282. if (xdr_stream_decode_u32(xdr, &length) < 0)
  283. goto out_overflow;
  284. if (xdr_inline_decode(xdr, length) == NULL)
  285. goto out_overflow;
  286. if (xdr_stream_decode_u32(xdr, &hdr->nops) < 0)
  287. goto out_overflow;
  288. return 0;
  289. out_overflow:
  290. return -EIO;
  291. }
  292. /*
  293. * CB_RECALL4args
  294. *
  295. * struct CB_RECALL4args {
  296. * stateid4 stateid;
  297. * bool truncate;
  298. * nfs_fh4 fh;
  299. * };
  300. */
  301. static void encode_cb_recall4args(struct xdr_stream *xdr,
  302. const struct nfs4_delegation *dp,
  303. struct nfs4_cb_compound_hdr *hdr)
  304. {
  305. __be32 *p;
  306. encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
  307. encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
  308. p = xdr_reserve_space(xdr, 4);
  309. *p++ = xdr_zero; /* truncate */
  310. encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
  311. hdr->nops++;
  312. }
  313. /*
  314. * CB_RECALLANY4args
  315. *
  316. * struct CB_RECALLANY4args {
  317. * uint32_t craa_objects_to_keep;
  318. * bitmap4 craa_type_mask;
  319. * };
  320. */
  321. static void
  322. encode_cb_recallany4args(struct xdr_stream *xdr,
  323. struct nfs4_cb_compound_hdr *hdr, struct nfsd4_cb_recall_any *ra)
  324. {
  325. encode_nfs_cb_opnum4(xdr, OP_CB_RECALL_ANY);
  326. encode_uint32(xdr, ra->ra_keep);
  327. encode_bitmap4(xdr, ra->ra_bmval, ARRAY_SIZE(ra->ra_bmval));
  328. hdr->nops++;
  329. }
  330. /*
  331. * CB_GETATTR4args
  332. * struct CB_GETATTR4args {
  333. * nfs_fh4 fh;
  334. * bitmap4 attr_request;
  335. * };
  336. *
  337. * The size and change attributes are the only one
  338. * guaranteed to be serviced by the client.
  339. */
  340. static void
  341. encode_cb_getattr4args(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr,
  342. struct nfs4_cb_fattr *fattr)
  343. {
  344. struct nfs4_delegation *dp = container_of(fattr, struct nfs4_delegation, dl_cb_fattr);
  345. struct knfsd_fh *fh = &dp->dl_stid.sc_file->fi_fhandle;
  346. struct nfs4_cb_fattr *ncf = &dp->dl_cb_fattr;
  347. u32 bmap_size = 1;
  348. u32 bmap[3];
  349. bmap[0] = FATTR4_WORD0_SIZE;
  350. if (!ncf->ncf_file_modified)
  351. bmap[0] |= FATTR4_WORD0_CHANGE;
  352. if (deleg_attrs_deleg(dp->dl_type)) {
  353. bmap[1] = 0;
  354. bmap[2] = FATTR4_WORD2_TIME_DELEG_ACCESS | FATTR4_WORD2_TIME_DELEG_MODIFY;
  355. bmap_size = 3;
  356. }
  357. encode_nfs_cb_opnum4(xdr, OP_CB_GETATTR);
  358. encode_nfs_fh4(xdr, fh);
  359. encode_bitmap4(xdr, bmap, bmap_size);
  360. hdr->nops++;
  361. }
  362. static u32 highest_slotid(struct nfsd4_session *ses)
  363. {
  364. u32 idx;
  365. spin_lock(&ses->se_lock);
  366. idx = fls(~ses->se_cb_slot_avail);
  367. if (idx > 0)
  368. --idx;
  369. idx = max(idx, ses->se_cb_highest_slot);
  370. spin_unlock(&ses->se_lock);
  371. return idx;
  372. }
  373. static void
  374. encode_referring_call4(struct xdr_stream *xdr,
  375. const struct nfsd4_referring_call *rc)
  376. {
  377. encode_uint32(xdr, rc->rc_sequenceid);
  378. encode_uint32(xdr, rc->rc_slotid);
  379. }
  380. static void
  381. encode_referring_call_list4(struct xdr_stream *xdr,
  382. const struct nfsd4_referring_call_list *rcl)
  383. {
  384. struct nfsd4_referring_call *rc;
  385. __be32 *p;
  386. p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
  387. xdr_encode_opaque_fixed(p, rcl->rcl_sessionid.data,
  388. NFS4_MAX_SESSIONID_LEN);
  389. encode_uint32(xdr, rcl->__nr_referring_calls);
  390. list_for_each_entry(rc, &rcl->rcl_referring_calls, __list)
  391. encode_referring_call4(xdr, rc);
  392. }
  393. /*
  394. * CB_SEQUENCE4args
  395. *
  396. * struct CB_SEQUENCE4args {
  397. * sessionid4 csa_sessionid;
  398. * sequenceid4 csa_sequenceid;
  399. * slotid4 csa_slotid;
  400. * slotid4 csa_highest_slotid;
  401. * bool csa_cachethis;
  402. * referring_call_list4 csa_referring_call_lists<>;
  403. * };
  404. */
  405. static void encode_cb_sequence4args(struct xdr_stream *xdr,
  406. const struct nfsd4_callback *cb,
  407. struct nfs4_cb_compound_hdr *hdr)
  408. {
  409. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  410. struct nfsd4_referring_call_list *rcl;
  411. __be32 *p;
  412. if (hdr->minorversion == 0)
  413. return;
  414. encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
  415. encode_sessionid4(xdr, session);
  416. p = xdr_reserve_space(xdr, XDR_UNIT * 4);
  417. *p++ = cpu_to_be32(session->se_cb_seq_nr[cb->cb_held_slot]); /* csa_sequenceid */
  418. *p++ = cpu_to_be32(cb->cb_held_slot); /* csa_slotid */
  419. *p++ = cpu_to_be32(highest_slotid(session)); /* csa_highest_slotid */
  420. *p++ = xdr_zero; /* csa_cachethis */
  421. /* csa_referring_call_lists */
  422. encode_uint32(xdr, cb->cb_nr_referring_call_list);
  423. list_for_each_entry(rcl, &cb->cb_referring_call_list, __list)
  424. encode_referring_call_list4(xdr, rcl);
  425. hdr->nops++;
  426. }
  427. static void update_cb_slot_table(struct nfsd4_session *ses, u32 target)
  428. {
  429. /* No need to do anything if nothing changed */
  430. if (likely(target == READ_ONCE(ses->se_cb_highest_slot)))
  431. return;
  432. spin_lock(&ses->se_lock);
  433. if (target > ses->se_cb_highest_slot) {
  434. int i;
  435. target = min(target, NFSD_BC_SLOT_TABLE_SIZE - 1);
  436. /*
  437. * Growing the slot table. Reset any new sequences to 1.
  438. *
  439. * NB: There is some debate about whether the RFC requires this,
  440. * but the Linux client expects it.
  441. */
  442. for (i = ses->se_cb_highest_slot + 1; i <= target; ++i)
  443. ses->se_cb_seq_nr[i] = 1;
  444. }
  445. ses->se_cb_highest_slot = target;
  446. spin_unlock(&ses->se_lock);
  447. }
  448. /*
  449. * CB_SEQUENCE4resok
  450. *
  451. * struct CB_SEQUENCE4resok {
  452. * sessionid4 csr_sessionid;
  453. * sequenceid4 csr_sequenceid;
  454. * slotid4 csr_slotid;
  455. * slotid4 csr_highest_slotid;
  456. * slotid4 csr_target_highest_slotid;
  457. * };
  458. *
  459. * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
  460. * case NFS4_OK:
  461. * CB_SEQUENCE4resok csr_resok4;
  462. * default:
  463. * void;
  464. * };
  465. *
  466. * Our current back channel implmentation supports a single backchannel
  467. * with a single slot.
  468. */
  469. static int decode_cb_sequence4resok(struct xdr_stream *xdr,
  470. struct nfsd4_callback *cb)
  471. {
  472. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  473. int status = -ESERVERFAULT;
  474. __be32 *p;
  475. u32 seqid, slotid, target;
  476. /*
  477. * If the server returns different values for sessionID, slotID or
  478. * sequence number, the server is looney tunes.
  479. */
  480. p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
  481. if (unlikely(p == NULL))
  482. goto out_overflow;
  483. if (memcmp(p, session->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
  484. dprintk("NFS: %s Invalid session id\n", __func__);
  485. goto out;
  486. }
  487. p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
  488. seqid = be32_to_cpup(p++);
  489. if (seqid != session->se_cb_seq_nr[cb->cb_held_slot]) {
  490. dprintk("NFS: %s Invalid sequence number\n", __func__);
  491. goto out;
  492. }
  493. slotid = be32_to_cpup(p++);
  494. if (slotid != cb->cb_held_slot) {
  495. dprintk("NFS: %s Invalid slotid\n", __func__);
  496. goto out;
  497. }
  498. p++; // ignore current highest slot value
  499. target = be32_to_cpup(p++);
  500. update_cb_slot_table(session, target);
  501. status = 0;
  502. out:
  503. cb->cb_seq_status = status;
  504. return status;
  505. out_overflow:
  506. status = -EIO;
  507. goto out;
  508. }
  509. static int decode_cb_sequence4res(struct xdr_stream *xdr,
  510. struct nfsd4_callback *cb)
  511. {
  512. int status;
  513. if (cb->cb_clp->cl_minorversion == 0)
  514. return 0;
  515. status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &cb->cb_seq_status);
  516. if (unlikely(status || cb->cb_seq_status))
  517. return status;
  518. return decode_cb_sequence4resok(xdr, cb);
  519. }
  520. /*
  521. * NFSv4.0 and NFSv4.1 XDR encode functions
  522. *
  523. * NFSv4.0 callback argument types are defined in section 15 of RFC
  524. * 3530: "Network File System (NFS) version 4 Protocol" and section 20
  525. * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
  526. * Protocol".
  527. */
  528. /*
  529. * NB: Without this zero space reservation, callbacks over krb5p fail
  530. */
  531. static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
  532. const void *__unused)
  533. {
  534. xdr_reserve_space(xdr, 0);
  535. }
  536. /*
  537. * 20.1. Operation 3: CB_GETATTR - Get Attributes
  538. */
  539. static void nfs4_xdr_enc_cb_getattr(struct rpc_rqst *req,
  540. struct xdr_stream *xdr, const void *data)
  541. {
  542. const struct nfsd4_callback *cb = data;
  543. struct nfs4_cb_fattr *ncf =
  544. container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
  545. struct nfs4_cb_compound_hdr hdr = {
  546. .ident = cb->cb_clp->cl_cb_ident,
  547. .minorversion = cb->cb_clp->cl_minorversion,
  548. };
  549. encode_cb_compound4args(xdr, &hdr);
  550. encode_cb_sequence4args(xdr, cb, &hdr);
  551. encode_cb_getattr4args(xdr, &hdr, ncf);
  552. encode_cb_nops(&hdr);
  553. }
  554. /*
  555. * 20.2. Operation 4: CB_RECALL - Recall a Delegation
  556. */
  557. static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
  558. const void *data)
  559. {
  560. const struct nfsd4_callback *cb = data;
  561. const struct nfs4_delegation *dp = cb_to_delegation(cb);
  562. struct nfs4_cb_compound_hdr hdr = {
  563. .ident = cb->cb_clp->cl_cb_ident,
  564. .minorversion = cb->cb_clp->cl_minorversion,
  565. };
  566. encode_cb_compound4args(xdr, &hdr);
  567. encode_cb_sequence4args(xdr, cb, &hdr);
  568. encode_cb_recall4args(xdr, dp, &hdr);
  569. encode_cb_nops(&hdr);
  570. }
  571. /*
  572. * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects
  573. */
  574. static void
  575. nfs4_xdr_enc_cb_recall_any(struct rpc_rqst *req,
  576. struct xdr_stream *xdr, const void *data)
  577. {
  578. const struct nfsd4_callback *cb = data;
  579. struct nfsd4_cb_recall_any *ra;
  580. struct nfs4_cb_compound_hdr hdr = {
  581. .ident = cb->cb_clp->cl_cb_ident,
  582. .minorversion = cb->cb_clp->cl_minorversion,
  583. };
  584. ra = container_of(cb, struct nfsd4_cb_recall_any, ra_cb);
  585. encode_cb_compound4args(xdr, &hdr);
  586. encode_cb_sequence4args(xdr, cb, &hdr);
  587. encode_cb_recallany4args(xdr, &hdr, ra);
  588. encode_cb_nops(&hdr);
  589. }
  590. /*
  591. * NFSv4.0 and NFSv4.1 XDR decode functions
  592. *
  593. * NFSv4.0 callback result types are defined in section 15 of RFC
  594. * 3530: "Network File System (NFS) version 4 Protocol" and section 20
  595. * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
  596. * Protocol".
  597. */
  598. static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
  599. void *__unused)
  600. {
  601. return 0;
  602. }
  603. /*
  604. * 20.1. Operation 3: CB_GETATTR - Get Attributes
  605. */
  606. static int nfs4_xdr_dec_cb_getattr(struct rpc_rqst *rqstp,
  607. struct xdr_stream *xdr,
  608. void *data)
  609. {
  610. struct nfsd4_callback *cb = data;
  611. struct nfs4_cb_compound_hdr hdr;
  612. int status;
  613. u32 bitmap[3] = {0};
  614. u32 attrlen, maxlen;
  615. struct nfs4_cb_fattr *ncf =
  616. container_of(cb, struct nfs4_cb_fattr, ncf_getattr);
  617. status = decode_cb_compound4res(xdr, &hdr);
  618. if (unlikely(status))
  619. return status;
  620. status = decode_cb_sequence4res(xdr, cb);
  621. if (unlikely(status || cb->cb_seq_status))
  622. return status;
  623. status = decode_cb_op_status(xdr, OP_CB_GETATTR, &cb->cb_status);
  624. if (unlikely(status || cb->cb_status))
  625. return status;
  626. if (xdr_stream_decode_uint32_array(xdr, bitmap, 3) < 0)
  627. return -EIO;
  628. if (xdr_stream_decode_u32(xdr, &attrlen) < 0)
  629. return -EIO;
  630. maxlen = sizeof(ncf->ncf_cb_change) + sizeof(ncf->ncf_cb_fsize);
  631. if (bitmap[2] != 0)
  632. maxlen += (sizeof(ncf->ncf_cb_mtime.tv_sec) +
  633. sizeof(ncf->ncf_cb_mtime.tv_nsec)) * 2;
  634. if (attrlen > maxlen)
  635. return -EIO;
  636. status = decode_cb_fattr4(xdr, bitmap, ncf);
  637. return status;
  638. }
  639. /*
  640. * 20.2. Operation 4: CB_RECALL - Recall a Delegation
  641. */
  642. static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
  643. struct xdr_stream *xdr,
  644. void *data)
  645. {
  646. struct nfsd4_callback *cb = data;
  647. struct nfs4_cb_compound_hdr hdr;
  648. int status;
  649. status = decode_cb_compound4res(xdr, &hdr);
  650. if (unlikely(status))
  651. return status;
  652. status = decode_cb_sequence4res(xdr, cb);
  653. if (unlikely(status || cb->cb_seq_status))
  654. return status;
  655. return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status);
  656. }
  657. /*
  658. * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects
  659. */
  660. static int
  661. nfs4_xdr_dec_cb_recall_any(struct rpc_rqst *rqstp,
  662. struct xdr_stream *xdr,
  663. void *data)
  664. {
  665. struct nfsd4_callback *cb = data;
  666. struct nfs4_cb_compound_hdr hdr;
  667. int status;
  668. status = decode_cb_compound4res(xdr, &hdr);
  669. if (unlikely(status))
  670. return status;
  671. status = decode_cb_sequence4res(xdr, cb);
  672. if (unlikely(status || cb->cb_seq_status))
  673. return status;
  674. status = decode_cb_op_status(xdr, OP_CB_RECALL_ANY, &cb->cb_status);
  675. return status;
  676. }
  677. #ifdef CONFIG_NFSD_PNFS
  678. /*
  679. * CB_LAYOUTRECALL4args
  680. *
  681. * struct layoutrecall_file4 {
  682. * nfs_fh4 lor_fh;
  683. * offset4 lor_offset;
  684. * length4 lor_length;
  685. * stateid4 lor_stateid;
  686. * };
  687. *
  688. * union layoutrecall4 switch(layoutrecall_type4 lor_recalltype) {
  689. * case LAYOUTRECALL4_FILE:
  690. * layoutrecall_file4 lor_layout;
  691. * case LAYOUTRECALL4_FSID:
  692. * fsid4 lor_fsid;
  693. * case LAYOUTRECALL4_ALL:
  694. * void;
  695. * };
  696. *
  697. * struct CB_LAYOUTRECALL4args {
  698. * layouttype4 clora_type;
  699. * layoutiomode4 clora_iomode;
  700. * bool clora_changed;
  701. * layoutrecall4 clora_recall;
  702. * };
  703. */
  704. static void encode_cb_layout4args(struct xdr_stream *xdr,
  705. const struct nfs4_layout_stateid *ls,
  706. struct nfs4_cb_compound_hdr *hdr)
  707. {
  708. __be32 *p;
  709. BUG_ON(hdr->minorversion == 0);
  710. p = xdr_reserve_space(xdr, 5 * 4);
  711. *p++ = cpu_to_be32(OP_CB_LAYOUTRECALL);
  712. *p++ = cpu_to_be32(ls->ls_layout_type);
  713. *p++ = cpu_to_be32(IOMODE_ANY);
  714. *p++ = cpu_to_be32(1);
  715. *p = cpu_to_be32(RETURN_FILE);
  716. encode_nfs_fh4(xdr, &ls->ls_stid.sc_file->fi_fhandle);
  717. p = xdr_reserve_space(xdr, 2 * 8);
  718. p = xdr_encode_hyper(p, 0);
  719. xdr_encode_hyper(p, NFS4_MAX_UINT64);
  720. encode_stateid4(xdr, &ls->ls_recall_sid);
  721. hdr->nops++;
  722. }
  723. static void nfs4_xdr_enc_cb_layout(struct rpc_rqst *req,
  724. struct xdr_stream *xdr,
  725. const void *data)
  726. {
  727. const struct nfsd4_callback *cb = data;
  728. const struct nfs4_layout_stateid *ls =
  729. container_of(cb, struct nfs4_layout_stateid, ls_recall);
  730. struct nfs4_cb_compound_hdr hdr = {
  731. .ident = 0,
  732. .minorversion = cb->cb_clp->cl_minorversion,
  733. };
  734. encode_cb_compound4args(xdr, &hdr);
  735. encode_cb_sequence4args(xdr, cb, &hdr);
  736. encode_cb_layout4args(xdr, ls, &hdr);
  737. encode_cb_nops(&hdr);
  738. }
  739. static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp,
  740. struct xdr_stream *xdr,
  741. void *data)
  742. {
  743. struct nfsd4_callback *cb = data;
  744. struct nfs4_cb_compound_hdr hdr;
  745. int status;
  746. status = decode_cb_compound4res(xdr, &hdr);
  747. if (unlikely(status))
  748. return status;
  749. status = decode_cb_sequence4res(xdr, cb);
  750. if (unlikely(status || cb->cb_seq_status))
  751. return status;
  752. return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status);
  753. }
  754. #endif /* CONFIG_NFSD_PNFS */
  755. static void encode_stateowner(struct xdr_stream *xdr, struct nfs4_stateowner *so)
  756. {
  757. __be32 *p;
  758. p = xdr_reserve_space(xdr, 8 + 4 + so->so_owner.len);
  759. p = xdr_encode_opaque_fixed(p, &so->so_client->cl_clientid, 8);
  760. xdr_encode_opaque(p, so->so_owner.data, so->so_owner.len);
  761. }
  762. static void nfs4_xdr_enc_cb_notify_lock(struct rpc_rqst *req,
  763. struct xdr_stream *xdr,
  764. const void *data)
  765. {
  766. const struct nfsd4_callback *cb = data;
  767. const struct nfsd4_blocked_lock *nbl =
  768. container_of(cb, struct nfsd4_blocked_lock, nbl_cb);
  769. struct nfs4_lockowner *lo = (struct nfs4_lockowner *)nbl->nbl_lock.c.flc_owner;
  770. struct nfs4_cb_compound_hdr hdr = {
  771. .ident = 0,
  772. .minorversion = cb->cb_clp->cl_minorversion,
  773. };
  774. __be32 *p;
  775. BUG_ON(hdr.minorversion == 0);
  776. encode_cb_compound4args(xdr, &hdr);
  777. encode_cb_sequence4args(xdr, cb, &hdr);
  778. p = xdr_reserve_space(xdr, 4);
  779. *p = cpu_to_be32(OP_CB_NOTIFY_LOCK);
  780. encode_nfs_fh4(xdr, &nbl->nbl_fh);
  781. encode_stateowner(xdr, &lo->lo_owner);
  782. hdr.nops++;
  783. encode_cb_nops(&hdr);
  784. }
  785. static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp,
  786. struct xdr_stream *xdr,
  787. void *data)
  788. {
  789. struct nfsd4_callback *cb = data;
  790. struct nfs4_cb_compound_hdr hdr;
  791. int status;
  792. status = decode_cb_compound4res(xdr, &hdr);
  793. if (unlikely(status))
  794. return status;
  795. status = decode_cb_sequence4res(xdr, cb);
  796. if (unlikely(status || cb->cb_seq_status))
  797. return status;
  798. return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status);
  799. }
  800. /*
  801. * struct write_response4 {
  802. * stateid4 wr_callback_id<1>;
  803. * length4 wr_count;
  804. * stable_how4 wr_committed;
  805. * verifier4 wr_writeverf;
  806. * };
  807. * union offload_info4 switch (nfsstat4 coa_status) {
  808. * case NFS4_OK:
  809. * write_response4 coa_resok4;
  810. * default:
  811. * length4 coa_bytes_copied;
  812. * };
  813. * struct CB_OFFLOAD4args {
  814. * nfs_fh4 coa_fh;
  815. * stateid4 coa_stateid;
  816. * offload_info4 coa_offload_info;
  817. * };
  818. */
  819. static void encode_offload_info4(struct xdr_stream *xdr,
  820. const struct nfsd4_cb_offload *cbo)
  821. {
  822. __be32 *p;
  823. p = xdr_reserve_space(xdr, 4);
  824. *p = cbo->co_nfserr;
  825. switch (cbo->co_nfserr) {
  826. case nfs_ok:
  827. p = xdr_reserve_space(xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE);
  828. p = xdr_encode_empty_array(p);
  829. p = xdr_encode_hyper(p, cbo->co_res.wr_bytes_written);
  830. *p++ = cpu_to_be32(cbo->co_res.wr_stable_how);
  831. p = xdr_encode_opaque_fixed(p, cbo->co_res.wr_verifier.data,
  832. NFS4_VERIFIER_SIZE);
  833. break;
  834. default:
  835. p = xdr_reserve_space(xdr, 8);
  836. /* We always return success if bytes were written */
  837. p = xdr_encode_hyper(p, 0);
  838. }
  839. }
  840. static void encode_cb_offload4args(struct xdr_stream *xdr,
  841. const struct nfsd4_cb_offload *cbo,
  842. struct nfs4_cb_compound_hdr *hdr)
  843. {
  844. __be32 *p;
  845. p = xdr_reserve_space(xdr, 4);
  846. *p = cpu_to_be32(OP_CB_OFFLOAD);
  847. encode_nfs_fh4(xdr, &cbo->co_fh);
  848. encode_stateid4(xdr, &cbo->co_res.cb_stateid);
  849. encode_offload_info4(xdr, cbo);
  850. hdr->nops++;
  851. }
  852. static void nfs4_xdr_enc_cb_offload(struct rpc_rqst *req,
  853. struct xdr_stream *xdr,
  854. const void *data)
  855. {
  856. const struct nfsd4_callback *cb = data;
  857. const struct nfsd4_cb_offload *cbo =
  858. container_of(cb, struct nfsd4_cb_offload, co_cb);
  859. struct nfs4_cb_compound_hdr hdr = {
  860. .ident = 0,
  861. .minorversion = cb->cb_clp->cl_minorversion,
  862. };
  863. encode_cb_compound4args(xdr, &hdr);
  864. encode_cb_sequence4args(xdr, cb, &hdr);
  865. encode_cb_offload4args(xdr, cbo, &hdr);
  866. encode_cb_nops(&hdr);
  867. }
  868. static int nfs4_xdr_dec_cb_offload(struct rpc_rqst *rqstp,
  869. struct xdr_stream *xdr,
  870. void *data)
  871. {
  872. struct nfsd4_callback *cb = data;
  873. struct nfs4_cb_compound_hdr hdr;
  874. int status;
  875. status = decode_cb_compound4res(xdr, &hdr);
  876. if (unlikely(status))
  877. return status;
  878. status = decode_cb_sequence4res(xdr, cb);
  879. if (unlikely(status || cb->cb_seq_status))
  880. return status;
  881. return decode_cb_op_status(xdr, OP_CB_OFFLOAD, &cb->cb_status);
  882. }
  883. /*
  884. * RPC procedure tables
  885. */
  886. #define PROC(proc, call, argtype, restype) \
  887. [NFSPROC4_CLNT_##proc] = { \
  888. .p_proc = NFSPROC4_CB_##call, \
  889. .p_encode = nfs4_xdr_enc_##argtype, \
  890. .p_decode = nfs4_xdr_dec_##restype, \
  891. .p_arglen = NFS4_enc_##argtype##_sz, \
  892. .p_replen = NFS4_dec_##restype##_sz, \
  893. .p_statidx = NFSPROC4_CB_##call, \
  894. .p_name = #proc, \
  895. }
  896. static const struct rpc_procinfo nfs4_cb_procedures[] = {
  897. PROC(CB_NULL, NULL, cb_null, cb_null),
  898. PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
  899. #ifdef CONFIG_NFSD_PNFS
  900. PROC(CB_LAYOUT, COMPOUND, cb_layout, cb_layout),
  901. #endif
  902. PROC(CB_NOTIFY_LOCK, COMPOUND, cb_notify_lock, cb_notify_lock),
  903. PROC(CB_OFFLOAD, COMPOUND, cb_offload, cb_offload),
  904. PROC(CB_RECALL_ANY, COMPOUND, cb_recall_any, cb_recall_any),
  905. PROC(CB_GETATTR, COMPOUND, cb_getattr, cb_getattr),
  906. };
  907. static unsigned int nfs4_cb_counts[ARRAY_SIZE(nfs4_cb_procedures)];
  908. static const struct rpc_version nfs_cb_version4 = {
  909. /*
  910. * Note on the callback rpc program version number: despite language in rfc
  911. * 5661 section 18.36.3 requiring servers to use 4 in this field, the
  912. * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
  913. * in practice that appears to be what implementations use. The section
  914. * 18.36.3 language is expected to be fixed in an erratum.
  915. */
  916. .number = 1,
  917. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  918. .procs = nfs4_cb_procedures,
  919. .counts = nfs4_cb_counts,
  920. };
  921. static const struct rpc_version *nfs_cb_version[2] = {
  922. [1] = &nfs_cb_version4,
  923. };
  924. static const struct rpc_program cb_program;
  925. static struct rpc_stat cb_stats = {
  926. .program = &cb_program
  927. };
  928. #define NFS4_CALLBACK 0x40000000
  929. static const struct rpc_program cb_program = {
  930. .name = "nfs4_cb",
  931. .number = NFS4_CALLBACK,
  932. .nrvers = ARRAY_SIZE(nfs_cb_version),
  933. .version = nfs_cb_version,
  934. .stats = &cb_stats,
  935. .pipe_dir_name = "nfsd4_cb",
  936. };
  937. static int max_cb_time(struct net *net)
  938. {
  939. struct nfsd_net *nn = net_generic(net, nfsd_net_id);
  940. /*
  941. * nfsd4_lease is set to at most one hour in __nfsd4_write_time,
  942. * so we can use 32-bit math on it. Warn if that assumption
  943. * ever stops being true.
  944. */
  945. if (WARN_ON_ONCE(nn->nfsd4_lease > 3600))
  946. return 360 * HZ;
  947. return max(((u32)nn->nfsd4_lease)/10, 1u) * HZ;
  948. }
  949. static bool nfsd4_queue_cb(struct nfsd4_callback *cb)
  950. {
  951. struct nfs4_client *clp = cb->cb_clp;
  952. trace_nfsd_cb_queue(clp, cb);
  953. return queue_work(clp->cl_callback_wq, &cb->cb_work);
  954. }
  955. static void nfsd4_requeue_cb(struct rpc_task *task, struct nfsd4_callback *cb)
  956. {
  957. struct nfs4_client *clp = cb->cb_clp;
  958. if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) {
  959. trace_nfsd_cb_restart(clp, cb);
  960. task->tk_status = 0;
  961. set_bit(NFSD4_CALLBACK_REQUEUE, &cb->cb_flags);
  962. }
  963. }
  964. static void nfsd41_cb_inflight_begin(struct nfs4_client *clp)
  965. {
  966. atomic_inc(&clp->cl_cb_inflight);
  967. }
  968. static void nfsd41_cb_inflight_end(struct nfs4_client *clp)
  969. {
  970. atomic_dec_and_wake_up(&clp->cl_cb_inflight);
  971. }
  972. static void nfsd41_cb_inflight_wait_complete(struct nfs4_client *clp)
  973. {
  974. wait_var_event(&clp->cl_cb_inflight,
  975. !atomic_read(&clp->cl_cb_inflight));
  976. }
  977. static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
  978. {
  979. if (clp->cl_minorversion == 0) {
  980. client->cl_principal = clp->cl_cred.cr_targ_princ ?
  981. clp->cl_cred.cr_targ_princ : "nfs";
  982. return get_cred(rpc_machine_cred());
  983. } else {
  984. struct cred *kcred;
  985. kcred = prepare_kernel_cred(&init_task);
  986. if (!kcred)
  987. return NULL;
  988. kcred->fsuid = ses->se_cb_sec.uid;
  989. kcred->fsgid = ses->se_cb_sec.gid;
  990. return kcred;
  991. }
  992. }
  993. static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
  994. {
  995. int maxtime = max_cb_time(clp->net);
  996. struct rpc_timeout timeparms = {
  997. .to_initval = maxtime,
  998. .to_retries = 0,
  999. .to_maxval = maxtime,
  1000. };
  1001. struct rpc_create_args args = {
  1002. .net = clp->net,
  1003. .address = (struct sockaddr *) &conn->cb_addr,
  1004. .addrsize = conn->cb_addrlen,
  1005. .saddress = (struct sockaddr *) &conn->cb_saddr,
  1006. .timeout = &timeparms,
  1007. .program = &cb_program,
  1008. .version = 1,
  1009. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  1010. .cred = current_cred(),
  1011. };
  1012. struct rpc_clnt *client;
  1013. const struct cred *cred;
  1014. if (clp->cl_minorversion == 0) {
  1015. if (!clp->cl_cred.cr_principal &&
  1016. (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5)) {
  1017. trace_nfsd_cb_setup_err(clp, -EINVAL);
  1018. return -EINVAL;
  1019. }
  1020. args.client_name = clp->cl_cred.cr_principal;
  1021. args.prognumber = conn->cb_prog;
  1022. args.protocol = XPRT_TRANSPORT_TCP;
  1023. args.authflavor = clp->cl_cred.cr_flavor;
  1024. clp->cl_cb_ident = conn->cb_ident;
  1025. } else {
  1026. if (!conn->cb_xprt || !ses)
  1027. return -EINVAL;
  1028. clp->cl_cb_session = ses;
  1029. args.bc_xprt = conn->cb_xprt;
  1030. args.prognumber = clp->cl_cb_session->se_cb_prog;
  1031. args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
  1032. XPRT_TRANSPORT_BC;
  1033. args.authflavor = ses->se_cb_sec.flavor;
  1034. }
  1035. /* Create RPC client */
  1036. client = rpc_create(&args);
  1037. if (IS_ERR(client)) {
  1038. trace_nfsd_cb_setup_err(clp, PTR_ERR(client));
  1039. return PTR_ERR(client);
  1040. }
  1041. cred = get_backchannel_cred(clp, client, ses);
  1042. if (!cred) {
  1043. trace_nfsd_cb_setup_err(clp, -ENOMEM);
  1044. rpc_shutdown_client(client);
  1045. return -ENOMEM;
  1046. }
  1047. if (clp->cl_minorversion != 0)
  1048. clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
  1049. clp->cl_cb_client = client;
  1050. clp->cl_cb_cred = cred;
  1051. rcu_read_lock();
  1052. trace_nfsd_cb_setup(clp, rpc_peeraddr2str(client, RPC_DISPLAY_NETID),
  1053. args.authflavor);
  1054. rcu_read_unlock();
  1055. return 0;
  1056. }
  1057. static void nfsd4_mark_cb_state(struct nfs4_client *clp, int newstate)
  1058. {
  1059. if (clp->cl_cb_state != newstate) {
  1060. clp->cl_cb_state = newstate;
  1061. trace_nfsd_cb_new_state(clp);
  1062. }
  1063. }
  1064. static void nfsd4_mark_cb_down(struct nfs4_client *clp)
  1065. {
  1066. if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
  1067. return;
  1068. nfsd4_mark_cb_state(clp, NFSD4_CB_DOWN);
  1069. }
  1070. static void nfsd4_mark_cb_fault(struct nfs4_client *clp)
  1071. {
  1072. if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags))
  1073. return;
  1074. nfsd4_mark_cb_state(clp, NFSD4_CB_FAULT);
  1075. }
  1076. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  1077. {
  1078. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  1079. if (task->tk_status)
  1080. nfsd4_mark_cb_down(clp);
  1081. else
  1082. nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
  1083. }
  1084. static void nfsd4_cb_probe_release(void *calldata)
  1085. {
  1086. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  1087. nfsd41_cb_inflight_end(clp);
  1088. }
  1089. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  1090. /* XXX: release method to ensure we set the cb channel down if
  1091. * necessary on early failure? */
  1092. .rpc_call_done = nfsd4_cb_probe_done,
  1093. .rpc_release = nfsd4_cb_probe_release,
  1094. };
  1095. /*
  1096. * Poke the callback thread to process any updates to the callback
  1097. * parameters, and send a null probe.
  1098. */
  1099. void nfsd4_probe_callback(struct nfs4_client *clp)
  1100. {
  1101. trace_nfsd_cb_probe(clp);
  1102. nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
  1103. set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  1104. nfsd4_run_cb(&clp->cl_cb_null);
  1105. }
  1106. void nfsd4_probe_callback_sync(struct nfs4_client *clp)
  1107. {
  1108. nfsd4_probe_callback(clp);
  1109. flush_workqueue(clp->cl_callback_wq);
  1110. }
  1111. void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  1112. {
  1113. nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN);
  1114. spin_lock(&clp->cl_lock);
  1115. memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
  1116. spin_unlock(&clp->cl_lock);
  1117. }
  1118. static int grab_slot(struct nfsd4_session *ses)
  1119. {
  1120. int idx;
  1121. spin_lock(&ses->se_lock);
  1122. idx = ffs(ses->se_cb_slot_avail) - 1;
  1123. if (idx < 0 || idx > ses->se_cb_highest_slot) {
  1124. spin_unlock(&ses->se_lock);
  1125. return -1;
  1126. }
  1127. /* clear the bit for the slot */
  1128. ses->se_cb_slot_avail &= ~BIT(idx);
  1129. spin_unlock(&ses->se_lock);
  1130. return idx;
  1131. }
  1132. /*
  1133. * There's currently a single callback channel slot.
  1134. * If the slot is available, then mark it busy. Otherwise, set the
  1135. * thread for sleeping on the callback RPC wait queue.
  1136. */
  1137. static bool nfsd41_cb_get_slot(struct nfsd4_callback *cb, struct rpc_task *task)
  1138. {
  1139. struct nfs4_client *clp = cb->cb_clp;
  1140. struct nfsd4_session *ses = clp->cl_cb_session;
  1141. if (cb->cb_held_slot >= 0)
  1142. return true;
  1143. cb->cb_held_slot = grab_slot(ses);
  1144. if (cb->cb_held_slot < 0) {
  1145. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  1146. /* Race breaker */
  1147. cb->cb_held_slot = grab_slot(ses);
  1148. if (cb->cb_held_slot < 0)
  1149. return false;
  1150. rpc_wake_up_queued_task(&clp->cl_cb_waitq, task);
  1151. }
  1152. return true;
  1153. }
  1154. static void nfsd41_cb_release_slot(struct nfsd4_callback *cb)
  1155. {
  1156. struct nfs4_client *clp = cb->cb_clp;
  1157. struct nfsd4_session *ses = clp->cl_cb_session;
  1158. if (cb->cb_held_slot >= 0) {
  1159. spin_lock(&ses->se_lock);
  1160. ses->se_cb_slot_avail |= BIT(cb->cb_held_slot);
  1161. spin_unlock(&ses->se_lock);
  1162. cb->cb_held_slot = -1;
  1163. rpc_wake_up_next(&clp->cl_cb_waitq);
  1164. }
  1165. }
  1166. static void nfsd41_destroy_cb(struct nfsd4_callback *cb)
  1167. {
  1168. struct nfs4_client *clp = cb->cb_clp;
  1169. trace_nfsd_cb_destroy(clp, cb);
  1170. nfsd41_cb_release_slot(cb);
  1171. if (test_bit(NFSD4_CALLBACK_WAKE, &cb->cb_flags))
  1172. clear_and_wake_up_bit(NFSD4_CALLBACK_RUNNING, &cb->cb_flags);
  1173. else
  1174. clear_bit(NFSD4_CALLBACK_RUNNING, &cb->cb_flags);
  1175. if (cb->cb_ops && cb->cb_ops->release)
  1176. cb->cb_ops->release(cb);
  1177. nfsd41_cb_inflight_end(clp);
  1178. }
  1179. /**
  1180. * nfsd41_cb_referring_call - add a referring call to a callback operation
  1181. * @cb: context of callback to add the rc to
  1182. * @sessionid: referring call's session ID
  1183. * @slotid: referring call's session slot index
  1184. * @seqno: referring call's slot sequence number
  1185. *
  1186. * Caller serializes access to @cb.
  1187. *
  1188. * NB: If memory allocation fails, the referring call is not added.
  1189. */
  1190. void nfsd41_cb_referring_call(struct nfsd4_callback *cb,
  1191. struct nfs4_sessionid *sessionid,
  1192. u32 slotid, u32 seqno)
  1193. {
  1194. struct nfsd4_referring_call_list *rcl;
  1195. struct nfsd4_referring_call *rc;
  1196. bool found;
  1197. might_sleep();
  1198. found = false;
  1199. list_for_each_entry(rcl, &cb->cb_referring_call_list, __list) {
  1200. if (!memcmp(rcl->rcl_sessionid.data, sessionid->data,
  1201. NFS4_MAX_SESSIONID_LEN)) {
  1202. found = true;
  1203. break;
  1204. }
  1205. }
  1206. if (!found) {
  1207. rcl = kmalloc_obj(*rcl);
  1208. if (!rcl)
  1209. return;
  1210. memcpy(rcl->rcl_sessionid.data, sessionid->data,
  1211. NFS4_MAX_SESSIONID_LEN);
  1212. rcl->__nr_referring_calls = 0;
  1213. INIT_LIST_HEAD(&rcl->rcl_referring_calls);
  1214. list_add(&rcl->__list, &cb->cb_referring_call_list);
  1215. cb->cb_nr_referring_call_list++;
  1216. }
  1217. found = false;
  1218. list_for_each_entry(rc, &rcl->rcl_referring_calls, __list) {
  1219. if (rc->rc_sequenceid == seqno && rc->rc_slotid == slotid) {
  1220. found = true;
  1221. break;
  1222. }
  1223. }
  1224. if (!found) {
  1225. rc = kmalloc_obj(*rc);
  1226. if (!rc)
  1227. goto out;
  1228. rc->rc_sequenceid = seqno;
  1229. rc->rc_slotid = slotid;
  1230. rcl->__nr_referring_calls++;
  1231. list_add(&rc->__list, &rcl->rcl_referring_calls);
  1232. }
  1233. out:
  1234. if (!rcl->__nr_referring_calls) {
  1235. cb->cb_nr_referring_call_list--;
  1236. list_del(&rcl->__list);
  1237. kfree(rcl);
  1238. }
  1239. }
  1240. /**
  1241. * nfsd41_cb_destroy_referring_call_list - release referring call info
  1242. * @cb: context of a callback that has completed
  1243. *
  1244. * Callers who allocate referring calls using nfsd41_cb_referring_call() must
  1245. * release those resources by calling nfsd41_cb_destroy_referring_call_list.
  1246. *
  1247. * Caller serializes access to @cb.
  1248. */
  1249. void nfsd41_cb_destroy_referring_call_list(struct nfsd4_callback *cb)
  1250. {
  1251. struct nfsd4_referring_call_list *rcl;
  1252. struct nfsd4_referring_call *rc;
  1253. while (!list_empty(&cb->cb_referring_call_list)) {
  1254. rcl = list_first_entry(&cb->cb_referring_call_list,
  1255. struct nfsd4_referring_call_list,
  1256. __list);
  1257. while (!list_empty(&rcl->rcl_referring_calls)) {
  1258. rc = list_first_entry(&rcl->rcl_referring_calls,
  1259. struct nfsd4_referring_call,
  1260. __list);
  1261. list_del(&rc->__list);
  1262. kfree(rc);
  1263. }
  1264. list_del(&rcl->__list);
  1265. kfree(rcl);
  1266. }
  1267. }
  1268. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  1269. {
  1270. struct nfsd4_callback *cb = calldata;
  1271. struct nfs4_client *clp = cb->cb_clp;
  1272. u32 minorversion = clp->cl_minorversion;
  1273. /*
  1274. * cb_seq_status is only set in decode_cb_sequence4res,
  1275. * and so will remain 1 if an rpc level failure occurs.
  1276. */
  1277. trace_nfsd_cb_rpc_prepare(clp);
  1278. cb->cb_seq_status = 1;
  1279. cb->cb_status = 0;
  1280. if (minorversion && !nfsd41_cb_get_slot(cb, task))
  1281. return;
  1282. rpc_call_start(task);
  1283. }
  1284. /* Returns true if CB_COMPOUND processing should continue */
  1285. static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb)
  1286. {
  1287. struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
  1288. bool ret = false;
  1289. if (cb->cb_held_slot < 0)
  1290. goto requeue;
  1291. /* This is the operation status code for CB_SEQUENCE */
  1292. trace_nfsd_cb_seq_status(task, cb);
  1293. switch (cb->cb_seq_status) {
  1294. case 0:
  1295. /*
  1296. * No need for lock, access serialized in nfsd4_cb_prepare
  1297. *
  1298. * RFC5661 20.9.3
  1299. * If CB_SEQUENCE returns an error, then the state of the slot
  1300. * (sequence ID, cached reply) MUST NOT change.
  1301. */
  1302. ++session->se_cb_seq_nr[cb->cb_held_slot];
  1303. ret = true;
  1304. break;
  1305. case -ESERVERFAULT:
  1306. /*
  1307. * Call succeeded, but the session, slot index, or slot
  1308. * sequence number in the response do not match the same
  1309. * in the server's call. The sequence information is thus
  1310. * untrustworthy.
  1311. */
  1312. nfsd4_mark_cb_fault(cb->cb_clp);
  1313. break;
  1314. case 1:
  1315. /*
  1316. * cb_seq_status remains 1 if an RPC Reply was never
  1317. * received. NFSD can't know if the client processed
  1318. * the CB_SEQUENCE operation. Ask the client to send a
  1319. * DESTROY_SESSION to recover.
  1320. */
  1321. fallthrough;
  1322. case -NFS4ERR_BADSESSION:
  1323. nfsd4_mark_cb_fault(cb->cb_clp);
  1324. goto requeue;
  1325. case -NFS4ERR_DELAY:
  1326. cb->cb_seq_status = 1;
  1327. if (RPC_SIGNALLED(task) || !rpc_restart_call(task))
  1328. goto requeue;
  1329. rpc_delay(task, 2 * HZ);
  1330. return false;
  1331. case -NFS4ERR_SEQ_MISORDERED:
  1332. case -NFS4ERR_BADSLOT:
  1333. /*
  1334. * A SEQ_MISORDERED or BADSLOT error means that the client and
  1335. * server are out of sync as to the backchannel parameters. Mark
  1336. * the backchannel faulty and restart the RPC, but leak the slot
  1337. * so that it's no longer used.
  1338. */
  1339. nfsd4_mark_cb_fault(cb->cb_clp);
  1340. cb->cb_held_slot = -1;
  1341. goto retry_nowait;
  1342. default:
  1343. nfsd4_mark_cb_fault(cb->cb_clp);
  1344. }
  1345. trace_nfsd_cb_free_slot(task, cb);
  1346. nfsd41_cb_release_slot(cb);
  1347. return ret;
  1348. retry_nowait:
  1349. /*
  1350. * RPC_SIGNALLED() means that the rpc_client is being torn down and
  1351. * (possibly) recreated. Requeue the call in that case.
  1352. */
  1353. if (!RPC_SIGNALLED(task)) {
  1354. if (rpc_restart_call_prepare(task))
  1355. return false;
  1356. }
  1357. requeue:
  1358. nfsd41_cb_release_slot(cb);
  1359. nfsd4_requeue_cb(task, cb);
  1360. return false;
  1361. }
  1362. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  1363. {
  1364. struct nfsd4_callback *cb = calldata;
  1365. struct nfs4_client *clp = cb->cb_clp;
  1366. trace_nfsd_cb_rpc_done(clp);
  1367. if (!clp->cl_minorversion) {
  1368. /*
  1369. * If the backchannel connection was shut down while this
  1370. * task was queued, we need to resubmit it after setting up
  1371. * a new backchannel connection.
  1372. *
  1373. * Note that if we lost our callback connection permanently
  1374. * the submission code will error out, so we don't need to
  1375. * handle that case here.
  1376. */
  1377. if (RPC_SIGNALLED(task))
  1378. nfsd4_requeue_cb(task, cb);
  1379. } else if (!nfsd4_cb_sequence_done(task, cb)) {
  1380. return;
  1381. }
  1382. if (cb->cb_status) {
  1383. WARN_ONCE(task->tk_status,
  1384. "cb_status=%d tk_status=%d cb_opcode=%d",
  1385. cb->cb_status, task->tk_status, cb->cb_ops->opcode);
  1386. task->tk_status = cb->cb_status;
  1387. }
  1388. switch (cb->cb_ops->done(cb, task)) {
  1389. case 0:
  1390. task->tk_status = 0;
  1391. rpc_restart_call_prepare(task);
  1392. return;
  1393. case 1:
  1394. switch (task->tk_status) {
  1395. case -EIO:
  1396. case -ETIMEDOUT:
  1397. case -EACCES:
  1398. nfsd4_mark_cb_down(clp);
  1399. }
  1400. break;
  1401. default:
  1402. BUG();
  1403. }
  1404. }
  1405. static void nfsd4_cb_release(void *calldata)
  1406. {
  1407. struct nfsd4_callback *cb = calldata;
  1408. trace_nfsd_cb_rpc_release(cb->cb_clp);
  1409. if (test_bit(NFSD4_CALLBACK_REQUEUE, &cb->cb_flags))
  1410. nfsd4_queue_cb(cb);
  1411. else
  1412. nfsd41_destroy_cb(cb);
  1413. }
  1414. static const struct rpc_call_ops nfsd4_cb_ops = {
  1415. .rpc_call_prepare = nfsd4_cb_prepare,
  1416. .rpc_call_done = nfsd4_cb_done,
  1417. .rpc_release = nfsd4_cb_release,
  1418. };
  1419. /* must be called under the state lock */
  1420. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  1421. {
  1422. if (clp->cl_cb_state != NFSD4_CB_UNKNOWN)
  1423. trace_nfsd_cb_shutdown(clp);
  1424. set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
  1425. /*
  1426. * Note this won't actually result in a null callback;
  1427. * instead, nfsd4_run_cb_null() will detect the killed
  1428. * client, destroy the rpc client, and stop:
  1429. */
  1430. nfsd4_run_cb(&clp->cl_cb_null);
  1431. flush_workqueue(clp->cl_callback_wq);
  1432. nfsd41_cb_inflight_wait_complete(clp);
  1433. }
  1434. static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
  1435. {
  1436. struct nfsd4_session *s;
  1437. struct nfsd4_conn *c;
  1438. lockdep_assert_held(&clp->cl_lock);
  1439. list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
  1440. list_for_each_entry(c, &s->se_conns, cn_persession) {
  1441. if (c->cn_flags & NFS4_CDFC4_BACK)
  1442. return c;
  1443. }
  1444. }
  1445. return NULL;
  1446. }
  1447. /*
  1448. * Note there isn't a lot of locking in this code; instead we depend on
  1449. * the fact that it is run from clp->cl_callback_wq, which won't run two
  1450. * work items at once. So, for example, clp->cl_callback_wq handles all
  1451. * access of cl_cb_client and all calls to rpc_create or rpc_shutdown_client.
  1452. */
  1453. static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  1454. {
  1455. struct nfs4_cb_conn conn;
  1456. struct nfs4_client *clp = cb->cb_clp;
  1457. struct nfsd4_session *ses = NULL;
  1458. struct nfsd4_conn *c;
  1459. int err;
  1460. trace_nfsd_cb_bc_update(clp, cb);
  1461. /*
  1462. * This is either an update, or the client dying; in either case,
  1463. * kill the old client:
  1464. */
  1465. if (clp->cl_cb_client) {
  1466. trace_nfsd_cb_bc_shutdown(clp, cb);
  1467. rpc_shutdown_client(clp->cl_cb_client);
  1468. clp->cl_cb_client = NULL;
  1469. put_cred(clp->cl_cb_cred);
  1470. clp->cl_cb_cred = NULL;
  1471. }
  1472. if (clp->cl_cb_conn.cb_xprt) {
  1473. svc_xprt_put(clp->cl_cb_conn.cb_xprt);
  1474. clp->cl_cb_conn.cb_xprt = NULL;
  1475. }
  1476. if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
  1477. return;
  1478. spin_lock(&clp->cl_lock);
  1479. /*
  1480. * Only serialized callback code is allowed to clear these
  1481. * flags; main nfsd code can only set them:
  1482. */
  1483. WARN_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
  1484. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
  1485. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  1486. c = __nfsd4_find_backchannel(clp);
  1487. if (c) {
  1488. svc_xprt_get(c->cn_xprt);
  1489. conn.cb_xprt = c->cn_xprt;
  1490. ses = c->cn_session;
  1491. }
  1492. spin_unlock(&clp->cl_lock);
  1493. err = setup_callback_client(clp, &conn, ses);
  1494. if (err) {
  1495. nfsd4_mark_cb_down(clp);
  1496. if (c)
  1497. svc_xprt_put(c->cn_xprt);
  1498. return;
  1499. }
  1500. }
  1501. static void
  1502. nfsd4_run_cb_work(struct work_struct *work)
  1503. {
  1504. struct nfsd4_callback *cb =
  1505. container_of(work, struct nfsd4_callback, cb_work);
  1506. struct nfs4_client *clp = cb->cb_clp;
  1507. struct rpc_clnt *clnt;
  1508. int flags, ret;
  1509. trace_nfsd_cb_start(clp);
  1510. if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
  1511. nfsd4_process_cb_update(cb);
  1512. clnt = clp->cl_cb_client;
  1513. if (!clnt || clp->cl_state == NFSD4_COURTESY) {
  1514. /*
  1515. * Callback channel broken, client killed or
  1516. * nfs4_client in courtesy state; give up.
  1517. */
  1518. nfsd41_destroy_cb(cb);
  1519. return;
  1520. }
  1521. /*
  1522. * Don't send probe messages for 4.1 or later.
  1523. */
  1524. if (!cb->cb_ops && clp->cl_minorversion) {
  1525. nfsd4_mark_cb_state(clp, NFSD4_CB_UP);
  1526. nfsd41_destroy_cb(cb);
  1527. return;
  1528. }
  1529. if (!test_and_clear_bit(NFSD4_CALLBACK_REQUEUE, &cb->cb_flags)) {
  1530. if (cb->cb_ops && cb->cb_ops->prepare)
  1531. cb->cb_ops->prepare(cb);
  1532. }
  1533. cb->cb_msg.rpc_cred = clp->cl_cb_cred;
  1534. flags = clp->cl_minorversion ? RPC_TASK_NOCONNECT : RPC_TASK_SOFTCONN;
  1535. ret = rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | flags,
  1536. cb->cb_ops ? &nfsd4_cb_ops : &nfsd4_cb_probe_ops, cb);
  1537. if (ret != 0) {
  1538. set_bit(NFSD4_CALLBACK_REQUEUE, &cb->cb_flags);
  1539. nfsd4_queue_cb(cb);
  1540. }
  1541. }
  1542. void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
  1543. const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op)
  1544. {
  1545. cb->cb_clp = clp;
  1546. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
  1547. cb->cb_msg.rpc_argp = cb;
  1548. cb->cb_msg.rpc_resp = cb;
  1549. cb->cb_flags = 0;
  1550. cb->cb_ops = ops;
  1551. INIT_WORK(&cb->cb_work, nfsd4_run_cb_work);
  1552. cb->cb_status = 0;
  1553. cb->cb_held_slot = -1;
  1554. cb->cb_nr_referring_call_list = 0;
  1555. INIT_LIST_HEAD(&cb->cb_referring_call_list);
  1556. }
  1557. /**
  1558. * nfsd4_run_cb - queue up a callback job to run
  1559. * @cb: callback to queue
  1560. *
  1561. * Kick off a callback to do its thing. Returns false if it was already
  1562. * on a queue, true otherwise.
  1563. */
  1564. bool nfsd4_run_cb(struct nfsd4_callback *cb)
  1565. {
  1566. struct nfs4_client *clp = cb->cb_clp;
  1567. bool queued;
  1568. nfsd41_cb_inflight_begin(clp);
  1569. queued = nfsd4_queue_cb(cb);
  1570. if (!queued)
  1571. nfsd41_cb_inflight_end(clp);
  1572. return queued;
  1573. }