NFSD: Implement SEEK
[deliverable/linux.git] / fs / nfsd / nfs4callback.c
CommitLineData
1da177e4 1/*
1da177e4
LT
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
1da177e4 34#include <linux/sunrpc/clnt.h>
d531c008 35#include <linux/sunrpc/xprt.h>
b5a1a81e 36#include <linux/sunrpc/svc_xprt.h>
5a0e3ad6 37#include <linux/slab.h>
9a74af21
BH
38#include "nfsd.h"
39#include "state.h"
3d733711 40#include "netns.h"
06b332a5 41#include "xdr4cb.h"
1da177e4
LT
42
43#define NFSDDBG_FACILITY NFSDDBG_PROC
44
5423732a
BH
45static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
46
1da177e4
LT
47#define NFSPROC4_CB_NULL 0
48#define NFSPROC4_CB_COMPOUND 1
49
1da177e4
LT
50/* Index of predefined Linux callback client operations */
51
2faf3b43
CH
52#define to_delegation(cb) \
53 container_of(cb, struct nfs4_delegation, dl_recall)
54
1da177e4 55struct nfs4_cb_compound_hdr {
38524ab3
AA
56 /* args */
57 u32 ident; /* minorversion 0 only */
1da177e4 58 u32 nops;
ef52bff8 59 __be32 *nops_p;
ab52ae6d 60 u32 minorversion;
38524ab3
AA
61 /* res */
62 int status;
1da177e4
LT
63};
64
85a56480
CL
65/*
66 * Handle decode buffer overflows out-of-line.
67 */
68static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
1da177e4 69{
85a56480
CL
70 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
71 "Remaining buffer length is %tu words.\n",
72 func, xdr->end - xdr->p);
1da177e4
LT
73}
74
a033db48
CL
75static __be32 *xdr_encode_empty_array(__be32 *p)
76{
77 *p++ = xdr_zero;
78 return p;
79}
80
81/*
82 * Encode/decode NFSv4 CB basic data types
83 *
84 * Basic NFSv4 callback data types are defined in section 15 of RFC
85 * 3530: "Network File System (NFS) version 4 Protocol" and section
86 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
87 * 1 Protocol"
88 */
89
90/*
91 * nfs_cb_opnum4
92 *
93 * enum nfs_cb_opnum4 {
94 * OP_CB_GETATTR = 3,
95 * ...
96 * };
97 */
98enum nfs_cb_opnum4 {
99 OP_CB_GETATTR = 3,
100 OP_CB_RECALL = 4,
101 OP_CB_LAYOUTRECALL = 5,
102 OP_CB_NOTIFY = 6,
103 OP_CB_PUSH_DELEG = 7,
104 OP_CB_RECALL_ANY = 8,
105 OP_CB_RECALLABLE_OBJ_AVAIL = 9,
106 OP_CB_RECALL_SLOT = 10,
107 OP_CB_SEQUENCE = 11,
108 OP_CB_WANTS_CANCELLED = 12,
109 OP_CB_NOTIFY_LOCK = 13,
110 OP_CB_NOTIFY_DEVICEID = 14,
111 OP_CB_ILLEGAL = 10044
112};
113
114static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
115{
116 __be32 *p;
117
118 p = xdr_reserve_space(xdr, 4);
119 *p = cpu_to_be32(op);
120}
121
122/*
123 * nfs_fh4
124 *
125 * typedef opaque nfs_fh4<NFS4_FHSIZE>;
126 */
127static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
128{
129 u32 length = fh->fh_size;
130 __be32 *p;
131
132 BUG_ON(length > NFS4_FHSIZE);
133 p = xdr_reserve_space(xdr, 4 + length);
134 xdr_encode_opaque(p, &fh->fh_base, length);
135}
136
1da177e4 137/*
a033db48
CL
138 * stateid4
139 *
140 * struct stateid4 {
141 * uint32_t seqid;
142 * opaque other[12];
143 * };
1da177e4 144 */
a033db48
CL
145static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
146{
147 __be32 *p;
1da177e4 148
a033db48
CL
149 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
150 *p++ = cpu_to_be32(sid->si_generation);
151 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
152}
153
154/*
155 * sessionid4
156 *
157 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
158 */
159static void encode_sessionid4(struct xdr_stream *xdr,
160 const struct nfsd4_session *session)
9303bbd3
BH
161{
162 __be32 *p;
163
a033db48
CL
164 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
165 xdr_encode_opaque_fixed(p, session->se_sessionid.data,
166 NFS4_MAX_SESSIONID_LEN);
9303bbd3
BH
167}
168
85a56480
CL
169/*
170 * nfsstat4
171 */
172static const struct {
173 int stat;
174 int errno;
175} nfs_cb_errtbl[] = {
176 { NFS4_OK, 0 },
177 { NFS4ERR_PERM, -EPERM },
178 { NFS4ERR_NOENT, -ENOENT },
179 { NFS4ERR_IO, -EIO },
180 { NFS4ERR_NXIO, -ENXIO },
181 { NFS4ERR_ACCESS, -EACCES },
182 { NFS4ERR_EXIST, -EEXIST },
183 { NFS4ERR_XDEV, -EXDEV },
184 { NFS4ERR_NOTDIR, -ENOTDIR },
185 { NFS4ERR_ISDIR, -EISDIR },
186 { NFS4ERR_INVAL, -EINVAL },
187 { NFS4ERR_FBIG, -EFBIG },
188 { NFS4ERR_NOSPC, -ENOSPC },
189 { NFS4ERR_ROFS, -EROFS },
190 { NFS4ERR_MLINK, -EMLINK },
191 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
192 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
193 { NFS4ERR_DQUOT, -EDQUOT },
194 { NFS4ERR_STALE, -ESTALE },
195 { NFS4ERR_BADHANDLE, -EBADHANDLE },
196 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
197 { NFS4ERR_NOTSUPP, -ENOTSUPP },
198 { NFS4ERR_TOOSMALL, -ETOOSMALL },
199 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
200 { NFS4ERR_BADTYPE, -EBADTYPE },
201 { NFS4ERR_LOCKED, -EAGAIN },
202 { NFS4ERR_RESOURCE, -EREMOTEIO },
203 { NFS4ERR_SYMLINK, -ELOOP },
204 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
205 { NFS4ERR_DEADLOCK, -EDEADLK },
206 { -1, -EIO }
207};
208
209/*
210 * If we cannot translate the error, the recovery routines should
211 * handle it.
212 *
213 * Note: remaining NFSv4 error codes have values > 10000, so should
214 * not conflict with native Linux error codes.
215 */
216static int nfs_cb_stat_to_errno(int status)
217{
218 int i;
219
220 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
221 if (nfs_cb_errtbl[i].stat == status)
222 return nfs_cb_errtbl[i].errno;
223 }
224
225 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
226 return -status;
227}
228
229static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
230 enum nfsstat4 *status)
231{
232 __be32 *p;
233 u32 op;
234
235 p = xdr_inline_decode(xdr, 4 + 4);
236 if (unlikely(p == NULL))
237 goto out_overflow;
238 op = be32_to_cpup(p++);
239 if (unlikely(op != expected))
240 goto out_unexpected;
241 *status = be32_to_cpup(p);
242 return 0;
243out_overflow:
244 print_overflow_msg(__func__, xdr);
245 return -EIO;
246out_unexpected:
247 dprintk("NFSD: Callback server returned operation %d but "
248 "we issued a request for %d\n", op, expected);
249 return -EIO;
250}
251
a033db48
CL
252/*
253 * CB_COMPOUND4args
254 *
255 * struct CB_COMPOUND4args {
256 * utf8str_cs tag;
257 * uint32_t minorversion;
258 * uint32_t callback_ident;
259 * nfs_cb_argop4 argarray<>;
260 * };
261*/
262static void encode_cb_compound4args(struct xdr_stream *xdr,
263 struct nfs4_cb_compound_hdr *hdr)
1da177e4 264{
f00f328f 265 __be32 * p;
1da177e4 266
a033db48
CL
267 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
268 p = xdr_encode_empty_array(p); /* empty tag */
269 *p++ = cpu_to_be32(hdr->minorversion);
270 *p++ = cpu_to_be32(hdr->ident);
271
ef52bff8 272 hdr->nops_p = p;
a033db48 273 *p = cpu_to_be32(hdr->nops); /* argarray element count */
1da177e4
LT
274}
275
a033db48
CL
276/*
277 * Update argarray element count
278 */
ef52bff8
AA
279static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
280{
a033db48
CL
281 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
282 *hdr->nops_p = cpu_to_be32(hdr->nops);
ef52bff8
AA
283}
284
85a56480
CL
285/*
286 * CB_COMPOUND4res
287 *
288 * struct CB_COMPOUND4res {
289 * nfsstat4 status;
290 * utf8str_cs tag;
291 * nfs_cb_resop4 resarray<>;
292 * };
293 */
294static int decode_cb_compound4res(struct xdr_stream *xdr,
295 struct nfs4_cb_compound_hdr *hdr)
296{
297 u32 length;
298 __be32 *p;
299
300 p = xdr_inline_decode(xdr, 4 + 4);
301 if (unlikely(p == NULL))
302 goto out_overflow;
303 hdr->status = be32_to_cpup(p++);
304 /* Ignore the tag */
305 length = be32_to_cpup(p++);
306 p = xdr_inline_decode(xdr, length + 4);
307 if (unlikely(p == NULL))
308 goto out_overflow;
309 hdr->nops = be32_to_cpup(p);
310 return 0;
311out_overflow:
312 print_overflow_msg(__func__, xdr);
313 return -EIO;
314}
315
a033db48
CL
316/*
317 * CB_RECALL4args
318 *
319 * struct CB_RECALL4args {
320 * stateid4 stateid;
321 * bool truncate;
322 * nfs_fh4 fh;
323 * };
324 */
325static void encode_cb_recall4args(struct xdr_stream *xdr,
326 const struct nfs4_delegation *dp,
327 struct nfs4_cb_compound_hdr *hdr)
1da177e4 328{
f00f328f 329 __be32 *p;
a033db48
CL
330
331 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
d5477a8d 332 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
a033db48
CL
333
334 p = xdr_reserve_space(xdr, 4);
335 *p++ = xdr_zero; /* truncate */
336
11b9164a 337 encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
a033db48 338
ef52bff8 339 hdr->nops++;
1da177e4
LT
340}
341
a033db48
CL
342/*
343 * CB_SEQUENCE4args
344 *
345 * struct CB_SEQUENCE4args {
346 * sessionid4 csa_sessionid;
347 * sequenceid4 csa_sequenceid;
348 * slotid4 csa_slotid;
349 * slotid4 csa_highest_slotid;
350 * bool csa_cachethis;
351 * referring_call_list4 csa_referring_call_lists<>;
352 * };
353 */
354static void encode_cb_sequence4args(struct xdr_stream *xdr,
355 const struct nfsd4_callback *cb,
356 struct nfs4_cb_compound_hdr *hdr)
2af73580 357{
a033db48 358 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
2af73580
BH
359 __be32 *p;
360
361 if (hdr->minorversion == 0)
362 return;
363
a033db48
CL
364 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
365 encode_sessionid4(xdr, session);
366
367 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
368 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
369 *p++ = xdr_zero; /* csa_slotid */
370 *p++ = xdr_zero; /* csa_highest_slotid */
371 *p++ = xdr_zero; /* csa_cachethis */
372 xdr_encode_empty_array(p); /* csa_referring_call_lists */
2af73580 373
2af73580
BH
374 hdr->nops++;
375}
376
85a56480
CL
377/*
378 * CB_SEQUENCE4resok
379 *
380 * struct CB_SEQUENCE4resok {
381 * sessionid4 csr_sessionid;
382 * sequenceid4 csr_sequenceid;
383 * slotid4 csr_slotid;
384 * slotid4 csr_highest_slotid;
385 * slotid4 csr_target_highest_slotid;
386 * };
387 *
388 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
389 * case NFS4_OK:
390 * CB_SEQUENCE4resok csr_resok4;
391 * default:
392 * void;
393 * };
394 *
395 * Our current back channel implmentation supports a single backchannel
396 * with a single slot.
397 */
398static int decode_cb_sequence4resok(struct xdr_stream *xdr,
399 struct nfsd4_callback *cb)
400{
401 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
402 struct nfs4_sessionid id;
403 int status;
404 __be32 *p;
405 u32 dummy;
406
407 status = -ESERVERFAULT;
408
409 /*
410 * If the server returns different values for sessionID, slotID or
411 * sequence number, the server is looney tunes.
412 */
2c9c8f36 413 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
85a56480
CL
414 if (unlikely(p == NULL))
415 goto out_overflow;
416 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
417 if (memcmp(id.data, session->se_sessionid.data,
418 NFS4_MAX_SESSIONID_LEN) != 0) {
419 dprintk("NFS: %s Invalid session id\n", __func__);
420 goto out;
421 }
422 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
423
424 dummy = be32_to_cpup(p++);
425 if (dummy != session->se_cb_seq_nr) {
426 dprintk("NFS: %s Invalid sequence number\n", __func__);
427 goto out;
428 }
429
430 dummy = be32_to_cpup(p++);
431 if (dummy != 0) {
432 dprintk("NFS: %s Invalid slotid\n", __func__);
433 goto out;
434 }
435
436 /*
437 * FIXME: process highest slotid and target highest slotid
438 */
439 status = 0;
440out:
5423732a
BH
441 if (status)
442 nfsd4_mark_cb_fault(cb->cb_clp, status);
85a56480
CL
443 return status;
444out_overflow:
445 print_overflow_msg(__func__, xdr);
446 return -EIO;
447}
448
449static int decode_cb_sequence4res(struct xdr_stream *xdr,
450 struct nfsd4_callback *cb)
451{
452 enum nfsstat4 nfserr;
453 int status;
454
455 if (cb->cb_minorversion == 0)
456 return 0;
457
458 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
459 if (unlikely(status))
460 goto out;
461 if (unlikely(nfserr != NFS4_OK))
462 goto out_default;
463 status = decode_cb_sequence4resok(xdr, cb);
464out:
465 return status;
466out_default:
0af3f814 467 return nfs_cb_stat_to_errno(nfserr);
85a56480
CL
468}
469
a033db48
CL
470/*
471 * NFSv4.0 and NFSv4.1 XDR encode functions
472 *
473 * NFSv4.0 callback argument types are defined in section 15 of RFC
474 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
475 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
476 * Protocol".
477 */
478
479/*
480 * NB: Without this zero space reservation, callbacks over krb5p fail
481 */
9f06c719
CL
482static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
483 void *__unused)
1da177e4 484{
a033db48 485 xdr_reserve_space(xdr, 0);
1da177e4
LT
486}
487
a033db48
CL
488/*
489 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
490 */
9f06c719
CL
491static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
492 const struct nfsd4_callback *cb)
1da177e4 493{
2faf3b43 494 const struct nfs4_delegation *dp = to_delegation(cb);
1da177e4 495 struct nfs4_cb_compound_hdr hdr = {
6ff8da08 496 .ident = cb->cb_clp->cl_cb_ident,
fb003923 497 .minorversion = cb->cb_minorversion,
1da177e4
LT
498 };
499
9f06c719
CL
500 encode_cb_compound4args(xdr, &hdr);
501 encode_cb_sequence4args(xdr, cb, &hdr);
2faf3b43 502 encode_cb_recall4args(xdr, dp, &hdr);
ef52bff8 503 encode_cb_nops(&hdr);
1da177e4
LT
504}
505
506
2af73580 507/*
85a56480
CL
508 * NFSv4.0 and NFSv4.1 XDR decode functions
509 *
510 * NFSv4.0 callback result types are defined in section 15 of RFC
511 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
512 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
513 * Protocol".
2af73580 514 */
2af73580 515
bf269551
CL
516static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
517 void *__unused)
1da177e4
LT
518{
519 return 0;
520}
521
85a56480
CL
522/*
523 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
524 */
bf269551
CL
525static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
526 struct xdr_stream *xdr,
85a56480 527 struct nfsd4_callback *cb)
1da177e4 528{
1da177e4 529 struct nfs4_cb_compound_hdr hdr;
85a56480 530 enum nfsstat4 nfserr;
1da177e4
LT
531 int status;
532
bf269551 533 status = decode_cb_compound4res(xdr, &hdr);
85a56480 534 if (unlikely(status))
1da177e4 535 goto out;
85a56480
CL
536
537 if (cb != NULL) {
bf269551 538 status = decode_cb_sequence4res(xdr, cb);
85a56480 539 if (unlikely(status))
0421b5c5
RL
540 goto out;
541 }
85a56480 542
bf269551 543 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
85a56480
CL
544 if (unlikely(status))
545 goto out;
546 if (unlikely(nfserr != NFS4_OK))
0af3f814 547 status = nfs_cb_stat_to_errno(nfserr);
1da177e4
LT
548out:
549 return status;
550}
551
552/*
553 * RPC procedure tables
554 */
7d93bd71
CL
555#define PROC(proc, call, argtype, restype) \
556[NFSPROC4_CLNT_##proc] = { \
557 .p_proc = NFSPROC4_CB_##call, \
9f06c719 558 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
bf269551 559 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
7d93bd71
CL
560 .p_arglen = NFS4_enc_##argtype##_sz, \
561 .p_replen = NFS4_dec_##restype##_sz, \
562 .p_statidx = NFSPROC4_CB_##call, \
563 .p_name = #proc, \
1da177e4
LT
564}
565
7d93bd71
CL
566static struct rpc_procinfo nfs4_cb_procedures[] = {
567 PROC(CB_NULL, NULL, cb_null, cb_null),
568 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
1da177e4
LT
569};
570
7d93bd71 571static struct rpc_version nfs_cb_version4 = {
b7299f44
BF
572/*
573 * Note on the callback rpc program version number: despite language in rfc
574 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
575 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
576 * in practice that appears to be what implementations use. The section
577 * 18.36.3 language is expected to be fixed in an erratum.
578 */
7d93bd71
CL
579 .number = 1,
580 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
581 .procs = nfs4_cb_procedures
1da177e4
LT
582};
583
a613fa16 584static const struct rpc_version *nfs_cb_version[] = {
1da177e4
LT
585 &nfs_cb_version4,
586};
587
a613fa16 588static const struct rpc_program cb_program;
ff7d9756
OK
589
590static struct rpc_stat cb_stats = {
7d93bd71 591 .program = &cb_program
ff7d9756
OK
592};
593
594#define NFS4_CALLBACK 0x40000000
a613fa16 595static const struct rpc_program cb_program = {
7d93bd71
CL
596 .name = "nfs4_cb",
597 .number = NFS4_CALLBACK,
598 .nrvers = ARRAY_SIZE(nfs_cb_version),
599 .version = nfs_cb_version,
600 .stats = &cb_stats,
0157d021 601 .pipe_dir_name = "nfsd4_cb",
ff7d9756
OK
602};
603
3d733711 604static int max_cb_time(struct net *net)
595947ac 605{
3d733711
SK
606 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
607 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
595947ac
BF
608}
609
c6bb3ca2
BF
610static struct rpc_cred *callback_cred;
611
612int set_callback_cred(void)
613{
614 if (callback_cred)
615 return 0;
616 callback_cred = rpc_lookup_machine_cred("nfs");
617 if (!callback_cred)
618 return -ENOMEM;
619 return 0;
620}
621
2b4cf668 622static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
c6bb3ca2
BF
623{
624 if (clp->cl_minorversion == 0) {
625 return get_rpccred(callback_cred);
626 } else {
627 struct rpc_auth *auth = client->cl_auth;
628 struct auth_cred acred = {};
629
630 acred.uid = ses->se_cb_sec.uid;
631 acred.gid = ses->se_cb_sec.gid;
632 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
633 }
634}
2b47eece 635
d531c008
KM
636static struct rpc_clnt *create_backchannel_client(struct rpc_create_args *args)
637{
638 struct rpc_xprt *xprt;
639
640 if (args->protocol != XPRT_TRANSPORT_BC_TCP)
641 return rpc_create(args);
642
643 xprt = args->bc_xprt->xpt_bc_xprt;
644 if (xprt) {
645 xprt_get(xprt);
646 return rpc_create_xprt(args, xprt);
647 }
648
649 return rpc_create(args);
650}
651
dcbeaa68 652static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
2b47eece 653{
3758cf7e 654 int maxtime = max_cb_time(clp->net);
ae5c7947 655 struct rpc_timeout timeparms = {
3758cf7e 656 .to_initval = maxtime,
595947ac 657 .to_retries = 0,
3758cf7e 658 .to_maxval = maxtime,
ae5c7947 659 };
ae5c7947 660 struct rpc_create_args args = {
c212cecf 661 .net = clp->net,
07263f1e
BF
662 .address = (struct sockaddr *) &conn->cb_addr,
663 .addrsize = conn->cb_addrlen,
6f3d772f 664 .saddress = (struct sockaddr *) &conn->cb_saddr,
ae5c7947 665 .timeout = &timeparms,
ff7d9756 666 .program = &cb_program,
b7299f44 667 .version = 0,
b6b6152c 668 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
ae5c7947 669 };
63c86716 670 struct rpc_clnt *client;
c6bb3ca2 671 struct rpc_cred *cred;
1da177e4 672
5d18c1c2 673 if (clp->cl_minorversion == 0) {
03a4e1f6 674 if (!clp->cl_cred.cr_principal &&
39307655 675 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
5d18c1c2 676 return -EINVAL;
03a4e1f6 677 args.client_name = clp->cl_cred.cr_principal;
fc8e5a64 678 args.prognumber = conn->cb_prog;
5d18c1c2 679 args.protocol = XPRT_TRANSPORT_TCP;
39307655 680 args.authflavor = clp->cl_cred.cr_flavor;
5d18c1c2
BF
681 clp->cl_cb_ident = conn->cb_ident;
682 } else {
dcbeaa68
BF
683 if (!conn->cb_xprt)
684 return -EINVAL;
685 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
686 clp->cl_cb_session = ses;
07263f1e 687 args.bc_xprt = conn->cb_xprt;
8b5ce5cd 688 args.prognumber = clp->cl_cb_session->se_cb_prog;
3c45ddf8
CL
689 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
690 XPRT_TRANSPORT_BC;
12fc3e92 691 args.authflavor = ses->se_cb_sec.flavor;
3ddc8bf5 692 }
ae5c7947 693 /* Create RPC client */
d531c008 694 client = create_backchannel_client(&args);
e1cab5a5 695 if (IS_ERR(client)) {
a601caed
BF
696 dprintk("NFSD: couldn't create callback client: %ld\n",
697 PTR_ERR(client));
e1cab5a5
BF
698 return PTR_ERR(client);
699 }
c6bb3ca2
BF
700 cred = get_backchannel_cred(clp, client, ses);
701 if (IS_ERR(cred)) {
702 rpc_shutdown_client(client);
703 return PTR_ERR(cred);
704 }
6ff8da08 705 clp->cl_cb_client = client;
c6bb3ca2 706 clp->cl_cb_cred = cred;
e1cab5a5 707 return 0;
a601caed
BF
708}
709
ecdd03b7
BF
710static void warn_no_callback_path(struct nfs4_client *clp, int reason)
711{
712 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
713 (int)clp->cl_name.len, clp->cl_name.data, reason);
714}
715
77a3569d
BF
716static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
717{
718 clp->cl_cb_state = NFSD4_CB_DOWN;
719 warn_no_callback_path(clp, reason);
720}
721
5423732a
BH
722static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
723{
724 clp->cl_cb_state = NFSD4_CB_FAULT;
725 warn_no_callback_path(clp, reason);
726}
727
e300a63c
BF
728static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
729{
cee277d9 730 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
e300a63c
BF
731
732 if (task->tk_status)
77a3569d 733 nfsd4_mark_cb_down(clp, task->tk_status);
e300a63c 734 else
77a3569d 735 clp->cl_cb_state = NFSD4_CB_UP;
e300a63c
BF
736}
737
738static const struct rpc_call_ops nfsd4_cb_probe_ops = {
cee277d9
BF
739 /* XXX: release method to ensure we set the cb channel down if
740 * necessary on early failure? */
e300a63c
BF
741 .rpc_call_done = nfsd4_cb_probe_done,
742};
743
cee277d9 744static struct workqueue_struct *callback_wq;
80fc015b 745
63c86716 746/*
5a3c9d71
BF
747 * Poke the callback thread to process any updates to the callback
748 * parameters, and send a null probe.
63c86716 749 */
5a3c9d71
BF
750void nfsd4_probe_callback(struct nfs4_client *clp)
751{
77a3569d 752 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
a52d726b 753 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
f0b5de1b 754 nfsd4_run_cb(&clp->cl_cb_null);
5a3c9d71
BF
755}
756
84f5f7cc 757void nfsd4_probe_callback_sync(struct nfs4_client *clp)
63c86716 758{
84f5f7cc
BF
759 nfsd4_probe_callback(clp);
760 flush_workqueue(callback_wq);
761}
63c86716 762
5a3c9d71 763void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
63c86716 764{
77a3569d 765 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
6ff8da08
BF
766 spin_lock(&clp->cl_lock);
767 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
6ff8da08 768 spin_unlock(&clp->cl_lock);
1da177e4
LT
769}
770
2a1d1b59
RL
771/*
772 * There's currently a single callback channel slot.
773 * If the slot is available, then mark it busy. Otherwise, set the
774 * thread for sleeping on the callback RPC wait queue.
775 */
3ff3600e 776static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
2a1d1b59 777{
2a1d1b59
RL
778 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
779 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
780 dprintk("%s slot is busy\n", __func__);
3ff3600e 781 return false;
2a1d1b59 782 }
3ff3600e 783 return true;
2a1d1b59
RL
784}
785
786/*
787 * TODO: cb_sequence should support referring call lists, cachethis, multiple
788 * slots, and mark callback channel down on communication errors.
789 */
790static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
791{
5878453d 792 struct nfsd4_callback *cb = calldata;
8be2d234 793 struct nfs4_client *clp = cb->cb_clp;
8323c3b2 794 u32 minorversion = clp->cl_minorversion;
2a1d1b59 795
fb003923 796 cb->cb_minorversion = minorversion;
2a1d1b59 797 if (minorversion) {
3ff3600e 798 if (!nfsd41_cb_get_slot(clp, task))
2a1d1b59 799 return;
2a1d1b59 800 }
5ce8ba25 801 spin_lock(&clp->cl_lock);
a8f2800b
BF
802 if (list_empty(&cb->cb_per_client)) {
803 /* This is the first call, not a restart */
804 cb->cb_done = false;
805 list_add(&cb->cb_per_client, &clp->cl_callbacks);
806 }
5ce8ba25 807 spin_unlock(&clp->cl_lock);
2a1d1b59
RL
808 rpc_call_start(task);
809}
810
0421b5c5
RL
811static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
812{
5878453d 813 struct nfsd4_callback *cb = calldata;
8be2d234 814 struct nfs4_client *clp = cb->cb_clp;
0421b5c5
RL
815
816 dprintk("%s: minorversion=%d\n", __func__,
8323c3b2 817 clp->cl_minorversion);
0421b5c5 818
8323c3b2 819 if (clp->cl_minorversion) {
0421b5c5 820 /* No need for lock, access serialized in nfsd4_cb_prepare */
ac7c46f2 821 ++clp->cl_cb_session->se_cb_seq_nr;
0421b5c5
RL
822 clear_bit(0, &clp->cl_cb_slot_busy);
823 rpc_wake_up_next(&clp->cl_cb_waitq);
824 dprintk("%s: freed slot, new seqid=%d\n", __func__,
ac7c46f2 825 clp->cl_cb_session->se_cb_seq_nr);
0421b5c5 826 }
0421b5c5 827
0162ac2b 828 if (clp->cl_cb_client != task->tk_client) {
a8f2800b
BF
829 /* We're shutting down or changing cl_cb_client; leave
830 * it to nfsd4_process_cb_update to restart the call if
831 * necessary. */
4b21d0de
BF
832 return;
833 }
834
5ce8ba25
BF
835 if (cb->cb_done)
836 return;
0162ac2b
CH
837
838 switch (cb->cb_ops->done(cb, task)) {
172c85dd 839 case 0:
0162ac2b
CH
840 task->tk_status = 0;
841 rpc_restart_call_prepare(task);
842 return;
843 case 1:
444b6e91 844 break;
0162ac2b 845 case -1:
63e4863f 846 /* Network partition? */
77a3569d 847 nfsd4_mark_cb_down(clp, task->tk_status);
0162ac2b
CH
848 break;
849 default:
850 BUG();
63e4863f 851 }
5ce8ba25 852 cb->cb_done = true;
63e4863f
BF
853}
854
0162ac2b 855static void nfsd4_cb_release(void *calldata)
63e4863f 856{
5878453d 857 struct nfsd4_callback *cb = calldata;
5ce8ba25 858 struct nfs4_client *clp = cb->cb_clp;
63e4863f 859
5ce8ba25
BF
860 if (cb->cb_done) {
861 spin_lock(&clp->cl_lock);
862 list_del(&cb->cb_per_client);
863 spin_unlock(&clp->cl_lock);
0162ac2b
CH
864
865 cb->cb_ops->release(cb);
5ce8ba25 866 }
63e4863f
BF
867}
868
0162ac2b 869static const struct rpc_call_ops nfsd4_cb_ops = {
2a1d1b59 870 .rpc_call_prepare = nfsd4_cb_prepare,
0162ac2b
CH
871 .rpc_call_done = nfsd4_cb_done,
872 .rpc_release = nfsd4_cb_release,
63e4863f
BF
873};
874
b5a1a81e
BF
875int nfsd4_create_callback_queue(void)
876{
877 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
878 if (!callback_wq)
879 return -ENOMEM;
880 return 0;
881}
882
883void nfsd4_destroy_callback_queue(void)
884{
885 destroy_workqueue(callback_wq);
886}
887
ab707e15 888/* must be called under the state lock */
6ff8da08 889void nfsd4_shutdown_callback(struct nfs4_client *clp)
b5a1a81e 890{
a52d726b 891 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
b5a1a81e 892 /*
6ff8da08 893 * Note this won't actually result in a null callback;
02e1215f 894 * instead, nfsd4_run_cb_null() will detect the killed
6ff8da08 895 * client, destroy the rpc client, and stop:
b5a1a81e 896 */
f0b5de1b 897 nfsd4_run_cb(&clp->cl_cb_null);
b5a1a81e 898 flush_workqueue(callback_wq);
b5a1a81e
BF
899}
900
dcbeaa68
BF
901/* requires cl_lock: */
902static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
903{
904 struct nfsd4_session *s;
905 struct nfsd4_conn *c;
906
907 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
908 list_for_each_entry(c, &s->se_conns, cn_persession) {
909 if (c->cn_flags & NFS4_CDFC4_BACK)
910 return c;
911 }
912 }
913 return NULL;
914}
915
65e4c894 916static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
6ff8da08
BF
917{
918 struct nfs4_cb_conn conn;
919 struct nfs4_client *clp = cb->cb_clp;
dcbeaa68
BF
920 struct nfsd4_session *ses = NULL;
921 struct nfsd4_conn *c;
6ff8da08
BF
922 int err;
923
924 /*
925 * This is either an update, or the client dying; in either case,
926 * kill the old client:
927 */
928 if (clp->cl_cb_client) {
929 rpc_shutdown_client(clp->cl_cb_client);
930 clp->cl_cb_client = NULL;
c6bb3ca2
BF
931 put_rpccred(clp->cl_cb_cred);
932 clp->cl_cb_cred = NULL;
6ff8da08 933 }
dcbeaa68
BF
934 if (clp->cl_cb_conn.cb_xprt) {
935 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
936 clp->cl_cb_conn.cb_xprt = NULL;
937 }
a52d726b 938 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
6ff8da08
BF
939 return;
940 spin_lock(&clp->cl_lock);
941 /*
942 * Only serialized callback code is allowed to clear these
943 * flags; main nfsd code can only set them:
944 */
a52d726b
JL
945 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
946 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
6ff8da08 947 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
dcbeaa68
BF
948 c = __nfsd4_find_backchannel(clp);
949 if (c) {
950 svc_xprt_get(c->cn_xprt);
951 conn.cb_xprt = c->cn_xprt;
952 ses = c->cn_session;
953 }
6ff8da08
BF
954 spin_unlock(&clp->cl_lock);
955
dcbeaa68 956 err = setup_callback_client(clp, &conn, ses);
5ce8ba25 957 if (err) {
8546ee51 958 nfsd4_mark_cb_down(clp, err);
5ce8ba25
BF
959 return;
960 }
961 /* Yay, the callback channel's back! Restart any callbacks: */
962 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
326129d0 963 queue_work(callback_wq, &cb->cb_work);
6ff8da08
BF
964}
965
02e1215f 966static void
0162ac2b 967nfsd4_run_cb_work(struct work_struct *work)
5878453d 968{
0162ac2b
CH
969 struct nfsd4_callback *cb =
970 container_of(work, struct nfsd4_callback, cb_work);
fb003923 971 struct nfs4_client *clp = cb->cb_clp;
6ff8da08 972 struct rpc_clnt *clnt;
1da177e4 973
0162ac2b
CH
974 if (cb->cb_ops && cb->cb_ops->prepare)
975 cb->cb_ops->prepare(cb);
976
a52d726b 977 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
6ff8da08
BF
978 nfsd4_process_cb_update(cb);
979
980 clnt = clp->cl_cb_client;
981 if (!clnt) {
982 /* Callback channel broken, or client killed; give up: */
0162ac2b
CH
983 if (cb->cb_ops && cb->cb_ops->release)
984 cb->cb_ops->release(cb);
6ff8da08 985 return;
ac94bf58 986 }
c6bb3ca2 987 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
cee277d9 988 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
0162ac2b 989 cb->cb_ops ? &nfsd4_cb_ops : &nfsd4_cb_probe_ops, cb);
02e1215f
JL
990}
991
f0b5de1b 992void nfsd4_init_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
0162ac2b 993 struct nfsd4_callback_ops *ops, enum nfsd4_cb_op op)
b5a1a81e 994{
5ce8ba25 995 cb->cb_clp = clp;
326129d0 996 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
fb003923
BF
997 cb->cb_msg.rpc_argp = cb;
998 cb->cb_msg.rpc_resp = cb;
0162ac2b
CH
999 cb->cb_ops = ops;
1000 INIT_WORK(&cb->cb_work, nfsd4_run_cb_work);
9ee1ba54 1001 INIT_LIST_HEAD(&cb->cb_per_client);
5ce8ba25 1002 cb->cb_done = true;
f0b5de1b 1003}
5ce8ba25 1004
f0b5de1b
CH
1005void nfsd4_run_cb(struct nfsd4_callback *cb)
1006{
326129d0 1007 queue_work(callback_wq, &cb->cb_work);
b5a1a81e 1008}
This page took 0.640541 seconds and 5 git commands to generate.