xref: /freebsd/sys/nlm/nlm.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1dfdcada3SDoug Rabson /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4dfdcada3SDoug Rabson  * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5dfdcada3SDoug Rabson  * Authors: Doug Rabson <dfr@rabson.org>
6dfdcada3SDoug Rabson  * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
7dfdcada3SDoug Rabson  *
8dfdcada3SDoug Rabson  * Redistribution and use in source and binary forms, with or without
9dfdcada3SDoug Rabson  * modification, are permitted provided that the following conditions
10dfdcada3SDoug Rabson  * are met:
11dfdcada3SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
12dfdcada3SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
13dfdcada3SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
14dfdcada3SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
15dfdcada3SDoug Rabson  *    documentation and/or other materials provided with the distribution.
16dfdcada3SDoug Rabson  *
17dfdcada3SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18dfdcada3SDoug Rabson  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19dfdcada3SDoug Rabson  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20dfdcada3SDoug Rabson  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21dfdcada3SDoug Rabson  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22dfdcada3SDoug Rabson  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23dfdcada3SDoug Rabson  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24dfdcada3SDoug Rabson  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25dfdcada3SDoug Rabson  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26dfdcada3SDoug Rabson  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27dfdcada3SDoug Rabson  * SUCH DAMAGE.
28dfdcada3SDoug Rabson  */
29dfdcada3SDoug Rabson 
30dfdcada3SDoug Rabson #ifndef _NLM_NLM_H_
31dfdcada3SDoug Rabson #define _NLM_NLM_H_
32dfdcada3SDoug Rabson 
33dfdcada3SDoug Rabson #ifdef _KERNEL
34dfdcada3SDoug Rabson 
35dfdcada3SDoug Rabson #ifdef _SYS_MALLOC_H_
36dfdcada3SDoug Rabson MALLOC_DECLARE(M_NLM);
37dfdcada3SDoug Rabson #endif
38dfdcada3SDoug Rabson 
39c675522fSDoug Rabson /*
40c675522fSDoug Rabson  * This value is added to host system IDs when recording NFS client
41c675522fSDoug Rabson  * locks in the local lock manager.
42c675522fSDoug Rabson  */
43c675522fSDoug Rabson #define NLM_SYSID_CLIENT	0x1000000
44c675522fSDoug Rabson 
45dfdcada3SDoug Rabson struct nlm_host;
46c675522fSDoug Rabson struct vnode;
47c675522fSDoug Rabson 
48c675522fSDoug Rabson extern struct timeval nlm_zero_tv;
49c675522fSDoug Rabson extern int nlm_nsm_state;
50dfdcada3SDoug Rabson 
51dfdcada3SDoug Rabson /*
52fbaa591fSZachary Loafman  * Make a struct netobj.
53fbaa591fSZachary Loafman  */
54fbaa591fSZachary Loafman extern void nlm_make_netobj(struct netobj *dst, caddr_t srt,
55fbaa591fSZachary Loafman     size_t srcsize, struct malloc_type *type);
56fbaa591fSZachary Loafman 
57fbaa591fSZachary Loafman /*
58dfdcada3SDoug Rabson  * Copy a struct netobj.
59dfdcada3SDoug Rabson  */
60dfdcada3SDoug Rabson extern void nlm_copy_netobj(struct netobj *dst, struct netobj *src,
61dfdcada3SDoug Rabson     struct malloc_type *type);
62dfdcada3SDoug Rabson 
63dfdcada3SDoug Rabson /*
64dfdcada3SDoug Rabson  * Search for an existing NLM host that matches the given name
65dfdcada3SDoug Rabson  * (typically the caller_name element of an nlm4_lock).  If none is
66c675522fSDoug Rabson  * found, create a new host. If 'addr' is non-NULL, record the remote
67dfdcada3SDoug Rabson  * address of the host so that we can call it back for async
68c675522fSDoug Rabson  * responses. If 'vers' is greater than zero then record the NLM
69c675522fSDoug Rabson  * program version to use to communicate with this client. The host
70c675522fSDoug Rabson  * reference count is incremented - the caller must call
71c675522fSDoug Rabson  * nlm_host_release when it has finished using it.
72dfdcada3SDoug Rabson  */
73dfdcada3SDoug Rabson extern struct nlm_host *nlm_find_host_by_name(const char *name,
74c675522fSDoug Rabson     const struct sockaddr *addr, rpcvers_t vers);
75dfdcada3SDoug Rabson 
76dfdcada3SDoug Rabson /*
77dfdcada3SDoug Rabson  * Search for an existing NLM host that matches the given remote
78dfdcada3SDoug Rabson  * address. If none is found, create a new host with the requested
79dfdcada3SDoug Rabson  * address and remember 'vers' as the NLM protocol version to use for
80c675522fSDoug Rabson  * that host. The host reference count is incremented - the caller
81c675522fSDoug Rabson  * must call nlm_host_release when it has finished using it.
82dfdcada3SDoug Rabson  */
83dfdcada3SDoug Rabson extern struct nlm_host *nlm_find_host_by_addr(const struct sockaddr *addr,
84dfdcada3SDoug Rabson     int vers);
85dfdcada3SDoug Rabson 
86dfdcada3SDoug Rabson /*
87c675522fSDoug Rabson  * Register this NLM host with the local NSM so that we can be
88c675522fSDoug Rabson  * notified if it reboots.
89c675522fSDoug Rabson  */
90c675522fSDoug Rabson extern void nlm_host_monitor(struct nlm_host *host, int state);
91c675522fSDoug Rabson 
92c675522fSDoug Rabson /*
93c675522fSDoug Rabson  * Decrement the host reference count, freeing resources if the
94c675522fSDoug Rabson  * reference count reaches zero.
95c675522fSDoug Rabson  */
96c675522fSDoug Rabson extern void nlm_host_release(struct nlm_host *host);
97c675522fSDoug Rabson 
98c675522fSDoug Rabson /*
99dfdcada3SDoug Rabson  * Return an RPC client handle that can be used to talk to the NLM
100dfdcada3SDoug Rabson  * running on the given host.
101dfdcada3SDoug Rabson  */
102a9148abdSDoug Rabson extern CLIENT *nlm_host_get_rpc(struct nlm_host *host, bool_t isserver);
103dfdcada3SDoug Rabson 
104dfdcada3SDoug Rabson /*
105c675522fSDoug Rabson  * Return the system ID for a host.
106c675522fSDoug Rabson  */
107c675522fSDoug Rabson extern int nlm_host_get_sysid(struct nlm_host *host);
108c675522fSDoug Rabson 
109c675522fSDoug Rabson /*
110c675522fSDoug Rabson  * Return the remote NSM state value for a host.
111c675522fSDoug Rabson  */
112c675522fSDoug Rabson extern int nlm_host_get_state(struct nlm_host *host);
113c675522fSDoug Rabson 
114c675522fSDoug Rabson /*
115c675522fSDoug Rabson  * When sending a blocking lock request, we need to track the request
116c675522fSDoug Rabson  * in our waiting lock list. We add an entry to the waiting list
117c675522fSDoug Rabson  * before we send the lock RPC so that we can cope with a granted
118c675522fSDoug Rabson  * message arriving at any time. Call this function before sending the
119c675522fSDoug Rabson  * lock rpc. If the lock succeeds, call nlm_deregister_wait_lock with
120c675522fSDoug Rabson  * the handle this function returns, otherwise nlm_wait_lock. Both
121c675522fSDoug Rabson  * will remove the entry from the waiting list.
122c675522fSDoug Rabson  */
123c675522fSDoug Rabson extern void *nlm_register_wait_lock(struct nlm4_lock *lock, struct vnode *vp);
124c675522fSDoug Rabson 
125c675522fSDoug Rabson /*
126c675522fSDoug Rabson  * Deregister a blocking lock request. Call this if the lock succeeded
127c675522fSDoug Rabson  * without blocking.
128c675522fSDoug Rabson  */
129c675522fSDoug Rabson extern void nlm_deregister_wait_lock(void *handle);
130c675522fSDoug Rabson 
131c675522fSDoug Rabson /*
132c675522fSDoug Rabson  * Wait for a granted callback for a blocked lock request, waiting at
133c675522fSDoug Rabson  * most timo ticks. If no granted message is received within the
134c675522fSDoug Rabson  * timeout, return EWOULDBLOCK. If a signal interrupted the wait,
135c675522fSDoug Rabson  * return EINTR - the caller must arrange to send a cancellation to
136c675522fSDoug Rabson  * the server. In both cases, the request is removed from the waiting
137c675522fSDoug Rabson  * list.
138c675522fSDoug Rabson  */
139c675522fSDoug Rabson extern int nlm_wait_lock(void *handle, int timo);
140c675522fSDoug Rabson 
141c675522fSDoug Rabson /*
142c675522fSDoug Rabson  * Cancel any pending waits for this vnode - called on forcible unmounts.
143c675522fSDoug Rabson  */
144c675522fSDoug Rabson extern void nlm_cancel_wait(struct vnode *vp);
145c675522fSDoug Rabson 
146c675522fSDoug Rabson /*
147dfdcada3SDoug Rabson  * Called when a host restarts.
148dfdcada3SDoug Rabson  */
149dfdcada3SDoug Rabson extern void nlm_sm_notify(nlm_sm_status *argp);
150dfdcada3SDoug Rabson 
151dfdcada3SDoug Rabson /*
152c675522fSDoug Rabson  * Implementation for lock testing RPCs. If the request was handled
153c675522fSDoug Rabson  * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
154c675522fSDoug Rabson  * handle which can be used to send an async rpc reply. Returns zero
155c675522fSDoug Rabson  * if the request was handled, or a suitable unix error code
156c675522fSDoug Rabson  * otherwise.
157dfdcada3SDoug Rabson  */
158c675522fSDoug Rabson extern int nlm_do_test(nlm4_testargs *argp, nlm4_testres *result,
159c675522fSDoug Rabson     struct svc_req *rqstp, CLIENT **rpcp);
160dfdcada3SDoug Rabson 
161dfdcada3SDoug Rabson /*
162c675522fSDoug Rabson  * Implementation for lock setting RPCs. If the request was handled
163c675522fSDoug Rabson  * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
164c675522fSDoug Rabson  * handle which can be used to send an async rpc reply. Returns zero
165c675522fSDoug Rabson  * if the request was handled, or a suitable unix error code
166c675522fSDoug Rabson  * otherwise.
167dfdcada3SDoug Rabson  */
168c675522fSDoug Rabson extern int nlm_do_lock(nlm4_lockargs *argp, nlm4_res *result,
169c675522fSDoug Rabson     struct svc_req *rqstp, bool_t monitor, CLIENT **rpcp);
170dfdcada3SDoug Rabson 
171dfdcada3SDoug Rabson /*
172c675522fSDoug Rabson  * Implementation for cancelling a pending lock request. If the
173c675522fSDoug Rabson  * request was handled successfully and rpcp is non-NULL, *rpcp is set
174c675522fSDoug Rabson  * to an RPC client handle which can be used to send an async rpc
175c675522fSDoug Rabson  * reply. Returns zero if the request was handled, or a suitable unix
176c675522fSDoug Rabson  * error code otherwise.
177dfdcada3SDoug Rabson  */
178c675522fSDoug Rabson extern int nlm_do_cancel(nlm4_cancargs *argp, nlm4_res *result,
179c675522fSDoug Rabson     struct svc_req *rqstp, CLIENT **rpcp);
180dfdcada3SDoug Rabson 
181dfdcada3SDoug Rabson /*
182c675522fSDoug Rabson  * Implementation for unlocking RPCs. If the request was handled
183c675522fSDoug Rabson  * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
184c675522fSDoug Rabson  * handle which can be used to send an async rpc reply. Returns zero
185c675522fSDoug Rabson  * if the request was handled, or a suitable unix error code
186c675522fSDoug Rabson  * otherwise.
187dfdcada3SDoug Rabson  */
188c675522fSDoug Rabson extern int nlm_do_unlock(nlm4_unlockargs *argp, nlm4_res *result,
189c675522fSDoug Rabson     struct svc_req *rqstp, CLIENT **rpcp);
190c675522fSDoug Rabson 
191c675522fSDoug Rabson /*
192c675522fSDoug Rabson  * Implementation for granted RPCs. If the request was handled
193c675522fSDoug Rabson  * successfully and rpcp is non-NULL, *rpcp is set to an RPC client
194c675522fSDoug Rabson  * handle which can be used to send an async rpc reply. Returns zero
195c675522fSDoug Rabson  * if the request was handled, or a suitable unix error code
196c675522fSDoug Rabson  * otherwise.
197c675522fSDoug Rabson  */
198c675522fSDoug Rabson extern int nlm_do_granted(nlm4_testargs *argp, nlm4_res *result,
199c675522fSDoug Rabson     struct svc_req *rqstp, CLIENT **rpcp);
200dfdcada3SDoug Rabson 
201dfdcada3SDoug Rabson /*
202fbaa591fSZachary Loafman  * Implementation for the granted result RPC. The client may reject the granted
203fbaa591fSZachary Loafman  * message, in which case we need to handle it appropriately.
204fbaa591fSZachary Loafman  */
205fbaa591fSZachary Loafman extern void nlm_do_granted_res(nlm4_res *argp, struct svc_req *rqstp);
206fbaa591fSZachary Loafman 
207fbaa591fSZachary Loafman /*
208dfdcada3SDoug Rabson  * Free all locks associated with the hostname argp->name.
209dfdcada3SDoug Rabson  */
210dfdcada3SDoug Rabson extern void nlm_do_free_all(nlm4_notify *argp);
211dfdcada3SDoug Rabson 
212dfdcada3SDoug Rabson /*
213c675522fSDoug Rabson  * Recover client lock state after a server reboot.
214dfdcada3SDoug Rabson  */
215c675522fSDoug Rabson extern void nlm_client_recovery(struct nlm_host *);
216c675522fSDoug Rabson 
217c675522fSDoug Rabson /*
218c675522fSDoug Rabson  * Interface from NFS client code to the NLM.
219c675522fSDoug Rabson  */
220c675522fSDoug Rabson struct vop_advlock_args;
221c675522fSDoug Rabson struct vop_reclaim_args;
222c675522fSDoug Rabson extern int nlm_advlock(struct vop_advlock_args *ap);
223c675522fSDoug Rabson extern int nlm_reclaim(struct vop_reclaim_args *ap);
224dfdcada3SDoug Rabson 
225596c0880SRick Macklem /*
226596c0880SRick Macklem  * Acquire the next sysid for remote locks not handled by the NLM.
227596c0880SRick Macklem  */
228596c0880SRick Macklem extern uint32_t nlm_acquire_next_sysid(void);
229596c0880SRick Macklem 
230dfdcada3SDoug Rabson #endif
231dfdcada3SDoug Rabson 
232dfdcada3SDoug Rabson #endif
233