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 void nfs4_cb_null(struct rpc_task *task);
  53. extern spinlock_t recall_lock;
  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 16
  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 u32 *
  80. xdr_writemem(u32 *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), __FUNCTION__); \
  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.", \
  123. __FUNCTION__, __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. u32 * 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. u32 *p;
  203. int len = cb_rec->cbr_fhlen;
  204. RESERVE_SPACE(12+sizeof(cb_rec->cbr_stateid) + len);
  205. WRITE32(OP_CB_RECALL);
  206. WRITEMEM(&cb_rec->cbr_stateid, sizeof(stateid_t));
  207. WRITE32(cb_rec->cbr_trunc);
  208. WRITE32(len);
  209. WRITEMEM(cb_rec->cbr_fhval, len);
  210. return 0;
  211. }
  212. static int
  213. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, u32 *p)
  214. {
  215. struct xdr_stream xdrs, *xdr = &xdrs;
  216. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  217. RESERVE_SPACE(0);
  218. return 0;
  219. }
  220. static int
  221. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, u32 *p, struct nfs4_cb_recall *args)
  222. {
  223. struct xdr_stream xdr;
  224. struct nfs4_cb_compound_hdr hdr = {
  225. .ident = args->cbr_ident,
  226. .nops = 1,
  227. };
  228. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  229. encode_cb_compound_hdr(&xdr, &hdr);
  230. return (encode_cb_recall(&xdr, args));
  231. }
  232. static int
  233. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  234. u32 *p;
  235. READ_BUF(8);
  236. READ32(hdr->status);
  237. READ32(hdr->taglen);
  238. READ_BUF(hdr->taglen + 4);
  239. hdr->tag = (char *)p;
  240. p += XDR_QUADLEN(hdr->taglen);
  241. READ32(hdr->nops);
  242. return 0;
  243. }
  244. static int
  245. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  246. {
  247. u32 *p;
  248. u32 op;
  249. int32_t nfserr;
  250. READ_BUF(8);
  251. READ32(op);
  252. if (op != expected) {
  253. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  254. " operation %d but we issued a request for %d\n",
  255. op, expected);
  256. return -EIO;
  257. }
  258. READ32(nfserr);
  259. if (nfserr != NFS_OK)
  260. return -nfs_cb_stat_to_errno(nfserr);
  261. return 0;
  262. }
  263. static int
  264. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, u32 *p)
  265. {
  266. return 0;
  267. }
  268. static int
  269. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, u32 *p)
  270. {
  271. struct xdr_stream xdr;
  272. struct nfs4_cb_compound_hdr hdr;
  273. int status;
  274. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  275. status = decode_cb_compound_hdr(&xdr, &hdr);
  276. if (status)
  277. goto out;
  278. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  279. out:
  280. return status;
  281. }
  282. /*
  283. * RPC procedure tables
  284. */
  285. #ifndef MAX
  286. # define MAX(a, b) (((a) > (b))? (a) : (b))
  287. #endif
  288. #define PROC(proc, call, argtype, restype) \
  289. [NFSPROC4_CLNT_##proc] = { \
  290. .p_proc = NFSPROC4_CB_##call, \
  291. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  292. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  293. .p_bufsiz = MAX(NFS4_##argtype##_sz,NFS4_##restype##_sz) << 2, \
  294. }
  295. 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. struct rpc_version nfs_cb_version4 = {
  300. .number = 1,
  301. .nrprocs = sizeof(nfs4_cb_procedures)/sizeof(nfs4_cb_procedures[0]),
  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. 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. struct rpc_xprt * xprt;
  337. struct rpc_program * program = &cb->cb_program;
  338. struct rpc_stat * stat = &cb->cb_stat;
  339. struct rpc_clnt * clnt;
  340. struct rpc_message msg = {
  341. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
  342. .rpc_argp = clp,
  343. };
  344. char hostname[32];
  345. int status;
  346. dprintk("NFSD: probe_callback. cb_parsed %d cb_set %d\n",
  347. cb->cb_parsed, atomic_read(&cb->cb_set));
  348. if (!cb->cb_parsed || atomic_read(&cb->cb_set))
  349. return;
  350. /* Initialize address */
  351. memset(&addr, 0, sizeof(addr));
  352. addr.sin_family = AF_INET;
  353. addr.sin_port = htons(cb->cb_port);
  354. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  355. /* Initialize timeout */
  356. timeparms.to_initval = (NFSD_LEASE_TIME/4) * HZ;
  357. timeparms.to_retries = 0;
  358. timeparms.to_maxval = (NFSD_LEASE_TIME/2) * HZ;
  359. timeparms.to_exponential = 1;
  360. /* Create RPC transport */
  361. xprt = xprt_create_proto(IPPROTO_TCP, &addr, &timeparms);
  362. if (IS_ERR(xprt)) {
  363. dprintk("NFSD: couldn't create callback transport!\n");
  364. goto out_err;
  365. }
  366. /* Initialize rpc_program */
  367. program->name = "nfs4_cb";
  368. program->number = cb->cb_prog;
  369. program->nrvers = sizeof(nfs_cb_version)/sizeof(nfs_cb_version[0]);
  370. program->version = nfs_cb_version;
  371. program->stats = stat;
  372. /* Initialize rpc_stat */
  373. memset(stat, 0, sizeof(struct rpc_stat));
  374. stat->program = program;
  375. /* Create RPC client
  376. *
  377. * XXX AUTH_UNIX only - need AUTH_GSS....
  378. */
  379. sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(addr.sin_addr.s_addr));
  380. clnt = rpc_create_client(xprt, hostname, program, 1, RPC_AUTH_UNIX);
  381. if (IS_ERR(clnt)) {
  382. dprintk("NFSD: couldn't create callback client\n");
  383. goto out_err;
  384. }
  385. clnt->cl_intr = 0;
  386. clnt->cl_softrtry = 1;
  387. clnt->cl_chatty = 1;
  388. /* Kick rpciod, put the call on the wire. */
  389. if (rpciod_up() != 0) {
  390. dprintk("nfsd: couldn't start rpciod for callbacks!\n");
  391. goto out_clnt;
  392. }
  393. /* the task holds a reference to the nfs4_client struct */
  394. cb->cb_client = clnt;
  395. atomic_inc(&clp->cl_count);
  396. msg.rpc_cred = nfsd4_lookupcred(clp,0);
  397. if (IS_ERR(msg.rpc_cred))
  398. goto out_rpciod;
  399. status = rpc_call_async(clnt, &msg, RPC_TASK_ASYNC, nfs4_cb_null, NULL);
  400. put_rpccred(msg.rpc_cred);
  401. if (status != 0) {
  402. dprintk("NFSD: asynchronous NFSPROC4_CB_NULL failed!\n");
  403. goto out_rpciod;
  404. }
  405. return;
  406. out_rpciod:
  407. atomic_dec(&clp->cl_count);
  408. rpciod_down();
  409. out_clnt:
  410. rpc_shutdown_client(clnt);
  411. goto out_err;
  412. out_err:
  413. dprintk("NFSD: warning: no callback path to client %.*s\n",
  414. (int)clp->cl_name.len, clp->cl_name.data);
  415. cb->cb_client = NULL;
  416. }
  417. static void
  418. nfs4_cb_null(struct rpc_task *task)
  419. {
  420. struct nfs4_client *clp = (struct nfs4_client *)task->tk_msg.rpc_argp;
  421. struct nfs4_callback *cb = &clp->cl_callback;
  422. u32 addr = htonl(cb->cb_addr);
  423. dprintk("NFSD: nfs4_cb_null task->tk_status %d\n", task->tk_status);
  424. if (task->tk_status < 0) {
  425. dprintk("NFSD: callback establishment to client %.*s failed\n",
  426. (int)clp->cl_name.len, clp->cl_name.data);
  427. goto out;
  428. }
  429. atomic_set(&cb->cb_set, 1);
  430. dprintk("NFSD: callback set to client %u.%u.%u.%u\n", NIPQUAD(addr));
  431. out:
  432. put_nfs4_client(clp);
  433. }
  434. /*
  435. * called with dp->dl_count inc'ed.
  436. * nfs4_lock_state() may or may not have been called.
  437. */
  438. void
  439. nfsd4_cb_recall(struct nfs4_delegation *dp)
  440. {
  441. struct nfs4_client *clp = dp->dl_client;
  442. struct rpc_clnt *clnt = clp->cl_callback.cb_client;
  443. struct nfs4_cb_recall *cbr = &dp->dl_recall;
  444. struct rpc_message msg = {
  445. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  446. .rpc_argp = cbr,
  447. };
  448. int retries = 1;
  449. int status = 0;
  450. if ((!atomic_read(&clp->cl_callback.cb_set)) || !clnt)
  451. return;
  452. msg.rpc_cred = nfsd4_lookupcred(clp, 0);
  453. if (IS_ERR(msg.rpc_cred))
  454. goto out;
  455. cbr->cbr_trunc = 0; /* XXX need to implement truncate optimization */
  456. cbr->cbr_dp = dp;
  457. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  458. while (retries--) {
  459. switch (status) {
  460. case -EIO:
  461. /* Network partition? */
  462. case -EBADHANDLE:
  463. case -NFS4ERR_BAD_STATEID:
  464. /* Race: client probably got cb_recall
  465. * before open reply granting delegation */
  466. break;
  467. default:
  468. goto out_put_cred;
  469. }
  470. ssleep(2);
  471. status = rpc_call_sync(clnt, &msg, RPC_TASK_SOFT);
  472. }
  473. out_put_cred:
  474. put_rpccred(msg.rpc_cred);
  475. out:
  476. if (status == -EIO)
  477. atomic_set(&clp->cl_callback.cb_set, 0);
  478. /* Success or failure, now we're either waiting for lease expiration
  479. * or deleg_return. */
  480. dprintk("NFSD: nfs4_cb_recall: dp %p dl_flock %p dl_count %d\n",dp, dp->dl_flock, atomic_read(&dp->dl_count));
  481. nfs4_put_delegation(dp);
  482. return;
  483. }