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