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