nfsd4: initialize cb_per_client
[pandora-kernel.git] / fs / nfsd / nfs4callback.c
1 /*
2  *  Copyright (c) 2001 The Regents of the University of Michigan.
3  *  All rights reserved.
4  *
5  *  Kendrick Smith <kmsmith@umich.edu>
6  *  Andy Adamson <andros@umich.edu>
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions
10  *  are met:
11  *
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of the University nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <linux/sunrpc/clnt.h>
35 #include <linux/sunrpc/svc_xprt.h>
36 #include <linux/slab.h>
37 #include "nfsd.h"
38 #include "state.h"
39
40 #define NFSDDBG_FACILITY                NFSDDBG_PROC
41
42 #define NFSPROC4_CB_NULL 0
43 #define NFSPROC4_CB_COMPOUND 1
44
45 /* Index of predefined Linux callback client operations */
46
47 enum {
48         NFSPROC4_CLNT_CB_NULL = 0,
49         NFSPROC4_CLNT_CB_RECALL,
50         NFSPROC4_CLNT_CB_SEQUENCE,
51 };
52
53 enum nfs_cb_opnum4 {
54         OP_CB_RECALL            = 4,
55         OP_CB_SEQUENCE          = 11,
56 };
57
58 #define NFS4_MAXTAGLEN          20
59
60 #define NFS4_enc_cb_null_sz             0
61 #define NFS4_dec_cb_null_sz             0
62 #define cb_compound_enc_hdr_sz          4
63 #define cb_compound_dec_hdr_sz          (3 + (NFS4_MAXTAGLEN >> 2))
64 #define sessionid_sz                    (NFS4_MAX_SESSIONID_LEN >> 2)
65 #define cb_sequence_enc_sz              (sessionid_sz + 4 +             \
66                                         1 /* no referring calls list yet */)
67 #define cb_sequence_dec_sz              (op_dec_sz + sessionid_sz + 4)
68
69 #define op_enc_sz                       1
70 #define op_dec_sz                       2
71 #define enc_nfs4_fh_sz                  (1 + (NFS4_FHSIZE >> 2))
72 #define enc_stateid_sz                  (NFS4_STATEID_SIZE >> 2)
73 #define NFS4_enc_cb_recall_sz           (cb_compound_enc_hdr_sz +       \
74                                         cb_sequence_enc_sz +            \
75                                         1 + enc_stateid_sz +            \
76                                         enc_nfs4_fh_sz)
77
78 #define NFS4_dec_cb_recall_sz           (cb_compound_dec_hdr_sz  +      \
79                                         cb_sequence_dec_sz +            \
80                                         op_dec_sz)
81
82 /*
83 * Generic encode routines from fs/nfs/nfs4xdr.c
84 */
85 static inline __be32 *
86 xdr_writemem(__be32 *p, const void *ptr, int nbytes)
87 {
88         int tmp = XDR_QUADLEN(nbytes);
89         if (!tmp)
90                 return p;
91         p[tmp-1] = 0;
92         memcpy(p, ptr, nbytes);
93         return p + tmp;
94 }
95
96 #define WRITE32(n)               *p++ = htonl(n)
97 #define WRITEMEM(ptr,nbytes)     do {                           \
98         p = xdr_writemem(p, ptr, nbytes);                       \
99 } while (0)
100 #define RESERVE_SPACE(nbytes)   do {                            \
101         p = xdr_reserve_space(xdr, nbytes);                     \
102         if (!p) dprintk("NFSD: RESERVE_SPACE(%d) failed in function %s\n", (int) (nbytes), __func__); \
103         BUG_ON(!p);                                             \
104 } while (0)
105
106 /*
107  * Generic decode routines from fs/nfs/nfs4xdr.c
108  */
109 #define DECODE_TAIL                             \
110         status = 0;                             \
111 out:                                            \
112         return status;                          \
113 xdr_error:                                      \
114         dprintk("NFSD: xdr error! (%s:%d)\n", __FILE__, __LINE__); \
115         status = -EIO;                          \
116         goto out
117
118 #define READ32(x)         (x) = ntohl(*p++)
119 #define READ64(x)         do {                  \
120         (x) = (u64)ntohl(*p++) << 32;           \
121         (x) |= ntohl(*p++);                     \
122 } while (0)
123 #define READTIME(x)       do {                  \
124         p++;                                    \
125         (x.tv_sec) = ntohl(*p++);               \
126         (x.tv_nsec) = ntohl(*p++);              \
127 } while (0)
128 #define READ_BUF(nbytes)  do { \
129         p = xdr_inline_decode(xdr, nbytes); \
130         if (!p) { \
131                 dprintk("NFSD: %s: reply buffer overflowed in line %d.\n", \
132                         __func__, __LINE__); \
133                 return -EIO; \
134         } \
135 } while (0)
136
137 struct nfs4_cb_compound_hdr {
138         /* args */
139         u32             ident;  /* minorversion 0 only */
140         u32             nops;
141         __be32          *nops_p;
142         u32             minorversion;
143         /* res */
144         int             status;
145 };
146
147 static struct {
148 int stat;
149 int errno;
150 } nfs_cb_errtbl[] = {
151         { NFS4_OK,              0               },
152         { NFS4ERR_PERM,         EPERM           },
153         { NFS4ERR_NOENT,        ENOENT          },
154         { NFS4ERR_IO,           EIO             },
155         { NFS4ERR_NXIO,         ENXIO           },
156         { NFS4ERR_ACCESS,       EACCES          },
157         { NFS4ERR_EXIST,        EEXIST          },
158         { NFS4ERR_XDEV,         EXDEV           },
159         { NFS4ERR_NOTDIR,       ENOTDIR         },
160         { NFS4ERR_ISDIR,        EISDIR          },
161         { NFS4ERR_INVAL,        EINVAL          },
162         { NFS4ERR_FBIG,         EFBIG           },
163         { NFS4ERR_NOSPC,        ENOSPC          },
164         { NFS4ERR_ROFS,         EROFS           },
165         { NFS4ERR_MLINK,        EMLINK          },
166         { NFS4ERR_NAMETOOLONG,  ENAMETOOLONG    },
167         { NFS4ERR_NOTEMPTY,     ENOTEMPTY       },
168         { NFS4ERR_DQUOT,        EDQUOT          },
169         { NFS4ERR_STALE,        ESTALE          },
170         { NFS4ERR_BADHANDLE,    EBADHANDLE      },
171         { NFS4ERR_BAD_COOKIE,   EBADCOOKIE      },
172         { NFS4ERR_NOTSUPP,      ENOTSUPP        },
173         { NFS4ERR_TOOSMALL,     ETOOSMALL       },
174         { NFS4ERR_SERVERFAULT,  ESERVERFAULT    },
175         { NFS4ERR_BADTYPE,      EBADTYPE        },
176         { NFS4ERR_LOCKED,       EAGAIN          },
177         { NFS4ERR_RESOURCE,     EREMOTEIO       },
178         { NFS4ERR_SYMLINK,      ELOOP           },
179         { NFS4ERR_OP_ILLEGAL,   EOPNOTSUPP      },
180         { NFS4ERR_DEADLOCK,     EDEADLK         },
181         { -1,                   EIO             }
182 };
183
184 static int
185 nfs_cb_stat_to_errno(int stat)
186 {
187         int i;
188         for (i = 0; nfs_cb_errtbl[i].stat != -1; i++) {
189                 if (nfs_cb_errtbl[i].stat == stat)
190                         return nfs_cb_errtbl[i].errno;
191         }
192         /* If we cannot translate the error, the recovery routines should
193         * handle it.
194         * Note: remaining NFSv4 error codes have values > 10000, so should
195         * not conflict with native Linux error codes.
196         */
197         return stat;
198 }
199
200 /*
201  * XDR encode
202  */
203
204 static void
205 encode_stateid(struct xdr_stream *xdr, stateid_t *sid)
206 {
207         __be32 *p;
208
209         RESERVE_SPACE(sizeof(stateid_t));
210         WRITE32(sid->si_generation);
211         WRITEMEM(&sid->si_opaque, sizeof(stateid_opaque_t));
212 }
213
214 static void
215 encode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr)
216 {
217         __be32 * p;
218
219         RESERVE_SPACE(16);
220         WRITE32(0);            /* tag length is always 0 */
221         WRITE32(hdr->minorversion);
222         WRITE32(hdr->ident);
223         hdr->nops_p = p;
224         WRITE32(hdr->nops);
225 }
226
227 static void encode_cb_nops(struct nfs4_cb_compound_hdr *hdr)
228 {
229         *hdr->nops_p = htonl(hdr->nops);
230 }
231
232 static void
233 encode_cb_recall(struct xdr_stream *xdr, struct nfs4_delegation *dp,
234                 struct nfs4_cb_compound_hdr *hdr)
235 {
236         __be32 *p;
237         int len = dp->dl_fh.fh_size;
238
239         RESERVE_SPACE(4);
240         WRITE32(OP_CB_RECALL);
241         encode_stateid(xdr, &dp->dl_stateid);
242         RESERVE_SPACE(8 + (XDR_QUADLEN(len) << 2));
243         WRITE32(0); /* truncate optimization not implemented */
244         WRITE32(len);
245         WRITEMEM(&dp->dl_fh.fh_base, len);
246         hdr->nops++;
247 }
248
249 static void
250 encode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_callback *cb,
251                    struct nfs4_cb_compound_hdr *hdr)
252 {
253         __be32 *p;
254         struct nfsd4_session *ses = cb->cb_clp->cl_cb_session;
255
256         if (hdr->minorversion == 0)
257                 return;
258
259         RESERVE_SPACE(1 + NFS4_MAX_SESSIONID_LEN + 20);
260
261         WRITE32(OP_CB_SEQUENCE);
262         WRITEMEM(ses->se_sessionid.data, NFS4_MAX_SESSIONID_LEN);
263         WRITE32(ses->se_cb_seq_nr);
264         WRITE32(0);             /* slotid, always 0 */
265         WRITE32(0);             /* highest slotid always 0 */
266         WRITE32(0);             /* cachethis always 0 */
267         WRITE32(0); /* FIXME: support referring_call_lists */
268         hdr->nops++;
269 }
270
271 static int
272 nfs4_xdr_enc_cb_null(struct rpc_rqst *req, __be32 *p)
273 {
274         struct xdr_stream xdrs, *xdr = &xdrs;
275
276         xdr_init_encode(&xdrs, &req->rq_snd_buf, p);
277         RESERVE_SPACE(0);
278         return 0;
279 }
280
281 static int
282 nfs4_xdr_enc_cb_recall(struct rpc_rqst *req, __be32 *p,
283                 struct nfsd4_callback *cb)
284 {
285         struct xdr_stream xdr;
286         struct nfs4_delegation *args = cb->cb_op;
287         struct nfs4_cb_compound_hdr hdr = {
288                 .ident = cb->cb_clp->cl_cb_ident,
289                 .minorversion = cb->cb_minorversion,
290         };
291
292         xdr_init_encode(&xdr, &req->rq_snd_buf, p);
293         encode_cb_compound_hdr(&xdr, &hdr);
294         encode_cb_sequence(&xdr, cb, &hdr);
295         encode_cb_recall(&xdr, args, &hdr);
296         encode_cb_nops(&hdr);
297         return 0;
298 }
299
300
301 static int
302 decode_cb_compound_hdr(struct xdr_stream *xdr, struct nfs4_cb_compound_hdr *hdr){
303         __be32 *p;
304         u32 taglen;
305
306         READ_BUF(8);
307         READ32(hdr->status);
308         /* We've got no use for the tag; ignore it: */
309         READ32(taglen);
310         READ_BUF(taglen + 4);
311         p += XDR_QUADLEN(taglen);
312         READ32(hdr->nops);
313         return 0;
314 }
315
316 static int
317 decode_cb_op_hdr(struct xdr_stream *xdr, enum nfs_opnum4 expected)
318 {
319         __be32 *p;
320         u32 op;
321         int32_t nfserr;
322
323         READ_BUF(8);
324         READ32(op);
325         if (op != expected) {
326                 dprintk("NFSD: decode_cb_op_hdr: Callback server returned "
327                          " operation %d but we issued a request for %d\n",
328                          op, expected);
329                 return -EIO;
330         }
331         READ32(nfserr);
332         if (nfserr != NFS_OK)
333                 return -nfs_cb_stat_to_errno(nfserr);
334         return 0;
335 }
336
337 /*
338  * Our current back channel implmentation supports a single backchannel
339  * with a single slot.
340  */
341 static int
342 decode_cb_sequence(struct xdr_stream *xdr, struct nfsd4_callback *cb,
343                    struct rpc_rqst *rqstp)
344 {
345         struct nfsd4_session *ses = cb->cb_clp->cl_cb_session;
346         struct nfs4_sessionid id;
347         int status;
348         u32 dummy;
349         __be32 *p;
350
351         if (cb->cb_minorversion == 0)
352                 return 0;
353
354         status = decode_cb_op_hdr(xdr, OP_CB_SEQUENCE);
355         if (status)
356                 return status;
357
358         /*
359          * If the server returns different values for sessionID, slotID or
360          * sequence number, the server is looney tunes.
361          */
362         status = -ESERVERFAULT;
363
364         READ_BUF(NFS4_MAX_SESSIONID_LEN + 16);
365         memcpy(id.data, p, NFS4_MAX_SESSIONID_LEN);
366         p += XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN);
367         if (memcmp(id.data, ses->se_sessionid.data, NFS4_MAX_SESSIONID_LEN)) {
368                 dprintk("%s Invalid session id\n", __func__);
369                 goto out;
370         }
371         READ32(dummy);
372         if (dummy != ses->se_cb_seq_nr) {
373                 dprintk("%s Invalid sequence number\n", __func__);
374                 goto out;
375         }
376         READ32(dummy);  /* slotid must be 0 */
377         if (dummy != 0) {
378                 dprintk("%s Invalid slotid\n", __func__);
379                 goto out;
380         }
381         /* FIXME: process highest slotid and target highest slotid */
382         status = 0;
383 out:
384         return status;
385 }
386
387
388 static int
389 nfs4_xdr_dec_cb_null(struct rpc_rqst *req, __be32 *p)
390 {
391         return 0;
392 }
393
394 static int
395 nfs4_xdr_dec_cb_recall(struct rpc_rqst *rqstp, __be32 *p,
396                 struct nfsd4_callback *cb)
397 {
398         struct xdr_stream xdr;
399         struct nfs4_cb_compound_hdr hdr;
400         int status;
401
402         xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, p);
403         status = decode_cb_compound_hdr(&xdr, &hdr);
404         if (status)
405                 goto out;
406         if (cb) {
407                 status = decode_cb_sequence(&xdr, cb, rqstp);
408                 if (status)
409                         goto out;
410         }
411         status = decode_cb_op_hdr(&xdr, OP_CB_RECALL);
412 out:
413         return status;
414 }
415
416 /*
417  * RPC procedure tables
418  */
419 #define PROC(proc, call, argtype, restype)                              \
420 [NFSPROC4_CLNT_##proc] = {                                              \
421         .p_proc   = NFSPROC4_CB_##call,                                 \
422         .p_encode = (kxdrproc_t) nfs4_xdr_##argtype,                    \
423         .p_decode = (kxdrproc_t) nfs4_xdr_##restype,                    \
424         .p_arglen = NFS4_##argtype##_sz,                                \
425         .p_replen = NFS4_##restype##_sz,                                \
426         .p_statidx = NFSPROC4_CB_##call,                                \
427         .p_name   = #proc,                                              \
428 }
429
430 static struct rpc_procinfo     nfs4_cb_procedures[] = {
431     PROC(CB_NULL,      NULL,     enc_cb_null,     dec_cb_null),
432     PROC(CB_RECALL,    COMPOUND,   enc_cb_recall,      dec_cb_recall),
433 };
434
435 static struct rpc_version       nfs_cb_version4 = {
436 /*
437  * Note on the callback rpc program version number: despite language in rfc
438  * 5661 section 18.36.3 requiring servers to use 4 in this field, the
439  * official xdr descriptions for both 4.0 and 4.1 specify version 1, and
440  * in practice that appears to be what implementations use.  The section
441  * 18.36.3 language is expected to be fixed in an erratum.
442  */
443         .number                 = 1,
444         .nrprocs                = ARRAY_SIZE(nfs4_cb_procedures),
445         .procs                  = nfs4_cb_procedures
446 };
447
448 static struct rpc_version *     nfs_cb_version[] = {
449         &nfs_cb_version4,
450 };
451
452 static struct rpc_program cb_program;
453
454 static struct rpc_stat cb_stats = {
455                 .program        = &cb_program
456 };
457
458 #define NFS4_CALLBACK 0x40000000
459 static struct rpc_program cb_program = {
460                 .name           = "nfs4_cb",
461                 .number         = NFS4_CALLBACK,
462                 .nrvers         = ARRAY_SIZE(nfs_cb_version),
463                 .version        = nfs_cb_version,
464                 .stats          = &cb_stats,
465                 .pipe_dir_name  = "/nfsd4_cb",
466 };
467
468 static int max_cb_time(void)
469 {
470         return max(nfsd4_lease/10, (time_t)1) * HZ;
471 }
472
473
474 static int setup_callback_client(struct nfs4_client *clp, struct nfs4_cb_conn *conn, struct nfsd4_session *ses)
475 {
476         struct rpc_timeout      timeparms = {
477                 .to_initval     = max_cb_time(),
478                 .to_retries     = 0,
479         };
480         struct rpc_create_args args = {
481                 .net            = &init_net,
482                 .address        = (struct sockaddr *) &conn->cb_addr,
483                 .addrsize       = conn->cb_addrlen,
484                 .saddress       = (struct sockaddr *) &conn->cb_saddr,
485                 .timeout        = &timeparms,
486                 .program        = &cb_program,
487                 .version        = 0,
488                 .authflavor     = clp->cl_flavor,
489                 .flags          = (RPC_CLNT_CREATE_NOPING | RPC_CLNT_CREATE_QUIET),
490         };
491         struct rpc_clnt *client;
492
493         if (clp->cl_minorversion == 0) {
494                 if (!clp->cl_principal && (clp->cl_flavor >= RPC_AUTH_GSS_KRB5))
495                         return -EINVAL;
496                 args.client_name = clp->cl_principal;
497                 args.prognumber = conn->cb_prog,
498                 args.protocol = XPRT_TRANSPORT_TCP;
499                 clp->cl_cb_ident = conn->cb_ident;
500         } else {
501                 if (!conn->cb_xprt)
502                         return -EINVAL;
503                 clp->cl_cb_conn.cb_xprt = conn->cb_xprt;
504                 clp->cl_cb_session = ses;
505                 args.bc_xprt = conn->cb_xprt;
506                 args.prognumber = clp->cl_cb_session->se_cb_prog;
507                 args.protocol = XPRT_TRANSPORT_BC_TCP;
508         }
509         /* Create RPC client */
510         client = rpc_create(&args);
511         if (IS_ERR(client)) {
512                 dprintk("NFSD: couldn't create callback client: %ld\n",
513                         PTR_ERR(client));
514                 return PTR_ERR(client);
515         }
516         clp->cl_cb_client = client;
517         return 0;
518
519 }
520
521 static void warn_no_callback_path(struct nfs4_client *clp, int reason)
522 {
523         dprintk("NFSD: warning: no callback path to client %.*s: error %d\n",
524                 (int)clp->cl_name.len, clp->cl_name.data, reason);
525 }
526
527 static void nfsd4_mark_cb_down(struct nfs4_client *clp, int reason)
528 {
529         clp->cl_cb_state = NFSD4_CB_DOWN;
530         warn_no_callback_path(clp, reason);
531 }
532
533 static void nfsd4_cb_probe_done(struct rpc_task *task, void *calldata)
534 {
535         struct nfs4_client *clp = container_of(calldata, struct nfs4_client, cl_cb_null);
536
537         if (task->tk_status)
538                 nfsd4_mark_cb_down(clp, task->tk_status);
539         else
540                 clp->cl_cb_state = NFSD4_CB_UP;
541 }
542
543 static const struct rpc_call_ops nfsd4_cb_probe_ops = {
544         /* XXX: release method to ensure we set the cb channel down if
545          * necessary on early failure? */
546         .rpc_call_done = nfsd4_cb_probe_done,
547 };
548
549 static struct rpc_cred *callback_cred;
550
551 int set_callback_cred(void)
552 {
553         if (callback_cred)
554                 return 0;
555         callback_cred = rpc_lookup_machine_cred();
556         if (!callback_cred)
557                 return -ENOMEM;
558         return 0;
559 }
560
561 static struct workqueue_struct *callback_wq;
562
563 static void run_nfsd4_cb(struct nfsd4_callback *cb)
564 {
565         queue_work(callback_wq, &cb->cb_work);
566 }
567
568 static void do_probe_callback(struct nfs4_client *clp)
569 {
570         struct nfsd4_callback *cb = &clp->cl_cb_null;
571
572         cb->cb_op = NULL;
573         cb->cb_clp = clp;
574
575         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_NULL];
576         cb->cb_msg.rpc_argp = NULL;
577         cb->cb_msg.rpc_resp = NULL;
578         cb->cb_msg.rpc_cred = callback_cred;
579
580         cb->cb_ops = &nfsd4_cb_probe_ops;
581
582         run_nfsd4_cb(cb);
583 }
584
585 /*
586  * Poke the callback thread to process any updates to the callback
587  * parameters, and send a null probe.
588  */
589 void nfsd4_probe_callback(struct nfs4_client *clp)
590 {
591         /* XXX: atomicity?  Also, should we be using cl_cb_flags? */
592         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
593         set_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
594         do_probe_callback(clp);
595 }
596
597 void nfsd4_probe_callback_sync(struct nfs4_client *clp)
598 {
599         nfsd4_probe_callback(clp);
600         flush_workqueue(callback_wq);
601 }
602
603 void nfsd4_change_callback(struct nfs4_client *clp, struct nfs4_cb_conn *conn)
604 {
605         clp->cl_cb_state = NFSD4_CB_UNKNOWN;
606         spin_lock(&clp->cl_lock);
607         memcpy(&clp->cl_cb_conn, conn, sizeof(struct nfs4_cb_conn));
608         spin_unlock(&clp->cl_lock);
609 }
610
611 /*
612  * There's currently a single callback channel slot.
613  * If the slot is available, then mark it busy.  Otherwise, set the
614  * thread for sleeping on the callback RPC wait queue.
615  */
616 static bool nfsd41_cb_get_slot(struct nfs4_client *clp, struct rpc_task *task)
617 {
618         if (test_and_set_bit(0, &clp->cl_cb_slot_busy) != 0) {
619                 rpc_sleep_on(&clp->cl_cb_waitq, task, NULL);
620                 dprintk("%s slot is busy\n", __func__);
621                 return false;
622         }
623         return true;
624 }
625
626 /*
627  * TODO: cb_sequence should support referring call lists, cachethis, multiple
628  * slots, and mark callback channel down on communication errors.
629  */
630 static void nfsd4_cb_prepare(struct rpc_task *task, void *calldata)
631 {
632         struct nfsd4_callback *cb = calldata;
633         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
634         struct nfs4_client *clp = dp->dl_client;
635         u32 minorversion = clp->cl_minorversion;
636
637         cb->cb_minorversion = minorversion;
638         if (minorversion) {
639                 if (!nfsd41_cb_get_slot(clp, task))
640                         return;
641         }
642         cb->cb_done = false;
643         spin_lock(&clp->cl_lock);
644         list_add(&cb->cb_per_client, &clp->cl_callbacks);
645         spin_unlock(&clp->cl_lock);
646         rpc_call_start(task);
647 }
648
649 static void nfsd4_cb_done(struct rpc_task *task, void *calldata)
650 {
651         struct nfsd4_callback *cb = calldata;
652         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
653         struct nfs4_client *clp = dp->dl_client;
654
655         dprintk("%s: minorversion=%d\n", __func__,
656                 clp->cl_minorversion);
657
658         if (clp->cl_minorversion) {
659                 /* No need for lock, access serialized in nfsd4_cb_prepare */
660                 ++clp->cl_cb_session->se_cb_seq_nr;
661                 clear_bit(0, &clp->cl_cb_slot_busy);
662                 rpc_wake_up_next(&clp->cl_cb_waitq);
663                 dprintk("%s: freed slot, new seqid=%d\n", __func__,
664                         clp->cl_cb_session->se_cb_seq_nr);
665
666                 /* We're done looking into the sequence information */
667                 task->tk_msg.rpc_resp = NULL;
668         }
669 }
670
671
672 static void nfsd4_cb_recall_done(struct rpc_task *task, void *calldata)
673 {
674         struct nfsd4_callback *cb = calldata;
675         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
676         struct nfs4_client *clp = dp->dl_client;
677         struct rpc_clnt *current_rpc_client = clp->cl_cb_client;
678
679         nfsd4_cb_done(task, calldata);
680
681         if (current_rpc_client == NULL) {
682                 /* We're shutting down; give up. */
683                 /* XXX: err, or is it ok just to fall through
684                  * and rpc_restart_call? */
685                 return;
686         }
687
688         if (cb->cb_done)
689                 return;
690         switch (task->tk_status) {
691         case 0:
692                 cb->cb_done = true;
693                 return;
694         case -EBADHANDLE:
695         case -NFS4ERR_BAD_STATEID:
696                 /* Race: client probably got cb_recall
697                  * before open reply granting delegation */
698                 break;
699         default:
700                 /* Network partition? */
701                 nfsd4_mark_cb_down(clp, task->tk_status);
702                 if (current_rpc_client != task->tk_client) {
703                         /* queue a callback on the new connection: */
704                         atomic_inc(&dp->dl_count);
705                         run_nfsd4_cb(&dp->dl_recall);
706                         return;
707                 }
708         }
709         if (dp->dl_retries--) {
710                 rpc_delay(task, 2*HZ);
711                 task->tk_status = 0;
712                 rpc_restart_call_prepare(task);
713                 return;
714         }
715         nfsd4_mark_cb_down(clp, task->tk_status);
716         cb->cb_done = true;
717 }
718
719 static void nfsd4_cb_recall_release(void *calldata)
720 {
721         struct nfsd4_callback *cb = calldata;
722         struct nfs4_client *clp = cb->cb_clp;
723         struct nfs4_delegation *dp = container_of(cb, struct nfs4_delegation, dl_recall);
724
725         if (cb->cb_done) {
726                 spin_lock(&clp->cl_lock);
727                 list_del(&cb->cb_per_client);
728                 spin_unlock(&clp->cl_lock);
729                 nfs4_put_delegation(dp);
730         }
731 }
732
733 static const struct rpc_call_ops nfsd4_cb_recall_ops = {
734         .rpc_call_prepare = nfsd4_cb_prepare,
735         .rpc_call_done = nfsd4_cb_recall_done,
736         .rpc_release = nfsd4_cb_recall_release,
737 };
738
739 int nfsd4_create_callback_queue(void)
740 {
741         callback_wq = create_singlethread_workqueue("nfsd4_callbacks");
742         if (!callback_wq)
743                 return -ENOMEM;
744         return 0;
745 }
746
747 void nfsd4_destroy_callback_queue(void)
748 {
749         destroy_workqueue(callback_wq);
750 }
751
752 /* must be called under the state lock */
753 void nfsd4_shutdown_callback(struct nfs4_client *clp)
754 {
755         set_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags);
756         /*
757          * Note this won't actually result in a null callback;
758          * instead, nfsd4_do_callback_rpc() will detect the killed
759          * client, destroy the rpc client, and stop:
760          */
761         do_probe_callback(clp);
762         flush_workqueue(callback_wq);
763 }
764
765 static void nfsd4_release_cb(struct nfsd4_callback *cb)
766 {
767         if (cb->cb_ops->rpc_release)
768                 cb->cb_ops->rpc_release(cb);
769 }
770
771 /* requires cl_lock: */
772 static struct nfsd4_conn * __nfsd4_find_backchannel(struct nfs4_client *clp)
773 {
774         struct nfsd4_session *s;
775         struct nfsd4_conn *c;
776
777         list_for_each_entry(s, &clp->cl_sessions, se_perclnt) {
778                 list_for_each_entry(c, &s->se_conns, cn_persession) {
779                         if (c->cn_flags & NFS4_CDFC4_BACK)
780                                 return c;
781                 }
782         }
783         return NULL;
784 }
785
786 static void nfsd4_process_cb_update(struct nfsd4_callback *cb)
787 {
788         struct nfs4_cb_conn conn;
789         struct nfs4_client *clp = cb->cb_clp;
790         struct nfsd4_session *ses = NULL;
791         struct nfsd4_conn *c;
792         int err;
793
794         /*
795          * This is either an update, or the client dying; in either case,
796          * kill the old client:
797          */
798         if (clp->cl_cb_client) {
799                 rpc_shutdown_client(clp->cl_cb_client);
800                 clp->cl_cb_client = NULL;
801         }
802         if (clp->cl_cb_conn.cb_xprt) {
803                 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
804                 clp->cl_cb_conn.cb_xprt = NULL;
805         }
806         if (test_bit(NFSD4_CLIENT_KILL, &clp->cl_cb_flags))
807                 return;
808         spin_lock(&clp->cl_lock);
809         /*
810          * Only serialized callback code is allowed to clear these
811          * flags; main nfsd code can only set them:
812          */
813         BUG_ON(!clp->cl_cb_flags);
814         clear_bit(NFSD4_CLIENT_CB_UPDATE, &clp->cl_cb_flags);
815         memcpy(&conn, &cb->cb_clp->cl_cb_conn, sizeof(struct nfs4_cb_conn));
816         c = __nfsd4_find_backchannel(clp);
817         if (c) {
818                 svc_xprt_get(c->cn_xprt);
819                 conn.cb_xprt = c->cn_xprt;
820                 ses = c->cn_session;
821         }
822         spin_unlock(&clp->cl_lock);
823
824         err = setup_callback_client(clp, &conn, ses);
825         if (err) {
826                 warn_no_callback_path(clp, err);
827                 return;
828         }
829         /* Yay, the callback channel's back! Restart any callbacks: */
830         list_for_each_entry(cb, &clp->cl_callbacks, cb_per_client)
831                 run_nfsd4_cb(cb);
832 }
833
834 void nfsd4_do_callback_rpc(struct work_struct *w)
835 {
836         struct nfsd4_callback *cb = container_of(w, struct nfsd4_callback, cb_work);
837         struct nfs4_client *clp = cb->cb_clp;
838         struct rpc_clnt *clnt;
839
840         if (clp->cl_cb_flags)
841                 nfsd4_process_cb_update(cb);
842
843         clnt = clp->cl_cb_client;
844         if (!clnt) {
845                 /* Callback channel broken, or client killed; give up: */
846                 nfsd4_release_cb(cb);
847                 return;
848         }
849         rpc_call_async(clnt, &cb->cb_msg, RPC_TASK_SOFT | RPC_TASK_SOFTCONN,
850                         cb->cb_ops, cb);
851 }
852
853 void nfsd4_cb_recall(struct nfs4_delegation *dp)
854 {
855         struct nfsd4_callback *cb = &dp->dl_recall;
856         struct nfs4_client *clp = dp->dl_client;
857
858         dp->dl_retries = 1;
859         cb->cb_op = dp;
860         cb->cb_clp = clp;
861         cb->cb_msg.rpc_proc = &nfs4_cb_procedures[NFSPROC4_CLNT_CB_RECALL];
862         cb->cb_msg.rpc_argp = cb;
863         cb->cb_msg.rpc_resp = cb;
864         cb->cb_msg.rpc_cred = callback_cred;
865
866         cb->cb_ops = &nfsd4_cb_recall_ops;
867         dp->dl_retries = 1;
868
869         INIT_LIST_HEAD(&cb->cb_per_client);
870         cb->cb_done = true;
871
872         run_nfsd4_cb(&dp->dl_recall);
873 }