nfs4callback.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. /* Index of predefined Linux callback client operations */
  53. enum {
  54. NFSPROC4_CLNT_CB_NULL = 0,
  55. NFSPROC4_CLNT_CB_RECALL,
  56. };
  57. enum nfs_cb_opnum4 {
  58. OP_CB_RECALL = 4,
  59. };
  60. #define NFS4_MAXTAGLEN 20
  61. #define NFS4_enc_cb_null_sz 0
  62. #define NFS4_dec_cb_null_sz 0
  63. #define cb_compound_enc_hdr_sz 4
  64. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  65. #define op_enc_sz 1
  66. #define op_dec_sz 2
  67. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  68. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  69. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  70. 1 + enc_stateid_sz + \
  71. enc_nfs4_fh_sz)
  72. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  73. op_dec_sz)
  74. /*
  75. * Generic encode routines from fs/nfs/nfs4xdr.c
  76. */
  77. static inline __be32 *
  78. xdr_writemem(__be32 *p, const void *ptr, int nbytes)
  79. {
  80. int tmp = XDR_QUADLEN(nbytes);
  81. if (!tmp)
  82. return p;
  83. p[tmp-1] = 0;
  84. memcpy(p, ptr, nbytes);
  85. return p + tmp;
  86. }
  87. #define WRITE32(n) *p++ = htonl(n)
  88. #define WRITEMEM(ptr,nbytes) do { \
  89. p = xdr_writemem(p, ptr, nbytes); \
  90. } while (0)
  91. #define RESERVE_SPACE(nbytes) do { \
  92. p = xdr_reserve_space(xdr, nbytes); \
  93. if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
  94. BUG_ON(!p); \
  95. } while (0)
  96. /*
  97. * Generic decode routines from fs/nfs/nfs4xdr.c
  98. */
  99. #define DECODE_TAIL \
  100. status = 0; \
  101. out: \
  102. return status; \
  103. xdr_error: \
  104. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  105. status = -EIO; \
  106. goto out
  107. #define READ32(x) (x) = ntohl(*p++)
  108. #define READ64(x) do { \
  109. (x) = (u64)ntohl(*p++) << 32; \
  110. (x) |= ntohl(*p++); \
  111. } while (0)
  112. #define READTIME(x) do { \
  113. p++; \
  114. (x.tv_sec) = ntohl(*p++); \
  115. (x.tv_nsec) = ntohl(*p++); \
  116. } while (0)
  117. #define READ_BUF(nbytes) do { \
  118. p = xdr_inline_decode(xdr, nbytes); \
  119. if (!p) { \
  120. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  121. __func__, __LINE__); \
  122. return -EIO; \
  123. } \
  124. } while (0)
  125. struct nfs4_cb_compound_hdr {
  126. int status;
  127. u32 ident;
  128. u32 nops;
  129. u32 taglen;
  130. char * tag;
  131. };
  132. static struct {
  133. int stat;
  134. int errno;
  135. } nfs_cb_errtbl[] = {
  136. { NFS4_OK, 0 },
  137. { NFS4ERR_PERM, EPERM },
  138. { NFS4ERR_NOENT, ENOENT },
  139. { NFS4ERR_IO, EIO },
  140. { NFS4ERR_NXIO, ENXIO },
  141. { NFS4ERR_ACCESS, EACCES },
  142. { NFS4ERR_EXIST, EEXIST },
  143. { NFS4ERR_XDEV, EXDEV },
  144. { NFS4ERR_NOTDIR, ENOTDIR },
  145. { NFS4ERR_ISDIR, EISDIR },
  146. { NFS4ERR_INVAL, EINVAL },
  147. { NFS4ERR_FBIG, EFBIG },
  148. { NFS4ERR_NOSPC, ENOSPC },
  149. { NFS4ERR_ROFS, EROFS },
  150. { NFS4ERR_MLINK, EMLINK },
  151. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  152. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  153. { NFS4ERR_DQUOT, EDQUOT },
  154. { NFS4ERR_STALE, ESTALE },
  155. { NFS4ERR_BADHANDLE, EBADHANDLE },
  156. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  157. { NFS4ERR_NOTSUPP, ENOTSUPP },
  158. { NFS4ERR_TOOSMALL, ETOOSMALL },
  159. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  160. { NFS4ERR_BADTYPE, EBADTYPE },
  161. { NFS4ERR_LOCKED, EAGAIN },
  162. { NFS4ERR_RESOURCE, EREMOTEIO },
  163. { NFS4ERR_SYMLINK, ELOOP },
  164. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  165. { NFS4ERR_DEADLOCK, EDEADLK },
  166. { -1, EIO }
  167. };
  168. static int
  169. nfs_cb_stat_to_errno(int stat)
  170. {
  171. int i;
  172. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  173. if (nfs_cb_errtbl[i].stat == stat)
  174. return nfs_cb_errtbl[i].errno;
  175. }
  176. /* If we cannot translate the error, the recovery routines should
  177. * handle it.
  178. * Note: remaining NFSv4 error codes have values > 10000, so should
  179. * not conflict with native Linux error codes.
  180. */
  181. return stat;
  182. }
  183. /*
  184. * XDR encode
  185. */
  186. static int
  187. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  188. {
  189. __be32 * p;
  190. RESERVE_SPACE(16);
  191. WRITE32(0); /* tag length is always 0 */
  192. WRITE32(NFS4_MINOR_VERSION);
  193. WRITE32(hdr->ident);
  194. WRITE32(hdr->nops);
  195. return 0;
  196. }
  197. static int
  198. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_cb_recall *cb_rec)
  199. {
  200. __be32 *p;
  201. int len = cb_rec->cbr_fh.fh_size;
  202. RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
  203. WRITE32(OP_CB_RECALL);
  204. WRITE32(cb_rec->cbr_stateid.si_generation);
  205. WRITEMEM(&cb_rec->cbr_stateid.si_opaque, sizeof(stateid_opaque_t));
  206. WRITE32(cb_rec->cbr_trunc);
  207. WRITE32(len);
  208. WRITEMEM(&cb_rec->cbr_fh.fh_base, len);
  209. return 0;
  210. }
  211. static int
  212. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  213. {
  214. struct xdr_stream xdrs, *xdr = &xdrs;
  215. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  216. RESERVE_SPACE(0);
  217. return 0;
  218. }
  219. static int
  220. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_cb_recall *args)
  221. {
  222. struct xdr_stream xdr;
  223. struct nfs4_cb_compound_hdr hdr = {
  224. .ident = args->cbr_ident,
  225. .nops = 1,
  226. };
  227. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  228. encode_cb_compound_hdr(&xdr, &hdr);
  229. return (encode_cb_recall(&xdr, args));
  230. }
  231. static int
  232. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  233. __be32 *p;
  234. READ_BUF(8);
  235. READ32(hdr->status);
  236. READ32(hdr->taglen);
  237. READ_BUF(hdr->taglen + 4);
  238. hdr->tag = (char *)p;
  239. p += XDR_QUADLEN(hdr->taglen);
  240. READ32(hdr->nops);
  241. return 0;
  242. }
  243. static int
  244. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  245. {
  246. __be32 *p;
  247. u32 op;
  248. int32_t nfserr;
  249. READ_BUF(8);
  250. READ32(op);
  251. if (op != expected) {
  252. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  253. " operation %d but we issued a request for %d\n",
  254. op, expected);
  255. return -EIO;
  256. }
  257. READ32(nfserr);
  258. if (nfserr != NFS_OK)
  259. return -nfs_cb_stat_to_errno(nfserr);
  260. return 0;
  261. }
  262. static int
  263. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  264. {
  265. return 0;
  266. }
  267. static int
  268. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
  269. {
  270. struct xdr_stream xdr;
  271. struct nfs4_cb_compound_hdr hdr;
  272. int status;
  273. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  274. status = decode_cb_compound_hdr(&xdr, &hdr);
  275. if (status)
  276. goto out;
  277. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  278. out:
  279. return status;
  280. }
  281. /*
  282. * RPC procedure tables
  283. */
  284. #define PROC(proc, call, argtype, restype) \
  285. [NFSPROC4_CLNT_##proc] = { \
  286. .p_proc = NFSPROC4_CB_##call, \
  287. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  288. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  289. .p_arglen = NFS4_##argtype##_sz, \
  290. .p_replen = NFS4_##restype##_sz, \
  291. .p_statidx = NFSPROC4_CB_##call, \
  292. .p_name = #proc, \
  293. }
  294. static struct rpc_procinfo nfs4_cb_procedures[] = {
  295. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  296. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  297. };
  298. static struct rpc_version nfs_cb_version4 = {
  299. .number = 1,
  300. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  301. .procs = nfs4_cb_procedures
  302. };
  303. static struct rpc_version * nfs_cb_version[] = {
  304. NULL,
  305. &nfs_cb_version4,
  306. };
  307. static struct rpc_program cb_program;
  308. static struct rpc_stat cb_stats = {
  309. .program = &cb_program
  310. };
  311. #define NFS4_CALLBACK 0x40000000
  312. static struct rpc_program cb_program = {
  313. .name = "nfs4_cb",
  314. .number = NFS4_CALLBACK,
  315. .nrvers = ARRAY_SIZE(nfs_cb_version),
  316. .version = nfs_cb_version,
  317. .stats = &cb_stats,
  318. .pipe_dir_name = "/nfsd4_cb",
  319. };
  320. /* Reference counting, callback cleanup, etc., all look racy as heck.
  321. * And why is cb_set an atomic? */
  322. static struct rpc_clnt *setup_callback_client(struct nfs4_client *clp)
  323. {
  324. struct sockaddr_in addr;
  325. struct nfs4_callback *cb = &clp->cl_callback;
  326. struct rpc_timeout timeparms = {
  327. .to_initval = (NFSD_LEASE_TIME/4) * HZ,
  328. .to_retries = 5,
  329. .to_maxval = (NFSD_LEASE_TIME/2) * HZ,
  330. .to_exponential = 1,
  331. };
  332. struct rpc_create_args args = {
  333. .protocol = IPPROTO_TCP,
  334. .address = (struct sockaddr *)&addr,
  335. .addrsize = sizeof(addr),
  336. .timeout = &timeparms,
  337. .program = &cb_program,
  338. .prognumber = cb->cb_prog,
  339. .version = nfs_cb_version[1]->number,
  340. .authflavor = clp->cl_flavor,
  341. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  342. .client_name = clp->cl_principal,
  343. };
  344. struct rpc_clnt *client;
  345. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  346. return ERR_PTR(-EINVAL);
  347. /* Initialize address */
  348. memset(&addr, 0, sizeof(addr));
  349. addr.sin_family = AF_INET;
  350. addr.sin_port = htons(cb->cb_port);
  351. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  352. /* Create RPC client */
  353. client = rpc_create(&args);
  354. if (IS_ERR(client))
  355. dprintk("NFSD: couldn't create callback client: %ld\n",
  356. PTR_ERR(client));
  357. return client;
  358. }
  359. static int do_probe_callback(void *data)
  360. {
  361. struct nfs4_client *clp = data;
  362. struct nfs4_callback *cb = &clp->cl_callback;
  363. struct rpc_message msg = {
  364. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  365. .rpc_argp = clp,
  366. };
  367. struct rpc_clnt *client;
  368. int status;
  369. client = setup_callback_client(clp);
  370. if (IS_ERR(client)) {
  371. status = PTR_ERR(client);
  372. dprintk("NFSD: couldn't create callback client: %d\n",
  373. status);
  374. goto out_err;
  375. }
  376. status = rpc_call_sync(client, &msg, RPC_TASK_SOFT);
  377. if (status)
  378. goto out_release_client;
  379. cb->cb_client = client;
  380. atomic_set(&cb->cb_set, 1);
  381. put_nfs4_client(clp);
  382. return 0;
  383. out_release_client:
  384. rpc_shutdown_client(client);
  385. out_err:
  386. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  387. (int)clp->cl_name.len, clp->cl_name.data, status);
  388. put_nfs4_client(clp);
  389. return 0;
  390. }
  391. /*
  392. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  393. */
  394. void
  395. nfsd4_probe_callback(struct nfs4_client *clp)
  396. {
  397. struct task_struct *t;
  398. BUG_ON(atomic_read(&clp->cl_callback.cb_set));
  399. /* the task holds a reference to the nfs4_client struct */
  400. atomic_inc(&clp->cl_count);
  401. t = kthread_run(do_probe_callback, clp, "nfs4_cb_probe");
  402. if (IS_ERR(t))
  403. atomic_dec(&clp->cl_count);
  404. return;
  405. }
  406. /*
  407. * called with dp->dl_count inc'ed.
  408. */
  409. void
  410. nfsd4_cb_recall(struct nfs4_delegation *dp)
  411. {
  412. struct nfs4_client *clp = dp->dl_client;
  413. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  414. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  415. struct rpc_message msg = {
  416. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  417. .rpc_argp = cbr,
  418. };
  419. int retries = 1;
  420. int status = 0;
  421. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  422. cbr->cbr_dp = dp;
  423. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  424. while (retries--) {
  425. switch (status) {
  426. case -EIO:
  427. /* Network partition? */
  428. atomic_set(&clp->cl_callback.cb_set, 0);
  429. case -EBADHANDLE:
  430. case -NFS4ERR_BAD_STATEID:
  431. /* Race: client probably got cb_recall
  432. * before open reply granting delegation */
  433. break;
  434. default:
  435. goto out_put_cred;
  436. }
  437. ssleep(2);
  438. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  439. }
  440. out_put_cred:
  441. /*
  442. * Success or failure, now we're either waiting for lease expiration
  443. * or deleg_return.
  444. */
  445. put_nfs4_client(clp);
  446. nfs4_put_delegation(dp);
  447. return;
  448. }