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