nfs4callback.c 26 KB

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