1177633Sdfr/*-
2177633Sdfr * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3177633Sdfr * Authors: Doug Rabson <dfr@rabson.org>
4177633Sdfr * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
5177633Sdfr *
6177633Sdfr * Redistribution and use in source and binary forms, with or without
7177633Sdfr * modification, are permitted provided that the following conditions
8177633Sdfr * are met:
9177633Sdfr * 1. Redistributions of source code must retain the above copyright
10177633Sdfr *    notice, this list of conditions and the following disclaimer.
11177633Sdfr * 2. Redistributions in binary form must reproduce the above copyright
12177633Sdfr *    notice, this list of conditions and the following disclaimer in the
13177633Sdfr *    documentation and/or other materials provided with the distribution.
14177633Sdfr *
15177633Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16177633Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17177633Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18177633Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19177633Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20177633Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21177633Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22177633Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23177633Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24177633Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25177633Sdfr * SUCH DAMAGE.
26177633Sdfr *
27177633Sdfr * $FreeBSD$
28177633Sdfr */
29177633Sdfr
30177633Sdfr#ifndef _NLM_NLM_H_
31177633Sdfr#define _NLM_NLM_H_
32177633Sdfr
33177633Sdfr#ifdef _KERNEL
34177633Sdfr
35177633Sdfr#ifdef _SYS_MALLOC_H_
36177633SdfrMALLOC_DECLARE(M_NLM);
37177633Sdfr#endif
38177633Sdfr
39180025Sdfr/*
40180025Sdfr * This value is added to host system IDs when recording NFS client
41180025Sdfr * locks in the local lock manager.
42180025Sdfr */
43180025Sdfr#define NLM_SYSID_CLIENT	0x1000000
44180025Sdfr
45177633Sdfrstruct nlm_host;
46180025Sdfrstruct vnode;
47177633Sdfr
48180025Sdfrextern struct timeval nlm_zero_tv;
49180025Sdfrextern int nlm_nsm_state;
50180025Sdfr
51177633Sdfr/*
52197840Szml * Make a struct netobj.
53197840Szml */
54197840Szmlextern void nlm_make_netobj(struct netobj *dst, caddr_t srt,
55197840Szml    size_t srcsize, struct malloc_type *type);
56197840Szml
57197840Szml/*
58177633Sdfr * Copy a struct netobj.
59177633Sdfr */
60177633Sdfrextern void nlm_copy_netobj(struct netobj *dst, struct netobj *src,
61177633Sdfr    struct malloc_type *type);
62177633Sdfr
63177633Sdfr/*
64177633Sdfr * Search for an existing NLM host that matches the given name
65177633Sdfr * (typically the caller_name element of an nlm4_lock).  If none is
66180025Sdfr * found, create a new host. If 'addr' is non-NULL, record the remote
67177633Sdfr * address of the host so that we can call it back for async
68180025Sdfr * responses. If 'vers' is greater than zero then record the NLM
69180025Sdfr * program version to use to communicate with this client. The host
70180025Sdfr * reference count is incremented - the caller must call
71180025Sdfr * nlm_host_release when it has finished using it.
72177633Sdfr */
73177633Sdfrextern struct nlm_host *nlm_find_host_by_name(const char *name,
74180025Sdfr    const struct sockaddr *addr, rpcvers_t vers);
75177633Sdfr
76177633Sdfr/*
77177633Sdfr * Search for an existing NLM host that matches the given remote
78177633Sdfr * address. If none is found, create a new host with the requested
79177633Sdfr * address and remember 'vers' as the NLM protocol version to use for
80180025Sdfr * that host. The host reference count is incremented - the caller
81180025Sdfr * must call nlm_host_release when it has finished using it.
82177633Sdfr */
83177633Sdfrextern struct nlm_host *nlm_find_host_by_addr(const struct sockaddr *addr,
84177633Sdfr    int vers);
85177633Sdfr
86177633Sdfr/*
87180025Sdfr * Register this NLM host with the local NSM so that we can be
88180025Sdfr * notified if it reboots.
89180025Sdfr */
90180025Sdfrextern void nlm_host_monitor(struct nlm_host *host, int state);
91180025Sdfr
92180025Sdfr/*
93180025Sdfr * Decrement the host reference count, freeing resources if the
94180025Sdfr * reference count reaches zero.
95180025Sdfr */
96180025Sdfrextern void nlm_host_release(struct nlm_host *host);
97180025Sdfr
98180025Sdfr/*
99177633Sdfr * Return an RPC client handle that can be used to talk to the NLM
100177633Sdfr * running on the given host.
101177633Sdfr */
102184588Sdfrextern CLIENT *nlm_host_get_rpc(struct nlm_host *host, bool_t isserver);
103177633Sdfr
104177633Sdfr/*
105180025Sdfr * Return the system ID for a host.
106180025Sdfr */
107180025Sdfrextern int nlm_host_get_sysid(struct nlm_host *host);
108180025Sdfr
109180025Sdfr/*
110180025Sdfr * Return the remote NSM state value for a host.
111180025Sdfr */
112180025Sdfrextern int nlm_host_get_state(struct nlm_host *host);
113180025Sdfr
114180025Sdfr/*
115180025Sdfr * When sending a blocking lock request, we need to track the request
116180025Sdfr * in our waiting lock list. We add an entry to the waiting list
117180025Sdfr * before we send the lock RPC so that we can cope with a granted
118180025Sdfr * message arriving at any time. Call this function before sending the
119180025Sdfr * lock rpc. If the lock succeeds, call nlm_deregister_wait_lock with
120180025Sdfr * the handle this function returns, otherwise nlm_wait_lock. Both
121180025Sdfr * will remove the entry from the waiting list.
122180025Sdfr */
123180025Sdfrextern void *nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp);
124180025Sdfr
125180025Sdfr/*
126180025Sdfr * Deregister a blocking lock request. Call this if the lock succeeded
127180025Sdfr * without blocking.
128180025Sdfr */
129180025Sdfrextern void nlm_deregister_wait_lock(void *handle);
130180025Sdfr
131180025Sdfr/*
132180025Sdfr * Wait for a granted callback for a blocked lock request, waiting at
133180025Sdfr * most timo ticks. If no granted message is received within the
134180025Sdfr * timeout, return EWOULDBLOCK. If a signal interrupted the wait,
135180025Sdfr * return EINTR - the caller must arrange to send a cancellation to
136180025Sdfr * the server. In both cases, the request is removed from the waiting
137180025Sdfr * list.
138180025Sdfr */
139180025Sdfrextern int nlm_wait_lock(void *handle, int timo);
140180025Sdfr
141180025Sdfr/*
142180025Sdfr * Cancel any pending waits for this vnode - called on forcible unmounts.
143180025Sdfr */
144180025Sdfrextern void nlm_cancel_wait(struct vnode *vp);
145180025Sdfr
146180025Sdfr/*
147177633Sdfr * Called when a host restarts.
148177633Sdfr */
149177633Sdfrextern void nlm_sm_notify(nlm_sm_status *argp);
150177633Sdfr
151177633Sdfr/*
152180025Sdfr * Implementation for lock testing RPCs. If the request was handled
153180025Sdfr * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
154180025Sdfr * handle which can be used to send an async rpc reply. Returns zero
155180025Sdfr * if the request was handled, or a suitable unix error code
156180025Sdfr * otherwise.
157177633Sdfr */
158180025Sdfrextern int nlm_do_test(nlm4_testargs *argp, nlm4_testres *result,
159180025Sdfr    struct svc_req *rqstp, CLIENT **rpcp);
160177633Sdfr
161177633Sdfr/*
162180025Sdfr * Implementation for lock setting RPCs. If the request was handled
163180025Sdfr * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
164180025Sdfr * handle which can be used to send an async rpc reply. Returns zero
165180025Sdfr * if the request was handled, or a suitable unix error code
166180025Sdfr * otherwise.
167177633Sdfr */
168180025Sdfrextern int nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result,
169180025Sdfr    struct svc_req *rqstp, bool_t monitor, CLIENT **rpcp);
170177633Sdfr
171177633Sdfr/*
172180025Sdfr * Implementation for cancelling a pending lock request. If the
173180025Sdfr * request was handled successfully and rpcp is non-NULL, *rpcp is set
174180025Sdfr * to an RPC client handle which can be used to send an async rpc
175180025Sdfr * reply. Returns zero if the request was handled, or a suitable unix
176180025Sdfr * error code otherwise.
177177633Sdfr */
178180025Sdfrextern int nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result,
179180025Sdfr    struct svc_req *rqstp, CLIENT **rpcp);
180177633Sdfr
181177633Sdfr/*
182180025Sdfr * Implementation for unlocking RPCs. If the request was handled
183180025Sdfr * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
184180025Sdfr * handle which can be used to send an async rpc reply. Returns zero
185180025Sdfr * if the request was handled, or a suitable unix error code
186180025Sdfr * otherwise.
187177633Sdfr */
188180025Sdfrextern int nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result,
189180025Sdfr    struct svc_req *rqstp, CLIENT **rpcp);
190177633Sdfr
191177633Sdfr/*
192180025Sdfr * Implementation for granted RPCs. If the request was handled
193180025Sdfr * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
194180025Sdfr * handle which can be used to send an async rpc reply. Returns zero
195180025Sdfr * if the request was handled, or a suitable unix error code
196180025Sdfr * otherwise.
197180025Sdfr */
198180025Sdfrextern int nlm_do_granted(nlm4_testargs *argp, nlm4_res *result,
199180025Sdfr    struct svc_req *rqstp, CLIENT **rpcp);
200180025Sdfr
201180025Sdfr/*
202197840Szml * Implementation for the granted result RPC. The client may reject the granted
203197840Szml * message, in which case we need to handle it appropriately.
204197840Szml */
205197840Szmlextern void nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp);
206197840Szml
207197840Szml/*
208177633Sdfr * Free all locks associated with the hostname argp->name.
209177633Sdfr */
210177633Sdfrextern void nlm_do_free_all(nlm4_notify *argp);
211177633Sdfr
212177633Sdfr/*
213180025Sdfr * Recover client lock state after a server reboot.
214177633Sdfr */
215180025Sdfrextern void nlm_client_recovery(struct nlm_host *);
216177633Sdfr
217180025Sdfr/*
218180025Sdfr * Interface from NFS client code to the NLM.
219180025Sdfr */
220180025Sdfrstruct vop_advlock_args;
221180025Sdfrstruct vop_reclaim_args;
222180025Sdfrextern int nlm_advlock(struct vop_advlock_args *ap);
223180025Sdfrextern int nlm_reclaim(struct vop_reclaim_args *ap);
224180025Sdfr
225192501Srmacklem/*
226192501Srmacklem * Acquire the next sysid for remote locks not handled by the NLM.
227192501Srmacklem */
228192501Srmacklemextern uint32_t nlm_acquire_next_sysid(void);
229192501Srmacklem
230177633Sdfr#endif
231177633Sdfr
232177633Sdfr#endif
233