nfs4callback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * linux/fs/nfsd/nfs4callback.c
  3. *
  4. * Copyright (c) 2001 The Regents of the University of Michigan.
  5. * All rights reserved.
  6. *
  7. * Kendrick Smith <kmsmith@umich.edu>
  8. * Andy Adamson <andros@umich.edu>
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * 3. Neither the name of the University nor the names of its
  20. * contributors may be used to endorse or promote products derived
  21. * from this software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  24. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  25. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  30. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  31. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  32. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  33. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #include <linux/module.h>
  36. #include <linux/list.h>
  37. #include <linux/inet.h>
  38. #include <linux/errno.h>
  39. #include <linux/delay.h>
  40. #include <linux/sched.h>
  41. #include <linux/kthread.h>
  42. #include <linux/sunrpc/xdr.h>
  43. #include <linux/sunrpc/svc.h>
  44. #include <linux/sunrpc/clnt.h>
  45. #include <linux/nfsd/nfsd.h>
  46. #include <linux/nfsd/state.h>
  47. #include <linux/sunrpc/sched.h>
  48. #include <linux/nfs4.h>
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. #define NFSPROC4_CB_NULL 0
  51. #define NFSPROC4_CB_COMPOUND 1
  52. /* declarations */
  53. static const struct rpc_call_ops nfs4_cb_null_ops;
  54. /* Index of predefined Linux callback client operations */
  55. enum {
  56. NFSPROC4_CLNT_CB_NULL = 0,
  57. NFSPROC4_CLNT_CB_RECALL,
  58. };
  59. enum nfs_cb_opnum4 {
  60. OP_CB_RECALL = 4,
  61. };
  62. #define NFS4_MAXTAGLEN 20
  63. #define NFS4_enc_cb_null_sz 0
  64. #define NFS4_dec_cb_null_sz 0
  65. #define cb_compound_enc_hdr_sz 4
  66. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  67. #define op_enc_sz 1
  68. #define op_dec_sz 2
  69. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  70. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  71. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  72. 1 + enc_stateid_sz + \
  73. enc_nfs4_fh_sz)
  74. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  75. op_dec_sz)
  76. /*
  77. * Generic encode routines from fs/nfs/nfs4xdr.c
  78. */
  79. static inline __be32 *
  80. xdr_writemem(__be32 *p, const void *ptr, int nbytes)
  81. {
  82. int tmp = XDR_QUADLEN(nbytes);
  83. if (!tmp)
  84. return p;
  85. p[tmp-1] = 0;
  86. memcpy(p, ptr, nbytes);
  87. return p + tmp;
  88. }
  89. #define WRITE32(n) *p++ = htonl(n)
  90. #define WRITEMEM(ptr,nbytes) do { \
  91. p = xdr_writemem(p, ptr, nbytes); \
  92. } while (0)
  93. #define RESERVE_SPACE(nbytes) do { \
  94. p = xdr_reserve_space(xdr, nbytes); \
  95. if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
  96. BUG_ON(!p); \
  97. } while (0)
  98. /*
  99. * Generic decode routines from fs/nfs/nfs4xdr.c
  100. */
  101. #define DECODE_TAIL \
  102. status = 0; \
  103. out: \
  104. return status; \
  105. xdr_error: \
  106. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  107. status = -EIO; \
  108. goto out
  109. #define READ32(x) (x) = ntohl(*p++)
  110. #define READ64(x) do { \
  111. (x) = (u64)ntohl(*p++) << 32; \
  112. (x) |= ntohl(*p++); \
  113. } while (0)
  114. #define READTIME(x) do { \
  115. p++; \
  116. (x.tv_sec) = ntohl(*p++); \
  117. (x.tv_nsec) = ntohl(*p++); \
  118. } while (0)
  119. #define READ_BUF(nbytes) do { \
  120. p = xdr_inline_decode(xdr, nbytes); \
  121. if (!p) { \
  122. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  123. __FUNCTION__, __LINE__); \
  124. return -EIO; \
  125. } \
  126. } while (0)
  127. struct nfs4_cb_compound_hdr {
  128. int status;
  129. u32 ident;
  130. u32 nops;
  131. u32 taglen;
  132. char * tag;
  133. };
  134. static struct {
  135. int stat;
  136. int errno;
  137. } nfs_cb_errtbl[] = {
  138. { NFS4_OK, 0 },
  139. { NFS4ERR_PERM, EPERM },
  140. { NFS4ERR_NOENT, ENOENT },
  141. { NFS4ERR_IO, EIO },
  142. { NFS4ERR_NXIO, ENXIO },
  143. { NFS4ERR_ACCESS, EACCES },
  144. { NFS4ERR_EXIST, EEXIST },
  145. { NFS4ERR_XDEV, EXDEV },
  146. { NFS4ERR_NOTDIR, ENOTDIR },
  147. { NFS4ERR_ISDIR, EISDIR },
  148. { NFS4ERR_INVAL, EINVAL },
  149. { NFS4ERR_FBIG, EFBIG },
  150. { NFS4ERR_NOSPC, ENOSPC },
  151. { NFS4ERR_ROFS, EROFS },
  152. { NFS4ERR_MLINK, EMLINK },
  153. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  154. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  155. { NFS4ERR_DQUOT, EDQUOT },
  156. { NFS4ERR_STALE, ESTALE },
  157. { NFS4ERR_BADHANDLE, EBADHANDLE },
  158. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  159. { NFS4ERR_NOTSUPP, ENOTSUPP },
  160. { NFS4ERR_TOOSMALL, ETOOSMALL },
  161. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  162. { NFS4ERR_BADTYPE, EBADTYPE },
  163. { NFS4ERR_LOCKED, EAGAIN },
  164. { NFS4ERR_RESOURCE, EREMOTEIO },
  165. { NFS4ERR_SYMLINK, ELOOP },
  166. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  167. { NFS4ERR_DEADLOCK, EDEADLK },
  168. { -1, EIO }
  169. };
  170. static int
  171. nfs_cb_stat_to_errno(int stat)
  172. {
  173. int i;
  174. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  175. if (nfs_cb_errtbl[i].stat == stat)
  176. return nfs_cb_errtbl[i].errno;
  177. }
  178. /* If we cannot translate the error, the recovery routines should
  179. * handle it.
  180. * Note: remaining NFSv4 error codes have values > 10000, so should
  181. * not conflict with native Linux error codes.
  182. */
  183. return stat;
  184. }
  185. /*
  186. * XDR encode
  187. */
  188. static int
  189. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  190. {
  191. __be32 * p;
  192. RESERVE_SPACE(16);
  193. WRITE32(0); /* tag length is always 0 */
  194. WRITE32(NFS4_MINOR_VERSION);
  195. WRITE32(hdr->ident);
  196. WRITE32(hdr->nops);
  197. return 0;
  198. }
  199. static int
  200. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_cb_recall *cb_rec)
  201. {
  202. __be32 *p;
  203. int len = cb_rec->cbr_fhlen;
  204. RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
  205. WRITE32(OP_CB_RECALL);
  206. WRITEMEM(&cb_rec->cbr_stateid, sizeof(stateid_t));
  207. WRITE32(cb_rec->cbr_trunc);
  208. WRITE32(len);
  209. WRITEMEM(cb_rec->cbr_fhval, len);
  210. return 0;
  211. }
  212. static int
  213. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  214. {
  215. struct xdr_stream xdrs, *xdr = &xdrs;
  216. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  217. RESERVE_SPACE(0);
  218. return 0;
  219. }
  220. static int
  221. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_cb_recall *args)
  222. {
  223. struct xdr_stream xdr;
  224. struct nfs4_cb_compound_hdr hdr = {
  225. .ident = args->cbr_ident,
  226. .nops = 1,
  227. };
  228. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  229. encode_cb_compound_hdr(&xdr, &hdr);
  230. return (encode_cb_recall(&xdr, args));
  231. }
  232. static int
  233. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  234. __be32 *p;
  235. READ_BUF(8);
  236. READ32(hdr->status);
  237. READ32(hdr->taglen);
  238. READ_BUF(hdr->taglen + 4);
  239. hdr->tag = (char *)p;
  240. p += XDR_QUADLEN(hdr->taglen);
  241. READ32(hdr->nops);
  242. return 0;
  243. }
  244. static int
  245. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  246. {
  247. __be32 *p;
  248. u32 op;
  249. int32_t nfserr;
  250. READ_BUF(8);
  251. READ32(op);
  252. if (op != expected) {
  253. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  254. " operation %d but we issued a request for %d\n",
  255. op, expected);
  256. return -EIO;
  257. }
  258. READ32(nfserr);
  259. if (nfserr != NFS_OK)
  260. return -nfs_cb_stat_to_errno(nfserr);
  261. return 0;
  262. }
  263. static int
  264. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  265. {
  266. return 0;
  267. }
  268. static int
  269. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
  270. {
  271. struct xdr_stream xdr;
  272. struct nfs4_cb_compound_hdr hdr;
  273. int status;
  274. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  275. status = decode_cb_compound_hdr(&xdr, &hdr);
  276. if (status)
  277. goto out;
  278. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  279. out:
  280. return status;
  281. }
  282. /*
  283. * RPC procedure tables
  284. */
  285. #define PROC(proc, call, argtype, restype) \
  286. [NFSPROC4_CLNT_##proc] = { \
  287. .p_proc = NFSPROC4_CB_##call, \
  288. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  289. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  290. .p_arglen = NFS4_##argtype##_sz, \
  291. .p_replen = NFS4_##restype##_sz, \
  292. .p_statidx = NFSPROC4_CB_##call, \
  293. .p_name = #proc, \
  294. }
  295. static struct rpc_procinfo nfs4_cb_procedures[] = {
  296. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  297. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  298. };
  299. static struct rpc_version nfs_cb_version4 = {
  300. .number = 1,
  301. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  302. .procs = nfs4_cb_procedures
  303. };
  304. static struct rpc_version * nfs_cb_version[] = {
  305. NULL,
  306. &nfs_cb_version4,
  307. };
  308. /* Reference counting, callback cleanup, etc., all look racy as heck.
  309. * And why is cb_set an atomic? */
  310. static int do_probe_callback(void *data)
  311. {
  312. struct nfs4_client *clp = data;
  313. struct nfs4_callback *cb = &clp->cl_callback;
  314. struct rpc_message msg = {
  315. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  316. .rpc_argp = clp,
  317. };
  318. int status;
  319. status = rpc_call_sync(cb->cb_client, &msg, RPC_TASK_SOFT);
  320. if (status) {
  321. rpc_shutdown_client(cb->cb_client);
  322. cb->cb_client = NULL;
  323. } else
  324. atomic_set(&cb->cb_set, 1);
  325. put_nfs4_client(clp);
  326. return 0;
  327. }
  328. /*
  329. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  330. */
  331. void
  332. nfsd4_probe_callback(struct nfs4_client *clp)
  333. {
  334. struct sockaddr_in addr;
  335. struct nfs4_callback *cb = &clp->cl_callback;
  336. struct rpc_timeout timeparms = {
  337. .to_initval = (NFSD_LEASE_TIME/4) * HZ,
  338. .to_retries = 5,
  339. .to_maxval = (NFSD_LEASE_TIME/2) * HZ,
  340. .to_exponential = 1,
  341. };
  342. struct rpc_program * program = &cb->cb_program;
  343. struct rpc_create_args args = {
  344. .protocol = IPPROTO_TCP,
  345. .address = (struct sockaddr *)&addr,
  346. .addrsize = sizeof(addr),
  347. .timeout = &timeparms,
  348. .program = program,
  349. .version = nfs_cb_version[1]->number,
  350. .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
  351. .flags = (RPC_CLNT_CREATE_NOPING),
  352. };
  353. struct task_struct *t;
  354. if (atomic_read(&cb->cb_set))
  355. return;
  356. /* Initialize address */
  357. memset(&addr, 0, sizeof(addr));
  358. addr.sin_family = AF_INET;
  359. addr.sin_port = htons(cb->cb_port);
  360. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  361. /* Initialize rpc_program */
  362. program->name = "nfs4_cb";
  363. program->number = cb->cb_prog;
  364. program->nrvers = ARRAY_SIZE(nfs_cb_version);
  365. program->version = nfs_cb_version;
  366. program->stats = &cb->cb_stat;
  367. /* Initialize rpc_stat */
  368. memset(program->stats, 0, sizeof(cb->cb_stat));
  369. program->stats->program = program;
  370. /* Create RPC client */
  371. cb->cb_client = rpc_create(&args);
  372. if (IS_ERR(cb->cb_client)) {
  373. dprintk("NFSD: couldn't create callback client\n");
  374. goto out_err;
  375. }
  376. /* the task holds a reference to the nfs4_client struct */
  377. atomic_inc(&clp->cl_count);
  378. t = kthread_run(do_probe_callback, clp, "nfs4_cb_probe");
  379. if (IS_ERR(t))
  380. goto out_release_clp;
  381. return;
  382. out_release_clp:
  383. atomic_dec(&clp->cl_count);
  384. rpc_shutdown_client(cb->cb_client);
  385. out_err:
  386. cb->cb_client = NULL;
  387. dprintk("NFSD: warning: no callback path to client %.*s\n",
  388. (int)clp->cl_name.len, clp->cl_name.data);
  389. }
  390. /*
  391. * called with dp->dl_count inc'ed.
  392. * nfs4_lock_state() may or may not have been called.
  393. */
  394. void
  395. nfsd4_cb_recall(struct nfs4_delegation *dp)
  396. {
  397. struct nfs4_client *clp = dp->dl_client;
  398. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  399. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  400. struct rpc_message msg = {
  401. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  402. .rpc_argp = cbr,
  403. };
  404. int retries = 1;
  405. int status = 0;
  406. if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
  407. return;
  408. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  409. cbr->cbr_dp = dp;
  410. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  411. while (retries--) {
  412. switch (status) {
  413. case -EIO:
  414. /* Network partition? */
  415. case -EBADHANDLE:
  416. case -NFS4ERR_BAD_STATEID:
  417. /* Race: client probably got cb_recall
  418. * before open reply granting delegation */
  419. break;
  420. default:
  421. goto out_put_cred;
  422. }
  423. ssleep(2);
  424. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  425. }
  426. out_put_cred:
  427. if (status == -EIO)
  428. atomic_set(&clp->cl_callback.cb_set, 0);
  429. /* Success or failure, now we're either waiting for lease expiration
  430. * or deleg_return. */
  431. dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
  432. put_nfs4_client(clp);
  433. nfs4_put_delegation(dp);
  434. return;
  435. }