nfs4callback.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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), __func__); \
  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. __func__, __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. WRITE32(cb_rec->cbr_stateid.si_generation);
  207. WRITEMEM(&cb_rec->cbr_stateid.si_opaque, sizeof(stateid_opaque_t));
  208. WRITE32(cb_rec->cbr_trunc);
  209. WRITE32(len);
  210. WRITEMEM(cb_rec->cbr_fhval, len);
  211. return 0;
  212. }
  213. static int
  214. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  215. {
  216. struct xdr_stream xdrs, *xdr = &xdrs;
  217. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  218. RESERVE_SPACE(0);
  219. return 0;
  220. }
  221. static int
  222. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_cb_recall *args)
  223. {
  224. struct xdr_stream xdr;
  225. struct nfs4_cb_compound_hdr hdr = {
  226. .ident = args->cbr_ident,
  227. .nops = 1,
  228. };
  229. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  230. encode_cb_compound_hdr(&xdr, &hdr);
  231. return (encode_cb_recall(&xdr, args));
  232. }
  233. static int
  234. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  235. __be32 *p;
  236. READ_BUF(8);
  237. READ32(hdr->status);
  238. READ32(hdr->taglen);
  239. READ_BUF(hdr->taglen + 4);
  240. hdr->tag = (char *)p;
  241. p += XDR_QUADLEN(hdr->taglen);
  242. READ32(hdr->nops);
  243. return 0;
  244. }
  245. static int
  246. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  247. {
  248. __be32 *p;
  249. u32 op;
  250. int32_t nfserr;
  251. READ_BUF(8);
  252. READ32(op);
  253. if (op != expected) {
  254. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  255. " operation %d but we issued a request for %d\n",
  256. op, expected);
  257. return -EIO;
  258. }
  259. READ32(nfserr);
  260. if (nfserr != NFS_OK)
  261. return -nfs_cb_stat_to_errno(nfserr);
  262. return 0;
  263. }
  264. static int
  265. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  266. {
  267. return 0;
  268. }
  269. static int
  270. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
  271. {
  272. struct xdr_stream xdr;
  273. struct nfs4_cb_compound_hdr hdr;
  274. int status;
  275. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  276. status = decode_cb_compound_hdr(&xdr, &hdr);
  277. if (status)
  278. goto out;
  279. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  280. out:
  281. return status;
  282. }
  283. /*
  284. * RPC procedure tables
  285. */
  286. #define PROC(proc, call, argtype, restype) \
  287. [NFSPROC4_CLNT_##proc] = { \
  288. .p_proc = NFSPROC4_CB_##call, \
  289. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  290. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  291. .p_arglen = NFS4_##argtype##_sz, \
  292. .p_replen = NFS4_##restype##_sz, \
  293. .p_statidx = NFSPROC4_CB_##call, \
  294. .p_name = #proc, \
  295. }
  296. static struct rpc_procinfo nfs4_cb_procedures[] = {
  297. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  298. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  299. };
  300. static struct rpc_version nfs_cb_version4 = {
  301. .number = 1,
  302. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  303. .procs = nfs4_cb_procedures
  304. };
  305. static struct rpc_version * nfs_cb_version[] = {
  306. NULL,
  307. &nfs_cb_version4,
  308. };
  309. static struct rpc_program cb_program;
  310. static struct rpc_stat cb_stats = {
  311. .program = &cb_program
  312. };
  313. #define NFS4_CALLBACK 0x40000000
  314. static struct rpc_program cb_program = {
  315. .name = "nfs4_cb",
  316. .number = NFS4_CALLBACK,
  317. .nrvers = ARRAY_SIZE(nfs_cb_version),
  318. .version = nfs_cb_version,
  319. .stats = &cb_stats,
  320. };
  321. /* Reference counting, callback cleanup, etc., all look racy as heck.
  322. * And why is cb_set an atomic? */
  323. static int do_probe_callback(void *data)
  324. {
  325. struct nfs4_client *clp = data;
  326. struct sockaddr_in addr;
  327. struct nfs4_callback *cb = &clp->cl_callback;
  328. struct rpc_timeout timeparms = {
  329. .to_initval = (NFSD_LEASE_TIME/4) * HZ,
  330. .to_retries = 5,
  331. .to_maxval = (NFSD_LEASE_TIME/2) * HZ,
  332. .to_exponential = 1,
  333. };
  334. struct rpc_create_args args = {
  335. .protocol = IPPROTO_TCP,
  336. .address = (struct sockaddr *)&addr,
  337. .addrsize = sizeof(addr),
  338. .timeout = &timeparms,
  339. .program = &cb_program,
  340. .prognumber = cb->cb_prog,
  341. .version = nfs_cb_version[1]->number,
  342. .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
  343. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  344. };
  345. struct rpc_message msg = {
  346. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  347. .rpc_argp = clp,
  348. };
  349. struct rpc_clnt *client;
  350. int status;
  351. /* Initialize address */
  352. memset(&addr, 0, sizeof(addr));
  353. addr.sin_family = AF_INET;
  354. addr.sin_port = htons(cb->cb_port);
  355. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  356. /* Create RPC client */
  357. client = rpc_create(&args);
  358. if (IS_ERR(client)) {
  359. dprintk("NFSD: couldn't create callback client\n");
  360. status = PTR_ERR(client);
  361. goto out_err;
  362. }
  363. status = rpc_call_sync(client, &msg, RPC_TASK_SOFT);
  364. if (status)
  365. goto out_release_client;
  366. cb->cb_client = client;
  367. atomic_set(&cb->cb_set, 1);
  368. put_nfs4_client(clp);
  369. return 0;
  370. out_release_client:
  371. rpc_shutdown_client(client);
  372. out_err:
  373. dprintk("NFSD: warning: no callback path to client %.*s\n",
  374. (int)clp->cl_name.len, clp->cl_name.data);
  375. put_nfs4_client(clp);
  376. return status;
  377. }
  378. /*
  379. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  380. */
  381. void
  382. nfsd4_probe_callback(struct nfs4_client *clp)
  383. {
  384. struct task_struct *t;
  385. BUG_ON(atomic_read(&clp->cl_callback.cb_set));
  386. /* the task holds a reference to the nfs4_client struct */
  387. atomic_inc(&clp->cl_count);
  388. t = kthread_run(do_probe_callback, clp, "nfs4_cb_probe");
  389. if (IS_ERR(t))
  390. atomic_dec(&clp->cl_count);
  391. return;
  392. }
  393. /*
  394. * called with dp->dl_count inc'ed.
  395. * nfs4_lock_state() may or may not have been called.
  396. */
  397. void
  398. nfsd4_cb_recall(struct nfs4_delegation *dp)
  399. {
  400. struct nfs4_client *clp = dp->dl_client;
  401. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  402. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  403. struct rpc_message msg = {
  404. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  405. .rpc_argp = cbr,
  406. };
  407. int retries = 1;
  408. int status = 0;
  409. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  410. cbr->cbr_dp = dp;
  411. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  412. while (retries--) {
  413. switch (status) {
  414. case -EIO:
  415. /* Network partition? */
  416. atomic_set(&clp->cl_callback.cb_set, 0);
  417. case -EBADHANDLE:
  418. case -NFS4ERR_BAD_STATEID:
  419. /* Race: client probably got cb_recall
  420. * before open reply granting delegation */
  421. break;
  422. default:
  423. goto out_put_cred;
  424. }
  425. ssleep(2);
  426. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  427. }
  428. out_put_cred:
  429. /*
  430. * Success or failure, now we're either waiting for lease expiration
  431. * or deleg_return.
  432. */
  433. put_nfs4_client(clp);
  434. nfs4_put_delegation(dp);
  435. return;
  436. }