nfs4callback.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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);
  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(status);
  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. goto out_default;
  504. out:
  505. return status;
  506. out_default:
  507. return nfs_cb_stat_to_errno(status);
  508. }
  509. /*
  510. * RPC procedure tables
  511. */
  512. #define PROC(proc, call, argtype, restype) \
  513. [NFSPROC4_CLNT_##proc] = { \
  514. .p_proc = NFSPROC4_CB_##call, \
  515. .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
  516. .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
  517. .p_arglen = NFS4_enc_##argtype##_sz, \
  518. .p_replen = NFS4_dec_##restype##_sz, \
  519. .p_statidx = NFSPROC4_CB_##call, \
  520. .p_name = #proc, \
  521. }
  522. static struct rpc_procinfo nfs4_cb_procedures[] = {
  523. PROC(CB_NULL, NULL, cb_null, cb_null),
  524. PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
  525. };
  526. static struct rpc_version nfs_cb_version4 = {
  527. /*
  528. * Note on the callback rpc program version number: despite language in rfc
  529. * 5661 section 18.36.3 requiring servers to use 4 in this field, the
  530. * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
  531. * in practice that appears to be what implementations use. The section
  532. * 18.36.3 language is expected to be fixed in an erratum.
  533. */
  534. .number = 1,
  535. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  536. .procs = nfs4_cb_procedures
  537. };
  538. static struct rpc_version *nfs_cb_version[] = {
  539. &nfs_cb_version4,
  540. };
  541. static struct rpc_program cb_program;
  542. static struct rpc_stat cb_stats = {
  543. .program = &cb_program
  544. };
  545. #define NFS4_CALLBACK 0x40000000
  546. static struct rpc_program cb_program = {
  547. .name = "nfs4_cb",
  548. .number = NFS4_CALLBACK,
  549. .nrvers = ARRAY_SIZE(nfs_cb_version),
  550. .version = nfs_cb_version,
  551. .stats = &cb_stats,
  552. .pipe_dir_name = "/nfsd4_cb",
  553. };
  554. static int max_cb_time(void)
  555. {
  556. return max(nfsd4_lease/10, (time_t)1) * HZ;
  557. }
  558. /* Reference counting, callback cleanup, etc., all look racy as heck.
  559. * And why is cl_cb_set an atomic? */
  560. int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  561. {
  562. struct rpc_timeout timeparms = {
  563. .to_initval = max_cb_time(),
  564. .to_retries = 0,
  565. };
  566. struct rpc_create_args args = {
  567. .net = &init_net,
  568. .address = (struct sockaddr *) &conn->cb_addr,
  569. .addrsize = conn->cb_addrlen,
  570. .timeout = &timeparms,
  571. .program = &cb_program,
  572. .version = 0,
  573. .authflavor = clp->cl_flavor,
  574. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  575. };
  576. struct rpc_clnt *client;
  577. if (clp->cl_minorversion == 0) {
  578. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  579. return -EINVAL;
  580. args.client_name = clp->cl_principal;
  581. args.prognumber = conn->cb_prog,
  582. args.protocol = XPRT_TRANSPORT_TCP;
  583. clp->cl_cb_ident = conn->cb_ident;
  584. } else {
  585. args.bc_xprt = conn->cb_xprt;
  586. args.prognumber = clp->cl_cb_session->se_cb_prog;
  587. args.protocol = XPRT_TRANSPORT_BC_TCP;
  588. }
  589. /* Create RPC client */
  590. client = rpc_create(&args);
  591. if (IS_ERR(client)) {
  592. dprintk("NFSD: couldn't create callback client: %ld\n",
  593. PTR_ERR(client));
  594. return PTR_ERR(client);
  595. }
  596. clp->cl_cb_client = client;
  597. return 0;
  598. }
  599. static void warn_no_callback_path(struct nfs4_client *clp, int reason)
  600. {
  601. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  602. (int)clp->cl_name.len, clp->cl_name.data, reason);
  603. }
  604. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  605. {
  606. struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
  607. if (task->tk_status)
  608. warn_no_callback_path(clp, task->tk_status);
  609. else
  610. atomic_set(&clp->cl_cb_set, 1);
  611. }
  612. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  613. /* XXX: release method to ensure we set the cb channel down if
  614. * necessary on early failure? */
  615. .rpc_call_done = nfsd4_cb_probe_done,
  616. };
  617. static struct rpc_cred *callback_cred;
  618. int set_callback_cred(void)
  619. {
  620. if (callback_cred)
  621. return 0;
  622. callback_cred = rpc_lookup_machine_cred();
  623. if (!callback_cred)
  624. return -ENOMEM;
  625. return 0;
  626. }
  627. static struct workqueue_struct *callback_wq;
  628. static void do_probe_callback(struct nfs4_client *clp)
  629. {
  630. struct nfsd4_callback *cb = &clp->cl_cb_null;
  631. cb->cb_op = NULL;
  632. cb->cb_clp = clp;
  633. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
  634. cb->cb_msg.rpc_argp = NULL;
  635. cb->cb_msg.rpc_resp = NULL;
  636. cb->cb_msg.rpc_cred = callback_cred;
  637. cb->cb_ops = &nfsd4_cb_probe_ops;
  638. queue_work(callback_wq, &cb->cb_work);
  639. }
  640. /*
  641. * Poke the callback thread to process any updates to the callback
  642. * parameters, and send a null probe.
  643. */
  644. void nfsd4_probe_callback(struct nfs4_client *clp)
  645. {
  646. set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
  647. do_probe_callback(clp);
  648. }
  649. void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
  650. {
  651. BUG_ON(atomic_read(&clp->cl_cb_set));
  652. spin_lock(&clp->cl_lock);
  653. memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
  654. spin_unlock(&clp->cl_lock);
  655. }
  656. /*
  657. * There's currently a single callback channel slot.
  658. * If the slot is available, then mark it busy. Otherwise, set the
  659. * thread for sleeping on the callback RPC wait queue.
  660. */
  661. static int nfsd41_cb_setup_sequence(struct nfs4_client *clp,
  662. struct rpc_task *task)
  663. {
  664. u32 *ptr = (u32 *)clp->cl_cb_session->se_sessionid.data;
  665. int status = 0;
  666. dprintk("%s: %u:%u:%u:%u\n", __func__,
  667. ptr[0], ptr[1], ptr[2], ptr[3]);
  668. if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
  669. rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
  670. dprintk("%s slot is busy\n", __func__);
  671. status = -EAGAIN;
  672. goto out;
  673. }
  674. out:
  675. dprintk("%s status=%d\n", __func__, status);
  676. return status;
  677. }
  678. /*
  679. * TODO: cb_sequence should support referring call lists, cachethis, multiple
  680. * slots, and mark callback channel down on communication errors.
  681. */
  682. static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
  683. {
  684. struct nfsd4_callback *cb = calldata;
  685. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  686. struct nfs4_client *clp = dp->dl_client;
  687. u32 minorversion = clp->cl_minorversion;
  688. int status = 0;
  689. cb->cb_minorversion = minorversion;
  690. if (minorversion) {
  691. status = nfsd41_cb_setup_sequence(clp, task);
  692. if (status) {
  693. if (status != -EAGAIN) {
  694. /* terminate rpc task */
  695. task->tk_status = status;
  696. task->tk_action = NULL;
  697. }
  698. return;
  699. }
  700. }
  701. rpc_call_start(task);
  702. }
  703. static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
  704. {
  705. struct nfsd4_callback *cb = calldata;
  706. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  707. struct nfs4_client *clp = dp->dl_client;
  708. dprintk("%s: minorversion=%d\n", __func__,
  709. clp->cl_minorversion);
  710. if (clp->cl_minorversion) {
  711. /* No need for lock, access serialized in nfsd4_cb_prepare */
  712. ++clp->cl_cb_session->se_cb_seq_nr;
  713. clear_bit(0, &clp->cl_cb_slot_busy);
  714. rpc_wake_up_next(&clp->cl_cb_waitq);
  715. dprintk("%s: freed slot, new seqid=%d\n", __func__,
  716. clp->cl_cb_session->se_cb_seq_nr);
  717. /* We're done looking into the sequence information */
  718. task->tk_msg.rpc_resp = NULL;
  719. }
  720. }
  721. static void nfsd4_cb_recall_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_client;
  726. struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
  727. nfsd4_cb_done(task, calldata);
  728. if (current_rpc_client == NULL) {
  729. /* We're shutting down; give up. */
  730. /* XXX: err, or is it ok just to fall through
  731. * and rpc_restart_call? */
  732. return;
  733. }
  734. switch (task->tk_status) {
  735. case 0:
  736. return;
  737. case -EBADHANDLE:
  738. case -NFS4ERR_BAD_STATEID:
  739. /* Race: client probably got cb_recall
  740. * before open reply granting delegation */
  741. break;
  742. default:
  743. /* Network partition? */
  744. atomic_set(&clp->cl_cb_set, 0);
  745. warn_no_callback_path(clp, task->tk_status);
  746. if (current_rpc_client != task->tk_client) {
  747. /* queue a callback on the new connection: */
  748. atomic_inc(&dp->dl_count);
  749. nfsd4_cb_recall(dp);
  750. return;
  751. }
  752. }
  753. if (dp->dl_retries--) {
  754. rpc_delay(task, 2*HZ);
  755. task->tk_status = 0;
  756. rpc_restart_call_prepare(task);
  757. return;
  758. } else {
  759. atomic_set(&clp->cl_cb_set, 0);
  760. warn_no_callback_path(clp, task->tk_status);
  761. }
  762. }
  763. static void nfsd4_cb_recall_release(void *calldata)
  764. {
  765. struct nfsd4_callback *cb = calldata;
  766. struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
  767. nfs4_put_delegation(dp);
  768. }
  769. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  770. .rpc_call_prepare = nfsd4_cb_prepare,
  771. .rpc_call_done = nfsd4_cb_recall_done,
  772. .rpc_release = nfsd4_cb_recall_release,
  773. };
  774. int nfsd4_create_callback_queue(void)
  775. {
  776. callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
  777. if (!callback_wq)
  778. return -ENOMEM;
  779. return 0;
  780. }
  781. void nfsd4_destroy_callback_queue(void)
  782. {
  783. destroy_workqueue(callback_wq);
  784. }
  785. /* must be called under the state lock */
  786. void nfsd4_shutdown_callback(struct nfs4_client *clp)
  787. {
  788. set_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags);
  789. /*
  790. * Note this won't actually result in a null callback;
  791. * instead, nfsd4_do_callback_rpc() will detect the killed
  792. * client, destroy the rpc client, and stop:
  793. */
  794. do_probe_callback(clp);
  795. flush_workqueue(callback_wq);
  796. }
  797. void nfsd4_release_cb(struct nfsd4_callback *cb)
  798. {
  799. if (cb->cb_ops->rpc_release)
  800. cb->cb_ops->rpc_release(cb);
  801. }
  802. void nfsd4_process_cb_update(struct nfsd4_callback *cb)
  803. {
  804. struct nfs4_cb_conn conn;
  805. struct nfs4_client *clp = cb->cb_clp;
  806. int err;
  807. /*
  808. * This is either an update, or the client dying; in either case,
  809. * kill the old client:
  810. */
  811. if (clp->cl_cb_client) {
  812. rpc_shutdown_client(clp->cl_cb_client);
  813. clp->cl_cb_client = NULL;
  814. }
  815. if (test_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags))
  816. return;
  817. spin_lock(&clp->cl_lock);
  818. /*
  819. * Only serialized callback code is allowed to clear these
  820. * flags; main nfsd code can only set them:
  821. */
  822. BUG_ON(!clp->cl_cb_flags);
  823. clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
  824. memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
  825. spin_unlock(&clp->cl_lock);
  826. err = setup_callback_client(clp, &conn);
  827. if (err)
  828. warn_no_callback_path(clp, err);
  829. }
  830. void nfsd4_do_callback_rpc(struct work_struct *w)
  831. {
  832. struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
  833. struct nfs4_client *clp = cb->cb_clp;
  834. struct rpc_clnt *clnt;
  835. if (clp->cl_cb_flags)
  836. nfsd4_process_cb_update(cb);
  837. clnt = clp->cl_cb_client;
  838. if (!clnt) {
  839. /* Callback channel broken, or client killed; give up: */
  840. nfsd4_release_cb(cb);
  841. return;
  842. }
  843. rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
  844. cb->cb_ops, cb);
  845. }
  846. void nfsd4_cb_recall(struct nfs4_delegation *dp)
  847. {
  848. struct nfsd4_callback *cb = &dp->dl_recall;
  849. dp->dl_retries = 1;
  850. cb->cb_op = dp;
  851. cb->cb_clp = dp->dl_client;
  852. cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
  853. cb->cb_msg.rpc_argp = cb;
  854. cb->cb_msg.rpc_resp = cb;
  855. cb->cb_msg.rpc_cred = callback_cred;
  856. cb->cb_ops = &nfsd4_cb_recall_ops;
  857. dp->dl_retries = 1;
  858. queue_work(callback_wq, &dp->dl_recall.cb_work);
  859. }