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