nfs4callback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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/sunrpc/xdr.h>
  42. #include <linux/sunrpc/svc.h>
  43. #include <linux/sunrpc/clnt.h>
  44. #include <linux/nfsd/nfsd.h>
  45. #include <linux/nfsd/state.h>
  46. #include <linux/sunrpc/sched.h>
  47. #include <linux/nfs4.h>
  48. #define NFSDDBG_FACILITY NFSDDBG_PROC
  49. #define NFSPROC4_CB_NULL 0
  50. #define NFSPROC4_CB_COMPOUND 1
  51. /* declarations */
  52. static const struct rpc_call_ops nfs4_cb_null_ops;
  53. /* Index of predefined Linux callback client operations */
  54. enum {
  55. NFSPROC4_CLNT_CB_NULL = 0,
  56. NFSPROC4_CLNT_CB_RECALL,
  57. };
  58. enum nfs_cb_opnum4 {
  59. OP_CB_RECALL = 4,
  60. };
  61. #define NFS4_MAXTAGLEN 20
  62. #define NFS4_enc_cb_null_sz 0
  63. #define NFS4_dec_cb_null_sz 0
  64. #define cb_compound_enc_hdr_sz 4
  65. #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
  66. #define op_enc_sz 1
  67. #define op_dec_sz 2
  68. #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
  69. #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
  70. #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
  71. 1 + enc_stateid_sz + \
  72. enc_nfs4_fh_sz)
  73. #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
  74. op_dec_sz)
  75. /*
  76. * Generic encode routines from fs/nfs/nfs4xdr.c
  77. */
  78. static inline __be32 *
  79. xdr_writemem(__be32 *p, const void *ptr, int nbytes)
  80. {
  81. int tmp = XDR_QUADLEN(nbytes);
  82. if (!tmp)
  83. return p;
  84. p[tmp-1] = 0;
  85. memcpy(p, ptr, nbytes);
  86. return p + tmp;
  87. }
  88. #define WRITE32(n) *p++ = htonl(n)
  89. #define WRITEMEM(ptr,nbytes) do { \
  90. p = xdr_writemem(p, ptr, nbytes); \
  91. } while (0)
  92. #define RESERVE_SPACE(nbytes) do { \
  93. p = xdr_reserve_space(xdr, nbytes); \
  94. if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __FUNCTION__); \
  95. BUG_ON(!p); \
  96. } while (0)
  97. /*
  98. * Generic decode routines from fs/nfs/nfs4xdr.c
  99. */
  100. #define DECODE_TAIL \
  101. status = 0; \
  102. out: \
  103. return status; \
  104. xdr_error: \
  105. dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
  106. status = -EIO; \
  107. goto out
  108. #define READ32(x) (x) = ntohl(*p++)
  109. #define READ64(x) do { \
  110. (x) = (u64)ntohl(*p++) << 32; \
  111. (x) |= ntohl(*p++); \
  112. } while (0)
  113. #define READTIME(x) do { \
  114. p++; \
  115. (x.tv_sec) = ntohl(*p++); \
  116. (x.tv_nsec) = ntohl(*p++); \
  117. } while (0)
  118. #define READ_BUF(nbytes) do { \
  119. p = xdr_inline_decode(xdr, nbytes); \
  120. if (!p) { \
  121. dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
  122. __FUNCTION__, __LINE__); \
  123. return -EIO; \
  124. } \
  125. } while (0)
  126. struct nfs4_cb_compound_hdr {
  127. int status;
  128. u32 ident;
  129. u32 nops;
  130. u32 taglen;
  131. char * tag;
  132. };
  133. static struct {
  134. int stat;
  135. int errno;
  136. } nfs_cb_errtbl[] = {
  137. { NFS4_OK, 0 },
  138. { NFS4ERR_PERM, EPERM },
  139. { NFS4ERR_NOENT, ENOENT },
  140. { NFS4ERR_IO, EIO },
  141. { NFS4ERR_NXIO, ENXIO },
  142. { NFS4ERR_ACCESS, EACCES },
  143. { NFS4ERR_EXIST, EEXIST },
  144. { NFS4ERR_XDEV, EXDEV },
  145. { NFS4ERR_NOTDIR, ENOTDIR },
  146. { NFS4ERR_ISDIR, EISDIR },
  147. { NFS4ERR_INVAL, EINVAL },
  148. { NFS4ERR_FBIG, EFBIG },
  149. { NFS4ERR_NOSPC, ENOSPC },
  150. { NFS4ERR_ROFS, EROFS },
  151. { NFS4ERR_MLINK, EMLINK },
  152. { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
  153. { NFS4ERR_NOTEMPTY, ENOTEMPTY },
  154. { NFS4ERR_DQUOT, EDQUOT },
  155. { NFS4ERR_STALE, ESTALE },
  156. { NFS4ERR_BADHANDLE, EBADHANDLE },
  157. { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
  158. { NFS4ERR_NOTSUPP, ENOTSUPP },
  159. { NFS4ERR_TOOSMALL, ETOOSMALL },
  160. { NFS4ERR_SERVERFAULT, ESERVERFAULT },
  161. { NFS4ERR_BADTYPE, EBADTYPE },
  162. { NFS4ERR_LOCKED, EAGAIN },
  163. { NFS4ERR_RESOURCE, EREMOTEIO },
  164. { NFS4ERR_SYMLINK, ELOOP },
  165. { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
  166. { NFS4ERR_DEADLOCK, EDEADLK },
  167. { -1, EIO }
  168. };
  169. static int
  170. nfs_cb_stat_to_errno(int stat)
  171. {
  172. int i;
  173. for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
  174. if (nfs_cb_errtbl[i].stat == stat)
  175. return nfs_cb_errtbl[i].errno;
  176. }
  177. /* If we cannot translate the error, the recovery routines should
  178. * handle it.
  179. * Note: remaining NFSv4 error codes have values > 10000, so should
  180. * not conflict with native Linux error codes.
  181. */
  182. return stat;
  183. }
  184. /*
  185. * XDR encode
  186. */
  187. static int
  188. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  189. {
  190. __be32 * p;
  191. RESERVE_SPACE(16);
  192. WRITE32(0); /* tag length is always 0 */
  193. WRITE32(NFS4_MINOR_VERSION);
  194. WRITE32(hdr->ident);
  195. WRITE32(hdr->nops);
  196. return 0;
  197. }
  198. static int
  199. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_cb_recall *cb_rec)
  200. {
  201. __be32 *p;
  202. int len = cb_rec->cbr_fhlen;
  203. RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
  204. WRITE32(OP_CB_RECALL);
  205. WRITEMEM(&cb_rec->cbr_stateid, sizeof(stateid_t));
  206. WRITE32(cb_rec->cbr_trunc);
  207. WRITE32(len);
  208. WRITEMEM(cb_rec->cbr_fhval, 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. /*
  308. * Use the SETCLIENTID credential
  309. */
  310. static struct rpc_cred *
  311. nfsd4_lookupcred(struct nfs4_client *clp, int taskflags)
  312. {
  313. struct auth_cred acred;
  314. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  315. struct rpc_cred *ret;
  316. get_group_info(clp->cl_cred.cr_group_info);
  317. acred.uid = clp->cl_cred.cr_uid;
  318. acred.gid = clp->cl_cred.cr_gid;
  319. acred.group_info = clp->cl_cred.cr_group_info;
  320. dprintk("NFSD: looking up %s cred\n",
  321. clnt->cl_auth->au_ops->au_name);
  322. ret = rpcauth_lookup_credcache(clnt->cl_auth, &acred, taskflags);
  323. put_group_info(clp->cl_cred.cr_group_info);
  324. return ret;
  325. }
  326. /*
  327. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  328. */
  329. void
  330. nfsd4_probe_callback(struct nfs4_client *clp)
  331. {
  332. struct sockaddr_in addr;
  333. struct nfs4_callback *cb = &clp->cl_callback;
  334. struct rpc_timeout timeparms = {
  335. .to_initval = (NFSD_LEASE_TIME/4) * HZ,
  336. .to_retries = 5,
  337. .to_maxval = (NFSD_LEASE_TIME/2) * HZ,
  338. .to_exponential = 1,
  339. };
  340. struct rpc_program * program = &cb->cb_program;
  341. struct rpc_create_args args = {
  342. .protocol = IPPROTO_TCP,
  343. .address = (struct sockaddr *)&addr,
  344. .addrsize = sizeof(addr),
  345. .timeout = &timeparms,
  346. .program = program,
  347. .version = nfs_cb_version[1]->number,
  348. .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
  349. .flags = (RPC_CLNT_CREATE_NOPING),
  350. };
  351. struct rpc_message msg = {
  352. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  353. .rpc_argp = clp,
  354. };
  355. int status;
  356. if (atomic_read(&cb->cb_set))
  357. return;
  358. /* Initialize address */
  359. memset(&addr, 0, sizeof(addr));
  360. addr.sin_family = AF_INET;
  361. addr.sin_port = htons(cb->cb_port);
  362. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  363. /* Initialize rpc_program */
  364. program->name = "nfs4_cb";
  365. program->number = cb->cb_prog;
  366. program->nrvers = ARRAY_SIZE(nfs_cb_version);
  367. program->version = nfs_cb_version;
  368. program->stats = &cb->cb_stat;
  369. /* Initialize rpc_stat */
  370. memset(program->stats, 0, sizeof(cb->cb_stat));
  371. program->stats->program = program;
  372. /* Create RPC client */
  373. cb->cb_client = rpc_create(&args);
  374. if (IS_ERR(cb->cb_client)) {
  375. dprintk("NFSD: couldn't create callback client\n");
  376. goto out_err;
  377. }
  378. /* the task holds a reference to the nfs4_client struct */
  379. atomic_inc(&clp->cl_count);
  380. msg.rpc_cred = nfsd4_lookupcred(clp,0);
  381. if (IS_ERR(msg.rpc_cred))
  382. goto out_release_clp;
  383. status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_ASYNC, &nfs4_cb_null_ops, NULL);
  384. put_rpccred(msg.rpc_cred);
  385. if (status != 0) {
  386. dprintk("NFSD: asynchronous NFSPROC4_CB_NULL failed!\n");
  387. goto out_release_clp;
  388. }
  389. return;
  390. out_release_clp:
  391. atomic_dec(&clp->cl_count);
  392. rpc_shutdown_client(cb->cb_client);
  393. out_err:
  394. cb->cb_client = NULL;
  395. dprintk("NFSD: warning: no callback path to client %.*s\n",
  396. (int)clp->cl_name.len, clp->cl_name.data);
  397. }
  398. static void
  399. nfs4_cb_null(struct rpc_task *task, void *dummy)
  400. {
  401. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  402. struct nfs4_callback *cb = &clp->cl_callback;
  403. __be32 addr = htonl(cb->cb_addr);
  404. dprintk("NFSD: nfs4_cb_null task->tk_status %d\n", task->tk_status);
  405. if (task->tk_status < 0) {
  406. dprintk("NFSD: callback establishment to client %.*s failed\n",
  407. (int)clp->cl_name.len, clp->cl_name.data);
  408. goto out;
  409. }
  410. atomic_set(&cb->cb_set, 1);
  411. dprintk("NFSD: callback set to client %u.%u.%u.%u\n", NIPQUAD(addr));
  412. out:
  413. put_nfs4_client(clp);
  414. }
  415. static const struct rpc_call_ops nfs4_cb_null_ops = {
  416. .rpc_call_done = nfs4_cb_null,
  417. };
  418. /*
  419. * called with dp->dl_count inc'ed.
  420. * nfs4_lock_state() may or may not have been called.
  421. */
  422. void
  423. nfsd4_cb_recall(struct nfs4_delegation *dp)
  424. {
  425. struct nfs4_client *clp = dp->dl_client;
  426. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  427. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  428. struct rpc_message msg = {
  429. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  430. .rpc_argp = cbr,
  431. };
  432. int retries = 1;
  433. int status = 0;
  434. if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
  435. return;
  436. msg.rpc_cred = nfsd4_lookupcred(clp, 0);
  437. if (IS_ERR(msg.rpc_cred))
  438. goto out;
  439. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  440. cbr->cbr_dp = dp;
  441. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  442. while (retries--) {
  443. switch (status) {
  444. case -EIO:
  445. /* Network partition? */
  446. case -EBADHANDLE:
  447. case -NFS4ERR_BAD_STATEID:
  448. /* Race: client probably got cb_recall
  449. * before open reply granting delegation */
  450. break;
  451. default:
  452. goto out_put_cred;
  453. }
  454. ssleep(2);
  455. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  456. }
  457. out_put_cred:
  458. put_rpccred(msg.rpc_cred);
  459. out:
  460. if (status == -EIO)
  461. atomic_set(&clp->cl_callback.cb_set, 0);
  462. /* Success or failure, now we're either waiting for lease expiration
  463. * or deleg_return. */
  464. dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
  465. nfs4_put_delegation(dp);
  466. return;
  467. }