nfs4callback.c 26 KB

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