nfs4callback.c 15 KB

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