blob: db4188ce9b000d74c76cd09e3f5d1296d0329268 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfsd/nfs4callback.c
3 *
4 * Copyright (c) 2001 The Regents of the University of Michigan.
5 * All rights reserved.
6 *
7 * Kendrick Smith <kmsmith@umich.edu>
8 * Andy Adamson <andros@umich.edu>
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/module.h>
37#include <linux/list.h>
38#include <linux/inet.h>
39#include <linux/errno.h>
40#include <linux/delay.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040041#include <linux/sched.h>
J. Bruce Fields2b47eec2007-07-27 18:06:50 -040042#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/sunrpc/xdr.h>
44#include <linux/sunrpc/svc.h>
45#include <linux/sunrpc/clnt.h>
Andy Adamson38524ab2009-09-10 12:25:59 +030046#include <linux/sunrpc/svcsock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/nfsd/nfsd.h>
48#include <linux/nfsd/state.h>
49#include <linux/sunrpc/sched.h>
50#include <linux/nfs4.h>
51
52#define NFSDDBG_FACILITY NFSDDBG_PROC
53
54#define NFSPROC4_CB_NULL 0
55#define NFSPROC4_CB_COMPOUND 1
Andy Adamson38524ab2009-09-10 12:25:59 +030056#define NFS4_STATEID_SIZE 16
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/* Index of predefined Linux callback client operations */
59
60enum {
Benny Halevy4be36ca2009-09-10 12:25:46 +030061 NFSPROC4_CLNT_CB_NULL = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 NFSPROC4_CLNT_CB_RECALL,
Andy Adamson38524ab2009-09-10 12:25:59 +030063 NFSPROC4_CLNT_CB_SEQUENCE,
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66enum nfs_cb_opnum4 {
67 OP_CB_RECALL = 4,
Andy Adamson38524ab2009-09-10 12:25:59 +030068 OP_CB_SEQUENCE = 11,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71#define NFS4_MAXTAGLEN 20
72
73#define NFS4_enc_cb_null_sz 0
74#define NFS4_dec_cb_null_sz 0
75#define cb_compound_enc_hdr_sz 4
76#define cb_compound_dec_hdr_sz (3 + (NFS4_MAXTAGLEN >> 2))
Andy Adamson38524ab2009-09-10 12:25:59 +030077#define sessionid_sz (NFS4_MAX_SESSIONID_LEN >> 2)
78#define cb_sequence_enc_sz (sessionid_sz + 4 + \
79 1 /* no referring calls list yet */)
80#define cb_sequence_dec_sz (op_dec_sz + sessionid_sz + 4)
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082#define op_enc_sz 1
83#define op_dec_sz 2
84#define enc_nfs4_fh_sz (1 + (NFS4_FHSIZE >> 2))
Benny Halevy0ac68d12007-07-17 04:04:37 -070085#define enc_stateid_sz (NFS4_STATEID_SIZE >> 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#define NFS4_enc_cb_recall_sz (cb_compound_enc_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030087 cb_sequence_enc_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 1 + enc_stateid_sz + \
89 enc_nfs4_fh_sz)
90
91#define NFS4_dec_cb_recall_sz (cb_compound_dec_hdr_sz + \
Andy Adamson38524ab2009-09-10 12:25:59 +030092 cb_sequence_dec_sz + \
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 op_dec_sz)
94
95/*
96* Generic encode routines from fs/nfs/nfs4xdr.c
97*/
Al Virof00f3282006-10-19 23:29:01 -070098static inline __be32 *
99xdr_writemem(__be32 *p, const void *ptr, int nbytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 int tmp = XDR_QUADLEN(nbytes);
102 if (!tmp)
103 return p;
104 p[tmp-1] = 0;
105 memcpy(p, ptr, nbytes);
106 return p + tmp;
107}
108
109#define WRITE32(n) *p++ = htonl(n)
110#define WRITEMEM(ptr,nbytes) do { \
111 p = xdr_writemem(p, ptr, nbytes); \
112} while (0)
113#define RESERVE_SPACE(nbytes) do { \
114 p = xdr_reserve_space(xdr, nbytes); \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700115 if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 BUG_ON(!p); \
117} while (0)
118
119/*
120 * Generic decode routines from fs/nfs/nfs4xdr.c
121 */
122#define DECODE_TAIL \
123 status = 0; \
124out: \
125 return status; \
126xdr_error: \
127 dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
128 status = -EIO; \
129 goto out
130
131#define READ32(x) (x) = ntohl(*p++)
132#define READ64(x) do { \
133 (x) = (u64)ntohl(*p++) << 32; \
134 (x) |= ntohl(*p++); \
135} while (0)
136#define READTIME(x) do { \
137 p++; \
138 (x.tv_sec) = ntohl(*p++); \
139 (x.tv_nsec) = ntohl(*p++); \
140} while (0)
141#define READ_BUF(nbytes) do { \
142 p = xdr_inline_decode(xdr, nbytes); \
143 if (!p) { \
Greg Banks3e3b4802006-10-02 02:17:41 -0700144 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
Harvey Harrison8e24eea2008-04-30 00:55:09 -0700145 __func__, __LINE__); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EIO; \
147 } \
148} while (0)
149
150struct nfs4_cb_compound_hdr {
Andy Adamson38524ab2009-09-10 12:25:59 +0300151 /* args */
152 u32 ident; /* minorversion 0 only */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 u32 nops;
Andy Adamsonef52bff2009-06-16 04:20:50 +0300154 __be32 *nops_p;
Andy Adamsonab52ae62009-06-16 04:20:53 +0300155 u32 minorversion;
Andy Adamson38524ab2009-09-10 12:25:59 +0300156 /* res */
157 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 u32 taglen;
Andy Adamsonef52bff2009-06-16 04:20:50 +0300159 char *tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160};
161
162static struct {
163int stat;
164int errno;
165} nfs_cb_errtbl[] = {
166 { NFS4_OK, 0 },
167 { NFS4ERR_PERM, EPERM },
168 { NFS4ERR_NOENT, ENOENT },
169 { NFS4ERR_IO, EIO },
170 { NFS4ERR_NXIO, ENXIO },
171 { NFS4ERR_ACCESS, EACCES },
172 { NFS4ERR_EXIST, EEXIST },
173 { NFS4ERR_XDEV, EXDEV },
174 { NFS4ERR_NOTDIR, ENOTDIR },
175 { NFS4ERR_ISDIR, EISDIR },
176 { NFS4ERR_INVAL, EINVAL },
177 { NFS4ERR_FBIG, EFBIG },
178 { NFS4ERR_NOSPC, ENOSPC },
179 { NFS4ERR_ROFS, EROFS },
180 { NFS4ERR_MLINK, EMLINK },
181 { NFS4ERR_NAMETOOLONG, ENAMETOOLONG },
182 { NFS4ERR_NOTEMPTY, ENOTEMPTY },
183 { NFS4ERR_DQUOT, EDQUOT },
184 { NFS4ERR_STALE, ESTALE },
185 { NFS4ERR_BADHANDLE, EBADHANDLE },
186 { NFS4ERR_BAD_COOKIE, EBADCOOKIE },
187 { NFS4ERR_NOTSUPP, ENOTSUPP },
188 { NFS4ERR_TOOSMALL, ETOOSMALL },
189 { NFS4ERR_SERVERFAULT, ESERVERFAULT },
190 { NFS4ERR_BADTYPE, EBADTYPE },
191 { NFS4ERR_LOCKED, EAGAIN },
192 { NFS4ERR_RESOURCE, EREMOTEIO },
193 { NFS4ERR_SYMLINK, ELOOP },
194 { NFS4ERR_OP_ILLEGAL, EOPNOTSUPP },
195 { NFS4ERR_DEADLOCK, EDEADLK },
196 { -1, EIO }
197};
198
199static int
200nfs_cb_stat_to_errno(int stat)
201{
202 int i;
203 for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
204 if (nfs_cb_errtbl[i].stat == stat)
205 return nfs_cb_errtbl[i].errno;
206 }
207 /* If we cannot translate the error, the recovery routines should
208 * handle it.
209 * Note: remaining NFSv4 error codes have values > 10000, so should
210 * not conflict with native Linux error codes.
211 */
212 return stat;
213}
214
215/*
216 * XDR encode
217 */
218
Andy Adamsonef52bff2009-06-16 04:20:50 +0300219static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
221{
Al Virof00f3282006-10-19 23:29:01 -0700222 __be32 * p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
224 RESERVE_SPACE(16);
225 WRITE32(0); /* tag length is always 0 */
Andy Adamsonab52ae62009-06-16 04:20:53 +0300226 WRITE32(hdr->minorversion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 WRITE32(hdr->ident);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300228 hdr->nops_p = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 WRITE32(hdr->nops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Andy Adamsonef52bff2009-06-16 04:20:50 +0300232static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
233{
234 *hdr->nops_p = htonl(hdr->nops);
235}
236
237static void
238encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
239 struct nfs4_cb_compound_hdr *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Al Virof00f3282006-10-19 23:29:01 -0700241 __be32 *p;
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400242 int len = dp->dl_fh.fh_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400244 RESERVE_SPACE(12+sizeof(dp->dl_stateid) + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 WRITE32(OP_CB_RECALL);
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400246 WRITE32(dp->dl_stateid.si_generation);
247 WRITEMEM(&dp->dl_stateid.si_opaque, sizeof(stateid_opaque_t));
J. Bruce Fields6707bd32009-05-01 19:57:46 -0400248 WRITE32(0); /* truncate optimization not implemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 WRITE32(len);
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400250 WRITEMEM(&dp->dl_fh.fh_base, len);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300251 hdr->nops++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252}
253
254static int
Al Virof00f3282006-10-19 23:29:01 -0700255nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct xdr_stream xdrs, *xdr = &xdrs;
258
259 xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
260 RESERVE_SPACE(0);
261 return 0;
262}
263
264static int
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400265nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p, struct nfs4_delegation *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 struct xdr_stream xdr;
268 struct nfs4_cb_compound_hdr hdr = {
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400269 .ident = args->dl_ident,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 };
271
272 xdr_init_encode(&xdr, &req->rq_snd_buf, p);
273 encode_cb_compound_hdr(&xdr, &hdr);
Andy Adamsonef52bff2009-06-16 04:20:50 +0300274 encode_cb_recall(&xdr, args, &hdr);
275 encode_cb_nops(&hdr);
276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279
280static int
281decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
Al Virof00f3282006-10-19 23:29:01 -0700282 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 READ_BUF(8);
285 READ32(hdr->status);
286 READ32(hdr->taglen);
287 READ_BUF(hdr->taglen + 4);
288 hdr->tag = (char *)p;
289 p += XDR_QUADLEN(hdr->taglen);
290 READ32(hdr->nops);
291 return 0;
292}
293
294static int
295decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
296{
Al Virof00f3282006-10-19 23:29:01 -0700297 __be32 *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 u32 op;
299 int32_t nfserr;
300
301 READ_BUF(8);
302 READ32(op);
303 if (op != expected) {
304 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
305 " operation %d but we issued a request for %d\n",
306 op, expected);
307 return -EIO;
308 }
309 READ32(nfserr);
310 if (nfserr != NFS_OK)
311 return -nfs_cb_stat_to_errno(nfserr);
312 return 0;
313}
314
315static int
Al Virof00f3282006-10-19 23:29:01 -0700316nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317{
318 return 0;
319}
320
321static int
Al Virof00f3282006-10-19 23:29:01 -0700322nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 struct xdr_stream xdr;
325 struct nfs4_cb_compound_hdr hdr;
326 int status;
327
328 xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
329 status = decode_cb_compound_hdr(&xdr, &hdr);
330 if (status)
331 goto out;
332 status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
333out:
334 return status;
335}
336
337/*
338 * RPC procedure tables
339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340#define PROC(proc, call, argtype, restype) \
341[NFSPROC4_CLNT_##proc] = { \
342 .p_proc = NFSPROC4_CB_##call, \
343 .p_encode = (kxdrproc_t) nfs4_xdr_##argtype, \
344 .p_decode = (kxdrproc_t) nfs4_xdr_##restype, \
Chuck Lever2bea90d2007-03-29 16:47:53 -0400345 .p_arglen = NFS4_##argtype##_sz, \
346 .p_replen = NFS4_##restype##_sz, \
Chuck Levercc0175c2006-03-20 13:44:22 -0500347 .p_statidx = NFSPROC4_CB_##call, \
348 .p_name = #proc, \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
NeilBrownfd39ca92005-06-23 22:04:03 -0700351static struct rpc_procinfo nfs4_cb_procedures[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 PROC(CB_NULL, NULL, enc_cb_null, dec_cb_null),
353 PROC(CB_RECALL, COMPOUND, enc_cb_recall, dec_cb_recall),
354};
355
NeilBrownfd39ca92005-06-23 22:04:03 -0700356static struct rpc_version nfs_cb_version4 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 .number = 1,
Tobias Klausere8c96f82006-03-24 03:15:34 -0800358 .nrprocs = ARRAY_SIZE(nfs4_cb_procedures),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .procs = nfs4_cb_procedures
360};
361
362static struct rpc_version * nfs_cb_version[] = {
363 NULL,
364 &nfs_cb_version4,
365};
366
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400367static struct rpc_program cb_program;
368
369static struct rpc_stat cb_stats = {
370 .program = &cb_program
371};
372
373#define NFS4_CALLBACK 0x40000000
374static struct rpc_program cb_program = {
375 .name = "nfs4_cb",
376 .number = NFS4_CALLBACK,
377 .nrvers = ARRAY_SIZE(nfs_cb_version),
378 .version = nfs_cb_version,
379 .stats = &cb_stats,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500380 .pipe_dir_name = "/nfsd4_cb",
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400381};
382
J. Bruce Fields595947a2009-03-05 17:18:10 -0500383static int max_cb_time(void)
384{
385 return max(NFSD_LEASE_TIME/10, (time_t)1) * HZ;
386}
387
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400388/* Reference counting, callback cleanup, etc., all look racy as heck.
389 * And why is cb_set an atomic? */
390
J. Bruce Fieldse1cab5a2009-02-23 10:45:27 -0800391int setup_callback_client(struct nfs4_client *clp)
J. Bruce Fields2b47eec2007-07-27 18:06:50 -0400392{
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400393 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
Chuck Leverae5c7942006-08-22 20:06:21 -0400394 struct rpc_timeout timeparms = {
J. Bruce Fields595947a2009-03-05 17:18:10 -0500395 .to_initval = max_cb_time(),
396 .to_retries = 0,
Chuck Leverae5c7942006-08-22 20:06:21 -0400397 };
Chuck Leverae5c7942006-08-22 20:06:21 -0400398 struct rpc_create_args args = {
399 .protocol = IPPROTO_TCP,
Jeff Laytonaa9a4ec2009-08-14 12:57:57 -0400400 .address = (struct sockaddr *) &cb->cb_addr,
401 .addrsize = cb->cb_addrlen,
Chuck Leverae5c7942006-08-22 20:06:21 -0400402 .timeout = &timeparms,
Olga Kornievskaiaff7d9752008-03-28 16:04:56 -0400403 .program = &cb_program,
Benny Halevyd5b337b2008-09-28 09:21:26 +0300404 .prognumber = cb->cb_prog,
Chuck Leverae5c7942006-08-22 20:06:21 -0400405 .version = nfs_cb_version[1]->number,
Olga Kornievskaia61054b12008-12-23 16:19:00 -0500406 .authflavor = clp->cl_flavor,
Olga Kornievskaiab6b61522008-06-09 16:51:31 -0400407 .flags = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500408 .client_name = clp->cl_principal,
Chuck Leverae5c7942006-08-22 20:06:21 -0400409 };
J. Bruce Fields63c86712007-10-25 19:00:26 -0400410 struct rpc_clnt *client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
J. Bruce Fields418cd202009-02-22 15:52:13 -0800412 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
J. Bruce Fieldse1cab5a2009-02-23 10:45:27 -0800413 return -EINVAL;
Olga Kornievskaia608207e2008-12-23 16:17:40 -0500414
Chuck Leverae5c7942006-08-22 20:06:21 -0400415 /* Create RPC client */
J. Bruce Fields63c86712007-10-25 19:00:26 -0400416 client = rpc_create(&args);
J. Bruce Fieldse1cab5a2009-02-23 10:45:27 -0800417 if (IS_ERR(client)) {
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800418 dprintk("NFSD: couldn't create callback client: %ld\n",
419 PTR_ERR(client));
J. Bruce Fieldse1cab5a2009-02-23 10:45:27 -0800420 return PTR_ERR(client);
421 }
422 cb->cb_client = client;
423 return 0;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800424
425}
426
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800427static void warn_no_callback_path(struct nfs4_client *clp, int reason)
428{
429 dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
430 (int)clp->cl_name.len, clp->cl_name.data, reason);
431}
432
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500433static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
434{
435 struct nfs4_client *clp = calldata;
436
437 if (task->tk_status)
438 warn_no_callback_path(clp, task->tk_status);
439 else
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400440 atomic_set(&clp->cl_cb_conn.cb_set, 1);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500441 put_nfs4_client(clp);
442}
443
444static const struct rpc_call_ops nfsd4_cb_probe_ops = {
445 .rpc_call_done = nfsd4_cb_probe_done,
446};
447
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400448static struct rpc_cred *callback_cred;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800449
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400450int set_callback_cred(void)
451{
452 callback_cred = rpc_lookup_machine_cred();
453 if (!callback_cred)
454 return -ENOMEM;
455 return 0;
J. Bruce Fields3cef9ab2009-02-23 21:42:10 -0800456}
457
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400458
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500459void do_probe_callback(struct nfs4_client *clp)
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800460{
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400461 struct nfs4_cb_conn *cb = &clp->cl_cb_conn;
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800462 struct rpc_message msg = {
463 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL],
464 .rpc_argp = clp,
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400465 .rpc_cred = callback_cred
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800466 };
J. Bruce Fieldsa601cae2009-02-22 16:43:45 -0800467 int status;
468
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500469 status = rpc_call_async(cb->cb_client, &msg, RPC_TASK_SOFT,
470 &nfsd4_cb_probe_ops, (void *)clp);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500471 if (status) {
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800472 warn_no_callback_path(clp, status);
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500473 put_nfs4_client(clp);
474 }
J. Bruce Fields63c86712007-10-25 19:00:26 -0400475}
476
477/*
478 * Set up the callback client and put a NFSPROC4_CB_NULL on the wire...
479 */
480void
481nfsd4_probe_callback(struct nfs4_client *clp)
482{
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800483 int status;
J. Bruce Fields63c86712007-10-25 19:00:26 -0400484
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400485 BUG_ON(atomic_read(&clp->cl_cb_conn.cb_set));
J. Bruce Fields63c86712007-10-25 19:00:26 -0400486
J. Bruce Fieldsecdd03b2009-02-23 19:35:22 -0800487 status = setup_callback_client(clp);
488 if (status) {
489 warn_no_callback_path(clp, status);
490 return;
491 }
492
NeilBrown541e0e02006-04-10 22:55:38 -0700493 /* the task holds a reference to the nfs4_client struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 atomic_inc(&clp->cl_count);
495
J. Bruce Fieldse300a632009-03-05 15:01:11 -0500496 do_probe_callback(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
J. Bruce Fields63e48632009-05-01 22:36:55 -0400499static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
500{
501 struct nfs4_delegation *dp = calldata;
502 struct nfs4_client *clp = dp->dl_client;
503
504 switch (task->tk_status) {
505 case -EIO:
506 /* Network partition? */
507 atomic_set(&clp->cl_cb_conn.cb_set, 0);
508 warn_no_callback_path(clp, task->tk_status);
509 case -EBADHANDLE:
510 case -NFS4ERR_BAD_STATEID:
511 /* Race: client probably got cb_recall
512 * before open reply granting delegation */
513 break;
514 default:
515 /* success, or error we can't handle */
516 return;
517 }
518 if (dp->dl_retries--) {
519 rpc_delay(task, 2*HZ);
520 task->tk_status = 0;
521 rpc_restart_call(task);
522 } else {
523 atomic_set(&clp->cl_cb_conn.cb_set, 0);
524 warn_no_callback_path(clp, task->tk_status);
525 }
526}
527
528static void nfsd4_cb_recall_release(void *calldata)
529{
530 struct nfs4_delegation *dp = calldata;
531 struct nfs4_client *clp = dp->dl_client;
532
533 nfs4_put_delegation(dp);
534 put_nfs4_client(clp);
535}
536
537static const struct rpc_call_ops nfsd4_cb_recall_ops = {
538 .rpc_call_done = nfsd4_cb_recall_done,
539 .rpc_release = nfsd4_cb_recall_release,
540};
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542/*
543 * called with dp->dl_count inc'ed.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545void
546nfsd4_cb_recall(struct nfs4_delegation *dp)
547{
548 struct nfs4_client *clp = dp->dl_client;
J. Bruce Fieldsc237dc02009-04-29 19:09:19 -0400549 struct rpc_clnt *clnt = clp->cl_cb_conn.cb_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 struct rpc_message msg = {
551 .rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL],
J. Bruce Fieldsb53d40c2009-05-01 19:50:00 -0400552 .rpc_argp = dp,
J. Bruce Fields80fc0152009-09-15 18:07:35 -0400553 .rpc_cred = callback_cred
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 };
J. Bruce Fields63e48632009-05-01 22:36:55 -0400555 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
J. Bruce Fields3aea09d2009-05-01 20:11:12 -0400557 dp->dl_retries = 1;
J. Bruce Fields63e48632009-05-01 22:36:55 -0400558 status = rpc_call_async(clnt, &msg, RPC_TASK_SOFT,
559 &nfsd4_cb_recall_ops, dp);
560 if (status) {
561 put_nfs4_client(clp);
562 nfs4_put_delegation(dp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}