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