blob: 03d9f4f298ec04b160f46ed6939fbe54bc7292d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
4 *
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <andros@umich.edu>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/sunrpc/clnt.h>
Kinglong Meed531c002014-03-24 11:59:46 +080035#include <linux/sunrpc/xprt.h>
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -050036#include <linux/sunrpc/svc_xprt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020038#include "nfsd.h"
39#include "state.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030040#include "netns.h"
J. Bruce Fields06b332a2013-04-09 11:34:36 -040041#include "xdr4cb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#define NFSDDBG_FACILITY NFSDDBG_PROC
44
Benny Halevy54237322011-10-19 19:12:58 -070045static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#define NFSPROC4_CB_NULL 0
48#define NFSPROC4_CB_COMPOUND 1
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050/* Index of predefined Linux callback client operations */
51
Christoph Hellwig2faf3b42014-09-24 12:19:16 +020052#define to_delegation(cb) \
53 container_of(cb, struct nfs4_delegation, dl_recall)
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055struct nfs4_cb_compound_hdr {
Andy Adamson38524ab2009-09-10 12:25:59 +030056 /* args */
57 u32 ident; /* minorversion 0 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 u32 nops;
Andy Adamsonef52bff2009-06-16 04:20:50 +030059 __be32 *nops_p;
Andy Adamsonab52ae62009-06-16 04:20:53 +030060 u32 minorversion;
Andy Adamson38524ab2009-09-10 12:25:59 +030061 /* res */
62 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063};
64
Chuck Lever85a56482010-12-14 14:57:32 +000065/*
66 * Handle decode buffer overflows out-of-line.
67 */
68static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Chuck Lever85a56482010-12-14 14:57:32 +000070 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Chuck Levera033db42010-12-14 14:57:22 +000075static __be32 *xdr_encode_empty_array(__be32 *p)
76{
77 *p++ = xdr_zero;
78 return p;
79}
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/*
Chuck Levera033db42010-12-14 14:57:22 +000082 * 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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 */
89
Chuck Levera033db42010-12-14 14:57:22 +000090/*
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)
Benny Halevy9303bbd2010-05-25 09:50:23 +0300115{
116 __be32 *p;
117
Chuck Levera033db42010-12-14 14:57:22 +0000118 p = xdr_reserve_space(xdr, 4);
119 *p = cpu_to_be32(op);
Benny Halevy9303bbd2010-05-25 09:50:23 +0300120}
121
Chuck Levera033db42010-12-14 14:57:22 +0000122/*
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
137/*
138 * stateid4
139 *
140 * struct stateid4 {
141 * uint32_t seqid;
142 * opaque other[12];
143 * };
144 */
145static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
146{
147 __be32 *p;
148
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)
161{
162 __be32 *p;
163
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);
167}
168
169/*
Chuck Lever85a56482010-12-14 14:57:32 +0000170 * 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
252/*
Chuck Levera033db42010-12-14 14:57:22 +0000253 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Al Virof00f3282006-10-19 23:29:01 -0700265 __be32 * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Chuck Levera033db42010-12-14 14:57:22 +0000267 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
Andy Adamsonef52bff2009-06-16 04:20:50 +0300272 hdr->nops_p = p;
Chuck Levera033db42010-12-14 14:57:22 +0000273 *p = cpu_to_be32(hdr->nops); /* argarray element count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Chuck Levera033db42010-12-14 14:57:22 +0000276/*
277 * Update argarray element count
278 */
Andy Adamsonef52bff2009-06-16 04:20:50 +0300279static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
280{
Chuck Levera033db42010-12-14 14:57:22 +0000281 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
282 *hdr->nops_p = cpu_to_be32(hdr->nops);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300283}
284
Chuck Levera033db42010-12-14 14:57:22 +0000285/*
Chuck Lever85a56482010-12-14 14:57:32 +0000286 * 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
316/*
Chuck Levera033db42010-12-14 14:57:22 +0000317 * 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Al Virof00f3282006-10-19 23:29:01 -0700329 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Chuck Levera033db42010-12-14 14:57:22 +0000331 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400332 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
Chuck Levera033db42010-12-14 14:57:22 +0000333
334 p = xdr_reserve_space(xdr, 4);
335 *p++ = xdr_zero; /* truncate */
336
Trond Myklebust11b91642014-07-29 21:34:08 -0400337 encode_nfs_fh4(xdr, &dp->dl_stid.sc_file->fi_fhandle);
Chuck Levera033db42010-12-14 14:57:22 +0000338
Andy Adamsonef52bff2009-06-16 04:20:50 +0300339 hdr->nops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Chuck Levera033db42010-12-14 14:57:22 +0000342/*
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)
Benny Halevy2af73582009-09-10 12:26:51 +0300357{
Chuck Levera033db42010-12-14 14:57:22 +0000358 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
Benny Halevy2af73582009-09-10 12:26:51 +0300359 __be32 *p;
360
361 if (hdr->minorversion == 0)
362 return;
363
Chuck Levera033db42010-12-14 14:57:22 +0000364 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
365 encode_sessionid4(xdr, session);
Benny Halevy2af73582009-09-10 12:26:51 +0300366
Chuck Levera033db42010-12-14 14:57:22 +0000367 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 */
373
Benny Halevy2af73582009-09-10 12:26:51 +0300374 hdr->nops++;
375}
376
Chuck Levera033db42010-12-14 14:57:22 +0000377/*
Chuck Lever85a56482010-12-14 14:57:32 +0000378 * 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 */
Benny Halevy2c9c8f32011-02-22 14:43:22 -0800413 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
Chuck Lever85a56482010-12-14 14:57:32 +0000414 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:
Benny Halevy54237322011-10-19 19:12:58 -0700441 if (status)
442 nfsd4_mark_cb_fault(cb->cb_clp, status);
Chuck Lever85a56482010-12-14 14:57:32 +0000443 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:
Benny Halevy0af3f812011-01-13 11:25:31 +0200467 return nfs_cb_stat_to_errno(nfserr);
Chuck Lever85a56482010-12-14 14:57:32 +0000468}
469
470/*
Chuck Levera033db42010-12-14 14:57:22 +0000471 * 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 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000482static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
483 void *__unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Chuck Levera033db42010-12-14 14:57:22 +0000485 xdr_reserve_space(xdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Chuck Levera033db42010-12-14 14:57:22 +0000488/*
489 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
490 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000491static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
492 const struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Christoph Hellwig2faf3b42014-09-24 12:19:16 +0200494 const struct nfs4_delegation *dp = to_delegation(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 struct nfs4_cb_compound_hdr hdr = {
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400496 .ident = cb->cb_clp->cl_cb_ident,
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400497 .minorversion = cb->cb_minorversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 };
499
Chuck Lever9f06c712010-12-14 14:59:18 +0000500 encode_cb_compound4args(xdr, &hdr);
501 encode_cb_sequence4args(xdr, cb, &hdr);
Christoph Hellwig2faf3b42014-09-24 12:19:16 +0200502 encode_cb_recall4args(xdr, dp, &hdr);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300503 encode_cb_nops(&hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506
Chuck Lever85a56482010-12-14 14:57:32 +0000507/*
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".
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Chuck Leverbf269552010-12-14 14:59:29 +0000516static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
517 void *__unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return 0;
520}
521
Benny Halevy2af73582009-09-10 12:26:51 +0300522/*
Chuck Lever85a56482010-12-14 14:57:32 +0000523 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
Benny Halevy2af73582009-09-10 12:26:51 +0300524 */
Chuck Leverbf269552010-12-14 14:59:29 +0000525static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
526 struct xdr_stream *xdr,
Chuck Lever85a56482010-12-14 14:57:32 +0000527 struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 struct nfs4_cb_compound_hdr hdr;
Chuck Lever85a56482010-12-14 14:57:32 +0000530 enum nfsstat4 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 int status;
532
Chuck Leverbf269552010-12-14 14:59:29 +0000533 status = decode_cb_compound4res(xdr, &hdr);
Chuck Lever85a56482010-12-14 14:57:32 +0000534 if (unlikely(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 goto out;
Chuck Lever85a56482010-12-14 14:57:32 +0000536
537 if (cb != NULL) {
Chuck Leverbf269552010-12-14 14:59:29 +0000538 status = decode_cb_sequence4res(xdr, cb);
Chuck Lever85a56482010-12-14 14:57:32 +0000539 if (unlikely(status))
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300540 goto out;
541 }
Chuck Lever85a56482010-12-14 14:57:32 +0000542
Chuck Leverbf269552010-12-14 14:59:29 +0000543 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
Chuck Lever85a56482010-12-14 14:57:32 +0000544 if (unlikely(status))
545 goto out;
546 if (unlikely(nfserr != NFS4_OK))
Benny Halevy0af3f812011-01-13 11:25:31 +0200547 status = nfs_cb_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548out:
549 return status;
550}
551
552/*
553 * RPC procedure tables
554 */
Chuck Lever7d93bd712010-12-14 14:57:42 +0000555#define PROC(proc, call, argtype, restype) \
556[NFSPROC4_CLNT_##proc] = { \
557 .p_proc = NFSPROC4_CB_##call, \
Chuck Lever9f06c712010-12-14 14:59:18 +0000558 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
Chuck Leverbf269552010-12-14 14:59:29 +0000559 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
Chuck Lever7d93bd712010-12-14 14:57:42 +0000560 .p_arglen = NFS4_enc_##argtype##_sz, \
561 .p_replen = NFS4_dec_##restype##_sz, \
562 .p_statidx = NFSPROC4_CB_##call, \
563 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Chuck Lever7d93bd712010-12-14 14:57:42 +0000566static struct rpc_procinfo nfs4_cb_procedures[] = {
567 PROC(CB_NULL, NULL, cb_null, cb_null),
568 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569};
570
Chuck Lever7d93bd712010-12-14 14:57:42 +0000571static struct rpc_version nfs_cb_version4 = {
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400572/*
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 */
Chuck Lever7d93bd712010-12-14 14:57:42 +0000579 .number = 1,
580 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
581 .procs = nfs4_cb_procedures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582};
583
Trond Myklebusta613fa12012-01-20 13:53:56 -0500584static const struct rpc_version *nfs_cb_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 &nfs_cb_version4,
586};
587
Trond Myklebusta613fa12012-01-20 13:53:56 -0500588static const struct rpc_program cb_program;
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400589
590static struct rpc_stat cb_stats = {
Chuck Lever7d93bd712010-12-14 14:57:42 +0000591 .program = &cb_program
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400592};
593
594#define NFS4_CALLBACK 0x40000000
Trond Myklebusta613fa12012-01-20 13:53:56 -0500595static const struct rpc_program cb_program = {
Chuck Lever7d93bd712010-12-14 14:57:42 +0000596 .name = "nfs4_cb",
597 .number = NFS4_CALLBACK,
598 .nrvers = ARRAY_SIZE(nfs_cb_version),
599 .version = nfs_cb_version,
600 .stats = &cb_stats,
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400601 .pipe_dir_name = "nfsd4_cb",
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400602};
603
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +0300604static int max_cb_time(struct net *net)
J. Bruce Fields595947a2009-03-05 17:18:10 -0500605{
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +0300606 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
607 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
J. Bruce Fields595947a2009-03-05 17:18:10 -0500608}
609
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400610static 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
Fengguang Wu2b4cf662012-11-13 15:41:27 -0500622static struct rpc_cred *get_backchannel_cred(struct nfs4_client *clp, struct rpc_clnt *client, struct nfsd4_session *ses)
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400623{
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}
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400635
Kinglong Meed531c002014-03-24 11:59:46 +0800636static 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
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400652static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400653{
Jeff Layton3758cf72014-04-15 08:51:48 -0400654 int maxtime = max_cb_time(clp->net);
Chuck Leverae5c7942006-08-22 20:06:21 -0400655 struct rpc_timeout timeparms = {
Jeff Layton3758cf72014-04-15 08:51:48 -0400656 .to_initval = maxtime,
J. Bruce Fields595947a2009-03-05 17:18:10 -0500657 .to_retries = 0,
Jeff Layton3758cf72014-04-15 08:51:48 -0400658 .to_maxval = maxtime,
Chuck Leverae5c7942006-08-22 20:06:21 -0400659 };
Chuck Leverae5c7942006-08-22 20:06:21 -0400660 struct rpc_create_args args = {
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +0300661 .net = clp->net,
J. Bruce Fields07263f12010-05-31 19:09:40 -0400662 .address = (struct sockaddr *) &conn->cb_addr,
663 .addrsize = conn->cb_addrlen,
Takuma Umeya6f3d7722010-12-15 14:09:01 +0900664 .saddress = (struct sockaddr *) &conn->cb_saddr,
Chuck Leverae5c7942006-08-22 20:06:21 -0400665 .timeout = &timeparms,
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400666 .program = &cb_program,
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400667 .version = 0,
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400668 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
Chuck Leverae5c7942006-08-22 20:06:21 -0400669 };
J. Bruce Fields63c86712007-10-25 19:00:26 -0400670 struct rpc_clnt *client;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400671 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400673 if (clp->cl_minorversion == 0) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400674 if (!clp->cl_cred.cr_principal &&
J. Bruce Fields39307652012-08-16 17:01:21 -0400675 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400676 return -EINVAL;
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400677 args.client_name = clp->cl_cred.cr_principal;
Himangi Saraogifc8e5a62014-07-23 20:12:31 +0530678 args.prognumber = conn->cb_prog;
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400679 args.protocol = XPRT_TRANSPORT_TCP;
J. Bruce Fields39307652012-08-16 17:01:21 -0400680 args.authflavor = clp->cl_cred.cr_flavor;
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400681 clp->cl_cb_ident = conn->cb_ident;
682 } else {
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400683 if (!conn->cb_xprt)
684 return -EINVAL;
685 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
686 clp->cl_cb_session = ses;
J. Bruce Fields07263f12010-05-31 19:09:40 -0400687 args.bc_xprt = conn->cb_xprt;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400688 args.prognumber = clp->cl_cb_session->se_cb_prog;
Chuck Lever3c45ddf2014-07-16 15:38:32 -0400689 args.protocol = conn->cb_xprt->xpt_class->xcl_ident |
690 XPRT_TRANSPORT_BC;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500691 args.authflavor = ses->se_cb_sec.flavor;
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300692 }
Chuck Leverae5c7942006-08-22 20:06:21 -0400693 /* Create RPC client */
Kinglong Meed531c002014-03-24 11:59:46 +0800694 client = create_backchannel_client(&args);
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800695 if (IS_ERR(client)) {
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800696 dprintk("NFSD: couldn't create callback client: %ld\n",
697 PTR_ERR(client));
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800698 return PTR_ERR(client);
699 }
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400700 cred = get_backchannel_cred(clp, client, ses);
701 if (IS_ERR(cred)) {
702 rpc_shutdown_client(client);
703 return PTR_ERR(cred);
704 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400705 clp->cl_cb_client = client;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400706 clp->cl_cb_cred = cred;
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800707 return 0;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800708}
709
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800710static 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
J. Bruce Fields77a35692010-04-30 18:51:44 -0400716static 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
Benny Halevy54237322011-10-19 19:12:58 -0700722static 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
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500728static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
729{
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400730 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500731
732 if (task->tk_status)
J. Bruce Fields77a35692010-04-30 18:51:44 -0400733 nfsd4_mark_cb_down(clp, task->tk_status);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500734 else
J. Bruce Fields77a35692010-04-30 18:51:44 -0400735 clp->cl_cb_state = NFSD4_CB_UP;
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500736}
737
738static const struct rpc_call_ops nfsd4_cb_probe_ops = {
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400739 /* XXX: release method to ensure we set the cb channel down if
740 * necessary on early failure? */
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500741 .rpc_call_done = nfsd4_cb_probe_done,
742};
743
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400744static struct workqueue_struct *callback_wq;
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400745
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400746static void do_probe_callback(struct nfs4_client *clp)
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800747{
Christoph Hellwig326129d2014-09-24 12:19:17 +0200748 return nfsd4_cb(&clp->cl_cb_null, clp, NFSPROC4_CLNT_CB_NULL);
J. Bruce Fields63c86712007-10-25 19:00:26 -0400749}
750
751/*
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400752 * Poke the callback thread to process any updates to the callback
753 * parameters, and send a null probe.
J. Bruce Fields63c86712007-10-25 19:00:26 -0400754 */
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400755void nfsd4_probe_callback(struct nfs4_client *clp)
756{
J. Bruce Fields77a35692010-04-30 18:51:44 -0400757 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Jeff Laytona52d7262012-03-21 09:52:02 -0400758 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400759 do_probe_callback(clp);
760}
761
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -0500762void nfsd4_probe_callback_sync(struct nfs4_client *clp)
763{
764 nfsd4_probe_callback(clp);
765 flush_workqueue(callback_wq);
766}
767
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400768void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
J. Bruce Fields63c86712007-10-25 19:00:26 -0400769{
J. Bruce Fields77a35692010-04-30 18:51:44 -0400770 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400771 spin_lock(&clp->cl_lock);
772 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400773 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300776/*
777 * There's currently a single callback channel slot.
778 * If the slot is available, then mark it busy. Otherwise, set the
779 * thread for sleeping on the callback RPC wait queue.
780 */
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500781static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300782{
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300783 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
784 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
785 dprintk("%s slot is busy\n", __func__);
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500786 return false;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300787 }
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500788 return true;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300789}
790
791/*
792 * TODO: cb_sequence should support referring call lists, cachethis, multiple
793 * slots, and mark callback channel down on communication errors.
794 */
795static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
796{
J. Bruce Fields58784532010-05-16 16:47:08 -0400797 struct nfsd4_callback *cb = calldata;
J. Bruce Fields8be2d232013-03-21 10:59:29 -0400798 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400799 u32 minorversion = clp->cl_minorversion;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300800
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400801 cb->cb_minorversion = minorversion;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300802 if (minorversion) {
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500803 if (!nfsd41_cb_get_slot(clp, task))
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300804 return;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300805 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500806 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa8f28002011-01-14 14:25:48 -0500807 if (list_empty(&cb->cb_per_client)) {
808 /* This is the first call, not a restart */
809 cb->cb_done = false;
810 list_add(&cb->cb_per_client, &clp->cl_callbacks);
811 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500812 spin_unlock(&clp->cl_lock);
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300813 rpc_call_start(task);
814}
815
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300816static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
817{
J. Bruce Fields58784532010-05-16 16:47:08 -0400818 struct nfsd4_callback *cb = calldata;
J. Bruce Fields8be2d232013-03-21 10:59:29 -0400819 struct nfs4_client *clp = cb->cb_clp;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300820
821 dprintk("%s: minorversion=%d\n", __func__,
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400822 clp->cl_minorversion);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300823
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400824 if (clp->cl_minorversion) {
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300825 /* No need for lock, access serialized in nfsd4_cb_prepare */
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400826 ++clp->cl_cb_session->se_cb_seq_nr;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300827 clear_bit(0, &clp->cl_cb_slot_busy);
828 rpc_wake_up_next(&clp->cl_cb_waitq);
829 dprintk("%s: freed slot, new seqid=%d\n", __func__,
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400830 clp->cl_cb_session->se_cb_seq_nr);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300831 }
832}
833
J. Bruce Fields63e48632009-05-01 22:36:55 -0400834static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
835{
J. Bruce Fields58784532010-05-16 16:47:08 -0400836 struct nfsd4_callback *cb = calldata;
Christoph Hellwig2faf3b42014-09-24 12:19:16 +0200837 struct nfs4_delegation *dp = to_delegation(cb);
J. Bruce Fields8be2d232013-03-21 10:59:29 -0400838 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500839 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400840
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300841 nfsd4_cb_done(task, calldata);
842
J. Bruce Fieldsa8f28002011-01-14 14:25:48 -0500843 if (current_rpc_client != task->tk_client) {
844 /* We're shutting down or changing cl_cb_client; leave
845 * it to nfsd4_process_cb_update to restart the call if
846 * necessary. */
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500847 return;
848 }
849
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500850 if (cb->cb_done)
851 return;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400852 switch (task->tk_status) {
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400853 case 0:
Christoph Hellwig444b6e92014-09-23 08:58:48 +0200854 break;
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400855 case -EBADHANDLE:
856 case -NFS4ERR_BAD_STATEID:
857 /* Race: client probably got cb_recall
858 * before open reply granting delegation */
Christoph Hellwig444b6e92014-09-23 08:58:48 +0200859 if (dp->dl_retries--) {
860 rpc_delay(task, 2*HZ);
861 task->tk_status = 0;
862 rpc_restart_call_prepare(task);
863 return;
864 }
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400865 default:
J. Bruce Fields63e48632009-05-01 22:36:55 -0400866 /* Network partition? */
J. Bruce Fields77a35692010-04-30 18:51:44 -0400867 nfsd4_mark_cb_down(clp, task->tk_status);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400868 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500869 cb->cb_done = true;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400870}
871
872static void nfsd4_cb_recall_release(void *calldata)
873{
J. Bruce Fields58784532010-05-16 16:47:08 -0400874 struct nfsd4_callback *cb = calldata;
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500875 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400876
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500877 if (cb->cb_done) {
Christoph Hellwig2faf3b42014-09-24 12:19:16 +0200878 struct nfs4_delegation *dp = to_delegation(cb);
879
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500880 spin_lock(&clp->cl_lock);
881 list_del(&cb->cb_per_client);
882 spin_unlock(&clp->cl_lock);
Trond Myklebust60116952014-07-29 21:34:06 -0400883 nfs4_put_stid(&dp->dl_stid);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500884 }
J. Bruce Fields63e48632009-05-01 22:36:55 -0400885}
886
887static const struct rpc_call_ops nfsd4_cb_recall_ops = {
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300888 .rpc_call_prepare = nfsd4_cb_prepare,
J. Bruce Fields63e48632009-05-01 22:36:55 -0400889 .rpc_call_done = nfsd4_cb_recall_done,
890 .rpc_release = nfsd4_cb_recall_release,
891};
892
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500893int nfsd4_create_callback_queue(void)
894{
895 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
896 if (!callback_wq)
897 return -ENOMEM;
898 return 0;
899}
900
901void nfsd4_destroy_callback_queue(void)
902{
903 destroy_workqueue(callback_wq);
904}
905
Benny Halevyab707e152010-05-12 00:14:06 +0300906/* must be called under the state lock */
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400907void nfsd4_shutdown_callback(struct nfs4_client *clp)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500908{
Jeff Laytona52d7262012-03-21 09:52:02 -0400909 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500910 /*
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400911 * Note this won't actually result in a null callback;
Jeff Layton02e12152014-07-16 10:31:57 -0400912 * instead, nfsd4_run_cb_null() will detect the killed
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400913 * client, destroy the rpc client, and stop:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500914 */
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400915 do_probe_callback(clp);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500916 flush_workqueue(callback_wq);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500917}
918
Kirill A. Shutemov65e4c892010-12-16 15:25:54 +0200919static void nfsd4_release_cb(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
J. Bruce Fields58784532010-05-16 16:47:08 -0400921 if (cb->cb_ops->rpc_release)
922 cb->cb_ops->rpc_release(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500924
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400925/* requires cl_lock: */
926static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
927{
928 struct nfsd4_session *s;
929 struct nfsd4_conn *c;
930
931 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
932 list_for_each_entry(c, &s->se_conns, cn_persession) {
933 if (c->cn_flags & NFS4_CDFC4_BACK)
934 return c;
935 }
936 }
937 return NULL;
938}
939
Kirill A. Shutemov65e4c892010-12-16 15:25:54 +0200940static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400941{
942 struct nfs4_cb_conn conn;
943 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400944 struct nfsd4_session *ses = NULL;
945 struct nfsd4_conn *c;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400946 int err;
947
948 /*
949 * This is either an update, or the client dying; in either case,
950 * kill the old client:
951 */
952 if (clp->cl_cb_client) {
953 rpc_shutdown_client(clp->cl_cb_client);
954 clp->cl_cb_client = NULL;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400955 put_rpccred(clp->cl_cb_cred);
956 clp->cl_cb_cred = NULL;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400957 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400958 if (clp->cl_cb_conn.cb_xprt) {
959 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
960 clp->cl_cb_conn.cb_xprt = NULL;
961 }
Jeff Laytona52d7262012-03-21 09:52:02 -0400962 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400963 return;
964 spin_lock(&clp->cl_lock);
965 /*
966 * Only serialized callback code is allowed to clear these
967 * flags; main nfsd code can only set them:
968 */
Jeff Laytona52d7262012-03-21 09:52:02 -0400969 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
970 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400971 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400972 c = __nfsd4_find_backchannel(clp);
973 if (c) {
974 svc_xprt_get(c->cn_xprt);
975 conn.cb_xprt = c->cn_xprt;
976 ses = c->cn_session;
977 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400978 spin_unlock(&clp->cl_lock);
979
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400980 err = setup_callback_client(clp, &conn, ses);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500981 if (err) {
J. Bruce Fields8546ee52012-03-09 17:02:28 -0500982 nfsd4_mark_cb_down(clp, err);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500983 return;
984 }
985 /* Yay, the callback channel's back! Restart any callbacks: */
986 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
Christoph Hellwig326129d2014-09-24 12:19:17 +0200987 queue_work(callback_wq, &cb->cb_work);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400988}
989
Jeff Layton02e12152014-07-16 10:31:57 -0400990static void
991nfsd4_run_callback_rpc(struct nfsd4_callback *cb)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500992{
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400993 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400994 struct rpc_clnt *clnt;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500995
Jeff Laytona52d7262012-03-21 09:52:02 -0400996 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400997 nfsd4_process_cb_update(cb);
998
999 clnt = clp->cl_cb_client;
1000 if (!clnt) {
1001 /* Callback channel broken, or client killed; give up: */
J. Bruce Fields58784532010-05-16 16:47:08 -04001002 nfsd4_release_cb(cb);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001003 return;
J. Bruce Fields58784532010-05-16 16:47:08 -04001004 }
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001005 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001006 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1007 cb->cb_ops, cb);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001008}
1009
Jeff Layton02e12152014-07-16 10:31:57 -04001010void
1011nfsd4_run_cb_null(struct work_struct *w)
1012{
1013 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1014 cb_work);
1015 nfsd4_run_callback_rpc(cb);
1016}
1017
1018void
1019nfsd4_run_cb_recall(struct work_struct *w)
1020{
1021 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback,
1022 cb_work);
1023
Christoph Hellwig2faf3b42014-09-24 12:19:16 +02001024 nfsd4_prepare_cb_recall(to_delegation(cb));
Jeff Layton02e12152014-07-16 10:31:57 -04001025 nfsd4_run_callback_rpc(cb);
1026}
1027
Christoph Hellwig326129d2014-09-24 12:19:17 +02001028void nfsd4_cb(struct nfsd4_callback *cb, struct nfs4_client *clp,
1029 enum nfsd4_cb_op op)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001030{
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001031 cb->cb_clp = clp;
Christoph Hellwig326129d2014-09-24 12:19:17 +02001032 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[op];
J. Bruce Fieldsfb003922010-05-31 18:21:37 -04001033 cb->cb_msg.rpc_argp = cb;
1034 cb->cb_msg.rpc_resp = cb;
Christoph Hellwig326129d2014-09-24 12:19:17 +02001035 if (op == NFSPROC4_CLNT_CB_NULL)
1036 cb->cb_ops = &nfsd4_cb_probe_ops;
1037 else
1038 cb->cb_ops = &nfsd4_cb_recall_ops;
J. Bruce Fields9ee1ba52011-01-13 17:08:19 -05001039 INIT_LIST_HEAD(&cb->cb_per_client);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001040 cb->cb_done = true;
1041
Christoph Hellwig326129d2014-09-24 12:19:17 +02001042 queue_work(callback_wq, &cb->cb_work);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001043}