nfs4callback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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/sunrpc/xdr.h>
  41. #include <linux/sunrpc/svc.h>
  42. #include <linux/sunrpc/clnt.h>
  43. #include <linux/nfsd/nfsd.h>
  44. #include <linux/nfsd/state.h>
  45. #include <linux/sunrpc/sched.h>
  46. #include <linux/nfs4.h>
  47. #define NFSDDBG_FACILITY NFSDDBG_PROC
  48. #define NFSPROC4_CB_NULL 0
  49. #define NFSPROC4_CB_COMPOUND 1
  50. /* declarations */
  51. static const struct rpc_call_ops nfs4_cb_null_ops;
  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 16
  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), __FUNCTION__); \
  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. __FUNCTION__, __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_fhlen;
  202. RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
  203. WRITE32(OP_CB_RECALL);
  204. WRITEMEM(&cb_rec->cbr_stateid, sizeof(stateid_t));
  205. WRITE32(cb_rec->cbr_trunc);
  206. WRITE32(len);
  207. WRITEMEM(cb_rec->cbr_fhval, len);
  208. return 0;
  209. }
  210. static int
  211. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  212. {
  213. struct xdr_stream xdrs, *xdr = &xdrs;
  214. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  215. RESERVE_SPACE(0);
  216. return 0;
  217. }
  218. static int
  219. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_cb_recall *args)
  220. {
  221. struct xdr_stream xdr;
  222. struct nfs4_cb_compound_hdr hdr = {
  223. .ident = args->cbr_ident,
  224. .nops = 1,
  225. };
  226. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  227. encode_cb_compound_hdr(&xdr, &hdr);
  228. return (encode_cb_recall(&xdr, args));
  229. }
  230. static int
  231. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  232. __be32 *p;
  233. READ_BUF(8);
  234. READ32(hdr->status);
  235. READ32(hdr->taglen);
  236. READ_BUF(hdr->taglen + 4);
  237. hdr->tag = (char *)p;
  238. p += XDR_QUADLEN(hdr->taglen);
  239. READ32(hdr->nops);
  240. return 0;
  241. }
  242. static int
  243. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  244. {
  245. __be32 *p;
  246. u32 op;
  247. int32_t nfserr;
  248. READ_BUF(8);
  249. READ32(op);
  250. if (op != expected) {
  251. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  252. " operation %d but we issued a request for %d\n",
  253. op, expected);
  254. return -EIO;
  255. }
  256. READ32(nfserr);
  257. if (nfserr != NFS_OK)
  258. return -nfs_cb_stat_to_errno(nfserr);
  259. return 0;
  260. }
  261. static int
  262. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  263. {
  264. return 0;
  265. }
  266. static int
  267. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
  268. {
  269. struct xdr_stream xdr;
  270. struct nfs4_cb_compound_hdr hdr;
  271. int status;
  272. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  273. status = decode_cb_compound_hdr(&xdr, &hdr);
  274. if (status)
  275. goto out;
  276. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  277. out:
  278. return status;
  279. }
  280. /*
  281. * RPC procedure tables
  282. */
  283. #ifndef MAX
  284. # define MAX(a, b) (((a) > (b))? (a) : (b))
  285. #endif
  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_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
  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. /*
  309. * Use the SETCLIENTID credential
  310. */
  311. static struct rpc_cred *
  312. nfsd4_lookupcred(struct nfs4_client *clp, int taskflags)
  313. {
  314. struct auth_cred acred;
  315. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  316. struct rpc_cred *ret;
  317. get_group_info(clp->cl_cred.cr_group_info);
  318. acred.uid = clp->cl_cred.cr_uid;
  319. acred.gid = clp->cl_cred.cr_gid;
  320. acred.group_info = clp->cl_cred.cr_group_info;
  321. dprintk("NFSD: looking up %s cred\n",
  322. clnt->cl_auth->au_ops->au_name);
  323. ret = rpcauth_lookup_credcache(clnt->cl_auth, &acred, taskflags);
  324. put_group_info(clp->cl_cred.cr_group_info);
  325. return ret;
  326. }
  327. /*
  328. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  329. */
  330. void
  331. nfsd4_probe_callback(struct nfs4_client *clp)
  332. {
  333. struct sockaddr_in addr;
  334. struct nfs4_callback *cb = &clp->cl_callback;
  335. struct rpc_timeout timeparms = {
  336. .to_initval = (NFSD_LEASE_TIME/4) * HZ,
  337. .to_retries = 5,
  338. .to_maxval = (NFSD_LEASE_TIME/2) * HZ,
  339. .to_exponential = 1,
  340. };
  341. struct rpc_program * program = &cb->cb_program;
  342. struct rpc_create_args args = {
  343. .protocol = IPPROTO_TCP,
  344. .address = (struct sockaddr *)&addr,
  345. .addrsize = sizeof(addr),
  346. .timeout = &timeparms,
  347. .program = program,
  348. .version = nfs_cb_version[1]->number,
  349. .authflavor = RPC_AUTH_UNIX, /* XXX: need AUTH_GSS... */
  350. .flags = (RPC_CLNT_CREATE_NOPING),
  351. };
  352. struct rpc_message msg = {
  353. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  354. .rpc_argp = clp,
  355. };
  356. char clientname[16];
  357. int status;
  358. if (atomic_read(&cb->cb_set))
  359. return;
  360. /* Initialize address */
  361. memset(&addr, 0, sizeof(addr));
  362. addr.sin_family = AF_INET;
  363. addr.sin_port = htons(cb->cb_port);
  364. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  365. /* Initialize rpc_program */
  366. program->name = "nfs4_cb";
  367. program->number = cb->cb_prog;
  368. program->nrvers = ARRAY_SIZE(nfs_cb_version);
  369. program->version = nfs_cb_version;
  370. program->stats = &cb->cb_stat;
  371. /* Initialize rpc_stat */
  372. memset(program->stats, 0, sizeof(cb->cb_stat));
  373. program->stats->program = program;
  374. /* Just here to make some printk's more useful: */
  375. snprintf(clientname, sizeof(clientname),
  376. "%u.%u.%u.%u", NIPQUAD(addr.sin_addr));
  377. args.servername = clientname;
  378. /* Create RPC client */
  379. cb->cb_client = rpc_create(&args);
  380. if (IS_ERR(cb->cb_client)) {
  381. dprintk("NFSD: couldn't create callback client\n");
  382. goto out_err;
  383. }
  384. /* Kick rpciod, put the call on the wire. */
  385. if (rpciod_up() != 0)
  386. goto out_clnt;
  387. /* the task holds a reference to the nfs4_client struct */
  388. atomic_inc(&clp->cl_count);
  389. msg.rpc_cred = nfsd4_lookupcred(clp,0);
  390. if (IS_ERR(msg.rpc_cred))
  391. goto out_rpciod;
  392. status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_ASYNC, &nfs4_cb_null_ops, NULL);
  393. put_rpccred(msg.rpc_cred);
  394. if (status != 0) {
  395. dprintk("NFSD: asynchronous NFSPROC4_CB_NULL failed!\n");
  396. goto out_rpciod;
  397. }
  398. return;
  399. out_rpciod:
  400. atomic_dec(&clp->cl_count);
  401. rpciod_down();
  402. out_clnt:
  403. rpc_shutdown_client(cb->cb_client);
  404. out_err:
  405. cb->cb_client = NULL;
  406. dprintk("NFSD: warning: no callback path to client %.*s\n",
  407. (int)clp->cl_name.len, clp->cl_name.data);
  408. }
  409. static void
  410. nfs4_cb_null(struct rpc_task *task, void *dummy)
  411. {
  412. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  413. struct nfs4_callback *cb = &clp->cl_callback;
  414. __be32 addr = htonl(cb->cb_addr);
  415. dprintk("NFSD: nfs4_cb_null task->tk_status %d\n", task->tk_status);
  416. if (task->tk_status < 0) {
  417. dprintk("NFSD: callback establishment to client %.*s failed\n",
  418. (int)clp->cl_name.len, clp->cl_name.data);
  419. goto out;
  420. }
  421. atomic_set(&cb->cb_set, 1);
  422. dprintk("NFSD: callback set to client %u.%u.%u.%u\n", NIPQUAD(addr));
  423. out:
  424. put_nfs4_client(clp);
  425. }
  426. static const struct rpc_call_ops nfs4_cb_null_ops = {
  427. .rpc_call_done = nfs4_cb_null,
  428. };
  429. /*
  430. * called with dp->dl_count inc'ed.
  431. * nfs4_lock_state() may or may not have been called.
  432. */
  433. void
  434. nfsd4_cb_recall(struct nfs4_delegation *dp)
  435. {
  436. struct nfs4_client *clp = dp->dl_client;
  437. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  438. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  439. struct rpc_message msg = {
  440. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  441. .rpc_argp = cbr,
  442. };
  443. int retries = 1;
  444. int status = 0;
  445. if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
  446. return;
  447. msg.rpc_cred = nfsd4_lookupcred(clp, 0);
  448. if (IS_ERR(msg.rpc_cred))
  449. goto out;
  450. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  451. cbr->cbr_dp = dp;
  452. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  453. while (retries--) {
  454. switch (status) {
  455. case -EIO:
  456. /* Network partition? */
  457. case -EBADHANDLE:
  458. case -NFS4ERR_BAD_STATEID:
  459. /* Race: client probably got cb_recall
  460. * before open reply granting delegation */
  461. break;
  462. default:
  463. goto out_put_cred;
  464. }
  465. ssleep(2);
  466. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  467. }
  468. out_put_cred:
  469. put_rpccred(msg.rpc_cred);
  470. out:
  471. if (status == -EIO)
  472. atomic_set(&clp->cl_callback.cb_set, 0);
  473. /* Success or failure, now we're either waiting for lease expiration
  474. * or deleg_return. */
  475. dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
  476. nfs4_put_delegation(dp);
  477. return;
  478. }