nfs4callback.c 27 KB

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