blob: 99bc85ff02179f3e28ed36f56872e1e48d190e21 [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>
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -050035#include <linux/sunrpc/svc_xprt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Boaz Harrosh9a74af22009-12-03 20:30:56 +020037#include "nfsd.h"
38#include "state.h"
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +030039#include "netns.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define NFSDDBG_FACILITY NFSDDBG_PROC
42
Benny Halevy54237322011-10-19 19:12:58 -070043static void nfsd4_mark_cb_fault(struct nfs4_client *, int reason);
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define NFSPROC4_CB_NULL 0
46#define NFSPROC4_CB_COMPOUND 1
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/* Index of predefined Linux callback client operations */
49
50enum {
Benny Halevy4be36ca2009-09-10 12:25:46 +030051 NFSPROC4_CLNT_CB_NULL = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 NFSPROC4_CLNT_CB_RECALL,
Andy Adamson38524ab2009-09-10 12:25:59 +030053 NFSPROC4_CLNT_CB_SEQUENCE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#define NFS4_MAXTAGLEN 20
57
58#define NFS4_enc_cb_null_sz 0
59#define NFS4_dec_cb_null_sz 0
60#define cb_compound_enc_hdr_sz 4
61#define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
Andy Adamson38524ab2009-09-10 12:25:59 +030062#define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
63#define cb_sequence_enc_sz (sessionid_sz + 4 + \
64 1 /* no referring calls list yet */)
65#define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define op_enc_sz 1
68#define op_dec_sz 2
69#define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
Benny Halevy0ac68d12007-07-17 04:04:37 -070070#define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030072 cb_sequence_enc_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 1 + enc_stateid_sz + \
74 enc_nfs4_fh_sz)
75
76#define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030077 cb_sequence_dec_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 op_dec_sz)
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080struct nfs4_cb_compound_hdr {
Andy Adamson38524ab2009-09-10 12:25:59 +030081 /* args */
82 u32 ident; /* minorversion 0 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 u32 nops;
Andy Adamsonef52bff2009-06-16 04:20:50 +030084 __be32 *nops_p;
Andy Adamsonab52ae62009-06-16 04:20:53 +030085 u32 minorversion;
Andy Adamson38524ab2009-09-10 12:25:59 +030086 /* res */
87 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088};
89
Chuck Lever85a56482010-12-14 14:57:32 +000090/*
91 * Handle decode buffer overflows out-of-line.
92 */
93static void print_overflow_msg(const char *func, const struct xdr_stream *xdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Chuck Lever85a56482010-12-14 14:57:32 +000095 dprintk("NFS: %s prematurely hit the end of our receive buffer. "
96 "Remaining buffer length is %tu words.\n",
97 func, xdr->end - xdr->p);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
Chuck Levera033db42010-12-14 14:57:22 +0000100static __be32 *xdr_encode_empty_array(__be32 *p)
101{
102 *p++ = xdr_zero;
103 return p;
104}
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106/*
Chuck Levera033db42010-12-14 14:57:22 +0000107 * Encode/decode NFSv4 CB basic data types
108 *
109 * Basic NFSv4 callback data types are defined in section 15 of RFC
110 * 3530: "Network File System (NFS) version 4 Protocol" and section
111 * 20 of RFC 5661: "Network File System (NFS) Version 4 Minor Version
112 * 1 Protocol"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 */
114
Chuck Levera033db42010-12-14 14:57:22 +0000115/*
116 * nfs_cb_opnum4
117 *
118 * enum nfs_cb_opnum4 {
119 * OP_CB_GETATTR = 3,
120 * ...
121 * };
122 */
123enum nfs_cb_opnum4 {
124 OP_CB_GETATTR = 3,
125 OP_CB_RECALL = 4,
126 OP_CB_LAYOUTRECALL = 5,
127 OP_CB_NOTIFY = 6,
128 OP_CB_PUSH_DELEG = 7,
129 OP_CB_RECALL_ANY = 8,
130 OP_CB_RECALLABLE_OBJ_AVAIL = 9,
131 OP_CB_RECALL_SLOT = 10,
132 OP_CB_SEQUENCE = 11,
133 OP_CB_WANTS_CANCELLED = 12,
134 OP_CB_NOTIFY_LOCK = 13,
135 OP_CB_NOTIFY_DEVICEID = 14,
136 OP_CB_ILLEGAL = 10044
137};
138
139static void encode_nfs_cb_opnum4(struct xdr_stream *xdr, enum nfs_cb_opnum4 op)
Benny Halevy9303bbd2010-05-25 09:50:23 +0300140{
141 __be32 *p;
142
Chuck Levera033db42010-12-14 14:57:22 +0000143 p = xdr_reserve_space(xdr, 4);
144 *p = cpu_to_be32(op);
Benny Halevy9303bbd2010-05-25 09:50:23 +0300145}
146
Chuck Levera033db42010-12-14 14:57:22 +0000147/*
148 * nfs_fh4
149 *
150 * typedef opaque nfs_fh4<NFS4_FHSIZE>;
151 */
152static void encode_nfs_fh4(struct xdr_stream *xdr, const struct knfsd_fh *fh)
153{
154 u32 length = fh->fh_size;
155 __be32 *p;
156
157 BUG_ON(length > NFS4_FHSIZE);
158 p = xdr_reserve_space(xdr, 4 + length);
159 xdr_encode_opaque(p, &fh->fh_base, length);
160}
161
162/*
163 * stateid4
164 *
165 * struct stateid4 {
166 * uint32_t seqid;
167 * opaque other[12];
168 * };
169 */
170static void encode_stateid4(struct xdr_stream *xdr, const stateid_t *sid)
171{
172 __be32 *p;
173
174 p = xdr_reserve_space(xdr, NFS4_STATEID_SIZE);
175 *p++ = cpu_to_be32(sid->si_generation);
176 xdr_encode_opaque_fixed(p, &sid->si_opaque, NFS4_STATEID_OTHER_SIZE);
177}
178
179/*
180 * sessionid4
181 *
182 * typedef opaque sessionid4[NFS4_SESSIONID_SIZE];
183 */
184static void encode_sessionid4(struct xdr_stream *xdr,
185 const struct nfsd4_session *session)
186{
187 __be32 *p;
188
189 p = xdr_reserve_space(xdr, NFS4_MAX_SESSIONID_LEN);
190 xdr_encode_opaque_fixed(p, session->se_sessionid.data,
191 NFS4_MAX_SESSIONID_LEN);
192}
193
194/*
Chuck Lever85a56482010-12-14 14:57:32 +0000195 * nfsstat4
196 */
197static const struct {
198 int stat;
199 int errno;
200} nfs_cb_errtbl[] = {
201 { NFS4_OK, 0 },
202 { NFS4ERR_PERM, -EPERM },
203 { NFS4ERR_NOENT, -ENOENT },
204 { NFS4ERR_IO, -EIO },
205 { NFS4ERR_NXIO, -ENXIO },
206 { NFS4ERR_ACCESS, -EACCES },
207 { NFS4ERR_EXIST, -EEXIST },
208 { NFS4ERR_XDEV, -EXDEV },
209 { NFS4ERR_NOTDIR, -ENOTDIR },
210 { NFS4ERR_ISDIR, -EISDIR },
211 { NFS4ERR_INVAL, -EINVAL },
212 { NFS4ERR_FBIG, -EFBIG },
213 { NFS4ERR_NOSPC, -ENOSPC },
214 { NFS4ERR_ROFS, -EROFS },
215 { NFS4ERR_MLINK, -EMLINK },
216 { NFS4ERR_NAMETOOLONG, -ENAMETOOLONG },
217 { NFS4ERR_NOTEMPTY, -ENOTEMPTY },
218 { NFS4ERR_DQUOT, -EDQUOT },
219 { NFS4ERR_STALE, -ESTALE },
220 { NFS4ERR_BADHANDLE, -EBADHANDLE },
221 { NFS4ERR_BAD_COOKIE, -EBADCOOKIE },
222 { NFS4ERR_NOTSUPP, -ENOTSUPP },
223 { NFS4ERR_TOOSMALL, -ETOOSMALL },
224 { NFS4ERR_SERVERFAULT, -ESERVERFAULT },
225 { NFS4ERR_BADTYPE, -EBADTYPE },
226 { NFS4ERR_LOCKED, -EAGAIN },
227 { NFS4ERR_RESOURCE, -EREMOTEIO },
228 { NFS4ERR_SYMLINK, -ELOOP },
229 { NFS4ERR_OP_ILLEGAL, -EOPNOTSUPP },
230 { NFS4ERR_DEADLOCK, -EDEADLK },
231 { -1, -EIO }
232};
233
234/*
235 * If we cannot translate the error, the recovery routines should
236 * handle it.
237 *
238 * Note: remaining NFSv4 error codes have values > 10000, so should
239 * not conflict with native Linux error codes.
240 */
241static int nfs_cb_stat_to_errno(int status)
242{
243 int i;
244
245 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
246 if (nfs_cb_errtbl[i].stat == status)
247 return nfs_cb_errtbl[i].errno;
248 }
249
250 dprintk("NFSD: Unrecognized NFS CB status value: %u\n", status);
251 return -status;
252}
253
254static int decode_cb_op_status(struct xdr_stream *xdr, enum nfs_opnum4 expected,
255 enum nfsstat4 *status)
256{
257 __be32 *p;
258 u32 op;
259
260 p = xdr_inline_decode(xdr, 4 + 4);
261 if (unlikely(p == NULL))
262 goto out_overflow;
263 op = be32_to_cpup(p++);
264 if (unlikely(op != expected))
265 goto out_unexpected;
266 *status = be32_to_cpup(p);
267 return 0;
268out_overflow:
269 print_overflow_msg(__func__, xdr);
270 return -EIO;
271out_unexpected:
272 dprintk("NFSD: Callback server returned operation %d but "
273 "we issued a request for %d\n", op, expected);
274 return -EIO;
275}
276
277/*
Chuck Levera033db42010-12-14 14:57:22 +0000278 * CB_COMPOUND4args
279 *
280 * struct CB_COMPOUND4args {
281 * utf8str_cs tag;
282 * uint32_t minorversion;
283 * uint32_t callback_ident;
284 * nfs_cb_argop4 argarray<>;
285 * };
286*/
287static void encode_cb_compound4args(struct xdr_stream *xdr,
288 struct nfs4_cb_compound_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Al Virof00f3282006-10-19 23:29:01 -0700290 __be32 * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Chuck Levera033db42010-12-14 14:57:22 +0000292 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4);
293 p = xdr_encode_empty_array(p); /* empty tag */
294 *p++ = cpu_to_be32(hdr->minorversion);
295 *p++ = cpu_to_be32(hdr->ident);
296
Andy Adamsonef52bff2009-06-16 04:20:50 +0300297 hdr->nops_p = p;
Chuck Levera033db42010-12-14 14:57:22 +0000298 *p = cpu_to_be32(hdr->nops); /* argarray element count */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Chuck Levera033db42010-12-14 14:57:22 +0000301/*
302 * Update argarray element count
303 */
Andy Adamsonef52bff2009-06-16 04:20:50 +0300304static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
305{
Chuck Levera033db42010-12-14 14:57:22 +0000306 BUG_ON(hdr->nops > NFS4_MAX_BACK_CHANNEL_OPS);
307 *hdr->nops_p = cpu_to_be32(hdr->nops);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300308}
309
Chuck Levera033db42010-12-14 14:57:22 +0000310/*
Chuck Lever85a56482010-12-14 14:57:32 +0000311 * CB_COMPOUND4res
312 *
313 * struct CB_COMPOUND4res {
314 * nfsstat4 status;
315 * utf8str_cs tag;
316 * nfs_cb_resop4 resarray<>;
317 * };
318 */
319static int decode_cb_compound4res(struct xdr_stream *xdr,
320 struct nfs4_cb_compound_hdr *hdr)
321{
322 u32 length;
323 __be32 *p;
324
325 p = xdr_inline_decode(xdr, 4 + 4);
326 if (unlikely(p == NULL))
327 goto out_overflow;
328 hdr->status = be32_to_cpup(p++);
329 /* Ignore the tag */
330 length = be32_to_cpup(p++);
331 p = xdr_inline_decode(xdr, length + 4);
332 if (unlikely(p == NULL))
333 goto out_overflow;
334 hdr->nops = be32_to_cpup(p);
335 return 0;
336out_overflow:
337 print_overflow_msg(__func__, xdr);
338 return -EIO;
339}
340
341/*
Chuck Levera033db42010-12-14 14:57:22 +0000342 * CB_RECALL4args
343 *
344 * struct CB_RECALL4args {
345 * stateid4 stateid;
346 * bool truncate;
347 * nfs_fh4 fh;
348 * };
349 */
350static void encode_cb_recall4args(struct xdr_stream *xdr,
351 const struct nfs4_delegation *dp,
352 struct nfs4_cb_compound_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Al Virof00f3282006-10-19 23:29:01 -0700354 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Chuck Levera033db42010-12-14 14:57:22 +0000356 encode_nfs_cb_opnum4(xdr, OP_CB_RECALL);
J. Bruce Fieldsd5477a82011-09-08 12:07:44 -0400357 encode_stateid4(xdr, &dp->dl_stid.sc_stateid);
Chuck Levera033db42010-12-14 14:57:22 +0000358
359 p = xdr_reserve_space(xdr, 4);
360 *p++ = xdr_zero; /* truncate */
361
362 encode_nfs_fh4(xdr, &dp->dl_fh);
363
Andy Adamsonef52bff2009-06-16 04:20:50 +0300364 hdr->nops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Chuck Levera033db42010-12-14 14:57:22 +0000367/*
368 * CB_SEQUENCE4args
369 *
370 * struct CB_SEQUENCE4args {
371 * sessionid4 csa_sessionid;
372 * sequenceid4 csa_sequenceid;
373 * slotid4 csa_slotid;
374 * slotid4 csa_highest_slotid;
375 * bool csa_cachethis;
376 * referring_call_list4 csa_referring_call_lists<>;
377 * };
378 */
379static void encode_cb_sequence4args(struct xdr_stream *xdr,
380 const struct nfsd4_callback *cb,
381 struct nfs4_cb_compound_hdr *hdr)
Benny Halevy2af73582009-09-10 12:26:51 +0300382{
Chuck Levera033db42010-12-14 14:57:22 +0000383 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
Benny Halevy2af73582009-09-10 12:26:51 +0300384 __be32 *p;
385
386 if (hdr->minorversion == 0)
387 return;
388
Chuck Levera033db42010-12-14 14:57:22 +0000389 encode_nfs_cb_opnum4(xdr, OP_CB_SEQUENCE);
390 encode_sessionid4(xdr, session);
Benny Halevy2af73582009-09-10 12:26:51 +0300391
Chuck Levera033db42010-12-14 14:57:22 +0000392 p = xdr_reserve_space(xdr, 4 + 4 + 4 + 4 + 4);
393 *p++ = cpu_to_be32(session->se_cb_seq_nr); /* csa_sequenceid */
394 *p++ = xdr_zero; /* csa_slotid */
395 *p++ = xdr_zero; /* csa_highest_slotid */
396 *p++ = xdr_zero; /* csa_cachethis */
397 xdr_encode_empty_array(p); /* csa_referring_call_lists */
398
Benny Halevy2af73582009-09-10 12:26:51 +0300399 hdr->nops++;
400}
401
Chuck Levera033db42010-12-14 14:57:22 +0000402/*
Chuck Lever85a56482010-12-14 14:57:32 +0000403 * CB_SEQUENCE4resok
404 *
405 * struct CB_SEQUENCE4resok {
406 * sessionid4 csr_sessionid;
407 * sequenceid4 csr_sequenceid;
408 * slotid4 csr_slotid;
409 * slotid4 csr_highest_slotid;
410 * slotid4 csr_target_highest_slotid;
411 * };
412 *
413 * union CB_SEQUENCE4res switch (nfsstat4 csr_status) {
414 * case NFS4_OK:
415 * CB_SEQUENCE4resok csr_resok4;
416 * default:
417 * void;
418 * };
419 *
420 * Our current back channel implmentation supports a single backchannel
421 * with a single slot.
422 */
423static int decode_cb_sequence4resok(struct xdr_stream *xdr,
424 struct nfsd4_callback *cb)
425{
426 struct nfsd4_session *session = cb->cb_clp->cl_cb_session;
427 struct nfs4_sessionid id;
428 int status;
429 __be32 *p;
430 u32 dummy;
431
432 status = -ESERVERFAULT;
433
434 /*
435 * If the server returns different values for sessionID, slotID or
436 * sequence number, the server is looney tunes.
437 */
Benny Halevy2c9c8f32011-02-22 14:43:22 -0800438 p = xdr_inline_decode(xdr, NFS4_MAX_SESSIONID_LEN + 4 + 4 + 4 + 4);
Chuck Lever85a56482010-12-14 14:57:32 +0000439 if (unlikely(p == NULL))
440 goto out_overflow;
441 memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
442 if (memcmp(id.data, session->se_sessionid.data,
443 NFS4_MAX_SESSIONID_LEN) != 0) {
444 dprintk("NFS: %s Invalid session id\n", __func__);
445 goto out;
446 }
447 p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
448
449 dummy = be32_to_cpup(p++);
450 if (dummy != session->se_cb_seq_nr) {
451 dprintk("NFS: %s Invalid sequence number\n", __func__);
452 goto out;
453 }
454
455 dummy = be32_to_cpup(p++);
456 if (dummy != 0) {
457 dprintk("NFS: %s Invalid slotid\n", __func__);
458 goto out;
459 }
460
461 /*
462 * FIXME: process highest slotid and target highest slotid
463 */
464 status = 0;
465out:
Benny Halevy54237322011-10-19 19:12:58 -0700466 if (status)
467 nfsd4_mark_cb_fault(cb->cb_clp, status);
Chuck Lever85a56482010-12-14 14:57:32 +0000468 return status;
469out_overflow:
470 print_overflow_msg(__func__, xdr);
471 return -EIO;
472}
473
474static int decode_cb_sequence4res(struct xdr_stream *xdr,
475 struct nfsd4_callback *cb)
476{
477 enum nfsstat4 nfserr;
478 int status;
479
480 if (cb->cb_minorversion == 0)
481 return 0;
482
483 status = decode_cb_op_status(xdr, OP_CB_SEQUENCE, &nfserr);
484 if (unlikely(status))
485 goto out;
486 if (unlikely(nfserr != NFS4_OK))
487 goto out_default;
488 status = decode_cb_sequence4resok(xdr, cb);
489out:
490 return status;
491out_default:
Benny Halevy0af3f812011-01-13 11:25:31 +0200492 return nfs_cb_stat_to_errno(nfserr);
Chuck Lever85a56482010-12-14 14:57:32 +0000493}
494
495/*
Chuck Levera033db42010-12-14 14:57:22 +0000496 * NFSv4.0 and NFSv4.1 XDR encode functions
497 *
498 * NFSv4.0 callback argument types are defined in section 15 of RFC
499 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
500 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
501 * Protocol".
502 */
503
504/*
505 * NB: Without this zero space reservation, callbacks over krb5p fail
506 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000507static void nfs4_xdr_enc_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
508 void *__unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509{
Chuck Levera033db42010-12-14 14:57:22 +0000510 xdr_reserve_space(xdr, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Chuck Levera033db42010-12-14 14:57:22 +0000513/*
514 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
515 */
Chuck Lever9f06c712010-12-14 14:59:18 +0000516static void nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, struct xdr_stream *xdr,
517 const struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Chuck Levera033db42010-12-14 14:57:22 +0000519 const struct nfs4_delegation *args = cb->cb_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct nfs4_cb_compound_hdr hdr = {
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400521 .ident = cb->cb_clp->cl_cb_ident,
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400522 .minorversion = cb->cb_minorversion,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 };
524
Chuck Lever9f06c712010-12-14 14:59:18 +0000525 encode_cb_compound4args(xdr, &hdr);
526 encode_cb_sequence4args(xdr, cb, &hdr);
527 encode_cb_recall4args(xdr, args, &hdr);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300528 encode_cb_nops(&hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
531
Chuck Lever85a56482010-12-14 14:57:32 +0000532/*
533 * NFSv4.0 and NFSv4.1 XDR decode functions
534 *
535 * NFSv4.0 callback result types are defined in section 15 of RFC
536 * 3530: "Network File System (NFS) version 4 Protocol" and section 20
537 * of RFC 5661: "Network File System (NFS) Version 4 Minor Version 1
538 * Protocol".
539 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Chuck Leverbf269552010-12-14 14:59:29 +0000541static int nfs4_xdr_dec_cb_null(struct rpc_rqst *req, struct xdr_stream *xdr,
542 void *__unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 0;
545}
546
Benny Halevy2af73582009-09-10 12:26:51 +0300547/*
Chuck Lever85a56482010-12-14 14:57:32 +0000548 * 20.2. Operation 4: CB_RECALL - Recall a Delegation
Benny Halevy2af73582009-09-10 12:26:51 +0300549 */
Chuck Leverbf269552010-12-14 14:59:29 +0000550static int nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp,
551 struct xdr_stream *xdr,
Chuck Lever85a56482010-12-14 14:57:32 +0000552 struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 struct nfs4_cb_compound_hdr hdr;
Chuck Lever85a56482010-12-14 14:57:32 +0000555 enum nfsstat4 nfserr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 int status;
557
Chuck Leverbf269552010-12-14 14:59:29 +0000558 status = decode_cb_compound4res(xdr, &hdr);
Chuck Lever85a56482010-12-14 14:57:32 +0000559 if (unlikely(status))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto out;
Chuck Lever85a56482010-12-14 14:57:32 +0000561
562 if (cb != NULL) {
Chuck Leverbf269552010-12-14 14:59:29 +0000563 status = decode_cb_sequence4res(xdr, cb);
Chuck Lever85a56482010-12-14 14:57:32 +0000564 if (unlikely(status))
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300565 goto out;
566 }
Chuck Lever85a56482010-12-14 14:57:32 +0000567
Chuck Leverbf269552010-12-14 14:59:29 +0000568 status = decode_cb_op_status(xdr, OP_CB_RECALL, &nfserr);
Chuck Lever85a56482010-12-14 14:57:32 +0000569 if (unlikely(status))
570 goto out;
571 if (unlikely(nfserr != NFS4_OK))
Benny Halevy0af3f812011-01-13 11:25:31 +0200572 status = nfs_cb_stat_to_errno(nfserr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573out:
574 return status;
575}
576
577/*
578 * RPC procedure tables
579 */
Chuck Lever7d93bd712010-12-14 14:57:42 +0000580#define PROC(proc, call, argtype, restype) \
581[NFSPROC4_CLNT_##proc] = { \
582 .p_proc = NFSPROC4_CB_##call, \
Chuck Lever9f06c712010-12-14 14:59:18 +0000583 .p_encode = (kxdreproc_t)nfs4_xdr_enc_##argtype, \
Chuck Leverbf269552010-12-14 14:59:29 +0000584 .p_decode = (kxdrdproc_t)nfs4_xdr_dec_##restype, \
Chuck Lever7d93bd712010-12-14 14:57:42 +0000585 .p_arglen = NFS4_enc_##argtype##_sz, \
586 .p_replen = NFS4_dec_##restype##_sz, \
587 .p_statidx = NFSPROC4_CB_##call, \
588 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Chuck Lever7d93bd712010-12-14 14:57:42 +0000591static struct rpc_procinfo nfs4_cb_procedures[] = {
592 PROC(CB_NULL, NULL, cb_null, cb_null),
593 PROC(CB_RECALL, COMPOUND, cb_recall, cb_recall),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594};
595
Chuck Lever7d93bd712010-12-14 14:57:42 +0000596static struct rpc_version nfs_cb_version4 = {
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400597/*
598 * Note on the callback rpc program version number: despite language in rfc
599 * 5661 section 18.36.3 requiring servers to use 4 in this field, the
600 * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
601 * in practice that appears to be what implementations use. The section
602 * 18.36.3 language is expected to be fixed in an erratum.
603 */
Chuck Lever7d93bd712010-12-14 14:57:42 +0000604 .number = 1,
605 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
606 .procs = nfs4_cb_procedures
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607};
608
Trond Myklebusta613fa12012-01-20 13:53:56 -0500609static const struct rpc_version *nfs_cb_version[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 &nfs_cb_version4,
611};
612
Trond Myklebusta613fa12012-01-20 13:53:56 -0500613static const struct rpc_program cb_program;
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400614
615static struct rpc_stat cb_stats = {
Chuck Lever7d93bd712010-12-14 14:57:42 +0000616 .program = &cb_program
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400617};
618
619#define NFS4_CALLBACK 0x40000000
Trond Myklebusta613fa12012-01-20 13:53:56 -0500620static const struct rpc_program cb_program = {
Chuck Lever7d93bd712010-12-14 14:57:42 +0000621 .name = "nfs4_cb",
622 .number = NFS4_CALLBACK,
623 .nrvers = ARRAY_SIZE(nfs_cb_version),
624 .version = nfs_cb_version,
625 .stats = &cb_stats,
Stanislav Kinsbursky0157d022012-01-11 19:18:01 +0400626 .pipe_dir_name = "nfsd4_cb",
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400627};
628
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +0300629static int max_cb_time(struct net *net)
J. Bruce Fields595947a2009-03-05 17:18:10 -0500630{
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +0300631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
632 return max(nn->nfsd4_lease/10, (time_t)1) * HZ;
J. Bruce Fields595947a2009-03-05 17:18:10 -0500633}
634
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400635static struct rpc_cred *callback_cred;
636
637int set_callback_cred(void)
638{
639 if (callback_cred)
640 return 0;
641 callback_cred = rpc_lookup_machine_cred("nfs");
642 if (!callback_cred)
643 return -ENOMEM;
644 return 0;
645}
646
Fengguang Wu2b4cf662012-11-13 15:41:27 -0500647static 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 -0400648{
649 if (clp->cl_minorversion == 0) {
650 return get_rpccred(callback_cred);
651 } else {
652 struct rpc_auth *auth = client->cl_auth;
653 struct auth_cred acred = {};
654
655 acred.uid = ses->se_cb_sec.uid;
656 acred.gid = ses->se_cb_sec.gid;
657 return auth->au_ops->lookup_cred(client->cl_auth, &acred, 0);
658 }
659}
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400660
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400661static 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 -0400662{
Chuck Leverae5c7942006-08-22 20:06:21 -0400663 struct rpc_timeout timeparms = {
Stanislav Kinsbursky3d733712012-11-27 14:11:44 +0300664 .to_initval = max_cb_time(clp->net),
J. Bruce Fields595947a2009-03-05 17:18:10 -0500665 .to_retries = 0,
Chuck Leverae5c7942006-08-22 20:06:21 -0400666 };
Chuck Leverae5c7942006-08-22 20:06:21 -0400667 struct rpc_create_args args = {
Stanislav Kinsburskyc212cec2012-11-14 18:21:10 +0300668 .net = clp->net,
J. Bruce Fields07263f12010-05-31 19:09:40 -0400669 .address = (struct sockaddr *) &conn->cb_addr,
670 .addrsize = conn->cb_addrlen,
Takuma Umeya6f3d7722010-12-15 14:09:01 +0900671 .saddress = (struct sockaddr *) &conn->cb_saddr,
Chuck Leverae5c7942006-08-22 20:06:21 -0400672 .timeout = &timeparms,
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400673 .program = &cb_program,
J. Bruce Fieldsb7299f42010-05-14 17:57:35 -0400674 .version = 0,
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400675 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
Chuck Leverae5c7942006-08-22 20:06:21 -0400676 };
J. Bruce Fields63c86712007-10-25 19:00:26 -0400677 struct rpc_clnt *client;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400678 struct rpc_cred *cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400680 if (clp->cl_minorversion == 0) {
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400681 if (!clp->cl_cred.cr_principal &&
J. Bruce Fields39307652012-08-16 17:01:21 -0400682 (clp->cl_cred.cr_flavor >= RPC_AUTH_GSS_KRB5))
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400683 return -EINVAL;
J. Bruce Fields03a4e1f2012-05-14 19:55:22 -0400684 args.client_name = clp->cl_cred.cr_principal;
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400685 args.prognumber = conn->cb_prog,
686 args.protocol = XPRT_TRANSPORT_TCP;
J. Bruce Fields39307652012-08-16 17:01:21 -0400687 args.authflavor = clp->cl_cred.cr_flavor;
J. Bruce Fields5d18c1c2010-10-19 23:00:12 -0400688 clp->cl_cb_ident = conn->cb_ident;
689 } else {
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400690 if (!conn->cb_xprt)
691 return -EINVAL;
692 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
693 clp->cl_cb_session = ses;
J. Bruce Fields07263f12010-05-31 19:09:40 -0400694 args.bc_xprt = conn->cb_xprt;
J. Bruce Fields8b5ce5c2010-10-19 17:31:50 -0400695 args.prognumber = clp->cl_cb_session->se_cb_prog;
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300696 args.protocol = XPRT_TRANSPORT_BC_TCP;
J. Bruce Fields12fc3e92012-11-05 16:01:48 -0500697 args.authflavor = ses->se_cb_sec.flavor;
Alexandros Batsakis3ddc8bf2009-09-10 12:27:21 +0300698 }
Chuck Leverae5c7942006-08-22 20:06:21 -0400699 /* Create RPC client */
J. Bruce Fields63c86712007-10-25 19:00:26 -0400700 client = rpc_create(&args);
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800701 if (IS_ERR(client)) {
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800702 dprintk("NFSD: couldn't create callback client: %ld\n",
703 PTR_ERR(client));
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800704 return PTR_ERR(client);
705 }
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400706 cred = get_backchannel_cred(clp, client, ses);
707 if (IS_ERR(cred)) {
708 rpc_shutdown_client(client);
709 return PTR_ERR(cred);
710 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400711 clp->cl_cb_client = client;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400712 clp->cl_cb_cred = cred;
J. Bruce Fieldse1cab5a52009-02-23 10:45:27 -0800713 return 0;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800714}
715
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800716static void warn_no_callback_path(struct nfs4_client *clp, int reason)
717{
718 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
719 (int)clp->cl_name.len, clp->cl_name.data, reason);
720}
721
J. Bruce Fields77a35692010-04-30 18:51:44 -0400722static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
723{
724 clp->cl_cb_state = NFSD4_CB_DOWN;
725 warn_no_callback_path(clp, reason);
726}
727
Benny Halevy54237322011-10-19 19:12:58 -0700728static void nfsd4_mark_cb_fault(struct nfs4_client *clp, int reason)
729{
730 clp->cl_cb_state = NFSD4_CB_FAULT;
731 warn_no_callback_path(clp, reason);
732}
733
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500734static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
735{
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400736 struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500737
738 if (task->tk_status)
J. Bruce Fields77a35692010-04-30 18:51:44 -0400739 nfsd4_mark_cb_down(clp, task->tk_status);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500740 else
J. Bruce Fields77a35692010-04-30 18:51:44 -0400741 clp->cl_cb_state = NFSD4_CB_UP;
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500742}
743
744static const struct rpc_call_ops nfsd4_cb_probe_ops = {
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400745 /* XXX: release method to ensure we set the cb channel down if
746 * necessary on early failure? */
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500747 .rpc_call_done = nfsd4_cb_probe_done,
748};
749
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400750static struct workqueue_struct *callback_wq;
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400751
J. Bruce Fields229b2a02010-12-10 17:37:44 -0500752static void run_nfsd4_cb(struct nfsd4_callback *cb)
753{
754 queue_work(callback_wq, &cb->cb_work);
755}
756
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400757static void do_probe_callback(struct nfs4_client *clp)
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800758{
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400759 struct nfsd4_callback *cb = &clp->cl_cb_null;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800760
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400761 cb->cb_op = NULL;
762 cb->cb_clp = clp;
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400763
764 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
765 cb->cb_msg.rpc_argp = NULL;
766 cb->cb_msg.rpc_resp = NULL;
J. Bruce Fieldscee277d2010-05-26 17:52:14 -0400767
768 cb->cb_ops = &nfsd4_cb_probe_ops;
769
J. Bruce Fields229b2a02010-12-10 17:37:44 -0500770 run_nfsd4_cb(cb);
J. Bruce Fields63c86712007-10-25 19:00:26 -0400771}
772
773/*
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400774 * Poke the callback thread to process any updates to the callback
775 * parameters, and send a null probe.
J. Bruce Fields63c86712007-10-25 19:00:26 -0400776 */
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400777void nfsd4_probe_callback(struct nfs4_client *clp)
778{
J. Bruce Fields77a35692010-04-30 18:51:44 -0400779 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
Jeff Laytona52d7262012-03-21 09:52:02 -0400780 set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400781 do_probe_callback(clp);
782}
783
J. Bruce Fields84f5f7c2010-12-09 15:52:19 -0500784void nfsd4_probe_callback_sync(struct nfs4_client *clp)
785{
786 nfsd4_probe_callback(clp);
787 flush_workqueue(callback_wq);
788}
789
J. Bruce Fields5a3c9d72010-10-19 17:56:52 -0400790void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
J. Bruce Fields63c86712007-10-25 19:00:26 -0400791{
J. Bruce Fields77a35692010-04-30 18:51:44 -0400792 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400793 spin_lock(&clp->cl_lock);
794 memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400795 spin_unlock(&clp->cl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300798/*
799 * There's currently a single callback channel slot.
800 * If the slot is available, then mark it busy. Otherwise, set the
801 * thread for sleeping on the callback RPC wait queue.
802 */
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500803static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300804{
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300805 if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
806 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
807 dprintk("%s slot is busy\n", __func__);
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500808 return false;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300809 }
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500810 return true;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300811}
812
813/*
814 * TODO: cb_sequence should support referring call lists, cachethis, multiple
815 * slots, and mark callback channel down on communication errors.
816 */
817static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
818{
J. Bruce Fields58784532010-05-16 16:47:08 -0400819 struct nfsd4_callback *cb = calldata;
820 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400821 struct nfs4_client *clp = dp->dl_stid.sc_client;
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400822 u32 minorversion = clp->cl_minorversion;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300823
J. Bruce Fieldsfb003922010-05-31 18:21:37 -0400824 cb->cb_minorversion = minorversion;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300825 if (minorversion) {
J. Bruce Fields3ff36002011-01-10 16:37:51 -0500826 if (!nfsd41_cb_get_slot(clp, task))
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300827 return;
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300828 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500829 spin_lock(&clp->cl_lock);
J. Bruce Fieldsa8f28002011-01-14 14:25:48 -0500830 if (list_empty(&cb->cb_per_client)) {
831 /* This is the first call, not a restart */
832 cb->cb_done = false;
833 list_add(&cb->cb_per_client, &clp->cl_callbacks);
834 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500835 spin_unlock(&clp->cl_lock);
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300836 rpc_call_start(task);
837}
838
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300839static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
840{
J. Bruce Fields58784532010-05-16 16:47:08 -0400841 struct nfsd4_callback *cb = calldata;
842 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400843 struct nfs4_client *clp = dp->dl_stid.sc_client;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300844
845 dprintk("%s: minorversion=%d\n", __func__,
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400846 clp->cl_minorversion);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300847
J. Bruce Fields8323c3b2010-10-19 19:36:51 -0400848 if (clp->cl_minorversion) {
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300849 /* No need for lock, access serialized in nfsd4_cb_prepare */
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400850 ++clp->cl_cb_session->se_cb_seq_nr;
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300851 clear_bit(0, &clp->cl_cb_slot_busy);
852 rpc_wake_up_next(&clp->cl_cb_waitq);
853 dprintk("%s: freed slot, new seqid=%d\n", __func__,
J. Bruce Fieldsac7c46f2010-06-14 19:01:57 -0400854 clp->cl_cb_session->se_cb_seq_nr);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300855
856 /* We're done looking into the sequence information */
857 task->tk_msg.rpc_resp = NULL;
858 }
859}
860
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500861
J. Bruce Fields63e48632009-05-01 22:36:55 -0400862static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
863{
J. Bruce Fields58784532010-05-16 16:47:08 -0400864 struct nfsd4_callback *cb = calldata;
865 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
J. Bruce Fields2a74aba2011-09-23 17:20:02 -0400866 struct nfs4_client *clp = dp->dl_stid.sc_client;
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500867 struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400868
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300869 nfsd4_cb_done(task, calldata);
870
J. Bruce Fieldsa8f28002011-01-14 14:25:48 -0500871 if (current_rpc_client != task->tk_client) {
872 /* We're shutting down or changing cl_cb_client; leave
873 * it to nfsd4_process_cb_update to restart the call if
874 * necessary. */
J. Bruce Fields4b21d0d2010-03-07 23:39:01 -0500875 return;
876 }
877
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500878 if (cb->cb_done)
879 return;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400880 switch (task->tk_status) {
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400881 case 0:
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500882 cb->cb_done = true;
J. Bruce Fields172c85d2010-05-30 11:53:12 -0400883 return;
884 case -EBADHANDLE:
885 case -NFS4ERR_BAD_STATEID:
886 /* Race: client probably got cb_recall
887 * before open reply granting delegation */
888 break;
889 default:
J. Bruce Fields63e48632009-05-01 22:36:55 -0400890 /* Network partition? */
J. Bruce Fields77a35692010-04-30 18:51:44 -0400891 nfsd4_mark_cb_down(clp, task->tk_status);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400892 }
893 if (dp->dl_retries--) {
894 rpc_delay(task, 2*HZ);
895 task->tk_status = 0;
Boaz Harroshc18c8212010-06-29 14:33:55 +0300896 rpc_restart_call_prepare(task);
Ricardo Labiaga0421b5c2009-09-10 12:27:04 +0300897 return;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400898 }
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500899 nfsd4_mark_cb_down(clp, task->tk_status);
900 cb->cb_done = true;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400901}
902
903static void nfsd4_cb_recall_release(void *calldata)
904{
J. Bruce Fields58784532010-05-16 16:47:08 -0400905 struct nfsd4_callback *cb = calldata;
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500906 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields58784532010-05-16 16:47:08 -0400907 struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
J. Bruce Fields63e48632009-05-01 22:36:55 -0400908
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -0500909 if (cb->cb_done) {
910 spin_lock(&clp->cl_lock);
911 list_del(&cb->cb_per_client);
912 spin_unlock(&clp->cl_lock);
913 nfs4_put_delegation(dp);
914 }
J. Bruce Fields63e48632009-05-01 22:36:55 -0400915}
916
917static const struct rpc_call_ops nfsd4_cb_recall_ops = {
Ricardo Labiaga2a1d1b52009-09-10 12:26:38 +0300918 .rpc_call_prepare = nfsd4_cb_prepare,
J. Bruce Fields63e48632009-05-01 22:36:55 -0400919 .rpc_call_done = nfsd4_cb_recall_done,
920 .rpc_release = nfsd4_cb_recall_release,
921};
922
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500923int nfsd4_create_callback_queue(void)
924{
925 callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
926 if (!callback_wq)
927 return -ENOMEM;
928 return 0;
929}
930
931void nfsd4_destroy_callback_queue(void)
932{
933 destroy_workqueue(callback_wq);
934}
935
Benny Halevyab707e152010-05-12 00:14:06 +0300936/* must be called under the state lock */
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400937void nfsd4_shutdown_callback(struct nfs4_client *clp)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500938{
Jeff Laytona52d7262012-03-21 09:52:02 -0400939 set_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500940 /*
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400941 * Note this won't actually result in a null callback;
942 * instead, nfsd4_do_callback_rpc() will detect the killed
943 * client, destroy the rpc client, and stop:
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500944 */
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400945 do_probe_callback(clp);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500946 flush_workqueue(callback_wq);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500947}
948
Kirill A. Shutemov65e4c892010-12-16 15:25:54 +0200949static void nfsd4_release_cb(struct nfsd4_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
J. Bruce Fields58784532010-05-16 16:47:08 -0400951 if (cb->cb_ops->rpc_release)
952 cb->cb_ops->rpc_release(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -0500954
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400955/* requires cl_lock: */
956static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
957{
958 struct nfsd4_session *s;
959 struct nfsd4_conn *c;
960
961 list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
962 list_for_each_entry(c, &s->se_conns, cn_persession) {
963 if (c->cn_flags & NFS4_CDFC4_BACK)
964 return c;
965 }
966 }
967 return NULL;
968}
969
Kirill A. Shutemov65e4c892010-12-16 15:25:54 +0200970static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400971{
972 struct nfs4_cb_conn conn;
973 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400974 struct nfsd4_session *ses = NULL;
975 struct nfsd4_conn *c;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400976 int err;
977
978 /*
979 * This is either an update, or the client dying; in either case,
980 * kill the old client:
981 */
982 if (clp->cl_cb_client) {
983 rpc_shutdown_client(clp->cl_cb_client);
984 clp->cl_cb_client = NULL;
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -0400985 put_rpccred(clp->cl_cb_cred);
986 clp->cl_cb_cred = NULL;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400987 }
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -0400988 if (clp->cl_cb_conn.cb_xprt) {
989 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
990 clp->cl_cb_conn.cb_xprt = NULL;
991 }
Jeff Laytona52d7262012-03-21 09:52:02 -0400992 if (test_bit(NFSD4_CLIENT_CB_KILL, &clp->cl_flags))
J. Bruce Fields6ff8da02010-06-04 20:04:45 -0400993 return;
994 spin_lock(&clp->cl_lock);
995 /*
996 * Only serialized callback code is allowed to clear these
997 * flags; main nfsd code can only set them:
998 */
Jeff Laytona52d7262012-03-21 09:52:02 -0400999 BUG_ON(!(clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK));
1000 clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_flags);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001001 memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001002 c = __nfsd4_find_backchannel(clp);
1003 if (c) {
1004 svc_xprt_get(c->cn_xprt);
1005 conn.cb_xprt = c->cn_xprt;
1006 ses = c->cn_session;
1007 }
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001008 spin_unlock(&clp->cl_lock);
1009
J. Bruce Fieldsdcbeaa62010-06-15 17:25:45 -04001010 err = setup_callback_client(clp, &conn, ses);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001011 if (err) {
J. Bruce Fields8546ee52012-03-09 17:02:28 -05001012 nfsd4_mark_cb_down(clp, err);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001013 return;
1014 }
1015 /* Yay, the callback channel's back! Restart any callbacks: */
1016 list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
1017 run_nfsd4_cb(cb);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001018}
1019
J. Bruce Fields57725152012-11-05 15:10:26 -05001020static void nfsd4_do_callback_rpc(struct work_struct *w)
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001021{
J. Bruce Fields58784532010-05-16 16:47:08 -04001022 struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
J. Bruce Fieldsfb003922010-05-31 18:21:37 -04001023 struct nfs4_client *clp = cb->cb_clp;
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001024 struct rpc_clnt *clnt;
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001025
Jeff Laytona52d7262012-03-21 09:52:02 -04001026 if (clp->cl_flags & NFSD4_CLIENT_CB_FLAG_MASK)
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001027 nfsd4_process_cb_update(cb);
1028
1029 clnt = clp->cl_cb_client;
1030 if (!clnt) {
1031 /* Callback channel broken, or client killed; give up: */
J. Bruce Fields58784532010-05-16 16:47:08 -04001032 nfsd4_release_cb(cb);
J. Bruce Fields6ff8da02010-06-04 20:04:45 -04001033 return;
J. Bruce Fields58784532010-05-16 16:47:08 -04001034 }
J. Bruce Fieldsc6bb3ca2012-11-01 16:31:02 -04001035 cb->cb_msg.rpc_cred = clp->cl_cb_cred;
J. Bruce Fieldscee277d2010-05-26 17:52:14 -04001036 rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
1037 cb->cb_ops, cb);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001038}
1039
J. Bruce Fields57725152012-11-05 15:10:26 -05001040void nfsd4_init_callback(struct nfsd4_callback *cb)
1041{
1042 INIT_WORK(&cb->cb_work, nfsd4_do_callback_rpc);
1043}
1044
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001045void nfsd4_cb_recall(struct nfs4_delegation *dp)
1046{
J. Bruce Fields58784532010-05-16 16:47:08 -04001047 struct nfsd4_callback *cb = &dp->dl_recall;
J. Bruce Fields2a74aba2011-09-23 17:20:02 -04001048 struct nfs4_client *clp = dp->dl_stid.sc_client;
J. Bruce Fields58784532010-05-16 16:47:08 -04001049
1050 dp->dl_retries = 1;
J. Bruce Fieldsfb003922010-05-31 18:21:37 -04001051 cb->cb_op = dp;
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001052 cb->cb_clp = clp;
J. Bruce Fields58784532010-05-16 16:47:08 -04001053 cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
J. Bruce Fieldsfb003922010-05-31 18:21:37 -04001054 cb->cb_msg.rpc_argp = cb;
1055 cb->cb_msg.rpc_resp = cb;
J. Bruce Fields58784532010-05-16 16:47:08 -04001056
1057 cb->cb_ops = &nfsd4_cb_recall_ops;
J. Bruce Fields58784532010-05-16 16:47:08 -04001058
J. Bruce Fields9ee1ba52011-01-13 17:08:19 -05001059 INIT_LIST_HEAD(&cb->cb_per_client);
J. Bruce Fields5ce8ba22011-01-10 16:44:41 -05001060 cb->cb_done = true;
1061
J. Bruce Fields229b2a02010-12-10 17:37:44 -05001062 run_nfsd4_cb(&dp->dl_recall);
J. Bruce Fieldsb5a1a812010-03-03 14:52:55 -05001063}