nfs4callback.c 32 KB

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