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/nfs4.h> 35 #include <linux/sunrpc/clnt.h> 36 #include <linux/sunrpc/xprt.h> 37 #include <linux/sunrpc/svc_xprt.h> 38 #include <linux/slab.h> 39 #include "nfsd.h" 40 #include "state.h" 41 #include "netns.h" 42 #include "trace.h" 43 #include "xdr4cb.h" 44 #include "xdr4.h" 45 46 #define NFSDDBG_FACILITY NFSDDBG_PROC 47 48 static void nfsd4_mark_cb_fault(struct nfs4_client *clp); 49 50 #define NFSPROC4_CB_NULL 0 51 #define NFSPROC4_CB_COMPOUND 1 52 53 /* Index of predefined Linux callback client operations */ 54 55 struct nfs4_cb_compound_hdr { 56 /* args */ 57 u32 ident; /* minorversion 0 only */ 58 u32 nops; 59 __be32 *nops_p; 60 u32 minorversion; 61 /* res */ 62 int status; 63 }; 64 65 static __be32 *xdr_encode_empty_array(__be32 *p) 66 { 67 *p++ = xdr_zero; 68 return p; 69 } 70 71 /* 72 * Encode/decode NFSv4 CB basic data types 73 * 74 * Basic NFSv4 callback data types are defined in section 15 of RFC 75 * 3530: "Network File System (NFS) version 4 Protocol" and section 76 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version 77 * 1 Protocol" 78 */ 79 80 static void encode_uint32(struct xdr_stream *xdr, u32 n) 81 { 82 WARN_ON_ONCE(xdr_stream_encode_u32(xdr, n) < 0); 83 } 84 85 static void encode_bitmap4(struct xdr_stream *xdr, const __u32 *bitmap, 86 size_t len) 87 { 88 xdr_stream_encode_uint32_array(xdr, bitmap, len); 89 } 90 91 static int decode_cb_fattr4(struct xdr_stream *xdr, uint32_t *bitmap, 92 struct nfs4_cb_fattr *fattr) 93 { 94 fattr->ncf_cb_change = 0; 95 fattr->ncf_cb_fsize = 0; 96 if (bitmap[0] & FATTR4_WORD0_CHANGE) 97 if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_change) < 0) 98 return -NFSERR_BAD_XDR; 99 if (bitmap[0] & FATTR4_WORD0_SIZE) 100 if (xdr_stream_decode_u64(xdr, &fattr->ncf_cb_fsize) < 0) 101 return -NFSERR_BAD_XDR; 102 return 0; 103 } 104 105 static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op) 106 { 107 __be32 *p; 108 109 p = xdr_reserve_space(xdr, 4); 110 *p = cpu_to_be32(op); 111 } 112 113 /* 114 * nfs_fh4 115 * 116 * typedef opaque nfs_fh4<NFS4_FHSIZE>; 117 */ 118 static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh) 119 { 120 u32 length = fh->fh_size; 121 __be32 *p; 122 123 BUG_ON(length > NFS4_FHSIZE); 124 p = xdr_reserve_space(xdr, 4 + length); 125 xdr_encode_opaque(p, &fh->fh_raw, length); 126 } 127 128 /* 129 * stateid4 130 * 131 * struct stateid4 { 132 * uint32_t seqid; 133 * opaque other[12]; 134 * }; 135 */ 136 static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid) 137 { 138 __be32 *p; 139 140 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE); 141 *p++ = cpu_to_be32(sid->si_generation); 142 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE); 143 } 144 145 /* 146 * sessionid4 147 * 148 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE]; 149 */ 150 static void encode_sessionid4(struct xdr_stream *xdr, 151 const struct nfsd4_session *session) 152 { 153 __be32 *p; 154 155 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); 156 xdr_encode_opaque_fixed(p, session->se_sessionid.data, 157 NFS4_MAX_SESSIONID_LEN); 158 } 159 160 /* 161 * nfsstat4 162 */ 163 static const struct { 164 int stat; 165 int errno; 166 } nfs_cb_errtbl[] = { 167 { NFS4_OK, 0 }, 168 { NFS4ERR_PERM, -EPERM }, 169 { NFS4ERR_NOENT, -ENOENT }, 170 { NFS4ERR_IO, -EIO }, 171 { NFS4ERR_NXIO, -ENXIO }, 172 { NFS4ERR_ACCESS, -EACCES }, 173 { NFS4ERR_EXIST, -EEXIST }, 174 { NFS4ERR_XDEV, -EXDEV }, 175 { NFS4ERR_NOTDIR, -ENOTDIR }, 176 { NFS4ERR_ISDIR, -EISDIR }, 177 { NFS4ERR_INVAL, -EINVAL }, 178 { NFS4ERR_FBIG, -EFBIG }, 179 { NFS4ERR_NOSPC, -ENOSPC }, 180 { NFS4ERR_ROFS, -EROFS }, 181 { NFS4ERR_MLINK, -EMLINK }, 182 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG }, 183 { NFS4ERR_NOTEMPTY, -ENOTEMPTY }, 184 { NFS4ERR_DQUOT, -EDQUOT }, 185 { NFS4ERR_STALE, -ESTALE }, 186 { NFS4ERR_BADHANDLE, -EBADHANDLE }, 187 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE }, 188 { NFS4ERR_NOTSUPP, -ENOTSUPP }, 189 { NFS4ERR_TOOSMALL, -ETOOSMALL }, 190 { NFS4ERR_SERVERFAULT, -ESERVERFAULT }, 191 { NFS4ERR_BADTYPE, -EBADTYPE }, 192 { NFS4ERR_LOCKED, -EAGAIN }, 193 { NFS4ERR_RESOURCE, -EREMOTEIO }, 194 { NFS4ERR_SYMLINK, -ELOOP }, 195 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP }, 196 { NFS4ERR_DEADLOCK, -EDEADLK }, 197 { -1, -EIO } 198 }; 199 200 /* 201 * If we cannot translate the error, the recovery routines should 202 * handle it. 203 * 204 * Note: remaining NFSv4 error codes have values > 10000, so should 205 * not conflict with native Linux error codes. 206 */ 207 static int nfs_cb_stat_to_errno(int status) 208 { 209 int i; 210 211 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) { 212 if (nfs_cb_errtbl[i].stat == status) 213 return nfs_cb_errtbl[i].errno; 214 } 215 216 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status); 217 return -status; 218 } 219 220 static int decode_cb_op_status(struct xdr_stream *xdr, 221 enum nfs_cb_opnum4 expected, int *status) 222 { 223 __be32 *p; 224 u32 op; 225 226 p = xdr_inline_decode(xdr, 4 + 4); 227 if (unlikely(p == NULL)) 228 goto out_overflow; 229 op = be32_to_cpup(p++); 230 if (unlikely(op != expected)) 231 goto out_unexpected; 232 *status = nfs_cb_stat_to_errno(be32_to_cpup(p)); 233 return 0; 234 out_overflow: 235 return -EIO; 236 out_unexpected: 237 dprintk("NFSD: Callback server returned operation %d but " 238 "we issued a request for %d\n", op, expected); 239 return -EIO; 240 } 241 242 /* 243 * CB_COMPOUND4args 244 * 245 * struct CB_COMPOUND4args { 246 * utf8str_cs tag; 247 * uint32_t minorversion; 248 * uint32_t callback_ident; 249 * nfs_cb_argop4 argarray<>; 250 * }; 251 */ 252 static void encode_cb_compound4args(struct xdr_stream *xdr, 253 struct nfs4_cb_compound_hdr *hdr) 254 { 255 __be32 * p; 256 257 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4); 258 p = xdr_encode_empty_array(p); /* empty tag */ 259 *p++ = cpu_to_be32(hdr->minorversion); 260 *p++ = cpu_to_be32(hdr->ident); 261 262 hdr->nops_p = p; 263 *p = cpu_to_be32(hdr->nops); /* argarray element count */ 264 } 265 266 /* 267 * Update argarray element count 268 */ 269 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr) 270 { 271 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS); 272 *hdr->nops_p = cpu_to_be32(hdr->nops); 273 } 274 275 /* 276 * CB_COMPOUND4res 277 * 278 * struct CB_COMPOUND4res { 279 * nfsstat4 status; 280 * utf8str_cs tag; 281 * nfs_cb_resop4 resarray<>; 282 * }; 283 */ 284 static int decode_cb_compound4res(struct xdr_stream *xdr, 285 struct nfs4_cb_compound_hdr *hdr) 286 { 287 u32 length; 288 __be32 *p; 289 290 p = xdr_inline_decode(xdr, XDR_UNIT); 291 if (unlikely(p == NULL)) 292 goto out_overflow; 293 hdr->status = be32_to_cpup(p); 294 /* Ignore the tag */ 295 if (xdr_stream_decode_u32(xdr, &length) < 0) 296 goto out_overflow; 297 if (xdr_inline_decode(xdr, length) == NULL) 298 goto out_overflow; 299 if (xdr_stream_decode_u32(xdr, &hdr->nops) < 0) 300 goto out_overflow; 301 return 0; 302 out_overflow: 303 return -EIO; 304 } 305 306 /* 307 * CB_RECALL4args 308 * 309 * struct CB_RECALL4args { 310 * stateid4 stateid; 311 * bool truncate; 312 * nfs_fh4 fh; 313 * }; 314 */ 315 static void encode_cb_recall4args(struct xdr_stream *xdr, 316 const struct nfs4_delegation *dp, 317 struct nfs4_cb_compound_hdr *hdr) 318 { 319 __be32 *p; 320 321 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL); 322 encode_stateid4(xdr, &dp->dl_stid.sc_stateid); 323 324 p = xdr_reserve_space(xdr, 4); 325 *p++ = xdr_zero; /* truncate */ 326 327 encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle); 328 329 hdr->nops++; 330 } 331 332 /* 333 * CB_RECALLANY4args 334 * 335 * struct CB_RECALLANY4args { 336 * uint32_t craa_objects_to_keep; 337 * bitmap4 craa_type_mask; 338 * }; 339 */ 340 static void 341 encode_cb_recallany4args(struct xdr_stream *xdr, 342 struct nfs4_cb_compound_hdr *hdr, struct nfsd4_cb_recall_any *ra) 343 { 344 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL_ANY); 345 encode_uint32(xdr, ra->ra_keep); 346 encode_bitmap4(xdr, ra->ra_bmval, ARRAY_SIZE(ra->ra_bmval)); 347 hdr->nops++; 348 } 349 350 /* 351 * CB_GETATTR4args 352 * struct CB_GETATTR4args { 353 * nfs_fh4 fh; 354 * bitmap4 attr_request; 355 * }; 356 * 357 * The size and change attributes are the only one 358 * guaranteed to be serviced by the client. 359 */ 360 static void 361 encode_cb_getattr4args(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr, 362 struct nfs4_cb_fattr *fattr) 363 { 364 struct nfs4_delegation *dp = 365 container_of(fattr, struct nfs4_delegation, dl_cb_fattr); 366 struct knfsd_fh *fh = &dp->dl_stid.sc_file->fi_fhandle; 367 u32 bmap[1]; 368 369 bmap[0] = FATTR4_WORD0_CHANGE | FATTR4_WORD0_SIZE; 370 371 encode_nfs_cb_opnum4(xdr, OP_CB_GETATTR); 372 encode_nfs_fh4(xdr, fh); 373 encode_bitmap4(xdr, bmap, ARRAY_SIZE(bmap)); 374 hdr->nops++; 375 } 376 377 /* 378 * CB_SEQUENCE4args 379 * 380 * struct CB_SEQUENCE4args { 381 * sessionid4 csa_sessionid; 382 * sequenceid4 csa_sequenceid; 383 * slotid4 csa_slotid; 384 * slotid4 csa_highest_slotid; 385 * bool csa_cachethis; 386 * referring_call_list4 csa_referring_call_lists<>; 387 * }; 388 */ 389 static void encode_cb_sequence4args(struct xdr_stream *xdr, 390 const struct nfsd4_callback *cb, 391 struct nfs4_cb_compound_hdr *hdr) 392 { 393 struct nfsd4_session *session = cb->cb_clp->cl_cb_session; 394 __be32 *p; 395 396 if (hdr->minorversion == 0) 397 return; 398 399 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE); 400 encode_sessionid4(xdr, session); 401 402 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4); 403 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */ 404 *p++ = xdr_zero; /* csa_slotid */ 405 *p++ = xdr_zero; /* csa_highest_slotid */ 406 *p++ = xdr_zero; /* csa_cachethis */ 407 xdr_encode_empty_array(p); /* csa_referring_call_lists */ 408 409 hdr->nops++; 410 } 411 412 /* 413 * CB_SEQUENCE4resok 414 * 415 * struct CB_SEQUENCE4resok { 416 * sessionid4 csr_sessionid; 417 * sequenceid4 csr_sequenceid; 418 * slotid4 csr_slotid; 419 * slotid4 csr_highest_slotid; 420 * slotid4 csr_target_highest_slotid; 421 * }; 422 * 423 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) { 424 * case NFS4_OK: 425 * CB_SEQUENCE4resok csr_resok4; 426 * default: 427 * void; 428 * }; 429 * 430 * Our current back channel implmentation supports a single backchannel 431 * with a single slot. 432 */ 433 static int decode_cb_sequence4resok(struct xdr_stream *xdr, 434 struct nfsd4_callback *cb) 435 { 436 struct nfsd4_session *session = cb->cb_clp->cl_cb_session; 437 int status = -ESERVERFAULT; 438 __be32 *p; 439 u32 dummy; 440 441 /* 442 * If the server returns different values for sessionID, slotID or 443 * sequence number, the server is looney tunes. 444 */ 445 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4); 446 if (unlikely(p == NULL)) 447 goto out_overflow; 448 449 if (memcmp(p, session->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) { 450 dprintk("NFS: %s Invalid session id\n", __func__); 451 goto out; 452 } 453 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN); 454 455 dummy = be32_to_cpup(p++); 456 if (dummy != session->se_cb_seq_nr) { 457 dprintk("NFS: %s Invalid sequence number\n", __func__); 458 goto out; 459 } 460 461 dummy = be32_to_cpup(p++); 462 if (dummy != 0) { 463 dprintk("NFS: %s Invalid slotid\n", __func__); 464 goto out; 465 } 466 467 /* 468 * FIXME: process highest slotid and target highest slotid 469 */ 470 status = 0; 471 out: 472 cb->cb_seq_status = status; 473 return status; 474 out_overflow: 475 status = -EIO; 476 goto out; 477 } 478 479 static int decode_cb_sequence4res(struct xdr_stream *xdr, 480 struct nfsd4_callback *cb) 481 { 482 int status; 483 484 if (cb->cb_clp->cl_minorversion == 0) 485 return 0; 486 487 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &cb->cb_seq_status); 488 if (unlikely(status || cb->cb_seq_status)) 489 return status; 490 491 return decode_cb_sequence4resok(xdr, cb); 492 } 493 494 /* 495 * NFSv4.0 and NFSv4.1 XDR encode functions 496 * 497 * NFSv4.0 callback argument types are defined in section 15 of RFC 498 * 3530: "Network File System (NFS) version 4 Protocol" and section 20 499 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1 500 * Protocol". 501 */ 502 503 /* 504 * NB: Without this zero space reservation, callbacks over krb5p fail 505 */ 506 static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr, 507 const void *__unused) 508 { 509 xdr_reserve_space(xdr, 0); 510 } 511 512 /* 513 * 20.1. Operation 3: CB_GETATTR - Get Attributes 514 */ 515 static void nfs4_xdr_enc_cb_getattr(struct rpc_rqst *req, 516 struct xdr_stream *xdr, const void *data) 517 { 518 const struct nfsd4_callback *cb = data; 519 struct nfs4_cb_fattr *ncf = 520 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 521 struct nfs4_cb_compound_hdr hdr = { 522 .ident = cb->cb_clp->cl_cb_ident, 523 .minorversion = cb->cb_clp->cl_minorversion, 524 }; 525 526 encode_cb_compound4args(xdr, &hdr); 527 encode_cb_sequence4args(xdr, cb, &hdr); 528 encode_cb_getattr4args(xdr, &hdr, ncf); 529 encode_cb_nops(&hdr); 530 } 531 532 /* 533 * 20.2. Operation 4: CB_RECALL - Recall a Delegation 534 */ 535 static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr, 536 const void *data) 537 { 538 const struct nfsd4_callback *cb = data; 539 const struct nfs4_delegation *dp = cb_to_delegation(cb); 540 struct nfs4_cb_compound_hdr hdr = { 541 .ident = cb->cb_clp->cl_cb_ident, 542 .minorversion = cb->cb_clp->cl_minorversion, 543 }; 544 545 encode_cb_compound4args(xdr, &hdr); 546 encode_cb_sequence4args(xdr, cb, &hdr); 547 encode_cb_recall4args(xdr, dp, &hdr); 548 encode_cb_nops(&hdr); 549 } 550 551 /* 552 * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects 553 */ 554 static void 555 nfs4_xdr_enc_cb_recall_any(struct rpc_rqst *req, 556 struct xdr_stream *xdr, const void *data) 557 { 558 const struct nfsd4_callback *cb = data; 559 struct nfsd4_cb_recall_any *ra; 560 struct nfs4_cb_compound_hdr hdr = { 561 .ident = cb->cb_clp->cl_cb_ident, 562 .minorversion = cb->cb_clp->cl_minorversion, 563 }; 564 565 ra = container_of(cb, struct nfsd4_cb_recall_any, ra_cb); 566 encode_cb_compound4args(xdr, &hdr); 567 encode_cb_sequence4args(xdr, cb, &hdr); 568 encode_cb_recallany4args(xdr, &hdr, ra); 569 encode_cb_nops(&hdr); 570 } 571 572 /* 573 * NFSv4.0 and NFSv4.1 XDR decode functions 574 * 575 * NFSv4.0 callback result types are defined in section 15 of RFC 576 * 3530: "Network File System (NFS) version 4 Protocol" and section 20 577 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1 578 * Protocol". 579 */ 580 581 static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr, 582 void *__unused) 583 { 584 return 0; 585 } 586 587 /* 588 * 20.1. Operation 3: CB_GETATTR - Get Attributes 589 */ 590 static int nfs4_xdr_dec_cb_getattr(struct rpc_rqst *rqstp, 591 struct xdr_stream *xdr, 592 void *data) 593 { 594 struct nfsd4_callback *cb = data; 595 struct nfs4_cb_compound_hdr hdr; 596 int status; 597 u32 bitmap[3] = {0}; 598 u32 attrlen; 599 struct nfs4_cb_fattr *ncf = 600 container_of(cb, struct nfs4_cb_fattr, ncf_getattr); 601 602 status = decode_cb_compound4res(xdr, &hdr); 603 if (unlikely(status)) 604 return status; 605 606 status = decode_cb_sequence4res(xdr, cb); 607 if (unlikely(status || cb->cb_seq_status)) 608 return status; 609 610 status = decode_cb_op_status(xdr, OP_CB_GETATTR, &cb->cb_status); 611 if (status) 612 return status; 613 if (xdr_stream_decode_uint32_array(xdr, bitmap, 3) < 0) 614 return -NFSERR_BAD_XDR; 615 if (xdr_stream_decode_u32(xdr, &attrlen) < 0) 616 return -NFSERR_BAD_XDR; 617 if (attrlen > (sizeof(ncf->ncf_cb_change) + sizeof(ncf->ncf_cb_fsize))) 618 return -NFSERR_BAD_XDR; 619 status = decode_cb_fattr4(xdr, bitmap, ncf); 620 return status; 621 } 622 623 /* 624 * 20.2. Operation 4: CB_RECALL - Recall a Delegation 625 */ 626 static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, 627 struct xdr_stream *xdr, 628 void *data) 629 { 630 struct nfsd4_callback *cb = data; 631 struct nfs4_cb_compound_hdr hdr; 632 int status; 633 634 status = decode_cb_compound4res(xdr, &hdr); 635 if (unlikely(status)) 636 return status; 637 638 status = decode_cb_sequence4res(xdr, cb); 639 if (unlikely(status || cb->cb_seq_status)) 640 return status; 641 642 return decode_cb_op_status(xdr, OP_CB_RECALL, &cb->cb_status); 643 } 644 645 /* 646 * 20.6. Operation 8: CB_RECALL_ANY - Keep Any N Recallable Objects 647 */ 648 static int 649 nfs4_xdr_dec_cb_recall_any(struct rpc_rqst *rqstp, 650 struct xdr_stream *xdr, 651 void *data) 652 { 653 struct nfsd4_callback *cb = data; 654 struct nfs4_cb_compound_hdr hdr; 655 int status; 656 657 status = decode_cb_compound4res(xdr, &hdr); 658 if (unlikely(status)) 659 return status; 660 status = decode_cb_sequence4res(xdr, cb); 661 if (unlikely(status || cb->cb_seq_status)) 662 return status; 663 status = decode_cb_op_status(xdr, OP_CB_RECALL_ANY, &cb->cb_status); 664 return status; 665 } 666 667 #ifdef CONFIG_NFSD_PNFS 668 /* 669 * CB_LAYOUTRECALL4args 670 * 671 * struct layoutrecall_file4 { 672 * nfs_fh4 lor_fh; 673 * offset4 lor_offset; 674 * length4 lor_length; 675 * stateid4 lor_stateid; 676 * }; 677 * 678 * union layoutrecall4 switch(layoutrecall_type4 lor_recalltype) { 679 * case LAYOUTRECALL4_FILE: 680 * layoutrecall_file4 lor_layout; 681 * case LAYOUTRECALL4_FSID: 682 * fsid4 lor_fsid; 683 * case LAYOUTRECALL4_ALL: 684 * void; 685 * }; 686 * 687 * struct CB_LAYOUTRECALL4args { 688 * layouttype4 clora_type; 689 * layoutiomode4 clora_iomode; 690 * bool clora_changed; 691 * layoutrecall4 clora_recall; 692 * }; 693 */ 694 static void encode_cb_layout4args(struct xdr_stream *xdr, 695 const struct nfs4_layout_stateid *ls, 696 struct nfs4_cb_compound_hdr *hdr) 697 { 698 __be32 *p; 699 700 BUG_ON(hdr->minorversion == 0); 701 702 p = xdr_reserve_space(xdr, 5 * 4); 703 *p++ = cpu_to_be32(OP_CB_LAYOUTRECALL); 704 *p++ = cpu_to_be32(ls->ls_layout_type); 705 *p++ = cpu_to_be32(IOMODE_ANY); 706 *p++ = cpu_to_be32(1); 707 *p = cpu_to_be32(RETURN_FILE); 708 709 encode_nfs_fh4(xdr, &ls->ls_stid.sc_file->fi_fhandle); 710 711 p = xdr_reserve_space(xdr, 2 * 8); 712 p = xdr_encode_hyper(p, 0); 713 xdr_encode_hyper(p, NFS4_MAX_UINT64); 714 715 encode_stateid4(xdr, &ls->ls_recall_sid); 716 717 hdr->nops++; 718 } 719 720 static void nfs4_xdr_enc_cb_layout(struct rpc_rqst *req, 721 struct xdr_stream *xdr, 722 const void *data) 723 { 724 const struct nfsd4_callback *cb = data; 725 const struct nfs4_layout_stateid *ls = 726 container_of(cb, struct nfs4_layout_stateid, ls_recall); 727 struct nfs4_cb_compound_hdr hdr = { 728 .ident = 0, 729 .minorversion = cb->cb_clp->cl_minorversion, 730 }; 731 732 encode_cb_compound4args(xdr, &hdr); 733 encode_cb_sequence4args(xdr, cb, &hdr); 734 encode_cb_layout4args(xdr, ls, &hdr); 735 encode_cb_nops(&hdr); 736 } 737 738 static int nfs4_xdr_dec_cb_layout(struct rpc_rqst *rqstp, 739 struct xdr_stream *xdr, 740 void *data) 741 { 742 struct nfsd4_callback *cb = data; 743 struct nfs4_cb_compound_hdr hdr; 744 int status; 745 746 status = decode_cb_compound4res(xdr, &hdr); 747 if (unlikely(status)) 748 return status; 749 750 status = decode_cb_sequence4res(xdr, cb); 751 if (unlikely(status || cb->cb_seq_status)) 752 return status; 753 754 return decode_cb_op_status(xdr, OP_CB_LAYOUTRECALL, &cb->cb_status); 755 } 756 #endif /* CONFIG_NFSD_PNFS */ 757 758 static void encode_stateowner(struct xdr_stream *xdr, struct nfs4_stateowner *so) 759 { 760 __be32 *p; 761 762 p = xdr_reserve_space(xdr, 8 + 4 + so->so_owner.len); 763 p = xdr_encode_opaque_fixed(p, &so->so_client->cl_clientid, 8); 764 xdr_encode_opaque(p, so->so_owner.data, so->so_owner.len); 765 } 766 767 static void nfs4_xdr_enc_cb_notify_lock(struct rpc_rqst *req, 768 struct xdr_stream *xdr, 769 const void *data) 770 { 771 const struct nfsd4_callback *cb = data; 772 const struct nfsd4_blocked_lock *nbl = 773 container_of(cb, struct nfsd4_blocked_lock, nbl_cb); 774 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)nbl->nbl_lock.c.flc_owner; 775 struct nfs4_cb_compound_hdr hdr = { 776 .ident = 0, 777 .minorversion = cb->cb_clp->cl_minorversion, 778 }; 779 780 __be32 *p; 781 782 BUG_ON(hdr.minorversion == 0); 783 784 encode_cb_compound4args(xdr, &hdr); 785 encode_cb_sequence4args(xdr, cb, &hdr); 786 787 p = xdr_reserve_space(xdr, 4); 788 *p = cpu_to_be32(OP_CB_NOTIFY_LOCK); 789 encode_nfs_fh4(xdr, &nbl->nbl_fh); 790 encode_stateowner(xdr, &lo->lo_owner); 791 hdr.nops++; 792 793 encode_cb_nops(&hdr); 794 } 795 796 static int nfs4_xdr_dec_cb_notify_lock(struct rpc_rqst *rqstp, 797 struct xdr_stream *xdr, 798 void *data) 799 { 800 struct nfsd4_callback *cb = data; 801 struct nfs4_cb_compound_hdr hdr; 802 int status; 803 804 status = decode_cb_compound4res(xdr, &hdr); 805 if (unlikely(status)) 806 return status; 807 808 status = decode_cb_sequence4res(xdr, cb); 809 if (unlikely(status || cb->cb_seq_status)) 810 return status; 811 812 return decode_cb_op_status(xdr, OP_CB_NOTIFY_LOCK, &cb->cb_status); 813 } 814 815 /* 816 * struct write_response4 { 817 * stateid4 wr_callback_id<1>; 818 * length4 wr_count; 819 * stable_how4 wr_committed; 820 * verifier4 wr_writeverf; 821 * }; 822 * union offload_info4 switch (nfsstat4 coa_status) { 823 * case NFS4_OK: 824 * write_response4 coa_resok4; 825 * default: 826 * length4 coa_bytes_copied; 827 * }; 828 * struct CB_OFFLOAD4args { 829 * nfs_fh4 coa_fh; 830 * stateid4 coa_stateid; 831 * offload_info4 coa_offload_info; 832 * }; 833 */ 834 static void encode_offload_info4(struct xdr_stream *xdr, 835 const struct nfsd4_cb_offload *cbo) 836 { 837 __be32 *p; 838 839 p = xdr_reserve_space(xdr, 4); 840 *p = cbo->co_nfserr; 841 switch (cbo->co_nfserr) { 842 case nfs_ok: 843 p = xdr_reserve_space(xdr, 4 + 8 + 4 + NFS4_VERIFIER_SIZE); 844 p = xdr_encode_empty_array(p); 845 p = xdr_encode_hyper(p, cbo->co_res.wr_bytes_written); 846 *p++ = cpu_to_be32(cbo->co_res.wr_stable_how); 847 p = xdr_encode_opaque_fixed(p, cbo->co_res.wr_verifier.data, 848 NFS4_VERIFIER_SIZE); 849 break; 850 default: 851 p = xdr_reserve_space(xdr, 8); 852 /* We always return success if bytes were written */ 853 p = xdr_encode_hyper(p, 0); 854 } 855 } 856 857 static void encode_cb_offload4args(struct xdr_stream *xdr, 858 const struct nfsd4_cb_offload *cbo, 859 struct nfs4_cb_compound_hdr *hdr) 860 { 861 __be32 *p; 862 863 p = xdr_reserve_space(xdr, 4); 864 *p = cpu_to_be32(OP_CB_OFFLOAD); 865 encode_nfs_fh4(xdr, &cbo->co_fh); 866 encode_stateid4(xdr, &cbo->co_res.cb_stateid); 867 encode_offload_info4(xdr, cbo); 868 869 hdr->nops++; 870 } 871 872 static void nfs4_xdr_enc_cb_offload(struct rpc_rqst *req, 873 struct xdr_stream *xdr, 874 const void *data) 875 { 876 const struct nfsd4_callback *cb = data; 877 const struct nfsd4_cb_offload *cbo = 878 container_of(cb, struct nfsd4_cb_offload, co_cb); 879 struct nfs4_cb_compound_hdr hdr = { 880 .ident = 0, 881 .minorversion = cb->cb_clp->cl_minorversion, 882 }; 883 884 encode_cb_compound4args(xdr, &hdr); 885 encode_cb_sequence4args(xdr, cb, &hdr); 886 encode_cb_offload4args(xdr, cbo, &hdr); 887 encode_cb_nops(&hdr); 888 } 889 890 static int nfs4_xdr_dec_cb_offload(struct rpc_rqst *rqstp, 891 struct xdr_stream *xdr, 892 void *data) 893 { 894 struct nfsd4_callback *cb = data; 895 struct nfs4_cb_compound_hdr hdr; 896 int status; 897 898 status = decode_cb_compound4res(xdr, &hdr); 899 if (unlikely(status)) 900 return status; 901 902 status = decode_cb_sequence4res(xdr, cb); 903 if (unlikely(status || cb->cb_seq_status)) 904 return status; 905 906 return decode_cb_op_status(xdr, OP_CB_OFFLOAD, &cb->cb_status); 907 } 908 /* 909 * RPC procedure tables 910 */ 911 #define PROC(proc, call, argtype, restype) \ 912 [NFSPROC4_CLNT_##proc] = { \ 913 .p_proc = NFSPROC4_CB_##call, \ 914 .p_encode = nfs4_xdr_enc_##argtype, \ 915 .p_decode = nfs4_xdr_dec_##restype, \ 916 .p_arglen = NFS4_enc_##argtype##_sz, \ 917 .p_replen = NFS4_dec_##restype##_sz, \ 918 .p_statidx = NFSPROC4_CB_##call, \ 919 .p_name = #proc, \ 920 } 921 922 static const struct rpc_procinfo nfs4_cb_procedures[] = { 923 PROC(CB_NULL, NULL, cb_null, cb_null), 924 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall), 925 #ifdef CONFIG_NFSD_PNFS 926 PROC(CB_LAYOUT, COMPOUND, cb_layout, cb_layout), 927 #endif 928 PROC(CB_NOTIFY_LOCK, COMPOUND, cb_notify_lock, cb_notify_lock), 929 PROC(CB_OFFLOAD, COMPOUND, cb_offload, cb_offload), 930 PROC(CB_RECALL_ANY, COMPOUND, cb_recall_any, cb_recall_any), 931 PROC(CB_GETATTR, COMPOUND, cb_getattr, cb_getattr), 932 }; 933 934 static unsigned int nfs4_cb_counts[ARRAY_SIZE(nfs4_cb_procedures)]; 935 static const struct rpc_version nfs_cb_version4 = { 936 /* 937 * Note on the callback rpc program version number: despite language in rfc 938 * 5661 section 18.36.3 requiring servers to use 4 in this field, the 939 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and 940 * in practice that appears to be what implementations use. The section 941 * 18.36.3 language is expected to be fixed in an erratum. 942 */ 943 .number = 1, 944 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures), 945 .procs = nfs4_cb_procedures, 946 .counts = nfs4_cb_counts, 947 }; 948 949 static const struct rpc_version *nfs_cb_version[2] = { 950 [1] = &nfs_cb_version4, 951 }; 952 953 static const struct rpc_program cb_program; 954 955 static struct rpc_stat cb_stats = { 956 .program = &cb_program 957 }; 958 959 #define NFS4_CALLBACK 0x40000000 960 static const struct rpc_program cb_program = { 961 .name = "nfs4_cb", 962 .number = NFS4_CALLBACK, 963 .nrvers = ARRAY_SIZE(nfs_cb_version), 964 .version = nfs_cb_version, 965 .stats = &cb_stats, 966 .pipe_dir_name = "nfsd4_cb", 967 }; 968 969 static int max_cb_time(struct net *net) 970 { 971 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 972 973 /* 974 * nfsd4_lease is set to at most one hour in __nfsd4_write_time, 975 * so we can use 32-bit math on it. Warn if that assumption 976 * ever stops being true. 977 */ 978 if (WARN_ON_ONCE(nn->nfsd4_lease > 3600)) 979 return 360 * HZ; 980 981 return max(((u32)nn->nfsd4_lease)/10, 1u) * HZ; 982 } 983 984 static bool nfsd4_queue_cb(struct nfsd4_callback *cb) 985 { 986 struct nfs4_client *clp = cb->cb_clp; 987 988 trace_nfsd_cb_queue(clp, cb); 989 return queue_work(clp->cl_callback_wq, &cb->cb_work); 990 } 991 992 static void nfsd41_cb_inflight_begin(struct nfs4_client *clp) 993 { 994 atomic_inc(&clp->cl_cb_inflight); 995 } 996 997 static void nfsd41_cb_inflight_end(struct nfs4_client *clp) 998 { 999 1000 if (atomic_dec_and_test(&clp->cl_cb_inflight)) 1001 wake_up_var(&clp->cl_cb_inflight); 1002 } 1003 1004 static void nfsd41_cb_inflight_wait_complete(struct nfs4_client *clp) 1005 { 1006 wait_var_event(&clp->cl_cb_inflight, 1007 !atomic_read(&clp->cl_cb_inflight)); 1008 } 1009 1010 static const struct cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses) 1011 { 1012 if (clp->cl_minorversion == 0) { 1013 client->cl_principal = clp->cl_cred.cr_targ_princ ? 1014 clp->cl_cred.cr_targ_princ : "nfs"; 1015 1016 return get_cred(rpc_machine_cred()); 1017 } else { 1018 struct cred *kcred; 1019 1020 kcred = prepare_kernel_cred(&init_task); 1021 if (!kcred) 1022 return NULL; 1023 1024 kcred->fsuid = ses->se_cb_sec.uid; 1025 kcred->fsgid = ses->se_cb_sec.gid; 1026 return kcred; 1027 } 1028 } 1029 1030 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses) 1031 { 1032 int maxtime = max_cb_time(clp->net); 1033 struct rpc_timeout timeparms = { 1034 .to_initval = maxtime, 1035 .to_retries = 0, 1036 .to_maxval = maxtime, 1037 }; 1038 struct rpc_create_args args = { 1039 .net = clp->net, 1040 .address = (struct sockaddr *) &conn->cb_addr, 1041 .addrsize = conn->cb_addrlen, 1042 .saddress = (struct sockaddr *) &conn->cb_saddr, 1043 .timeout = &timeparms, 1044 .program = &cb_program, 1045 .version = 1, 1046 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET), 1047 .cred = current_cred(), 1048 }; 1049 struct rpc_clnt *client; 1050 const struct cred *cred; 1051 1052 if (clp->cl_minorversion == 0) { 1053 if (!clp->cl_cred.cr_principal && 1054 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5)) { 1055 trace_nfsd_cb_setup_err(clp, -EINVAL); 1056 return -EINVAL; 1057 } 1058 args.client_name = clp->cl_cred.cr_principal; 1059 args.prognumber = conn->cb_prog; 1060 args.protocol = XPRT_TRANSPORT_TCP; 1061 args.authflavor = clp->cl_cred.cr_flavor; 1062 clp->cl_cb_ident = conn->cb_ident; 1063 } else { 1064 if (!conn->cb_xprt) 1065 return -EINVAL; 1066 clp->cl_cb_session = ses; 1067 args.bc_xprt = conn->cb_xprt; 1068 args.prognumber = clp->cl_cb_session->se_cb_prog; 1069 args.protocol = conn->cb_xprt->xpt_class->xcl_ident | 1070 XPRT_TRANSPORT_BC; 1071 args.authflavor = ses->se_cb_sec.flavor; 1072 } 1073 /* Create RPC client */ 1074 client = rpc_create(&args); 1075 if (IS_ERR(client)) { 1076 trace_nfsd_cb_setup_err(clp, PTR_ERR(client)); 1077 return PTR_ERR(client); 1078 } 1079 cred = get_backchannel_cred(clp, client, ses); 1080 if (!cred) { 1081 trace_nfsd_cb_setup_err(clp, -ENOMEM); 1082 rpc_shutdown_client(client); 1083 return -ENOMEM; 1084 } 1085 1086 if (clp->cl_minorversion != 0) 1087 clp->cl_cb_conn.cb_xprt = conn->cb_xprt; 1088 clp->cl_cb_client = client; 1089 clp->cl_cb_cred = cred; 1090 rcu_read_lock(); 1091 trace_nfsd_cb_setup(clp, rpc_peeraddr2str(client, RPC_DISPLAY_NETID), 1092 args.authflavor); 1093 rcu_read_unlock(); 1094 return 0; 1095 } 1096 1097 static void nfsd4_mark_cb_state(struct nfs4_client *clp, int newstate) 1098 { 1099 if (clp->cl_cb_state != newstate) { 1100 clp->cl_cb_state = newstate; 1101 trace_nfsd_cb_new_state(clp); 1102 } 1103 } 1104 1105 static void nfsd4_mark_cb_down(struct nfs4_client *clp) 1106 { 1107 if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags)) 1108 return; 1109 nfsd4_mark_cb_state(clp, NFSD4_CB_DOWN); 1110 } 1111 1112 static void nfsd4_mark_cb_fault(struct nfs4_client *clp) 1113 { 1114 if (test_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags)) 1115 return; 1116 nfsd4_mark_cb_state(clp, NFSD4_CB_FAULT); 1117 } 1118 1119 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata) 1120 { 1121 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null); 1122 1123 if (task->tk_status) 1124 nfsd4_mark_cb_down(clp); 1125 else 1126 nfsd4_mark_cb_state(clp, NFSD4_CB_UP); 1127 } 1128 1129 static void nfsd4_cb_probe_release(void *calldata) 1130 { 1131 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null); 1132 1133 nfsd41_cb_inflight_end(clp); 1134 1135 } 1136 1137 static const struct rpc_call_ops nfsd4_cb_probe_ops = { 1138 /* XXX: release method to ensure we set the cb channel down if 1139 * necessary on early failure? */ 1140 .rpc_call_done = nfsd4_cb_probe_done, 1141 .rpc_release = nfsd4_cb_probe_release, 1142 }; 1143 1144 /* 1145 * Poke the callback thread to process any updates to the callback 1146 * parameters, and send a null probe. 1147 */ 1148 void nfsd4_probe_callback(struct nfs4_client *clp) 1149 { 1150 trace_nfsd_cb_probe(clp); 1151 nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN); 1152 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags); 1153 nfsd4_run_cb(&clp->cl_cb_null); 1154 } 1155 1156 void nfsd4_probe_callback_sync(struct nfs4_client *clp) 1157 { 1158 nfsd4_probe_callback(clp); 1159 flush_workqueue(clp->cl_callback_wq); 1160 } 1161 1162 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn) 1163 { 1164 nfsd4_mark_cb_state(clp, NFSD4_CB_UNKNOWN); 1165 spin_lock(&clp->cl_lock); 1166 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn)); 1167 spin_unlock(&clp->cl_lock); 1168 } 1169 1170 /* 1171 * There's currently a single callback channel slot. 1172 * If the slot is available, then mark it busy. Otherwise, set the 1173 * thread for sleeping on the callback RPC wait queue. 1174 */ 1175 static bool nfsd41_cb_get_slot(struct nfsd4_callback *cb, struct rpc_task *task) 1176 { 1177 struct nfs4_client *clp = cb->cb_clp; 1178 1179 if (!cb->cb_holds_slot && 1180 test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) { 1181 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL); 1182 /* Race breaker */ 1183 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) { 1184 dprintk("%s slot is busy\n", __func__); 1185 return false; 1186 } 1187 rpc_wake_up_queued_task(&clp->cl_cb_waitq, task); 1188 } 1189 cb->cb_holds_slot = true; 1190 return true; 1191 } 1192 1193 static void nfsd41_cb_release_slot(struct nfsd4_callback *cb) 1194 { 1195 struct nfs4_client *clp = cb->cb_clp; 1196 1197 if (cb->cb_holds_slot) { 1198 cb->cb_holds_slot = false; 1199 clear_bit(0, &clp->cl_cb_slot_busy); 1200 rpc_wake_up_next(&clp->cl_cb_waitq); 1201 } 1202 } 1203 1204 static void nfsd41_destroy_cb(struct nfsd4_callback *cb) 1205 { 1206 struct nfs4_client *clp = cb->cb_clp; 1207 1208 trace_nfsd_cb_destroy(clp, cb); 1209 nfsd41_cb_release_slot(cb); 1210 if (cb->cb_ops && cb->cb_ops->release) 1211 cb->cb_ops->release(cb); 1212 nfsd41_cb_inflight_end(clp); 1213 } 1214 1215 /* 1216 * TODO: cb_sequence should support referring call lists, cachethis, multiple 1217 * slots, and mark callback channel down on communication errors. 1218 */ 1219 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata) 1220 { 1221 struct nfsd4_callback *cb = calldata; 1222 struct nfs4_client *clp = cb->cb_clp; 1223 u32 minorversion = clp->cl_minorversion; 1224 1225 /* 1226 * cb_seq_status is only set in decode_cb_sequence4res, 1227 * and so will remain 1 if an rpc level failure occurs. 1228 */ 1229 trace_nfsd_cb_rpc_prepare(clp); 1230 cb->cb_seq_status = 1; 1231 cb->cb_status = 0; 1232 if (minorversion && !nfsd41_cb_get_slot(cb, task)) 1233 return; 1234 rpc_call_start(task); 1235 } 1236 1237 static bool nfsd4_cb_sequence_done(struct rpc_task *task, struct nfsd4_callback *cb) 1238 { 1239 struct nfs4_client *clp = cb->cb_clp; 1240 struct nfsd4_session *session = clp->cl_cb_session; 1241 bool ret = true; 1242 1243 if (!clp->cl_minorversion) { 1244 /* 1245 * If the backchannel connection was shut down while this 1246 * task was queued, we need to resubmit it after setting up 1247 * a new backchannel connection. 1248 * 1249 * Note that if we lost our callback connection permanently 1250 * the submission code will error out, so we don't need to 1251 * handle that case here. 1252 */ 1253 if (RPC_SIGNALLED(task)) 1254 goto need_restart; 1255 1256 return true; 1257 } 1258 1259 if (!cb->cb_holds_slot) 1260 goto need_restart; 1261 1262 /* This is the operation status code for CB_SEQUENCE */ 1263 trace_nfsd_cb_seq_status(task, cb); 1264 switch (cb->cb_seq_status) { 1265 case 0: 1266 /* 1267 * No need for lock, access serialized in nfsd4_cb_prepare 1268 * 1269 * RFC5661 20.9.3 1270 * If CB_SEQUENCE returns an error, then the state of the slot 1271 * (sequence ID, cached reply) MUST NOT change. 1272 */ 1273 ++session->se_cb_seq_nr; 1274 break; 1275 case -ESERVERFAULT: 1276 ++session->se_cb_seq_nr; 1277 nfsd4_mark_cb_fault(cb->cb_clp); 1278 ret = false; 1279 break; 1280 case 1: 1281 /* 1282 * cb_seq_status remains 1 if an RPC Reply was never 1283 * received. NFSD can't know if the client processed 1284 * the CB_SEQUENCE operation. Ask the client to send a 1285 * DESTROY_SESSION to recover. 1286 */ 1287 fallthrough; 1288 case -NFS4ERR_BADSESSION: 1289 nfsd4_mark_cb_fault(cb->cb_clp); 1290 ret = false; 1291 goto need_restart; 1292 case -NFS4ERR_DELAY: 1293 cb->cb_seq_status = 1; 1294 if (!rpc_restart_call(task)) 1295 goto out; 1296 1297 rpc_delay(task, 2 * HZ); 1298 return false; 1299 case -NFS4ERR_BADSLOT: 1300 goto retry_nowait; 1301 case -NFS4ERR_SEQ_MISORDERED: 1302 if (session->se_cb_seq_nr != 1) { 1303 session->se_cb_seq_nr = 1; 1304 goto retry_nowait; 1305 } 1306 break; 1307 default: 1308 nfsd4_mark_cb_fault(cb->cb_clp); 1309 } 1310 nfsd41_cb_release_slot(cb); 1311 1312 trace_nfsd_cb_free_slot(task, cb); 1313 1314 if (RPC_SIGNALLED(task)) 1315 goto need_restart; 1316 out: 1317 return ret; 1318 retry_nowait: 1319 if (rpc_restart_call_prepare(task)) 1320 ret = false; 1321 goto out; 1322 need_restart: 1323 if (!test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) { 1324 trace_nfsd_cb_restart(clp, cb); 1325 task->tk_status = 0; 1326 cb->cb_need_restart = true; 1327 } 1328 return false; 1329 } 1330 1331 static void nfsd4_cb_done(struct rpc_task *task, void *calldata) 1332 { 1333 struct nfsd4_callback *cb = calldata; 1334 struct nfs4_client *clp = cb->cb_clp; 1335 1336 trace_nfsd_cb_rpc_done(clp); 1337 1338 if (!nfsd4_cb_sequence_done(task, cb)) 1339 return; 1340 1341 if (cb->cb_status) { 1342 WARN_ONCE(task->tk_status, "cb_status=%d tk_status=%d", 1343 cb->cb_status, task->tk_status); 1344 task->tk_status = cb->cb_status; 1345 } 1346 1347 switch (cb->cb_ops->done(cb, task)) { 1348 case 0: 1349 task->tk_status = 0; 1350 rpc_restart_call_prepare(task); 1351 return; 1352 case 1: 1353 switch (task->tk_status) { 1354 case -EIO: 1355 case -ETIMEDOUT: 1356 case -EACCES: 1357 nfsd4_mark_cb_down(clp); 1358 } 1359 break; 1360 default: 1361 BUG(); 1362 } 1363 } 1364 1365 static void nfsd4_cb_release(void *calldata) 1366 { 1367 struct nfsd4_callback *cb = calldata; 1368 1369 trace_nfsd_cb_rpc_release(cb->cb_clp); 1370 1371 if (cb->cb_need_restart) 1372 nfsd4_queue_cb(cb); 1373 else 1374 nfsd41_destroy_cb(cb); 1375 1376 } 1377 1378 static const struct rpc_call_ops nfsd4_cb_ops = { 1379 .rpc_call_prepare = nfsd4_cb_prepare, 1380 .rpc_call_done = nfsd4_cb_done, 1381 .rpc_release = nfsd4_cb_release, 1382 }; 1383 1384 /* must be called under the state lock */ 1385 void nfsd4_shutdown_callback(struct nfs4_client *clp) 1386 { 1387 if (clp->cl_cb_state != NFSD4_CB_UNKNOWN) 1388 trace_nfsd_cb_shutdown(clp); 1389 1390 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags); 1391 /* 1392 * Note this won't actually result in a null callback; 1393 * instead, nfsd4_run_cb_null() will detect the killed 1394 * client, destroy the rpc client, and stop: 1395 */ 1396 nfsd4_run_cb(&clp->cl_cb_null); 1397 flush_workqueue(clp->cl_callback_wq); 1398 nfsd41_cb_inflight_wait_complete(clp); 1399 } 1400 1401 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp) 1402 { 1403 struct nfsd4_session *s; 1404 struct nfsd4_conn *c; 1405 1406 lockdep_assert_held(&clp->cl_lock); 1407 1408 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) { 1409 list_for_each_entry(c, &s->se_conns, cn_persession) { 1410 if (c->cn_flags & NFS4_CDFC4_BACK) 1411 return c; 1412 } 1413 } 1414 return NULL; 1415 } 1416 1417 /* 1418 * Note there isn't a lot of locking in this code; instead we depend on 1419 * the fact that it is run from clp->cl_callback_wq, which won't run two 1420 * work items at once. So, for example, clp->cl_callback_wq handles all 1421 * access of cl_cb_client and all calls to rpc_create or rpc_shutdown_client. 1422 */ 1423 static void nfsd4_process_cb_update(struct nfsd4_callback *cb) 1424 { 1425 struct nfs4_cb_conn conn; 1426 struct nfs4_client *clp = cb->cb_clp; 1427 struct nfsd4_session *ses = NULL; 1428 struct nfsd4_conn *c; 1429 int err; 1430 1431 trace_nfsd_cb_bc_update(clp, cb); 1432 1433 /* 1434 * This is either an update, or the client dying; in either case, 1435 * kill the old client: 1436 */ 1437 if (clp->cl_cb_client) { 1438 trace_nfsd_cb_bc_shutdown(clp, cb); 1439 rpc_shutdown_client(clp->cl_cb_client); 1440 clp->cl_cb_client = NULL; 1441 put_cred(clp->cl_cb_cred); 1442 clp->cl_cb_cred = NULL; 1443 } 1444 if (clp->cl_cb_conn.cb_xprt) { 1445 svc_xprt_put(clp->cl_cb_conn.cb_xprt); 1446 clp->cl_cb_conn.cb_xprt = NULL; 1447 } 1448 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags)) 1449 return; 1450 1451 spin_lock(&clp->cl_lock); 1452 /* 1453 * Only serialized callback code is allowed to clear these 1454 * flags; main nfsd code can only set them: 1455 */ 1456 WARN_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)); 1457 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags); 1458 1459 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn)); 1460 c = __nfsd4_find_backchannel(clp); 1461 if (c) { 1462 svc_xprt_get(c->cn_xprt); 1463 conn.cb_xprt = c->cn_xprt; 1464 ses = c->cn_session; 1465 } 1466 spin_unlock(&clp->cl_lock); 1467 if (!c) 1468 return; 1469 1470 err = setup_callback_client(clp, &conn, ses); 1471 if (err) { 1472 nfsd4_mark_cb_down(clp); 1473 if (c) 1474 svc_xprt_put(c->cn_xprt); 1475 return; 1476 } 1477 } 1478 1479 static void 1480 nfsd4_run_cb_work(struct work_struct *work) 1481 { 1482 struct nfsd4_callback *cb = 1483 container_of(work, struct nfsd4_callback, cb_work); 1484 struct nfs4_client *clp = cb->cb_clp; 1485 struct rpc_clnt *clnt; 1486 int flags; 1487 1488 trace_nfsd_cb_start(clp); 1489 1490 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK) 1491 nfsd4_process_cb_update(cb); 1492 1493 clnt = clp->cl_cb_client; 1494 if (!clnt) { 1495 /* Callback channel broken, or client killed; give up: */ 1496 nfsd41_destroy_cb(cb); 1497 return; 1498 } 1499 1500 /* 1501 * Don't send probe messages for 4.1 or later. 1502 */ 1503 if (!cb->cb_ops && clp->cl_minorversion) { 1504 nfsd4_mark_cb_state(clp, NFSD4_CB_UP); 1505 nfsd41_destroy_cb(cb); 1506 return; 1507 } 1508 1509 if (cb->cb_need_restart) { 1510 cb->cb_need_restart = false; 1511 } else { 1512 if (cb->cb_ops && cb->cb_ops->prepare) 1513 cb->cb_ops->prepare(cb); 1514 } 1515 cb->cb_msg.rpc_cred = clp->cl_cb_cred; 1516 flags = clp->cl_minorversion ? RPC_TASK_NOCONNECT : RPC_TASK_SOFTCONN; 1517 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | flags, 1518 cb->cb_ops ? &nfsd4_cb_ops : &nfsd4_cb_probe_ops, cb); 1519 } 1520 1521 void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp, 1522 const struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op) 1523 { 1524 cb->cb_clp = clp; 1525 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op]; 1526 cb->cb_msg.rpc_argp = cb; 1527 cb->cb_msg.rpc_resp = cb; 1528 cb->cb_ops = ops; 1529 INIT_WORK(&cb->cb_work, nfsd4_run_cb_work); 1530 cb->cb_status = 0; 1531 cb->cb_need_restart = false; 1532 cb->cb_holds_slot = false; 1533 } 1534 1535 /** 1536 * nfsd4_run_cb - queue up a callback job to run 1537 * @cb: callback to queue 1538 * 1539 * Kick off a callback to do its thing. Returns false if it was already 1540 * on a queue, true otherwise. 1541 */ 1542 bool nfsd4_run_cb(struct nfsd4_callback *cb) 1543 { 1544 struct nfs4_client *clp = cb->cb_clp; 1545 bool queued; 1546 1547 nfsd41_cb_inflight_begin(clp); 1548 queued = nfsd4_queue_cb(cb); 1549 if (!queued) 1550 nfsd41_cb_inflight_end(clp); 1551 return queued; 1552 } 1553