nfs4callback.c 15 KB

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