1 /* 2 * Copyright (c) 2001 The Regents of the University of Michigan. 3 * All rights reserved. 4 * 5 * Kendrick Smith <kmsmith@umich.edu> 6 * Andy Adamson <andros@umich.edu> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include <linux/sunrpc/clnt.h> 35 #include <linux/sunrpc/svc_xprt.h> 36 #include <linux/slab.h> 37 #include "nfsd.h" 38 #include "state.h" 39 #include "netns.h" 40 41 #define NFSDDBG_FACILITY NFSDDBG_PROC 42 43 static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason); 44 45 #define NFSPROC4_CB_NULL 0 46 #define NFSPROC4_CB_COMPOUND 1 47 48 /* Index of predefined Linux callback client operations */ 49 50 enum { 51 NFSPROC4_CLNT_CB_NULL = 0, 52 NFSPROC4_CLNT_CB_RECALL, 53 NFSPROC4_CLNT_CB_SEQUENCE, 54 }; 55 56 #define NFS4_MAXTAGLEN 20 57 58 #define NFS4_enc_cb_null_sz 0 59 #define NFS4_dec_cb_null_sz 0 60 #define cb_compound_enc_hdr_sz 4 61 #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2)) 62 #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2) 63 #define cb_sequence_enc_sz (sessionid_sz + 4 + \ 64 1 /* no referring calls list yet */) 65 #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4) 66 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 (NFS4_STATEID_SIZE >> 2) 71 #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \ 72 cb_sequence_enc_sz + \ 73 1 + enc_stateid_sz + \ 74 enc_nfs4_fh_sz) 75 76 #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \ 77 cb_sequence_dec_sz + \ 78 op_dec_sz) 79 80 struct nfs4_cb_compound_hdr { 81 /* args */ 82 u32 ident; /* minorversion 0 only */ 83 u32 nops; 84 __be32 *nops_p; 85 u32 minorversion; 86 /* res */ 87 int status; 88 }; 89 90 /* 91 * Handle decode buffer overflows out-of-line. 92 */ 93 static void print_overflow_msg(const char *func, const struct xdr_stream *xdr) 94 { 95 dprintk("NFS: %s prematurely hit the end of our receive buffer. " 96 "Remaining buffer length is %tu words.\n", 97 func, xdr->end - xdr->p); 98 } 99 100 static __be32 *xdr_encode_empty_array(__be32 *p) 101 { 102 *p++ = xdr_zero; 103 return p; 104 } 105 106 /* 107 * Encode/decode NFSv4 CB basic data types 108 * 109 * Basic NFSv4 callback data types are defined in section 15 of RFC 110 * 3530: "Network File System (NFS) version 4 Protocol" and section 111 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version 112 * 1 Protocol" 113 */ 114 115 /* 116 * nfs_cb_opnum4 117 * 118 * enum nfs_cb_opnum4 { 119 * OP_CB_GETATTR = 3, 120 * ... 121 * }; 122 */ 123 enum nfs_cb_opnum4 { 124 OP_CB_GETATTR = 3, 125 OP_CB_RECALL = 4, 126 OP_CB_LAYOUTRECALL = 5, 127 OP_CB_NOTIFY = 6, 128 OP_CB_PUSH_DELEG = 7, 129 OP_CB_RECALL_ANY = 8, 130 OP_CB_RECALLABLE_OBJ_AVAIL = 9, 131 OP_CB_RECALL_SLOT = 10, 132 OP_CB_SEQUENCE = 11, 133 OP_CB_WANTS_CANCELLED = 12, 134 OP_CB_NOTIFY_LOCK = 13, 135 OP_CB_NOTIFY_DEVICEID = 14, 136 OP_CB_ILLEGAL = 10044 137 }; 138 139 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op) 140 { 141 __be32 *p; 142 143 p = xdr_reserve_space(xdr, 4); 144 *p = cpu_to_be32(op); 145 } 146 147 /* 148 * nfs_fh4 149 * 150 * typedef opaque nfs_fh4<NFS4_FHSIZE>; 151 */ 152 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh) 153 { 154 u32 length = fh->fh_size; 155 __be32 *p; 156 157 BUG_ON(length > NFS4_FHSIZE); 158 p = xdr_reserve_space(xdr, 4 + length); 159 xdr_encode_opaque(p, &fh->fh_base, length); 160 } 161 162 /* 163 * stateid4 164 * 165 * struct stateid4 { 166 * uint32_t seqid; 167 * opaque other[12]; 168 * }; 169 */ 170 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid) 171 { 172 __be32 *p; 173 174 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE); 175 *p++ = cpu_to_be32(sid->si_generation); 176 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE); 177 } 178 179 /* 180 * sessionid4 181 * 182 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE]; 183 */ 184 static void encode_sessionid4(struct xdr_stream *xdr, 185 const struct nfsd4_session *session) 186 { 187 __be32 *p; 188 189 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 190 xdr_encode_opaque_fixed(p, session->se_sessionid.data, 191 NFS4_MAX_SESSIONID_LEN); 192 } 193 194 /* 195 * nfsstat4 196 */ 197 static const struct { 198 int stat; 199 int errno; 200 } nfs_cb_errtbl[] = { 201 { NFS4_OK, 0 }, 202 { NFS4ERR_PERM, -EPERM }, 203 { NFS4ERR_NOENT, -ENOENT }, 204 { NFS4ERR_IO, -EIO }, 205 { NFS4ERR_NXIO, -ENXIO }, 206 { NFS4ERR_ACCESS, -EACCES }, 207 { NFS4ERR_EXIST, -EEXIST }, 208 { NFS4ERR_XDEV, -EXDEV }, 209 { NFS4ERR_NOTDIR, -ENOTDIR }, 210 { NFS4ERR_ISDIR, -EISDIR }, 211 { NFS4ERR_INVAL, -EINVAL }, 212 { NFS4ERR_FBIG, -EFBIG }, 213 { NFS4ERR_NOSPC, -ENOSPC }, 214 { NFS4ERR_ROFS, -EROFS }, 215 { NFS4ERR_MLINK, -EMLINK }, 216 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG }, 217 { NFS4ERR_NOTEMPTY, -ENOTEMPTY }, 218 { NFS4ERR_DQUOT, -EDQUOT }, 219 { NFS4ERR_STALE, -ESTALE }, 220 { NFS4ERR_BADHANDLE, -EBADHANDLE }, 221 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE }, 222 { NFS4ERR_NOTSUPP, -ENOTSUPP }, 223 { NFS4ERR_TOOSMALL, -ETOOSMALL }, 224 { NFS4ERR_SERVERFAULT, -ESERVERFAULT }, 225 { NFS4ERR_BADTYPE, -EBADTYPE }, 226 { NFS4ERR_LOCKED, -EAGAIN }, 227 { NFS4ERR_RESOURCE, -EREMOTEIO }, 228 { NFS4ERR_SYMLINK, -ELOOP }, 229 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP }, 230 { NFS4ERR_DEADLOCK, -EDEADLK }, 231 { -1, -EIO } 232 }; 233 234 /* 235 * If we cannot translate the error, the recovery routines should 236 * handle it. 237 * 238 * Note: remaining NFSv4 error codes have values > 10000, so should 239 * not conflict with native Linux error codes. 240 */ 241 static int nfs_cb_stat_to_errno(int status) 242 { 243 int i; 244 245 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) { 246 if (nfs_cb_errtbl[i].stat == status) 247 return nfs_cb_errtbl[i].errno; 248 } 249 250 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status); 251 return -status; 252 } 253 254 static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected, 255 enum nfsstat4 *status) 256 { 257 __be32 *p; 258 u32 op; 259 260 p = xdr_inline_decode(xdr, 4 + 4); 261 if (unlikely(p == NULL)) 262 goto out_overflow; 263 op = be32_to_cpup(p++); 264 if (unlikely(op != expected)) 265 goto out_unexpected; 266 *status = be32_to_cpup(p); 267 return 0; 268 out_overflow: 269 print_overflow_msg(__func__, xdr); 270 return -EIO; 271 out_unexpected: 272 dprintk("NFSD: Callback server returned operation %d but " 273 "we issued a request for %d\n", op, expected); 274 return -EIO; 275 } 276 277 /* 278 * CB_COMPOUND4args 279 * 280 * struct CB_COMPOUND4args { 281 * utf8str_cs tag; 282 * uint32_t minorversion; 283 * uint32_t callback_ident; 284 * nfs_cb_argop4 argarray<>; 285 * }; 286 */ 287 static void encode_cb_compound4args(struct xdr_stream *xdr, 288 struct nfs4_cb_compound_hdr *hdr) 289 { 290 __be32 * p; 291 292 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4); 293 p = xdr_encode_empty_array(p); /* empty tag */ 294 *p++ = cpu_to_be32(hdr->minorversion); 295 *p++ = cpu_to_be32(hdr->ident); 296 297 hdr->nops_p = p; 298 *p = cpu_to_be32(hdr->nops); /* argarray element count */ 299 } 300 301 /* 302 * Update argarray element count 303 */ 304 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr) 305 { 306 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS); 307 *hdr->nops_p = cpu_to_be32(hdr->nops); 308 } 309 310 /* 311 * CB_COMPOUND4res 312 * 313 * struct CB_COMPOUND4res { 314 * nfsstat4 status; 315 * utf8str_cs tag; 316 * nfs_cb_resop4 resarray<>; 317 * }; 318 */ 319 static int decode_cb_compound4res(struct xdr_stream *xdr, 320 struct nfs4_cb_compound_hdr *hdr) 321 { 322 u32 length; 323 __be32 *p; 324 325 p = xdr_inline_decode(xdr, 4 + 4); 326 if (unlikely(p == NULL)) 327 goto out_overflow; 328 hdr->status = be32_to_cpup(p++); 329 /* Ignore the tag */ 330 length = be32_to_cpup(p++); 331 p = xdr_inline_decode(xdr, length + 4); 332 if (unlikely(p == NULL)) 333 goto out_overflow; 334 hdr->nops = be32_to_cpup(p); 335 return 0; 336 out_overflow: 337 print_overflow_msg(__func__, xdr); 338 return -EIO; 339 } 340 341 /* 342 * CB_RECALL4args 343 * 344 * struct CB_RECALL4args { 345 * stateid4 stateid; 346 * bool truncate; 347 * nfs_fh4 fh; 348 * }; 349 */ 350 static void encode_cb_recall4args(struct xdr_stream *xdr, 351 const struct nfs4_delegation *dp, 352 struct nfs4_cb_compound_hdr *hdr) 353 { 354 __be32 *p; 355 356 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL); 357 encode_stateid4(xdr, &dp->dl_stid.sc_stateid); 358 359 p = xdr_reserve_space(xdr, 4); 360 *p++ = xdr_zero; /* truncate */ 361 362 encode_nfs_fh4(xdr, &dp->dl_fh); 363 364 hdr->nops++; 365 } 366 367 /* 368 * CB_SEQUENCE4args 369 * 370 * struct CB_SEQUENCE4args { 371 * sessionid4 csa_sessionid; 372 * sequenceid4 csa_sequenceid; 373 * slotid4 csa_slotid; 374 * slotid4 csa_highest_slotid; 375 * bool csa_cachethis; 376 * referring_call_list4 csa_referring_call_lists<>; 377 * }; 378 */ 379 static void encode_cb_sequence4args(struct xdr_stream *xdr, 380 const struct nfsd4_callback *cb, 381 struct nfs4_cb_compound_hdr *hdr) 382 { 383 struct nfsd4_session *session = cb->cb_clp->cl_cb_session; 384 __be32 *p; 385 386 if (hdr->minorversion == 0) 387 return; 388 389 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE); 390 encode_sessionid4(xdr, session); 391 392 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4); 393 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */ 394 *p++ = xdr_zero; /* csa_slotid */ 395 *p++ = xdr_zero; /* csa_highest_slotid */ 396 *p++ = xdr_zero; /* csa_cachethis */ 397 xdr_encode_empty_array(p); /* csa_referring_call_lists */ 398 399 hdr->nops++; 400 } 401 402 /* 403 * CB_SEQUENCE4resok 404 * 405 * struct CB_SEQUENCE4resok { 406 * sessionid4 csr_sessionid; 407 * sequenceid4 csr_sequenceid; 408 * slotid4 csr_slotid; 409 * slotid4 csr_highest_slotid; 410 * slotid4 csr_target_highest_slotid; 411 * }; 412 * 413 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) { 414 * case NFS4_OK: 415 * CB_SEQUENCE4resok csr_resok4; 416 * default: 417 * void; 418 * }; 419 * 420 * Our current back channel implmentation supports a single backchannel 421 * with a single slot. 422 */ 423 static int decode_cb_sequence4resok(struct xdr_stream *xdr, 424 struct nfsd4_callback *cb) 425 { 426 struct nfsd4_session *session = cb->cb_clp->cl_cb_session; 427 struct nfs4_sessionid id; 428 int status; 429 __be32 *p; 430 u32 dummy; 431 432 status = -ESERVERFAULT; 433 434 /* 435 * If the server returns different values for sessionID, slotID or 436 * sequence number, the server is looney tunes. 437 */ 438 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4); 439 if (unlikely(p == NULL)) 440 goto out_overflow; 441 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN); 442 if (memcmp(id.data, session->se_sessionid.data, 443 NFS4_MAX_SESSIONID_LEN) != 0) { 444 dprintk("NFS: %s Invalid session id\n", __func__); 445 goto out; 446 } 447 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN); 448 449 dummy = be32_to_cpup(p++); 450 if (dummy != session->se_cb_seq_nr) { 451 dprintk("NFS: %s Invalid sequence number\n", __func__); 452 goto out; 453 } 454 455 dummy = be32_to_cpup(p++); 456 if (dummy != 0) { 457 dprintk("NFS: %s Invalid slotid\n", __func__); 458 goto out; 459 } 460 461 /* 462 * FIXME: process highest slotid and target highest slotid 463 */ 464 status = 0; 465 out: 466 if (status) 467 nfsd4_mark_cb_fault(cb->cb_clp, status); 468 return status; 469 out_overflow: 470 print_overflow_msg(__func__, xdr); 471 return -EIO; 472 } 473 474 static int decode_cb_sequence4res(struct xdr_stream *xdr, 475 struct nfsd4_callback *cb) 476 { 477 enum nfsstat4 nfserr; 478 int status; 479 480 if (cb->cb_minorversion == 0) 481 return 0; 482 483 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr); 484 if (unlikely(status)) 485 goto out; 486 if (unlikely(nfserr != NFS4_OK)) 487 goto out_default; 488 status = decode_cb_sequence4resok(xdr, cb); 489 out: 490 return status; 491 out_default: 492 return nfs_cb_stat_to_errno(nfserr); 493 } 494 495 /* 496 * NFSv4.0 and NFSv4.1 XDR encode functions 497 * 498 * NFSv4.0 callback argument types are defined in section 15 of RFC 499 * 3530: "Network File System (NFS) version 4 Protocol" and section 20 500 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1 501 * Protocol". 502 */ 503 504 /* 505 * NB: Without this zero space reservation, callbacks over krb5p fail 506 */ 507 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr, 508 void *__unused) 509 { 510 xdr_reserve_space(xdr, 0); 511 } 512 513 /* 514 * 20.2. Operation 4: CB_RECALL - Recall a Delegation 515 */ 516 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr, 517 const struct nfsd4_callback *cb) 518 { 519 const struct nfs4_delegation *args = cb->cb_op; 520 struct nfs4_cb_compound_hdr hdr = { 521 .ident = cb->cb_clp->cl_cb_ident, 522 .minorversion = cb->cb_minorversion, 523 }; 524 525 encode_cb_compound4args(xdr, &hdr); 526 encode_cb_sequence4args(xdr, cb, &hdr); 527 encode_cb_recall4args(xdr, args, &hdr); 528 encode_cb_nops(&hdr); 529 } 530 531 532 /* 533 * NFSv4.0 and NFSv4.1 XDR decode functions 534 * 535 * NFSv4.0 callback result types are defined in section 15 of RFC 536 * 3530: "Network File System (NFS) version 4 Protocol" and section 20 537 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1 538 * Protocol". 539 */ 540 541 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr, 542 void *__unused) 543 { 544 return 0; 545 } 546 547 /* 548 * 20.2. Operation 4: CB_RECALL - Recall a Delegation 549 */ 550 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, 551 struct xdr_stream *xdr, 552 struct nfsd4_callback *cb) 553 { 554 struct nfs4_cb_compound_hdr hdr; 555 enum nfsstat4 nfserr; 556 int status; 557 558 status = decode_cb_compound4res(xdr, &hdr); 559 if (unlikely(status)) 560 goto out; 561 562 if (cb != NULL) { 563 status = decode_cb_sequence4res(xdr, cb); 564 if (unlikely(status)) 565 goto out; 566 } 567 568 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr); 569 if (unlikely(status)) 570 goto out; 571 if (unlikely(nfserr != NFS4_OK)) 572 status = nfs_cb_stat_to_errno(nfserr); 573 out: 574 return status; 575 } 576 577 /* 578 * RPC procedure tables 579 */ 580 #define PROC(proc, call, argtype, restype) \ 581 [NFSPROC4_CLNT_##proc] = { \ 582 .p_proc = NFSPROC4_CB_##call, \ 583 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \ 584 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \ 585 .p_arglen = NFS4_enc_##argtype##_sz, \ 586 .p_replen = NFS4_dec_##restype##_sz, \ 587 .p_statidx = NFSPROC4_CB_##call, \ 588 .p_name = #proc, \ 589 } 590 591 static struct rpc_procinfo nfs4_cb_procedures[] = { 592 PROC(CB_NULL, NULL, cb_null, cb_null), 593 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall), 594 }; 595 596 static struct rpc_version nfs_cb_version4 = { 597 /* 598 * Note on the callback rpc program version number: despite language in rfc 599 * 5661 section 18.36.3 requiring servers to use 4 in this field, the 600 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and 601 * in practice that appears to be what implementations use. The section 602 * 18.36.3 language is expected to be fixed in an erratum. 603 */ 604 .number = 1, 605 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures), 606 .procs = nfs4_cb_procedures 607 }; 608 609 static const struct rpc_version *nfs_cb_version[] = { 610 &nfs_cb_version4, 611 }; 612 613 static const struct rpc_program cb_program; 614 615 static struct rpc_stat cb_stats = { 616 .program = &cb_program 617 }; 618 619 #define NFS4_CALLBACK 0x40000000 620 static const struct rpc_program cb_program = { 621 .name = "nfs4_cb", 622 .number = NFS4_CALLBACK, 623 .nrvers = ARRAY_SIZE(nfs_cb_version), 624 .version = nfs_cb_version, 625 .stats = &cb_stats, 626 .pipe_dir_name = "nfsd4_cb", 627 }; 628 629 static int max_cb_time(struct net *net) 630 { 631 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 632 return max(nn->nfsd4_lease/10, (time_t)1) * HZ; 633 } 634 635 static struct rpc_cred *callback_cred; 636 637 int set_callback_cred(void) 638 { 639 if (callback_cred) 640 return 0; 641 callback_cred = rpc_lookup_machine_cred("nfs"); 642 if (!callback_cred) 643 return -ENOMEM; 644 return 0; 645 } 646 647 static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses) 648 { 649 if (clp->cl_minorversion == 0) { 650 return get_rpccred(callback_cred); 651 } else { 652 struct rpc_auth *auth = client->cl_auth; 653 struct auth_cred acred = {}; 654 655 acred.uid = ses->se_cb_sec.uid; 656 acred.gid = ses->se_cb_sec.gid; 657 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0); 658 } 659 } 660 661 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses) 662 { 663 struct rpc_timeout timeparms = { 664 .to_initval = max_cb_time(clp->net), 665 .to_retries = 0, 666 }; 667 struct rpc_create_args args = { 668 .net = clp->net, 669 .address = (struct sockaddr *) &conn->cb_addr, 670 .addrsize = conn->cb_addrlen, 671 .saddress = (struct sockaddr *) &conn->cb_saddr, 672 .timeout = &timeparms, 673 .program = &cb_program, 674 .version = 0, 675 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET), 676 }; 677 struct rpc_clnt *client; 678 struct rpc_cred *cred; 679 680 if (clp->cl_minorversion == 0) { 681 if (!clp->cl_cred.cr_principal && 682 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5)) 683 return -EINVAL; 684 args.client_name = clp->cl_cred.cr_principal; 685 args.prognumber = conn->cb_prog, 686 args.protocol = XPRT_TRANSPORT_TCP; 687 args.authflavor = clp->cl_cred.cr_flavor; 688 clp->cl_cb_ident = conn->cb_ident; 689 } else { 690 if (!conn->cb_xprt) 691 return -EINVAL; 692 clp->cl_cb_conn.cb_xprt = conn->cb_xprt; 693 clp->cl_cb_session = ses; 694 args.bc_xprt = conn->cb_xprt; 695 args.prognumber = clp->cl_cb_session->se_cb_prog; 696 args.protocol = XPRT_TRANSPORT_BC_TCP; 697 args.authflavor = ses->se_cb_sec.flavor; 698 } 699 /* Create RPC client */ 700 client = rpc_create(&args); 701 if (IS_ERR(client)) { 702 dprintk("NFSD: couldn't create callback client: %ld\n", 703 PTR_ERR(client)); 704 return PTR_ERR(client); 705 } 706 cred = get_backchannel_cred(clp, client, ses); 707 if (IS_ERR(cred)) { 708 rpc_shutdown_client(client); 709 return PTR_ERR(cred); 710 } 711 clp->cl_cb_client = client; 712 clp->cl_cb_cred = cred; 713 return 0; 714 } 715 716 static void warn_no_callback_path(struct nfs4_client *clp, int reason) 717 { 718 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n", 719 (int)clp->cl_name.len, clp->cl_name.data, reason); 720 } 721 722 static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason) 723 { 724 clp->cl_cb_state = NFSD4_CB_DOWN; 725 warn_no_callback_path(clp, reason); 726 } 727 728 static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason) 729 { 730 clp->cl_cb_state = NFSD4_CB_FAULT; 731 warn_no_callback_path(clp, reason); 732 } 733 734 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata) 735 { 736 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null); 737 738 if (task->tk_status) 739 nfsd4_mark_cb_down(clp, task->tk_status); 740 else 741 clp->cl_cb_state = NFSD4_CB_UP; 742 } 743 744 static const struct rpc_call_ops nfsd4_cb_probe_ops = { 745 /* XXX: release method to ensure we set the cb channel down if 746 * necessary on early failure? */ 747 .rpc_call_done = nfsd4_cb_probe_done, 748 }; 749 750 static struct workqueue_struct *callback_wq; 751 752 static void run_nfsd4_cb(struct nfsd4_callback *cb) 753 { 754 queue_work(callback_wq, &cb->cb_work); 755 } 756 757 static void do_probe_callback(struct nfs4_client *clp) 758 { 759 struct nfsd4_callback *cb = &clp->cl_cb_null; 760 761 cb->cb_op = NULL; 762 cb->cb_clp = clp; 763 764 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL]; 765 cb->cb_msg.rpc_argp = NULL; 766 cb->cb_msg.rpc_resp = NULL; 767 768 cb->cb_ops = &nfsd4_cb_probe_ops; 769 770 run_nfsd4_cb(cb); 771 } 772 773 /* 774 * Poke the callback thread to process any updates to the callback 775 * parameters, and send a null probe. 776 */ 777 void nfsd4_probe_callback(struct nfs4_client *clp) 778 { 779 clp->cl_cb_state = NFSD4_CB_UNKNOWN; 780 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags); 781 do_probe_callback(clp); 782 } 783 784 void nfsd4_probe_callback_sync(struct nfs4_client *clp) 785 { 786 nfsd4_probe_callback(clp); 787 flush_workqueue(callback_wq); 788 } 789 790 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn) 791 { 792 clp->cl_cb_state = NFSD4_CB_UNKNOWN; 793 spin_lock(&clp->cl_lock); 794 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn)); 795 spin_unlock(&clp->cl_lock); 796 } 797 798 /* 799 * There's currently a single callback channel slot. 800 * If the slot is available, then mark it busy. Otherwise, set the 801 * thread for sleeping on the callback RPC wait queue. 802 */ 803 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task) 804 { 805 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) { 806 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL); 807 dprintk("%s slot is busy\n", __func__); 808 return false; 809 } 810 return true; 811 } 812 813 /* 814 * TODO: cb_sequence should support referring call lists, cachethis, multiple 815 * slots, and mark callback channel down on communication errors. 816 */ 817 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata) 818 { 819 struct nfsd4_callback *cb = calldata; 820 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); 821 struct nfs4_client *clp = dp->dl_stid.sc_client; 822 u32 minorversion = clp->cl_minorversion; 823 824 cb->cb_minorversion = minorversion; 825 if (minorversion) { 826 if (!nfsd41_cb_get_slot(clp, task)) 827 return; 828 } 829 spin_lock(&clp->cl_lock); 830 if (list_empty(&cb->cb_per_client)) { 831 /* This is the first call, not a restart */ 832 cb->cb_done = false; 833 list_add(&cb->cb_per_client, &clp->cl_callbacks); 834 } 835 spin_unlock(&clp->cl_lock); 836 rpc_call_start(task); 837 } 838 839 static void nfsd4_cb_done(struct rpc_task *task, void *calldata) 840 { 841 struct nfsd4_callback *cb = calldata; 842 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); 843 struct nfs4_client *clp = dp->dl_stid.sc_client; 844 845 dprintk("%s: minorversion=%d\n", __func__, 846 clp->cl_minorversion); 847 848 if (clp->cl_minorversion) { 849 /* No need for lock, access serialized in nfsd4_cb_prepare */ 850 ++clp->cl_cb_session->se_cb_seq_nr; 851 clear_bit(0, &clp->cl_cb_slot_busy); 852 rpc_wake_up_next(&clp->cl_cb_waitq); 853 dprintk("%s: freed slot, new seqid=%d\n", __func__, 854 clp->cl_cb_session->se_cb_seq_nr); 855 856 /* We're done looking into the sequence information */ 857 task->tk_msg.rpc_resp = NULL; 858 } 859 } 860 861 862 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata) 863 { 864 struct nfsd4_callback *cb = calldata; 865 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); 866 struct nfs4_client *clp = dp->dl_stid.sc_client; 867 struct rpc_clnt *current_rpc_client = clp->cl_cb_client; 868 869 nfsd4_cb_done(task, calldata); 870 871 if (current_rpc_client != task->tk_client) { 872 /* We're shutting down or changing cl_cb_client; leave 873 * it to nfsd4_process_cb_update to restart the call if 874 * necessary. */ 875 return; 876 } 877 878 if (cb->cb_done) 879 return; 880 switch (task->tk_status) { 881 case 0: 882 cb->cb_done = true; 883 return; 884 case -EBADHANDLE: 885 case -NFS4ERR_BAD_STATEID: 886 /* Race: client probably got cb_recall 887 * before open reply granting delegation */ 888 break; 889 default: 890 /* Network partition? */ 891 nfsd4_mark_cb_down(clp, task->tk_status); 892 } 893 if (dp->dl_retries--) { 894 rpc_delay(task, 2*HZ); 895 task->tk_status = 0; 896 rpc_restart_call_prepare(task); 897 return; 898 } 899 nfsd4_mark_cb_down(clp, task->tk_status); 900 cb->cb_done = true; 901 } 902 903 static void nfsd4_cb_recall_release(void *calldata) 904 { 905 struct nfsd4_callback *cb = calldata; 906 struct nfs4_client *clp = cb->cb_clp; 907 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); 908 909 if (cb->cb_done) { 910 spin_lock(&clp->cl_lock); 911 list_del(&cb->cb_per_client); 912 spin_unlock(&clp->cl_lock); 913 nfs4_put_delegation(dp); 914 } 915 } 916 917 static const struct rpc_call_ops nfsd4_cb_recall_ops = { 918 .rpc_call_prepare = nfsd4_cb_prepare, 919 .rpc_call_done = nfsd4_cb_recall_done, 920 .rpc_release = nfsd4_cb_recall_release, 921 }; 922 923 int nfsd4_create_callback_queue(void) 924 { 925 callback_wq = create_singlethread_workqueue("nfsd4_callbacks"); 926 if (!callback_wq) 927 return -ENOMEM; 928 return 0; 929 } 930 931 void nfsd4_destroy_callback_queue(void) 932 { 933 destroy_workqueue(callback_wq); 934 } 935 936 /* must be called under the state lock */ 937 void nfsd4_shutdown_callback(struct nfs4_client *clp) 938 { 939 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags); 940 /* 941 * Note this won't actually result in a null callback; 942 * instead, nfsd4_do_callback_rpc() will detect the killed 943 * client, destroy the rpc client, and stop: 944 */ 945 do_probe_callback(clp); 946 flush_workqueue(callback_wq); 947 } 948 949 static void nfsd4_release_cb(struct nfsd4_callback *cb) 950 { 951 if (cb->cb_ops->rpc_release) 952 cb->cb_ops->rpc_release(cb); 953 } 954 955 /* requires cl_lock: */ 956 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp) 957 { 958 struct nfsd4_session *s; 959 struct nfsd4_conn *c; 960 961 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) { 962 list_for_each_entry(c, &s->se_conns, cn_persession) { 963 if (c->cn_flags & NFS4_CDFC4_BACK) 964 return c; 965 } 966 } 967 return NULL; 968 } 969 970 static void nfsd4_process_cb_update(struct nfsd4_callback *cb) 971 { 972 struct nfs4_cb_conn conn; 973 struct nfs4_client *clp = cb->cb_clp; 974 struct nfsd4_session *ses = NULL; 975 struct nfsd4_conn *c; 976 int err; 977 978 /* 979 * This is either an update, or the client dying; in either case, 980 * kill the old client: 981 */ 982 if (clp->cl_cb_client) { 983 rpc_shutdown_client(clp->cl_cb_client); 984 clp->cl_cb_client = NULL; 985 put_rpccred(clp->cl_cb_cred); 986 clp->cl_cb_cred = NULL; 987 } 988 if (clp->cl_cb_conn.cb_xprt) { 989 svc_xprt_put(clp->cl_cb_conn.cb_xprt); 990 clp->cl_cb_conn.cb_xprt = NULL; 991 } 992 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) 993 return; 994 spin_lock(&clp->cl_lock); 995 /* 996 * Only serialized callback code is allowed to clear these 997 * flags; main nfsd code can only set them: 998 */ 999 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)); 1000 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags); 1001 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn)); 1002 c = __nfsd4_find_backchannel(clp); 1003 if (c) { 1004 svc_xprt_get(c->cn_xprt); 1005 conn.cb_xprt = c->cn_xprt; 1006 ses = c->cn_session; 1007 } 1008 spin_unlock(&clp->cl_lock); 1009 1010 err = setup_callback_client(clp, &conn, ses); 1011 if (err) { 1012 nfsd4_mark_cb_down(clp, err); 1013 return; 1014 } 1015 /* Yay, the callback channel's back! Restart any callbacks: */ 1016 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client) 1017 run_nfsd4_cb(cb); 1018 } 1019 1020 static void nfsd4_do_callback_rpc(struct work_struct *w) 1021 { 1022 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work); 1023 struct nfs4_client *clp = cb->cb_clp; 1024 struct rpc_clnt *clnt; 1025 1026 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK) 1027 nfsd4_process_cb_update(cb); 1028 1029 clnt = clp->cl_cb_client; 1030 if (!clnt) { 1031 /* Callback channel broken, or client killed; give up: */ 1032 nfsd4_release_cb(cb); 1033 return; 1034 } 1035 cb->cb_msg.rpc_cred = clp->cl_cb_cred; 1036 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN, 1037 cb->cb_ops, cb); 1038 } 1039 1040 void nfsd4_init_callback(struct nfsd4_callback *cb) 1041 { 1042 INIT_WORK(&cb->cb_work, nfsd4_do_callback_rpc); 1043 } 1044 1045 void nfsd4_cb_recall(struct nfs4_delegation *dp) 1046 { 1047 struct nfsd4_callback *cb = &dp->dl_recall; 1048 struct nfs4_client *clp = dp->dl_stid.sc_client; 1049 1050 dp->dl_retries = 1; 1051 cb->cb_op = dp; 1052 cb->cb_clp = clp; 1053 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL]; 1054 cb->cb_msg.rpc_argp = cb; 1055 cb->cb_msg.rpc_resp = cb; 1056 1057 cb->cb_ops = &nfsd4_cb_recall_ops; 1058 1059 INIT_LIST_HEAD(&cb->cb_per_client); 1060 cb->cb_done = true; 1061 1062 run_nfsd4_cb(&dp->dl_recall); 1063 } 1064