nfs4callback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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/config.h>
  36. #include <linux/module.h>
  37. #include <linux/list.h>
  38. #include <linux/inet.h>
  39. #include <linux/errno.h>
  40. #include <linux/delay.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 16
  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 u32 *
  79. xdr_writemem(u32 *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.", \
  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. u32 * 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. u32 *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, u32 *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, u32 *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. u32 *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. u32 *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, u32 *p)
  264. {
  265. return 0;
  266. }
  267. static int
  268. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, u32 *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. #ifndef MAX
  285. # define MAX(a, b) (((a) > (b))? (a) : (b))
  286. #endif
  287. #define PROC(proc, call, argtype, restype) \
  288. [NFSPROC4_CLNT_##proc] = { \
  289. .p_proc = NFSPROC4_CB_##call, \
  290. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  291. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  292. .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
  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 = sizeof(nfs4_cb_procedures)/sizeof(nfs4_cb_procedures[0]),
  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. struct rpc_xprt * xprt;
  336. struct rpc_program * program = &cb->cb_program;
  337. struct rpc_stat * stat = &cb->cb_stat;
  338. struct rpc_clnt * clnt;
  339. struct rpc_message msg = {
  340. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  341. .rpc_argp = clp,
  342. };
  343. char hostname[32];
  344. int status;
  345. if (atomic_read(&cb->cb_set))
  346. return;
  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. /* Initialize timeout */
  353. timeparms.to_initval = (NFSD_LEASE_TIME/4) * HZ;
  354. timeparms.to_retries = 0;
  355. timeparms.to_maxval = (NFSD_LEASE_TIME/2) * HZ;
  356. timeparms.to_exponential = 1;
  357. /* Create RPC transport */
  358. xprt = xprt_create_proto(IPPROTO_TCP, &addr, &timeparms);
  359. if (IS_ERR(xprt)) {
  360. dprintk("NFSD: couldn't create callback transport!\n");
  361. goto out_err;
  362. }
  363. /* Initialize rpc_program */
  364. program->name = "nfs4_cb";
  365. program->number = cb->cb_prog;
  366. program->nrvers = sizeof(nfs_cb_version)/sizeof(nfs_cb_version[0]);
  367. program->version = nfs_cb_version;
  368. program->stats = stat;
  369. /* Initialize rpc_stat */
  370. memset(stat, 0, sizeof(struct rpc_stat));
  371. stat->program = program;
  372. /* Create RPC client
  373. *
  374. * XXX AUTH_UNIX only - need AUTH_GSS....
  375. */
  376. sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr.sin_addr.s_addr));
  377. clnt = rpc_new_client(xprt, hostname, program, 1, RPC_AUTH_UNIX);
  378. if (IS_ERR(clnt)) {
  379. dprintk("NFSD: couldn't create callback client\n");
  380. goto out_err;
  381. }
  382. clnt->cl_intr = 0;
  383. clnt->cl_softrtry = 1;
  384. /* Kick rpciod, put the call on the wire. */
  385. if (rpciod_up() != 0) {
  386. dprintk("nfsd: couldn't start rpciod for callbacks!\n");
  387. goto out_clnt;
  388. }
  389. /* the task holds a reference to the nfs4_client struct */
  390. cb->cb_client = clnt;
  391. atomic_inc(&clp->cl_count);
  392. msg.rpc_cred = nfsd4_lookupcred(clp,0);
  393. if (IS_ERR(msg.rpc_cred))
  394. goto out_rpciod;
  395. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, &nfs4_cb_null_ops, NULL);
  396. put_rpccred(msg.rpc_cred);
  397. if (status != 0) {
  398. dprintk("NFSD: asynchronous NFSPROC4_CB_NULL failed!\n");
  399. goto out_rpciod;
  400. }
  401. return;
  402. out_rpciod:
  403. atomic_dec(&clp->cl_count);
  404. rpciod_down();
  405. out_clnt:
  406. rpc_shutdown_client(clnt);
  407. goto out_err;
  408. out_err:
  409. dprintk("NFSD: warning: no callback path to client %.*s\n",
  410. (int)clp->cl_name.len, clp->cl_name.data);
  411. cb->cb_client = NULL;
  412. }
  413. static void
  414. nfs4_cb_null(struct rpc_task *task, void *dummy)
  415. {
  416. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  417. struct nfs4_callback *cb = &clp->cl_callback;
  418. u32 addr = htonl(cb->cb_addr);
  419. dprintk("NFSD: nfs4_cb_null task->tk_status %d\n", task->tk_status);
  420. if (task->tk_status < 0) {
  421. dprintk("NFSD: callback establishment to client %.*s failed\n",
  422. (int)clp->cl_name.len, clp->cl_name.data);
  423. goto out;
  424. }
  425. atomic_set(&cb->cb_set, 1);
  426. dprintk("NFSD: callback set to client %u.%u.%u.%u\n", NIPQUAD(addr));
  427. out:
  428. put_nfs4_client(clp);
  429. }
  430. static const struct rpc_call_ops nfs4_cb_null_ops = {
  431. .rpc_call_done = nfs4_cb_null,
  432. };
  433. /*
  434. * called with dp->dl_count inc'ed.
  435. * nfs4_lock_state() may or may not have been called.
  436. */
  437. void
  438. nfsd4_cb_recall(struct nfs4_delegation *dp)
  439. {
  440. struct nfs4_client *clp = dp->dl_client;
  441. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  442. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  443. struct rpc_message msg = {
  444. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  445. .rpc_argp = cbr,
  446. };
  447. int retries = 1;
  448. int status = 0;
  449. if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
  450. return;
  451. msg.rpc_cred = nfsd4_lookupcred(clp, 0);
  452. if (IS_ERR(msg.rpc_cred))
  453. goto out;
  454. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  455. cbr->cbr_dp = dp;
  456. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  457. while (retries--) {
  458. switch (status) {
  459. case -EIO:
  460. /* Network partition? */
  461. case -EBADHANDLE:
  462. case -NFS4ERR_BAD_STATEID:
  463. /* Race: client probably got cb_recall
  464. * before open reply granting delegation */
  465. break;
  466. default:
  467. goto out_put_cred;
  468. }
  469. ssleep(2);
  470. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  471. }
  472. out_put_cred:
  473. put_rpccred(msg.rpc_cred);
  474. out:
  475. if (status == -EIO)
  476. atomic_set(&clp->cl_callback.cb_set, 0);
  477. /* Success or failure, now we're either waiting for lease expiration
  478. * or deleg_return. */
  479. dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
  480. nfs4_put_delegation(dp);
  481. return;
  482. }