Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/sunrpc/clnt.h> |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 35 | #include <linux/sunrpc/svc_xprt.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 36 | #include <linux/slab.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 37 | #include "nfsd.h" |
| 38 | #include "state.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 41 | |
| 42 | #define NFSPROC4_CB_NULL 0 |
| 43 | #define NFSPROC4_CB_COMPOUND 1 |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | /* Index of predefined Linux callback client operations */ |
| 46 | |
| 47 | enum { |
Benny Halevy | 4be36ca | 2009-09-10 12:25:46 +0300 | [diff] [blame] | 48 | NFSPROC4_CLNT_CB_NULL = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | NFSPROC4_CLNT_CB_RECALL, |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 50 | NFSPROC4_CLNT_CB_SEQUENCE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #define NFS4_MAXTAGLEN 20 |
| 54 | |
| 55 | #define NFS4_enc_cb_null_sz 0 |
| 56 | #define NFS4_dec_cb_null_sz 0 |
| 57 | #define cb_compound_enc_hdr_sz 4 |
| 58 | #define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2)) |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 59 | #define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2) |
| 60 | #define cb_sequence_enc_sz (sessionid_sz + 4 + \ |
| 61 | 1 /* no referring calls list yet */) |
| 62 | #define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4) |
| 63 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #define op_enc_sz 1 |
| 65 | #define op_dec_sz 2 |
| 66 | #define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2)) |
Benny Halevy | 0ac68d1 | 2007-07-17 04:04:37 -0700 | [diff] [blame] | 67 | #define enc_stateid_sz (NFS4_STATEID_SIZE >> 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \ |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 69 | cb_sequence_enc_sz + \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | 1 + enc_stateid_sz + \ |
| 71 | enc_nfs4_fh_sz) |
| 72 | |
| 73 | #define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \ |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 74 | cb_sequence_dec_sz + \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | op_dec_sz) |
| 76 | |
| 77 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * Generic decode routines from fs/nfs/nfs4xdr.c |
| 79 | */ |
| 80 | #define DECODE_TAIL \ |
| 81 | status = 0; \ |
| 82 | out: \ |
| 83 | return status; \ |
| 84 | xdr_error: \ |
| 85 | dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \ |
| 86 | status = -EIO; \ |
| 87 | goto out |
| 88 | |
| 89 | #define READ32(x) (x) = ntohl(*p++) |
| 90 | #define READ64(x) do { \ |
| 91 | (x) = (u64)ntohl(*p++) << 32; \ |
| 92 | (x) |= ntohl(*p++); \ |
| 93 | } while (0) |
| 94 | #define READTIME(x) do { \ |
| 95 | p++; \ |
| 96 | (x.tv_sec) = ntohl(*p++); \ |
| 97 | (x.tv_nsec) = ntohl(*p++); \ |
| 98 | } while (0) |
| 99 | #define READ_BUF(nbytes) do { \ |
| 100 | p = xdr_inline_decode(xdr, nbytes); \ |
| 101 | if (!p) { \ |
Greg Banks | 3e3b480 | 2006-10-02 02:17:41 -0700 | [diff] [blame] | 102 | dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \ |
Harvey Harrison | 8e24eea | 2008-04-30 00:55:09 -0700 | [diff] [blame] | 103 | __func__, __LINE__); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return -EIO; \ |
| 105 | } \ |
| 106 | } while (0) |
| 107 | |
| 108 | struct nfs4_cb_compound_hdr { |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 109 | /* args */ |
| 110 | u32 ident; /* minorversion 0 only */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | u32 nops; |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 112 | __be32 *nops_p; |
Andy Adamson | ab52ae6 | 2009-06-16 04:20:53 +0300 | [diff] [blame] | 113 | u32 minorversion; |
Andy Adamson | 38524ab | 2009-09-10 12:25:59 +0300 | [diff] [blame] | 114 | /* res */ |
| 115 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | static struct { |
| 119 | int stat; |
| 120 | int errno; |
| 121 | } nfs_cb_errtbl[] = { |
| 122 | { NFS4_OK, 0 }, |
| 123 | { NFS4ERR_PERM, EPERM }, |
| 124 | { NFS4ERR_NOENT, ENOENT }, |
| 125 | { NFS4ERR_IO, EIO }, |
| 126 | { NFS4ERR_NXIO, ENXIO }, |
| 127 | { NFS4ERR_ACCESS, EACCES }, |
| 128 | { NFS4ERR_EXIST, EEXIST }, |
| 129 | { NFS4ERR_XDEV, EXDEV }, |
| 130 | { NFS4ERR_NOTDIR, ENOTDIR }, |
| 131 | { NFS4ERR_ISDIR, EISDIR }, |
| 132 | { NFS4ERR_INVAL, EINVAL }, |
| 133 | { NFS4ERR_FBIG, EFBIG }, |
| 134 | { NFS4ERR_NOSPC, ENOSPC }, |
| 135 | { NFS4ERR_ROFS, EROFS }, |
| 136 | { NFS4ERR_MLINK, EMLINK }, |
| 137 | { NFS4ERR_NAMETOOLONG, ENAMETOOLONG }, |
| 138 | { NFS4ERR_NOTEMPTY, ENOTEMPTY }, |
| 139 | { NFS4ERR_DQUOT, EDQUOT }, |
| 140 | { NFS4ERR_STALE, ESTALE }, |
| 141 | { NFS4ERR_BADHANDLE, EBADHANDLE }, |
| 142 | { NFS4ERR_BAD_COOKIE, EBADCOOKIE }, |
| 143 | { NFS4ERR_NOTSUPP, ENOTSUPP }, |
| 144 | { NFS4ERR_TOOSMALL, ETOOSMALL }, |
| 145 | { NFS4ERR_SERVERFAULT, ESERVERFAULT }, |
| 146 | { NFS4ERR_BADTYPE, EBADTYPE }, |
| 147 | { NFS4ERR_LOCKED, EAGAIN }, |
| 148 | { NFS4ERR_RESOURCE, EREMOTEIO }, |
| 149 | { NFS4ERR_SYMLINK, ELOOP }, |
| 150 | { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP }, |
| 151 | { NFS4ERR_DEADLOCK, EDEADLK }, |
| 152 | { -1, EIO } |
| 153 | }; |
| 154 | |
| 155 | static int |
| 156 | nfs_cb_stat_to_errno(int stat) |
| 157 | { |
| 158 | int i; |
| 159 | for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) { |
| 160 | if (nfs_cb_errtbl[i].stat == stat) |
| 161 | return nfs_cb_errtbl[i].errno; |
| 162 | } |
| 163 | /* If we cannot translate the error, the recovery routines should |
| 164 | * handle it. |
| 165 | * Note: remaining NFSv4 error codes have values > 10000, so should |
| 166 | * not conflict with native Linux error codes. |
| 167 | */ |
| 168 | return stat; |
| 169 | } |
| 170 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 171 | static __be32 *xdr_encode_empty_array(__be32 *p) |
| 172 | { |
| 173 | *p++ = xdr_zero; |
| 174 | return p; |
| 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /* |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 178 | * Encode/decode NFSv4 CB basic data types |
| 179 | * |
| 180 | * Basic NFSv4 callback data types are defined in section 15 of RFC |
| 181 | * 3530: "Network File System (NFS) version 4 Protocol" and section |
| 182 | * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version |
| 183 | * 1 Protocol" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | */ |
| 185 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 186 | /* |
| 187 | * nfs_cb_opnum4 |
| 188 | * |
| 189 | * enum nfs_cb_opnum4 { |
| 190 | * OP_CB_GETATTR = 3, |
| 191 | * ... |
| 192 | * }; |
| 193 | */ |
| 194 | enum nfs_cb_opnum4 { |
| 195 | OP_CB_GETATTR = 3, |
| 196 | OP_CB_RECALL = 4, |
| 197 | OP_CB_LAYOUTRECALL = 5, |
| 198 | OP_CB_NOTIFY = 6, |
| 199 | OP_CB_PUSH_DELEG = 7, |
| 200 | OP_CB_RECALL_ANY = 8, |
| 201 | OP_CB_RECALLABLE_OBJ_AVAIL = 9, |
| 202 | OP_CB_RECALL_SLOT = 10, |
| 203 | OP_CB_SEQUENCE = 11, |
| 204 | OP_CB_WANTS_CANCELLED = 12, |
| 205 | OP_CB_NOTIFY_LOCK = 13, |
| 206 | OP_CB_NOTIFY_DEVICEID = 14, |
| 207 | OP_CB_ILLEGAL = 10044 |
| 208 | }; |
| 209 | |
| 210 | static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op) |
Benny Halevy | 9303bbd | 2010-05-25 09:50:23 +0300 | [diff] [blame] | 211 | { |
| 212 | __be32 *p; |
| 213 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 214 | p = xdr_reserve_space(xdr, 4); |
| 215 | *p = cpu_to_be32(op); |
Benny Halevy | 9303bbd | 2010-05-25 09:50:23 +0300 | [diff] [blame] | 216 | } |
| 217 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 218 | /* |
| 219 | * nfs_fh4 |
| 220 | * |
| 221 | * typedef opaque nfs_fh4<NFS4_FHSIZE>; |
| 222 | */ |
| 223 | static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh) |
| 224 | { |
| 225 | u32 length = fh->fh_size; |
| 226 | __be32 *p; |
| 227 | |
| 228 | BUG_ON(length > NFS4_FHSIZE); |
| 229 | p = xdr_reserve_space(xdr, 4 + length); |
| 230 | xdr_encode_opaque(p, &fh->fh_base, length); |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * stateid4 |
| 235 | * |
| 236 | * struct stateid4 { |
| 237 | * uint32_t seqid; |
| 238 | * opaque other[12]; |
| 239 | * }; |
| 240 | */ |
| 241 | static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid) |
| 242 | { |
| 243 | __be32 *p; |
| 244 | |
| 245 | p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE); |
| 246 | *p++ = cpu_to_be32(sid->si_generation); |
| 247 | xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE); |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * sessionid4 |
| 252 | * |
| 253 | * typedef opaque sessionid4[NFS4_SESSIONID_SIZE]; |
| 254 | */ |
| 255 | static void encode_sessionid4(struct xdr_stream *xdr, |
| 256 | const struct nfsd4_session *session) |
| 257 | { |
| 258 | __be32 *p; |
| 259 | |
| 260 | p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN); |
| 261 | xdr_encode_opaque_fixed(p, session->se_sessionid.data, |
| 262 | NFS4_MAX_SESSIONID_LEN); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * CB_COMPOUND4args |
| 267 | * |
| 268 | * struct CB_COMPOUND4args { |
| 269 | * utf8str_cs tag; |
| 270 | * uint32_t minorversion; |
| 271 | * uint32_t callback_ident; |
| 272 | * nfs_cb_argop4 argarray<>; |
| 273 | * }; |
| 274 | */ |
| 275 | static void encode_cb_compound4args(struct xdr_stream *xdr, |
| 276 | struct nfs4_cb_compound_hdr *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
Al Viro | f00f328 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 278 | __be32 * p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 280 | p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4); |
| 281 | p = xdr_encode_empty_array(p); /* empty tag */ |
| 282 | *p++ = cpu_to_be32(hdr->minorversion); |
| 283 | *p++ = cpu_to_be32(hdr->ident); |
| 284 | |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 285 | hdr->nops_p = p; |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 286 | *p = cpu_to_be32(hdr->nops); /* argarray element count */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 289 | /* |
| 290 | * Update argarray element count |
| 291 | */ |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 292 | static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr) |
| 293 | { |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 294 | BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS); |
| 295 | *hdr->nops_p = cpu_to_be32(hdr->nops); |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 296 | } |
| 297 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 298 | /* |
| 299 | * CB_RECALL4args |
| 300 | * |
| 301 | * struct CB_RECALL4args { |
| 302 | * stateid4 stateid; |
| 303 | * bool truncate; |
| 304 | * nfs_fh4 fh; |
| 305 | * }; |
| 306 | */ |
| 307 | static void encode_cb_recall4args(struct xdr_stream *xdr, |
| 308 | const struct nfs4_delegation *dp, |
| 309 | struct nfs4_cb_compound_hdr *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
Al Viro | f00f328 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 311 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 313 | encode_nfs_cb_opnum4(xdr, OP_CB_RECALL); |
| 314 | encode_stateid4(xdr, &dp->dl_stateid); |
| 315 | |
| 316 | p = xdr_reserve_space(xdr, 4); |
| 317 | *p++ = xdr_zero; /* truncate */ |
| 318 | |
| 319 | encode_nfs_fh4(xdr, &dp->dl_fh); |
| 320 | |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 321 | hdr->nops++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 324 | /* |
| 325 | * CB_SEQUENCE4args |
| 326 | * |
| 327 | * struct CB_SEQUENCE4args { |
| 328 | * sessionid4 csa_sessionid; |
| 329 | * sequenceid4 csa_sequenceid; |
| 330 | * slotid4 csa_slotid; |
| 331 | * slotid4 csa_highest_slotid; |
| 332 | * bool csa_cachethis; |
| 333 | * referring_call_list4 csa_referring_call_lists<>; |
| 334 | * }; |
| 335 | */ |
| 336 | static void encode_cb_sequence4args(struct xdr_stream *xdr, |
| 337 | const struct nfsd4_callback *cb, |
| 338 | struct nfs4_cb_compound_hdr *hdr) |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 339 | { |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 340 | struct nfsd4_session *session = cb->cb_clp->cl_cb_session; |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 341 | __be32 *p; |
| 342 | |
| 343 | if (hdr->minorversion == 0) |
| 344 | return; |
| 345 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 346 | encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE); |
| 347 | encode_sessionid4(xdr, session); |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 348 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 349 | p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4); |
| 350 | *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */ |
| 351 | *p++ = xdr_zero; /* csa_slotid */ |
| 352 | *p++ = xdr_zero; /* csa_highest_slotid */ |
| 353 | *p++ = xdr_zero; /* csa_cachethis */ |
| 354 | xdr_encode_empty_array(p); /* csa_referring_call_lists */ |
| 355 | |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 356 | hdr->nops++; |
| 357 | } |
| 358 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 359 | /* |
| 360 | * NFSv4.0 and NFSv4.1 XDR encode functions |
| 361 | * |
| 362 | * NFSv4.0 callback argument types are defined in section 15 of RFC |
| 363 | * 3530: "Network File System (NFS) version 4 Protocol" and section 20 |
| 364 | * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1 |
| 365 | * Protocol". |
| 366 | */ |
| 367 | |
| 368 | /* |
| 369 | * NB: Without this zero space reservation, callbacks over krb5p fail |
| 370 | */ |
| 371 | static int nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p, void *__unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
| 373 | struct xdr_stream xdrs, *xdr = &xdrs; |
| 374 | |
| 375 | xdr_init_encode(&xdrs, &req->rq_snd_buf, p); |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 376 | xdr_reserve_space(xdr, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 380 | /* |
| 381 | * 20.2. Operation 4: CB_RECALL - Recall a Delegation |
| 382 | */ |
| 383 | static int nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, |
| 384 | const struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { |
| 386 | struct xdr_stream xdr; |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 387 | const struct nfs4_delegation *args = cb->cb_op; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | struct nfs4_cb_compound_hdr hdr = { |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 389 | .ident = cb->cb_clp->cl_cb_ident, |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 390 | .minorversion = cb->cb_minorversion, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | }; |
| 392 | |
| 393 | xdr_init_encode(&xdr, &req->rq_snd_buf, p); |
Chuck Lever | a033db4 | 2010-12-14 14:57:22 +0000 | [diff] [blame^] | 394 | encode_cb_compound4args(&xdr, &hdr); |
| 395 | encode_cb_sequence4args(&xdr, cb, &hdr); |
| 396 | encode_cb_recall4args(&xdr, args, &hdr); |
Andy Adamson | ef52bff | 2009-06-16 04:20:50 +0300 | [diff] [blame] | 397 | encode_cb_nops(&hdr); |
| 398 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
| 402 | static int |
| 403 | decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){ |
Al Viro | f00f328 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 404 | __be32 *p; |
J. Bruce Fields | 68a4b48 | 2010-05-27 09:30:39 -0400 | [diff] [blame] | 405 | u32 taglen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | |
| 407 | READ_BUF(8); |
| 408 | READ32(hdr->status); |
J. Bruce Fields | 68a4b48 | 2010-05-27 09:30:39 -0400 | [diff] [blame] | 409 | /* We've got no use for the tag; ignore it: */ |
| 410 | READ32(taglen); |
| 411 | READ_BUF(taglen + 4); |
| 412 | p += XDR_QUADLEN(taglen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | READ32(hdr->nops); |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static int |
| 418 | decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected) |
| 419 | { |
Al Viro | f00f328 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 420 | __be32 *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | u32 op; |
| 422 | int32_t nfserr; |
| 423 | |
| 424 | READ_BUF(8); |
| 425 | READ32(op); |
| 426 | if (op != expected) { |
| 427 | dprintk("NFSD: decode_cb_op_hdr: Callback server returned " |
| 428 | " operation %d but we issued a request for %d\n", |
| 429 | op, expected); |
| 430 | return -EIO; |
| 431 | } |
| 432 | READ32(nfserr); |
| 433 | if (nfserr != NFS_OK) |
| 434 | return -nfs_cb_stat_to_errno(nfserr); |
| 435 | return 0; |
| 436 | } |
| 437 | |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 438 | /* |
| 439 | * Our current back channel implmentation supports a single backchannel |
| 440 | * with a single slot. |
| 441 | */ |
| 442 | static int |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 443 | decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_callback *cb, |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 444 | struct rpc_rqst *rqstp) |
| 445 | { |
J. Bruce Fields | 90c8145 | 2010-06-14 17:49:37 -0400 | [diff] [blame] | 446 | struct nfsd4_session *ses = cb->cb_clp->cl_cb_session; |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 447 | struct nfs4_sessionid id; |
| 448 | int status; |
| 449 | u32 dummy; |
| 450 | __be32 *p; |
| 451 | |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 452 | if (cb->cb_minorversion == 0) |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 453 | return 0; |
| 454 | |
| 455 | status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE); |
| 456 | if (status) |
| 457 | return status; |
| 458 | |
| 459 | /* |
| 460 | * If the server returns different values for sessionID, slotID or |
| 461 | * sequence number, the server is looney tunes. |
| 462 | */ |
| 463 | status = -ESERVERFAULT; |
| 464 | |
| 465 | READ_BUF(NFS4_MAX_SESSIONID_LEN + 16); |
| 466 | memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN); |
| 467 | p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 90c8145 | 2010-06-14 17:49:37 -0400 | [diff] [blame] | 468 | if (memcmp(id.data, ses->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) { |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 469 | dprintk("%s Invalid session id\n", __func__); |
| 470 | goto out; |
| 471 | } |
| 472 | READ32(dummy); |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 473 | if (dummy != ses->se_cb_seq_nr) { |
Benny Halevy | 2af7358 | 2009-09-10 12:26:51 +0300 | [diff] [blame] | 474 | dprintk("%s Invalid sequence number\n", __func__); |
| 475 | goto out; |
| 476 | } |
| 477 | READ32(dummy); /* slotid must be 0 */ |
| 478 | if (dummy != 0) { |
| 479 | dprintk("%s Invalid slotid\n", __func__); |
| 480 | goto out; |
| 481 | } |
| 482 | /* FIXME: process highest slotid and target highest slotid */ |
| 483 | status = 0; |
| 484 | out: |
| 485 | return status; |
| 486 | } |
| 487 | |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | static int |
Al Viro | f00f328 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 490 | nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 496 | nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p, |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 497 | struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
| 499 | struct xdr_stream xdr; |
| 500 | struct nfs4_cb_compound_hdr hdr; |
| 501 | int status; |
| 502 | |
| 503 | xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p); |
| 504 | status = decode_cb_compound_hdr(&xdr, &hdr); |
| 505 | if (status) |
| 506 | goto out; |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 507 | if (cb) { |
| 508 | status = decode_cb_sequence(&xdr, cb, rqstp); |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 509 | if (status) |
| 510 | goto out; |
| 511 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | status = decode_cb_op_hdr(&xdr, OP_CB_RECALL); |
| 513 | out: |
| 514 | return status; |
| 515 | } |
| 516 | |
| 517 | /* |
| 518 | * RPC procedure tables |
| 519 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | #define PROC(proc, call, argtype, restype) \ |
| 521 | [NFSPROC4_CLNT_##proc] = { \ |
| 522 | .p_proc = NFSPROC4_CB_##call, \ |
| 523 | .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \ |
| 524 | .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \ |
Chuck Lever | 2bea90d | 2007-03-29 16:47:53 -0400 | [diff] [blame] | 525 | .p_arglen = NFS4_##argtype##_sz, \ |
| 526 | .p_replen = NFS4_##restype##_sz, \ |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 527 | .p_statidx = NFSPROC4_CB_##call, \ |
| 528 | .p_name = #proc, \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 531 | static struct rpc_procinfo nfs4_cb_procedures[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null), |
| 533 | PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall), |
| 534 | }; |
| 535 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 536 | static struct rpc_version nfs_cb_version4 = { |
J. Bruce Fields | b7299f4 | 2010-05-14 17:57:35 -0400 | [diff] [blame] | 537 | /* |
| 538 | * Note on the callback rpc program version number: despite language in rfc |
| 539 | * 5661 section 18.36.3 requiring servers to use 4 in this field, the |
| 540 | * official xdr descriptions for both 4.0 and 4.1 specify version 1, and |
| 541 | * in practice that appears to be what implementations use. The section |
| 542 | * 18.36.3 language is expected to be fixed in an erratum. |
| 543 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | .number = 1, |
Tobias Klauser | e8c96f8 | 2006-03-24 03:15:34 -0800 | [diff] [blame] | 545 | .nrprocs = ARRAY_SIZE(nfs4_cb_procedures), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | .procs = nfs4_cb_procedures |
| 547 | }; |
| 548 | |
| 549 | static struct rpc_version * nfs_cb_version[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | &nfs_cb_version4, |
| 551 | }; |
| 552 | |
Olga Kornievskaia | ff7d975 | 2008-03-28 16:04:56 -0400 | [diff] [blame] | 553 | static struct rpc_program cb_program; |
| 554 | |
| 555 | static struct rpc_stat cb_stats = { |
| 556 | .program = &cb_program |
| 557 | }; |
| 558 | |
| 559 | #define NFS4_CALLBACK 0x40000000 |
| 560 | static struct rpc_program cb_program = { |
| 561 | .name = "nfs4_cb", |
| 562 | .number = NFS4_CALLBACK, |
| 563 | .nrvers = ARRAY_SIZE(nfs_cb_version), |
| 564 | .version = nfs_cb_version, |
| 565 | .stats = &cb_stats, |
Olga Kornievskaia | 61054b1 | 2008-12-23 16:19:00 -0500 | [diff] [blame] | 566 | .pipe_dir_name = "/nfsd4_cb", |
Olga Kornievskaia | ff7d975 | 2008-03-28 16:04:56 -0400 | [diff] [blame] | 567 | }; |
| 568 | |
J. Bruce Fields | 595947a | 2009-03-05 17:18:10 -0500 | [diff] [blame] | 569 | static int max_cb_time(void) |
| 570 | { |
J. Bruce Fields | cf07d2e | 2010-02-28 23:20:19 -0500 | [diff] [blame] | 571 | return max(nfsd4_lease/10, (time_t)1) * HZ; |
J. Bruce Fields | 595947a | 2009-03-05 17:18:10 -0500 | [diff] [blame] | 572 | } |
| 573 | |
J. Bruce Fields | 2b47eec | 2007-07-27 18:06:50 -0400 | [diff] [blame] | 574 | /* Reference counting, callback cleanup, etc., all look racy as heck. |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 575 | * And why is cl_cb_set an atomic? */ |
J. Bruce Fields | 2b47eec | 2007-07-27 18:06:50 -0400 | [diff] [blame] | 576 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 577 | int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn) |
J. Bruce Fields | 2b47eec | 2007-07-27 18:06:50 -0400 | [diff] [blame] | 578 | { |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 579 | struct rpc_timeout timeparms = { |
J. Bruce Fields | 595947a | 2009-03-05 17:18:10 -0500 | [diff] [blame] | 580 | .to_initval = max_cb_time(), |
| 581 | .to_retries = 0, |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 582 | }; |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 583 | struct rpc_create_args args = { |
Pavel Emelyanov | c653ce3 | 2010-09-29 16:04:45 +0400 | [diff] [blame] | 584 | .net = &init_net, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 585 | .address = (struct sockaddr *) &conn->cb_addr, |
| 586 | .addrsize = conn->cb_addrlen, |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 587 | .timeout = &timeparms, |
Olga Kornievskaia | ff7d975 | 2008-03-28 16:04:56 -0400 | [diff] [blame] | 588 | .program = &cb_program, |
J. Bruce Fields | b7299f4 | 2010-05-14 17:57:35 -0400 | [diff] [blame] | 589 | .version = 0, |
Olga Kornievskaia | 61054b1 | 2008-12-23 16:19:00 -0500 | [diff] [blame] | 590 | .authflavor = clp->cl_flavor, |
Olga Kornievskaia | b6b6152 | 2008-06-09 16:51:31 -0400 | [diff] [blame] | 591 | .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET), |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 592 | }; |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 593 | struct rpc_clnt *client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | |
J. Bruce Fields | 5d18c1c | 2010-10-19 23:00:12 -0400 | [diff] [blame] | 595 | if (clp->cl_minorversion == 0) { |
| 596 | if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5)) |
| 597 | return -EINVAL; |
| 598 | args.client_name = clp->cl_principal; |
| 599 | args.prognumber = conn->cb_prog, |
| 600 | args.protocol = XPRT_TRANSPORT_TCP; |
| 601 | clp->cl_cb_ident = conn->cb_ident; |
| 602 | } else { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 603 | args.bc_xprt = conn->cb_xprt; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 604 | args.prognumber = clp->cl_cb_session->se_cb_prog; |
Alexandros Batsakis | 3ddc8bf | 2009-09-10 12:27:21 +0300 | [diff] [blame] | 605 | args.protocol = XPRT_TRANSPORT_BC_TCP; |
| 606 | } |
Chuck Lever | ae5c794 | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 607 | /* Create RPC client */ |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 608 | client = rpc_create(&args); |
J. Bruce Fields | e1cab5a5 | 2009-02-23 10:45:27 -0800 | [diff] [blame] | 609 | if (IS_ERR(client)) { |
J. Bruce Fields | a601cae | 2009-02-22 16:43:45 -0800 | [diff] [blame] | 610 | dprintk("NFSD: couldn't create callback client: %ld\n", |
| 611 | PTR_ERR(client)); |
J. Bruce Fields | e1cab5a5 | 2009-02-23 10:45:27 -0800 | [diff] [blame] | 612 | return PTR_ERR(client); |
| 613 | } |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 614 | clp->cl_cb_client = client; |
J. Bruce Fields | e1cab5a5 | 2009-02-23 10:45:27 -0800 | [diff] [blame] | 615 | return 0; |
J. Bruce Fields | a601cae | 2009-02-22 16:43:45 -0800 | [diff] [blame] | 616 | |
| 617 | } |
| 618 | |
J. Bruce Fields | ecdd03b | 2009-02-23 19:35:22 -0800 | [diff] [blame] | 619 | static void warn_no_callback_path(struct nfs4_client *clp, int reason) |
| 620 | { |
| 621 | dprintk("NFSD: warning: no callback path to client %.*s: error %d\n", |
| 622 | (int)clp->cl_name.len, clp->cl_name.data, reason); |
| 623 | } |
| 624 | |
J. Bruce Fields | e300a63 | 2009-03-05 15:01:11 -0500 | [diff] [blame] | 625 | static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata) |
| 626 | { |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 627 | struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null); |
J. Bruce Fields | e300a63 | 2009-03-05 15:01:11 -0500 | [diff] [blame] | 628 | |
| 629 | if (task->tk_status) |
| 630 | warn_no_callback_path(clp, task->tk_status); |
| 631 | else |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 632 | atomic_set(&clp->cl_cb_set, 1); |
J. Bruce Fields | e300a63 | 2009-03-05 15:01:11 -0500 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static const struct rpc_call_ops nfsd4_cb_probe_ops = { |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 636 | /* XXX: release method to ensure we set the cb channel down if |
| 637 | * necessary on early failure? */ |
J. Bruce Fields | e300a63 | 2009-03-05 15:01:11 -0500 | [diff] [blame] | 638 | .rpc_call_done = nfsd4_cb_probe_done, |
| 639 | }; |
| 640 | |
J. Bruce Fields | 80fc015 | 2009-09-15 18:07:35 -0400 | [diff] [blame] | 641 | static struct rpc_cred *callback_cred; |
J. Bruce Fields | 3cef9ab | 2009-02-23 21:42:10 -0800 | [diff] [blame] | 642 | |
J. Bruce Fields | 80fc015 | 2009-09-15 18:07:35 -0400 | [diff] [blame] | 643 | int set_callback_cred(void) |
| 644 | { |
J. Bruce Fields | 8d75da8 | 2010-03-03 16:13:29 -0500 | [diff] [blame] | 645 | if (callback_cred) |
| 646 | return 0; |
J. Bruce Fields | 80fc015 | 2009-09-15 18:07:35 -0400 | [diff] [blame] | 647 | callback_cred = rpc_lookup_machine_cred(); |
| 648 | if (!callback_cred) |
| 649 | return -ENOMEM; |
| 650 | return 0; |
J. Bruce Fields | 3cef9ab | 2009-02-23 21:42:10 -0800 | [diff] [blame] | 651 | } |
| 652 | |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 653 | static struct workqueue_struct *callback_wq; |
J. Bruce Fields | 80fc015 | 2009-09-15 18:07:35 -0400 | [diff] [blame] | 654 | |
J. Bruce Fields | 5a3c9d7 | 2010-10-19 17:56:52 -0400 | [diff] [blame] | 655 | static void do_probe_callback(struct nfs4_client *clp) |
J. Bruce Fields | a601cae | 2009-02-22 16:43:45 -0800 | [diff] [blame] | 656 | { |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 657 | struct nfsd4_callback *cb = &clp->cl_cb_null; |
J. Bruce Fields | a601cae | 2009-02-22 16:43:45 -0800 | [diff] [blame] | 658 | |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 659 | cb->cb_op = NULL; |
| 660 | cb->cb_clp = clp; |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 661 | |
| 662 | cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL]; |
| 663 | cb->cb_msg.rpc_argp = NULL; |
| 664 | cb->cb_msg.rpc_resp = NULL; |
| 665 | cb->cb_msg.rpc_cred = callback_cred; |
| 666 | |
| 667 | cb->cb_ops = &nfsd4_cb_probe_ops; |
| 668 | |
| 669 | queue_work(callback_wq, &cb->cb_work); |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
J. Bruce Fields | 5a3c9d7 | 2010-10-19 17:56:52 -0400 | [diff] [blame] | 673 | * Poke the callback thread to process any updates to the callback |
| 674 | * parameters, and send a null probe. |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 675 | */ |
J. Bruce Fields | 5a3c9d7 | 2010-10-19 17:56:52 -0400 | [diff] [blame] | 676 | void nfsd4_probe_callback(struct nfs4_client *clp) |
| 677 | { |
| 678 | set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags); |
| 679 | do_probe_callback(clp); |
| 680 | } |
| 681 | |
| 682 | void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn) |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 683 | { |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 684 | BUG_ON(atomic_read(&clp->cl_cb_set)); |
J. Bruce Fields | 63c8671 | 2007-10-25 19:00:26 -0400 | [diff] [blame] | 685 | |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 686 | spin_lock(&clp->cl_lock); |
| 687 | memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn)); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 688 | spin_unlock(&clp->cl_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
| 690 | |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 691 | /* |
| 692 | * There's currently a single callback channel slot. |
| 693 | * If the slot is available, then mark it busy. Otherwise, set the |
| 694 | * thread for sleeping on the callback RPC wait queue. |
| 695 | */ |
| 696 | static int nfsd41_cb_setup_sequence(struct nfs4_client *clp, |
| 697 | struct rpc_task *task) |
| 698 | { |
J. Bruce Fields | 90c8145 | 2010-06-14 17:49:37 -0400 | [diff] [blame] | 699 | u32 *ptr = (u32 *)clp->cl_cb_session->se_sessionid.data; |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 700 | int status = 0; |
| 701 | |
| 702 | dprintk("%s: %u:%u:%u:%u\n", __func__, |
| 703 | ptr[0], ptr[1], ptr[2], ptr[3]); |
| 704 | |
| 705 | if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) { |
| 706 | rpc_sleep_on(&clp->cl_cb_waitq, task, NULL); |
| 707 | dprintk("%s slot is busy\n", __func__); |
| 708 | status = -EAGAIN; |
| 709 | goto out; |
| 710 | } |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 711 | out: |
| 712 | dprintk("%s status=%d\n", __func__, status); |
| 713 | return status; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * TODO: cb_sequence should support referring call lists, cachethis, multiple |
| 718 | * slots, and mark callback channel down on communication errors. |
| 719 | */ |
| 720 | static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata) |
| 721 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 722 | struct nfsd4_callback *cb = calldata; |
| 723 | struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 724 | struct nfs4_client *clp = dp->dl_client; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 725 | u32 minorversion = clp->cl_minorversion; |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 726 | int status = 0; |
| 727 | |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 728 | cb->cb_minorversion = minorversion; |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 729 | if (minorversion) { |
| 730 | status = nfsd41_cb_setup_sequence(clp, task); |
| 731 | if (status) { |
| 732 | if (status != -EAGAIN) { |
| 733 | /* terminate rpc task */ |
| 734 | task->tk_status = status; |
| 735 | task->tk_action = NULL; |
| 736 | } |
| 737 | return; |
| 738 | } |
| 739 | } |
| 740 | rpc_call_start(task); |
| 741 | } |
| 742 | |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 743 | static void nfsd4_cb_done(struct rpc_task *task, void *calldata) |
| 744 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 745 | struct nfsd4_callback *cb = calldata; |
| 746 | struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 747 | struct nfs4_client *clp = dp->dl_client; |
| 748 | |
| 749 | dprintk("%s: minorversion=%d\n", __func__, |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 750 | clp->cl_minorversion); |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 751 | |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 752 | if (clp->cl_minorversion) { |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 753 | /* No need for lock, access serialized in nfsd4_cb_prepare */ |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 754 | ++clp->cl_cb_session->se_cb_seq_nr; |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 755 | clear_bit(0, &clp->cl_cb_slot_busy); |
| 756 | rpc_wake_up_next(&clp->cl_cb_waitq); |
| 757 | dprintk("%s: freed slot, new seqid=%d\n", __func__, |
J. Bruce Fields | ac7c46f | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 758 | clp->cl_cb_session->se_cb_seq_nr); |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 759 | |
| 760 | /* We're done looking into the sequence information */ |
| 761 | task->tk_msg.rpc_resp = NULL; |
| 762 | } |
| 763 | } |
| 764 | |
J. Bruce Fields | 4b21d0d | 2010-03-07 23:39:01 -0500 | [diff] [blame] | 765 | |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 766 | static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata) |
| 767 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 768 | struct nfsd4_callback *cb = calldata; |
| 769 | struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 770 | struct nfs4_client *clp = dp->dl_client; |
J. Bruce Fields | 4b21d0d | 2010-03-07 23:39:01 -0500 | [diff] [blame] | 771 | struct rpc_clnt *current_rpc_client = clp->cl_cb_client; |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 772 | |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 773 | nfsd4_cb_done(task, calldata); |
| 774 | |
J. Bruce Fields | 4b21d0d | 2010-03-07 23:39:01 -0500 | [diff] [blame] | 775 | if (current_rpc_client == NULL) { |
| 776 | /* We're shutting down; give up. */ |
| 777 | /* XXX: err, or is it ok just to fall through |
| 778 | * and rpc_restart_call? */ |
| 779 | return; |
| 780 | } |
| 781 | |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 782 | switch (task->tk_status) { |
J. Bruce Fields | 172c85d | 2010-05-30 11:53:12 -0400 | [diff] [blame] | 783 | case 0: |
| 784 | return; |
| 785 | case -EBADHANDLE: |
| 786 | case -NFS4ERR_BAD_STATEID: |
| 787 | /* Race: client probably got cb_recall |
| 788 | * before open reply granting delegation */ |
| 789 | break; |
| 790 | default: |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 791 | /* Network partition? */ |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 792 | atomic_set(&clp->cl_cb_set, 0); |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 793 | warn_no_callback_path(clp, task->tk_status); |
J. Bruce Fields | 4b21d0d | 2010-03-07 23:39:01 -0500 | [diff] [blame] | 794 | if (current_rpc_client != task->tk_client) { |
| 795 | /* queue a callback on the new connection: */ |
J. Bruce Fields | cba9ba4 | 2010-06-01 11:21:40 -0400 | [diff] [blame] | 796 | atomic_inc(&dp->dl_count); |
J. Bruce Fields | 4b21d0d | 2010-03-07 23:39:01 -0500 | [diff] [blame] | 797 | nfsd4_cb_recall(dp); |
| 798 | return; |
| 799 | } |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 800 | } |
| 801 | if (dp->dl_retries--) { |
| 802 | rpc_delay(task, 2*HZ); |
| 803 | task->tk_status = 0; |
Boaz Harrosh | c18c821 | 2010-06-29 14:33:55 +0300 | [diff] [blame] | 804 | rpc_restart_call_prepare(task); |
Ricardo Labiaga | 0421b5c | 2009-09-10 12:27:04 +0300 | [diff] [blame] | 805 | return; |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 806 | } else { |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 807 | atomic_set(&clp->cl_cb_set, 0); |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 808 | warn_no_callback_path(clp, task->tk_status); |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | static void nfsd4_cb_recall_release(void *calldata) |
| 813 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 814 | struct nfsd4_callback *cb = calldata; |
| 815 | struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall); |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 816 | |
| 817 | nfs4_put_delegation(dp); |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | static const struct rpc_call_ops nfsd4_cb_recall_ops = { |
Ricardo Labiaga | 2a1d1b5 | 2009-09-10 12:26:38 +0300 | [diff] [blame] | 821 | .rpc_call_prepare = nfsd4_cb_prepare, |
J. Bruce Fields | 63e4863 | 2009-05-01 22:36:55 -0400 | [diff] [blame] | 822 | .rpc_call_done = nfsd4_cb_recall_done, |
| 823 | .rpc_release = nfsd4_cb_recall_release, |
| 824 | }; |
| 825 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 826 | int nfsd4_create_callback_queue(void) |
| 827 | { |
| 828 | callback_wq = create_singlethread_workqueue("nfsd4_callbacks"); |
| 829 | if (!callback_wq) |
| 830 | return -ENOMEM; |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | void nfsd4_destroy_callback_queue(void) |
| 835 | { |
| 836 | destroy_workqueue(callback_wq); |
| 837 | } |
| 838 | |
Benny Halevy | ab707e15 | 2010-05-12 00:14:06 +0300 | [diff] [blame] | 839 | /* must be called under the state lock */ |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 840 | void nfsd4_shutdown_callback(struct nfs4_client *clp) |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 841 | { |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 842 | set_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 843 | /* |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 844 | * Note this won't actually result in a null callback; |
| 845 | * instead, nfsd4_do_callback_rpc() will detect the killed |
| 846 | * client, destroy the rpc client, and stop: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 847 | */ |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 848 | do_probe_callback(clp); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 849 | flush_workqueue(callback_wq); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 850 | } |
| 851 | |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 852 | void nfsd4_release_cb(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 854 | if (cb->cb_ops->rpc_release) |
| 855 | cb->cb_ops->rpc_release(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 857 | |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 858 | void nfsd4_process_cb_update(struct nfsd4_callback *cb) |
| 859 | { |
| 860 | struct nfs4_cb_conn conn; |
| 861 | struct nfs4_client *clp = cb->cb_clp; |
| 862 | int err; |
| 863 | |
| 864 | /* |
| 865 | * This is either an update, or the client dying; in either case, |
| 866 | * kill the old client: |
| 867 | */ |
| 868 | if (clp->cl_cb_client) { |
| 869 | rpc_shutdown_client(clp->cl_cb_client); |
| 870 | clp->cl_cb_client = NULL; |
| 871 | } |
| 872 | if (test_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags)) |
| 873 | return; |
| 874 | spin_lock(&clp->cl_lock); |
| 875 | /* |
| 876 | * Only serialized callback code is allowed to clear these |
| 877 | * flags; main nfsd code can only set them: |
| 878 | */ |
| 879 | BUG_ON(!clp->cl_cb_flags); |
| 880 | clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags); |
| 881 | memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn)); |
| 882 | spin_unlock(&clp->cl_lock); |
| 883 | |
| 884 | err = setup_callback_client(clp, &conn); |
| 885 | if (err) |
| 886 | warn_no_callback_path(clp, err); |
| 887 | } |
| 888 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 889 | void nfsd4_do_callback_rpc(struct work_struct *w) |
| 890 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 891 | struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work); |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 892 | struct nfs4_client *clp = cb->cb_clp; |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 893 | struct rpc_clnt *clnt; |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 894 | |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 895 | if (clp->cl_cb_flags) |
| 896 | nfsd4_process_cb_update(cb); |
| 897 | |
| 898 | clnt = clp->cl_cb_client; |
| 899 | if (!clnt) { |
| 900 | /* Callback channel broken, or client killed; give up: */ |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 901 | nfsd4_release_cb(cb); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 902 | return; |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 903 | } |
J. Bruce Fields | cee277d | 2010-05-26 17:52:14 -0400 | [diff] [blame] | 904 | rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN, |
| 905 | cb->cb_ops, cb); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 906 | } |
| 907 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 908 | void nfsd4_cb_recall(struct nfs4_delegation *dp) |
| 909 | { |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 910 | struct nfsd4_callback *cb = &dp->dl_recall; |
| 911 | |
| 912 | dp->dl_retries = 1; |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 913 | cb->cb_op = dp; |
| 914 | cb->cb_clp = dp->dl_client; |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 915 | cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL]; |
J. Bruce Fields | fb00392 | 2010-05-31 18:21:37 -0400 | [diff] [blame] | 916 | cb->cb_msg.rpc_argp = cb; |
| 917 | cb->cb_msg.rpc_resp = cb; |
J. Bruce Fields | 5878453 | 2010-05-16 16:47:08 -0400 | [diff] [blame] | 918 | cb->cb_msg.rpc_cred = callback_cred; |
| 919 | |
| 920 | cb->cb_ops = &nfsd4_cb_recall_ops; |
| 921 | dp->dl_retries = 1; |
| 922 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 923 | queue_work(callback_wq, &dp->dl_recall.cb_work); |
| 924 | } |