nfs4callback.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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/nfsd/nfsd.h>
  46. #include <linux/nfsd/state.h>
  47. #include <linux/sunrpc/sched.h>
  48. #include <linux/nfs4.h>
  49. #define NFSDDBG_FACILITY NFSDDBG_PROC
  50. #define NFSPROC4_CB_NULL 0
  51. #define NFSPROC4_CB_COMPOUND 1
  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 (NFS4_STATEID_SIZE >> 2)
  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), __func__); \
  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. __func__, __LINE__); \
  122. return -EIO; \
  123. } \
  124. } while (0)
  125. struct nfs4_cb_compound_hdr {
  126. int status;
  127. u32 ident;
  128. u32 nops;
  129. __be32 *nops_p;
  130. u32 minorversion;
  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 void
  189. encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
  190. {
  191. __be32 * p;
  192. RESERVE_SPACE(16);
  193. WRITE32(0); /* tag length is always 0 */
  194. WRITE32(hdr->minorversion);
  195. WRITE32(hdr->ident);
  196. hdr->nops_p = p;
  197. WRITE32(hdr->nops);
  198. }
  199. static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
  200. {
  201. *hdr->nops_p = htonl(hdr->nops);
  202. }
  203. static void
  204. encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
  205. struct nfs4_cb_compound_hdr *hdr)
  206. {
  207. __be32 *p;
  208. int len = dp->dl_fh.fh_size;
  209. RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
  210. WRITE32(OP_CB_RECALL);
  211. WRITE32(dp->dl_stateid.si_generation);
  212. WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
  213. WRITE32(0); /* truncate optimization not implemented */
  214. WRITE32(len);
  215. WRITEMEM(&dp->dl_fh.fh_base, len);
  216. hdr->nops++;
  217. }
  218. static int
  219. nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
  220. {
  221. struct xdr_stream xdrs, *xdr = &xdrs;
  222. xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
  223. RESERVE_SPACE(0);
  224. return 0;
  225. }
  226. static int
  227. nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_delegation *args)
  228. {
  229. struct xdr_stream xdr;
  230. struct nfs4_cb_compound_hdr hdr = {
  231. .ident = args->dl_ident,
  232. };
  233. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  234. encode_cb_compound_hdr(&xdr, &hdr);
  235. encode_cb_recall(&xdr, args, &hdr);
  236. encode_cb_nops(&hdr);
  237. return 0;
  238. }
  239. static int
  240. decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
  241. __be32 *p;
  242. READ_BUF(8);
  243. READ32(hdr->status);
  244. READ32(hdr->taglen);
  245. READ_BUF(hdr->taglen + 4);
  246. hdr->tag = (char *)p;
  247. p += XDR_QUADLEN(hdr->taglen);
  248. READ32(hdr->nops);
  249. return 0;
  250. }
  251. static int
  252. decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
  253. {
  254. __be32 *p;
  255. u32 op;
  256. int32_t nfserr;
  257. READ_BUF(8);
  258. READ32(op);
  259. if (op != expected) {
  260. dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
  261. " operation %d but we issued a request for %d\n",
  262. op, expected);
  263. return -EIO;
  264. }
  265. READ32(nfserr);
  266. if (nfserr != NFS_OK)
  267. return -nfs_cb_stat_to_errno(nfserr);
  268. return 0;
  269. }
  270. static int
  271. nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
  272. {
  273. return 0;
  274. }
  275. static int
  276. nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
  277. {
  278. struct xdr_stream xdr;
  279. struct nfs4_cb_compound_hdr hdr;
  280. int status;
  281. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
  282. status = decode_cb_compound_hdr(&xdr, &hdr);
  283. if (status)
  284. goto out;
  285. status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
  286. out:
  287. return status;
  288. }
  289. /*
  290. * RPC procedure tables
  291. */
  292. #define PROC(proc, call, argtype, restype) \
  293. [NFSPROC4_CLNT_##proc] = { \
  294. .p_proc = NFSPROC4_CB_##call, \
  295. .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
  296. .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
  297. .p_arglen = NFS4_##argtype##_sz, \
  298. .p_replen = NFS4_##restype##_sz, \
  299. .p_statidx = NFSPROC4_CB_##call, \
  300. .p_name = #proc, \
  301. }
  302. static struct rpc_procinfo nfs4_cb_procedures[] = {
  303. PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
  304. PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
  305. };
  306. static struct rpc_version nfs_cb_version4 = {
  307. .number = 1,
  308. .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
  309. .procs = nfs4_cb_procedures
  310. };
  311. static struct rpc_version * nfs_cb_version[] = {
  312. NULL,
  313. &nfs_cb_version4,
  314. };
  315. static struct rpc_program cb_program;
  316. static struct rpc_stat cb_stats = {
  317. .program = &cb_program
  318. };
  319. #define NFS4_CALLBACK 0x40000000
  320. static struct rpc_program cb_program = {
  321. .name = "nfs4_cb",
  322. .number = NFS4_CALLBACK,
  323. .nrvers = ARRAY_SIZE(nfs_cb_version),
  324. .version = nfs_cb_version,
  325. .stats = &cb_stats,
  326. .pipe_dir_name = "/nfsd4_cb",
  327. };
  328. static int max_cb_time(void)
  329. {
  330. return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
  331. }
  332. /* Reference counting, callback cleanup, etc., all look racy as heck.
  333. * And why is cb_set an atomic? */
  334. int setup_callback_client(struct nfs4_client *clp)
  335. {
  336. struct sockaddr_in addr;
  337. struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
  338. struct rpc_timeout timeparms = {
  339. .to_initval = max_cb_time(),
  340. .to_retries = 0,
  341. };
  342. struct rpc_create_args args = {
  343. .protocol = IPPROTO_TCP,
  344. .address = (struct sockaddr *)&addr,
  345. .addrsize = sizeof(addr),
  346. .timeout = &timeparms,
  347. .program = &cb_program,
  348. .prognumber = cb->cb_prog,
  349. .version = nfs_cb_version[1]->number,
  350. .authflavor = clp->cl_flavor,
  351. .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
  352. .client_name = clp->cl_principal,
  353. };
  354. struct rpc_clnt *client;
  355. if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
  356. return -EINVAL;
  357. /* Initialize address */
  358. memset(&addr, 0, sizeof(addr));
  359. addr.sin_family = AF_INET;
  360. addr.sin_port = htons(cb->cb_port);
  361. addr.sin_addr.s_addr = htonl(cb->cb_addr);
  362. /* Create RPC client */
  363. client = rpc_create(&args);
  364. if (IS_ERR(client)) {
  365. dprintk("NFSD: couldn't create callback client: %ld\n",
  366. PTR_ERR(client));
  367. return PTR_ERR(client);
  368. }
  369. cb->cb_client = client;
  370. return 0;
  371. }
  372. static void warn_no_callback_path(struct nfs4_client *clp, int reason)
  373. {
  374. dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
  375. (int)clp->cl_name.len, clp->cl_name.data, reason);
  376. }
  377. static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
  378. {
  379. struct nfs4_client *clp = calldata;
  380. if (task->tk_status)
  381. warn_no_callback_path(clp, task->tk_status);
  382. else
  383. atomic_set(&clp->cl_cb_conn.cb_set, 1);
  384. put_nfs4_client(clp);
  385. }
  386. static const struct rpc_call_ops nfsd4_cb_probe_ops = {
  387. .rpc_call_done = nfsd4_cb_probe_done,
  388. };
  389. static struct rpc_cred *lookup_cb_cred(struct nfs4_cb_conn *cb)
  390. {
  391. struct auth_cred acred = {
  392. .machine_cred = 1
  393. };
  394. /*
  395. * Note in the gss case this doesn't actually have to wait for a
  396. * gss upcall (or any calls to the client); this just creates a
  397. * non-uptodate cred which the rpc state machine will fill in with
  398. * a refresh_upcall later.
  399. */
  400. return rpcauth_lookup_credcache(cb->cb_client->cl_auth, &acred,
  401. RPCAUTH_LOOKUP_NEW);
  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. };
  410. struct rpc_cred *cred;
  411. int status;
  412. cred = lookup_cb_cred(cb);
  413. if (IS_ERR(cred)) {
  414. status = PTR_ERR(cred);
  415. goto out;
  416. }
  417. cb->cb_cred = cred;
  418. msg.rpc_cred = cb->cb_cred;
  419. status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_SOFT,
  420. &nfsd4_cb_probe_ops, (void *)clp);
  421. out:
  422. if (status) {
  423. warn_no_callback_path(clp, status);
  424. put_nfs4_client(clp);
  425. }
  426. }
  427. /*
  428. * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
  429. */
  430. void
  431. nfsd4_probe_callback(struct nfs4_client *clp)
  432. {
  433. int status;
  434. BUG_ON(atomic_read(&clp->cl_cb_conn.cb_set));
  435. status = setup_callback_client(clp);
  436. if (status) {
  437. warn_no_callback_path(clp, status);
  438. return;
  439. }
  440. /* the task holds a reference to the nfs4_client struct */
  441. atomic_inc(&clp->cl_count);
  442. do_probe_callback(clp);
  443. }
  444. static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
  445. {
  446. struct nfs4_delegation *dp = calldata;
  447. struct nfs4_client *clp = dp->dl_client;
  448. switch (task->tk_status) {
  449. case -EIO:
  450. /* Network partition? */
  451. atomic_set(&clp->cl_cb_conn.cb_set, 0);
  452. warn_no_callback_path(clp, task->tk_status);
  453. case -EBADHANDLE:
  454. case -NFS4ERR_BAD_STATEID:
  455. /* Race: client probably got cb_recall
  456. * before open reply granting delegation */
  457. break;
  458. default:
  459. /* success, or error we can't handle */
  460. return;
  461. }
  462. if (dp->dl_retries--) {
  463. rpc_delay(task, 2*HZ);
  464. task->tk_status = 0;
  465. rpc_restart_call(task);
  466. } else {
  467. atomic_set(&clp->cl_cb_conn.cb_set, 0);
  468. warn_no_callback_path(clp, task->tk_status);
  469. }
  470. }
  471. static void nfsd4_cb_recall_release(void *calldata)
  472. {
  473. struct nfs4_delegation *dp = calldata;
  474. struct nfs4_client *clp = dp->dl_client;
  475. nfs4_put_delegation(dp);
  476. put_nfs4_client(clp);
  477. }
  478. static const struct rpc_call_ops nfsd4_cb_recall_ops = {
  479. .rpc_call_done = nfsd4_cb_recall_done,
  480. .rpc_release = nfsd4_cb_recall_release,
  481. };
  482. /*
  483. * called with dp->dl_count inc'ed.
  484. */
  485. void
  486. nfsd4_cb_recall(struct nfs4_delegation *dp)
  487. {
  488. struct nfs4_client *clp = dp->dl_client;
  489. struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
  490. struct rpc_message msg = {
  491. .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
  492. .rpc_argp = dp,
  493. .rpc_cred = clp->cl_cb_conn.cb_cred
  494. };
  495. int status;
  496. dp->dl_retries = 1;
  497. status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
  498. &nfsd4_cb_recall_ops, dp);
  499. if (status) {
  500. put_nfs4_client(clp);
  501. nfs4_put_delegation(dp);
  502. }
  503. }