nlm_advlock.c revision 1.1.1.1.4.2 1 1.1.1.1.4.2 rmind /* $NetBSD: nlm_advlock.c,v 1.1.1.1.4.2 2014/05/18 17:46:05 rmind Exp $ */
2 1.1.1.1.4.2 rmind /*-
3 1.1.1.1.4.2 rmind * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
4 1.1.1.1.4.2 rmind * Authors: Doug Rabson <dfr (at) rabson.org>
5 1.1.1.1.4.2 rmind * Developed with Red Inc: Alfred Perlstein <alfred (at) freebsd.org>
6 1.1.1.1.4.2 rmind *
7 1.1.1.1.4.2 rmind * Redistribution and use in source and binary forms, with or without
8 1.1.1.1.4.2 rmind * modification, are permitted provided that the following conditions
9 1.1.1.1.4.2 rmind * are met:
10 1.1.1.1.4.2 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1.1.1.4.2 rmind * notice, this list of conditions and the following disclaimer.
12 1.1.1.1.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1.1.1.4.2 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1.1.1.4.2 rmind * documentation and/or other materials provided with the distribution.
15 1.1.1.1.4.2 rmind *
16 1.1.1.1.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1.1.1.4.2 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1.1.1.4.2 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1.1.1.4.2 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1.1.1.4.2 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1.1.1.4.2 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1.1.1.4.2 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1.1.1.4.2 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1.1.1.4.2 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1.1.1.4.2 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1.1.1.4.2 rmind * SUCH DAMAGE.
27 1.1.1.1.4.2 rmind */
28 1.1.1.1.4.2 rmind
29 1.1.1.1.4.2 rmind #include <sys/cdefs.h>
30 1.1.1.1.4.2 rmind /* __FBSDID("FreeBSD: head/sys/nlm/nlm_advlock.c 239328 2012-08-16 13:01:56Z kib "); */
31 1.1.1.1.4.2 rmind __RCSID("$NetBSD: nlm_advlock.c,v 1.1.1.1.4.2 2014/05/18 17:46:05 rmind Exp $");
32 1.1.1.1.4.2 rmind
33 1.1.1.1.4.2 rmind #include <sys/param.h>
34 1.1.1.1.4.2 rmind #include <sys/fcntl.h>
35 1.1.1.1.4.2 rmind #include <sys/jail.h>
36 1.1.1.1.4.2 rmind #include <sys/kernel.h>
37 1.1.1.1.4.2 rmind #include <sys/limits.h>
38 1.1.1.1.4.2 rmind #include <sys/lock.h>
39 1.1.1.1.4.2 rmind #include <sys/lockf.h>
40 1.1.1.1.4.2 rmind #include <sys/malloc.h>
41 1.1.1.1.4.2 rmind #include <sys/mbuf.h>
42 1.1.1.1.4.2 rmind #include <sys/mount.h>
43 1.1.1.1.4.2 rmind #include <sys/mutex.h>
44 1.1.1.1.4.2 rmind #include <sys/proc.h>
45 1.1.1.1.4.2 rmind #include <sys/socket.h>
46 1.1.1.1.4.2 rmind #include <sys/syslog.h>
47 1.1.1.1.4.2 rmind #include <sys/systm.h>
48 1.1.1.1.4.2 rmind #include <sys/unistd.h>
49 1.1.1.1.4.2 rmind #include <sys/vnode.h>
50 1.1.1.1.4.2 rmind
51 1.1.1.1.4.2 rmind #include <nfs/nfsproto.h>
52 1.1.1.1.4.2 rmind #include <nfsclient/nfs.h>
53 1.1.1.1.4.2 rmind #include <nfsclient/nfsmount.h>
54 1.1.1.1.4.2 rmind
55 1.1.1.1.4.2 rmind #include <nlm/nlm_prot.h>
56 1.1.1.1.4.2 rmind #include <nlm/nlm.h>
57 1.1.1.1.4.2 rmind
58 1.1.1.1.4.2 rmind /*
59 1.1.1.1.4.2 rmind * We need to keep track of the svid values used for F_FLOCK locks.
60 1.1.1.1.4.2 rmind */
61 1.1.1.1.4.2 rmind struct nlm_file_svid {
62 1.1.1.1.4.2 rmind int ns_refs; /* thread count + 1 if active */
63 1.1.1.1.4.2 rmind int ns_svid; /* on-the-wire SVID for this file */
64 1.1.1.1.4.2 rmind struct ucred *ns_ucred; /* creds to use for lock recovery */
65 1.1.1.1.4.2 rmind void *ns_id; /* local struct file pointer */
66 1.1.1.1.4.2 rmind bool_t ns_active; /* TRUE if we own a lock */
67 1.1.1.1.4.2 rmind LIST_ENTRY(nlm_file_svid) ns_link;
68 1.1.1.1.4.2 rmind };
69 1.1.1.1.4.2 rmind LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
70 1.1.1.1.4.2 rmind
71 1.1.1.1.4.2 rmind #define NLM_SVID_HASH_SIZE 256
72 1.1.1.1.4.2 rmind struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
73 1.1.1.1.4.2 rmind
74 1.1.1.1.4.2 rmind struct mtx nlm_svid_lock;
75 1.1.1.1.4.2 rmind static struct unrhdr *nlm_svid_allocator;
76 1.1.1.1.4.2 rmind static volatile u_int nlm_xid = 1;
77 1.1.1.1.4.2 rmind
78 1.1.1.1.4.2 rmind static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
79 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
80 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
81 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim);
82 1.1.1.1.4.2 rmind static int nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
83 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
84 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
85 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size);
86 1.1.1.1.4.2 rmind static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
87 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
88 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
89 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size);
90 1.1.1.1.4.2 rmind static int nlm_map_status(nlm4_stats stat);
91 1.1.1.1.4.2 rmind static struct nlm_file_svid *nlm_find_svid(void *id);
92 1.1.1.1.4.2 rmind static void nlm_free_svid(struct nlm_file_svid *nf);
93 1.1.1.1.4.2 rmind static int nlm_init_lock(struct flock *fl, int flags, int svid,
94 1.1.1.1.4.2 rmind rpcvers_t vers, size_t fhlen, void *fh, off_t size,
95 1.1.1.1.4.2 rmind struct nlm4_lock *lock, char oh_space[32]);
96 1.1.1.1.4.2 rmind
97 1.1.1.1.4.2 rmind static void
98 1.1.1.1.4.2 rmind nlm_client_init(void *dummy)
99 1.1.1.1.4.2 rmind {
100 1.1.1.1.4.2 rmind int i;
101 1.1.1.1.4.2 rmind
102 1.1.1.1.4.2 rmind mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF);
103 1.1.1.1.4.2 rmind /* pid_max cannot be greater than PID_MAX */
104 1.1.1.1.4.2 rmind nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock);
105 1.1.1.1.4.2 rmind for (i = 0; i < NLM_SVID_HASH_SIZE; i++)
106 1.1.1.1.4.2 rmind LIST_INIT(&nlm_file_svids[i]);
107 1.1.1.1.4.2 rmind }
108 1.1.1.1.4.2 rmind SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
109 1.1.1.1.4.2 rmind
110 1.1.1.1.4.2 rmind static int
111 1.1.1.1.4.2 rmind nlm_msg(struct thread *td, const char *server, const char *msg, int error)
112 1.1.1.1.4.2 rmind {
113 1.1.1.1.4.2 rmind struct proc *p;
114 1.1.1.1.4.2 rmind
115 1.1.1.1.4.2 rmind p = td ? td->td_proc : NULL;
116 1.1.1.1.4.2 rmind if (error) {
117 1.1.1.1.4.2 rmind tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
118 1.1.1.1.4.2 rmind msg, error);
119 1.1.1.1.4.2 rmind } else {
120 1.1.1.1.4.2 rmind tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
121 1.1.1.1.4.2 rmind }
122 1.1.1.1.4.2 rmind return (0);
123 1.1.1.1.4.2 rmind }
124 1.1.1.1.4.2 rmind
125 1.1.1.1.4.2 rmind struct nlm_feedback_arg {
126 1.1.1.1.4.2 rmind bool_t nf_printed;
127 1.1.1.1.4.2 rmind struct nfsmount *nf_nmp;
128 1.1.1.1.4.2 rmind };
129 1.1.1.1.4.2 rmind
130 1.1.1.1.4.2 rmind static void
131 1.1.1.1.4.2 rmind nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
132 1.1.1.1.4.2 rmind const char *msg, int error)
133 1.1.1.1.4.2 rmind {
134 1.1.1.1.4.2 rmind struct nfsmount *nmp = nf->nf_nmp;
135 1.1.1.1.4.2 rmind
136 1.1.1.1.4.2 rmind if (nmp == NULL)
137 1.1.1.1.4.2 rmind return;
138 1.1.1.1.4.2 rmind mtx_lock(&nmp->nm_mtx);
139 1.1.1.1.4.2 rmind if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
140 1.1.1.1.4.2 rmind nmp->nm_state |= NFSSTA_LOCKTIMEO;
141 1.1.1.1.4.2 rmind mtx_unlock(&nmp->nm_mtx);
142 1.1.1.1.4.2 rmind vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
143 1.1.1.1.4.2 rmind VQ_NOTRESPLOCK, 0);
144 1.1.1.1.4.2 rmind } else {
145 1.1.1.1.4.2 rmind mtx_unlock(&nmp->nm_mtx);
146 1.1.1.1.4.2 rmind }
147 1.1.1.1.4.2 rmind
148 1.1.1.1.4.2 rmind nf->nf_printed = TRUE;
149 1.1.1.1.4.2 rmind nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
150 1.1.1.1.4.2 rmind }
151 1.1.1.1.4.2 rmind
152 1.1.1.1.4.2 rmind static void
153 1.1.1.1.4.2 rmind nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
154 1.1.1.1.4.2 rmind const char *msg)
155 1.1.1.1.4.2 rmind {
156 1.1.1.1.4.2 rmind struct nfsmount *nmp = nf->nf_nmp;
157 1.1.1.1.4.2 rmind
158 1.1.1.1.4.2 rmind if (!nf->nf_printed)
159 1.1.1.1.4.2 rmind return;
160 1.1.1.1.4.2 rmind
161 1.1.1.1.4.2 rmind nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
162 1.1.1.1.4.2 rmind
163 1.1.1.1.4.2 rmind mtx_lock(&nmp->nm_mtx);
164 1.1.1.1.4.2 rmind if (nmp->nm_state & NFSSTA_LOCKTIMEO) {
165 1.1.1.1.4.2 rmind nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
166 1.1.1.1.4.2 rmind mtx_unlock(&nmp->nm_mtx);
167 1.1.1.1.4.2 rmind vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
168 1.1.1.1.4.2 rmind VQ_NOTRESPLOCK, 1);
169 1.1.1.1.4.2 rmind } else {
170 1.1.1.1.4.2 rmind mtx_unlock(&nmp->nm_mtx);
171 1.1.1.1.4.2 rmind }
172 1.1.1.1.4.2 rmind }
173 1.1.1.1.4.2 rmind
174 1.1.1.1.4.2 rmind static void
175 1.1.1.1.4.2 rmind nlm_feedback(int type, int proc, void *arg)
176 1.1.1.1.4.2 rmind {
177 1.1.1.1.4.2 rmind struct thread *td = curthread;
178 1.1.1.1.4.2 rmind struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
179 1.1.1.1.4.2 rmind
180 1.1.1.1.4.2 rmind switch (type) {
181 1.1.1.1.4.2 rmind case FEEDBACK_REXMIT2:
182 1.1.1.1.4.2 rmind case FEEDBACK_RECONNECT:
183 1.1.1.1.4.2 rmind nlm_down(nf, td, "lockd not responding", 0);
184 1.1.1.1.4.2 rmind break;
185 1.1.1.1.4.2 rmind
186 1.1.1.1.4.2 rmind case FEEDBACK_OK:
187 1.1.1.1.4.2 rmind nlm_up(nf, td, "lockd is alive again");
188 1.1.1.1.4.2 rmind break;
189 1.1.1.1.4.2 rmind }
190 1.1.1.1.4.2 rmind }
191 1.1.1.1.4.2 rmind
192 1.1.1.1.4.2 rmind /*
193 1.1.1.1.4.2 rmind * nlm_advlock --
194 1.1.1.1.4.2 rmind * NFS advisory byte-level locks.
195 1.1.1.1.4.2 rmind */
196 1.1.1.1.4.2 rmind static int
197 1.1.1.1.4.2 rmind nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
198 1.1.1.1.4.2 rmind int flags, bool_t reclaim, bool_t unlock_vp)
199 1.1.1.1.4.2 rmind {
200 1.1.1.1.4.2 rmind struct thread *td = curthread;
201 1.1.1.1.4.2 rmind struct nfsmount *nmp;
202 1.1.1.1.4.2 rmind off_t size;
203 1.1.1.1.4.2 rmind size_t fhlen;
204 1.1.1.1.4.2 rmind union nfsfh fh;
205 1.1.1.1.4.2 rmind struct sockaddr *sa;
206 1.1.1.1.4.2 rmind struct sockaddr_storage ss;
207 1.1.1.1.4.2 rmind char servername[MNAMELEN];
208 1.1.1.1.4.2 rmind struct timeval timo;
209 1.1.1.1.4.2 rmind int retries;
210 1.1.1.1.4.2 rmind rpcvers_t vers;
211 1.1.1.1.4.2 rmind struct nlm_host *host;
212 1.1.1.1.4.2 rmind struct rpc_callextra ext;
213 1.1.1.1.4.2 rmind struct nlm_feedback_arg nf;
214 1.1.1.1.4.2 rmind AUTH *auth;
215 1.1.1.1.4.2 rmind struct ucred *cred;
216 1.1.1.1.4.2 rmind struct nlm_file_svid *ns;
217 1.1.1.1.4.2 rmind int svid;
218 1.1.1.1.4.2 rmind int error;
219 1.1.1.1.4.2 rmind int is_v3;
220 1.1.1.1.4.2 rmind
221 1.1.1.1.4.2 rmind ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
222 1.1.1.1.4.2 rmind
223 1.1.1.1.4.2 rmind nmp = VFSTONFS(vp->v_mount);
224 1.1.1.1.4.2 rmind /*
225 1.1.1.1.4.2 rmind * Push any pending writes to the server and flush our cache
226 1.1.1.1.4.2 rmind * so that if we are contending with another machine for a
227 1.1.1.1.4.2 rmind * file, we get whatever they wrote and vice-versa.
228 1.1.1.1.4.2 rmind */
229 1.1.1.1.4.2 rmind if (op == F_SETLK || op == F_UNLCK)
230 1.1.1.1.4.2 rmind nmp->nm_vinvalbuf(vp, V_SAVE, td, 1);
231 1.1.1.1.4.2 rmind
232 1.1.1.1.4.2 rmind strcpy(servername, nmp->nm_hostname);
233 1.1.1.1.4.2 rmind nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo);
234 1.1.1.1.4.2 rmind sa = (struct sockaddr *) &ss;
235 1.1.1.1.4.2 rmind if (is_v3 != 0)
236 1.1.1.1.4.2 rmind vers = NLM_VERS4;
237 1.1.1.1.4.2 rmind else
238 1.1.1.1.4.2 rmind vers = NLM_VERS;
239 1.1.1.1.4.2 rmind
240 1.1.1.1.4.2 rmind if (nmp->nm_flag & NFSMNT_SOFT)
241 1.1.1.1.4.2 rmind retries = nmp->nm_retry;
242 1.1.1.1.4.2 rmind else
243 1.1.1.1.4.2 rmind retries = INT_MAX;
244 1.1.1.1.4.2 rmind
245 1.1.1.1.4.2 rmind if (unlock_vp)
246 1.1.1.1.4.2 rmind VOP_UNLOCK(vp, 0);
247 1.1.1.1.4.2 rmind
248 1.1.1.1.4.2 rmind /*
249 1.1.1.1.4.2 rmind * We need to switch to mount-point creds so that we can send
250 1.1.1.1.4.2 rmind * packets from a privileged port.
251 1.1.1.1.4.2 rmind */
252 1.1.1.1.4.2 rmind cred = td->td_ucred;
253 1.1.1.1.4.2 rmind td->td_ucred = vp->v_mount->mnt_cred;
254 1.1.1.1.4.2 rmind
255 1.1.1.1.4.2 rmind host = nlm_find_host_by_name(servername, sa, vers);
256 1.1.1.1.4.2 rmind auth = authunix_create(cred);
257 1.1.1.1.4.2 rmind memset(&ext, 0, sizeof(ext));
258 1.1.1.1.4.2 rmind
259 1.1.1.1.4.2 rmind nf.nf_printed = FALSE;
260 1.1.1.1.4.2 rmind nf.nf_nmp = nmp;
261 1.1.1.1.4.2 rmind ext.rc_auth = auth;
262 1.1.1.1.4.2 rmind
263 1.1.1.1.4.2 rmind ext.rc_feedback = nlm_feedback;
264 1.1.1.1.4.2 rmind ext.rc_feedback_arg = &nf;
265 1.1.1.1.4.2 rmind ext.rc_timers = NULL;
266 1.1.1.1.4.2 rmind
267 1.1.1.1.4.2 rmind ns = NULL;
268 1.1.1.1.4.2 rmind if (flags & F_FLOCK) {
269 1.1.1.1.4.2 rmind ns = nlm_find_svid(id);
270 1.1.1.1.4.2 rmind KASSERT(fl->l_start == 0 && fl->l_len == 0,
271 1.1.1.1.4.2 rmind ("F_FLOCK lock requests must be whole-file locks"));
272 1.1.1.1.4.2 rmind if (!ns->ns_ucred) {
273 1.1.1.1.4.2 rmind /*
274 1.1.1.1.4.2 rmind * Remember the creds used for locking in case
275 1.1.1.1.4.2 rmind * we need to recover the lock later.
276 1.1.1.1.4.2 rmind */
277 1.1.1.1.4.2 rmind ns->ns_ucred = crdup(cred);
278 1.1.1.1.4.2 rmind }
279 1.1.1.1.4.2 rmind svid = ns->ns_svid;
280 1.1.1.1.4.2 rmind } else if (flags & F_REMOTE) {
281 1.1.1.1.4.2 rmind /*
282 1.1.1.1.4.2 rmind * If we are recovering after a server restart or
283 1.1.1.1.4.2 rmind * trashing locks on a force unmount, use the same
284 1.1.1.1.4.2 rmind * svid as last time.
285 1.1.1.1.4.2 rmind */
286 1.1.1.1.4.2 rmind svid = fl->l_pid;
287 1.1.1.1.4.2 rmind } else {
288 1.1.1.1.4.2 rmind svid = ((struct proc *) id)->p_pid;
289 1.1.1.1.4.2 rmind }
290 1.1.1.1.4.2 rmind
291 1.1.1.1.4.2 rmind switch(op) {
292 1.1.1.1.4.2 rmind case F_SETLK:
293 1.1.1.1.4.2 rmind if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
294 1.1.1.1.4.2 rmind && fl->l_type == F_WRLCK) {
295 1.1.1.1.4.2 rmind /*
296 1.1.1.1.4.2 rmind * The semantics for flock(2) require that any
297 1.1.1.1.4.2 rmind * shared lock on the file must be released
298 1.1.1.1.4.2 rmind * before an exclusive lock is granted. The
299 1.1.1.1.4.2 rmind * local locking code interprets this by
300 1.1.1.1.4.2 rmind * unlocking the file before sleeping on a
301 1.1.1.1.4.2 rmind * blocked exclusive lock request. We
302 1.1.1.1.4.2 rmind * approximate this by first attempting
303 1.1.1.1.4.2 rmind * non-blocking and if that fails, we unlock
304 1.1.1.1.4.2 rmind * the file and block.
305 1.1.1.1.4.2 rmind */
306 1.1.1.1.4.2 rmind error = nlm_setlock(host, &ext, vers, &timo, retries,
307 1.1.1.1.4.2 rmind vp, F_SETLK, fl, flags & ~F_WAIT,
308 1.1.1.1.4.2 rmind svid, fhlen, &fh.fh_bytes, size, reclaim);
309 1.1.1.1.4.2 rmind if (error == EAGAIN) {
310 1.1.1.1.4.2 rmind fl->l_type = F_UNLCK;
311 1.1.1.1.4.2 rmind error = nlm_clearlock(host, &ext, vers, &timo,
312 1.1.1.1.4.2 rmind retries, vp, F_UNLCK, fl, flags,
313 1.1.1.1.4.2 rmind svid, fhlen, &fh.fh_bytes, size);
314 1.1.1.1.4.2 rmind fl->l_type = F_WRLCK;
315 1.1.1.1.4.2 rmind if (!error) {
316 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
317 1.1.1.1.4.2 rmind if (ns->ns_active) {
318 1.1.1.1.4.2 rmind ns->ns_refs--;
319 1.1.1.1.4.2 rmind ns->ns_active = FALSE;
320 1.1.1.1.4.2 rmind }
321 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
322 1.1.1.1.4.2 rmind flags |= F_WAIT;
323 1.1.1.1.4.2 rmind error = nlm_setlock(host, &ext, vers,
324 1.1.1.1.4.2 rmind &timo, retries, vp, F_SETLK, fl,
325 1.1.1.1.4.2 rmind flags, svid, fhlen, &fh.fh_bytes,
326 1.1.1.1.4.2 rmind size, reclaim);
327 1.1.1.1.4.2 rmind }
328 1.1.1.1.4.2 rmind }
329 1.1.1.1.4.2 rmind } else {
330 1.1.1.1.4.2 rmind error = nlm_setlock(host, &ext, vers, &timo, retries,
331 1.1.1.1.4.2 rmind vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
332 1.1.1.1.4.2 rmind size, reclaim);
333 1.1.1.1.4.2 rmind }
334 1.1.1.1.4.2 rmind if (!error && ns) {
335 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
336 1.1.1.1.4.2 rmind if (!ns->ns_active) {
337 1.1.1.1.4.2 rmind /*
338 1.1.1.1.4.2 rmind * Add one to the reference count to
339 1.1.1.1.4.2 rmind * hold onto the SVID for the lifetime
340 1.1.1.1.4.2 rmind * of the lock. Note that since
341 1.1.1.1.4.2 rmind * F_FLOCK only supports whole-file
342 1.1.1.1.4.2 rmind * locks, there can only be one active
343 1.1.1.1.4.2 rmind * lock for this SVID.
344 1.1.1.1.4.2 rmind */
345 1.1.1.1.4.2 rmind ns->ns_refs++;
346 1.1.1.1.4.2 rmind ns->ns_active = TRUE;
347 1.1.1.1.4.2 rmind }
348 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
349 1.1.1.1.4.2 rmind }
350 1.1.1.1.4.2 rmind break;
351 1.1.1.1.4.2 rmind
352 1.1.1.1.4.2 rmind case F_UNLCK:
353 1.1.1.1.4.2 rmind error = nlm_clearlock(host, &ext, vers, &timo, retries,
354 1.1.1.1.4.2 rmind vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
355 1.1.1.1.4.2 rmind if (!error && ns) {
356 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
357 1.1.1.1.4.2 rmind if (ns->ns_active) {
358 1.1.1.1.4.2 rmind ns->ns_refs--;
359 1.1.1.1.4.2 rmind ns->ns_active = FALSE;
360 1.1.1.1.4.2 rmind }
361 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
362 1.1.1.1.4.2 rmind }
363 1.1.1.1.4.2 rmind break;
364 1.1.1.1.4.2 rmind
365 1.1.1.1.4.2 rmind case F_GETLK:
366 1.1.1.1.4.2 rmind error = nlm_getlock(host, &ext, vers, &timo, retries,
367 1.1.1.1.4.2 rmind vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
368 1.1.1.1.4.2 rmind break;
369 1.1.1.1.4.2 rmind
370 1.1.1.1.4.2 rmind default:
371 1.1.1.1.4.2 rmind error = EINVAL;
372 1.1.1.1.4.2 rmind break;
373 1.1.1.1.4.2 rmind }
374 1.1.1.1.4.2 rmind
375 1.1.1.1.4.2 rmind if (ns)
376 1.1.1.1.4.2 rmind nlm_free_svid(ns);
377 1.1.1.1.4.2 rmind
378 1.1.1.1.4.2 rmind td->td_ucred = cred;
379 1.1.1.1.4.2 rmind AUTH_DESTROY(auth);
380 1.1.1.1.4.2 rmind
381 1.1.1.1.4.2 rmind nlm_host_release(host);
382 1.1.1.1.4.2 rmind
383 1.1.1.1.4.2 rmind return (error);
384 1.1.1.1.4.2 rmind }
385 1.1.1.1.4.2 rmind
386 1.1.1.1.4.2 rmind int
387 1.1.1.1.4.2 rmind nlm_advlock(struct vop_advlock_args *ap)
388 1.1.1.1.4.2 rmind {
389 1.1.1.1.4.2 rmind
390 1.1.1.1.4.2 rmind return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
391 1.1.1.1.4.2 rmind ap->a_flags, FALSE, TRUE));
392 1.1.1.1.4.2 rmind }
393 1.1.1.1.4.2 rmind
394 1.1.1.1.4.2 rmind /*
395 1.1.1.1.4.2 rmind * Set the creds of td to the creds of the given lock's owner. The new
396 1.1.1.1.4.2 rmind * creds reference count will be incremented via crhold. The caller is
397 1.1.1.1.4.2 rmind * responsible for calling crfree and restoring td's original creds.
398 1.1.1.1.4.2 rmind */
399 1.1.1.1.4.2 rmind static void
400 1.1.1.1.4.2 rmind nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
401 1.1.1.1.4.2 rmind {
402 1.1.1.1.4.2 rmind int i;
403 1.1.1.1.4.2 rmind struct nlm_file_svid *ns;
404 1.1.1.1.4.2 rmind struct proc *p;
405 1.1.1.1.4.2 rmind struct ucred *cred;
406 1.1.1.1.4.2 rmind
407 1.1.1.1.4.2 rmind cred = NULL;
408 1.1.1.1.4.2 rmind if (fl->l_pid > PID_MAX) {
409 1.1.1.1.4.2 rmind /*
410 1.1.1.1.4.2 rmind * If this was originally a F_FLOCK-style lock, we
411 1.1.1.1.4.2 rmind * recorded the creds used when it was originally
412 1.1.1.1.4.2 rmind * locked in the nlm_file_svid structure.
413 1.1.1.1.4.2 rmind */
414 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
415 1.1.1.1.4.2 rmind for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
416 1.1.1.1.4.2 rmind for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
417 1.1.1.1.4.2 rmind ns = LIST_NEXT(ns, ns_link)) {
418 1.1.1.1.4.2 rmind if (ns->ns_svid == fl->l_pid) {
419 1.1.1.1.4.2 rmind cred = crhold(ns->ns_ucred);
420 1.1.1.1.4.2 rmind break;
421 1.1.1.1.4.2 rmind }
422 1.1.1.1.4.2 rmind }
423 1.1.1.1.4.2 rmind }
424 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
425 1.1.1.1.4.2 rmind } else {
426 1.1.1.1.4.2 rmind /*
427 1.1.1.1.4.2 rmind * This lock is owned by a process. Get a reference to
428 1.1.1.1.4.2 rmind * the process creds.
429 1.1.1.1.4.2 rmind */
430 1.1.1.1.4.2 rmind p = pfind(fl->l_pid);
431 1.1.1.1.4.2 rmind if (p) {
432 1.1.1.1.4.2 rmind cred = crhold(p->p_ucred);
433 1.1.1.1.4.2 rmind PROC_UNLOCK(p);
434 1.1.1.1.4.2 rmind }
435 1.1.1.1.4.2 rmind }
436 1.1.1.1.4.2 rmind
437 1.1.1.1.4.2 rmind /*
438 1.1.1.1.4.2 rmind * If we can't find a cred, fall back on the recovery
439 1.1.1.1.4.2 rmind * thread's cred.
440 1.1.1.1.4.2 rmind */
441 1.1.1.1.4.2 rmind if (!cred) {
442 1.1.1.1.4.2 rmind cred = crhold(td->td_ucred);
443 1.1.1.1.4.2 rmind }
444 1.1.1.1.4.2 rmind
445 1.1.1.1.4.2 rmind td->td_ucred = cred;
446 1.1.1.1.4.2 rmind }
447 1.1.1.1.4.2 rmind
448 1.1.1.1.4.2 rmind static int
449 1.1.1.1.4.2 rmind nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
450 1.1.1.1.4.2 rmind {
451 1.1.1.1.4.2 rmind struct flock newfl;
452 1.1.1.1.4.2 rmind struct thread *td = curthread;
453 1.1.1.1.4.2 rmind struct ucred *oldcred;
454 1.1.1.1.4.2 rmind int error;
455 1.1.1.1.4.2 rmind
456 1.1.1.1.4.2 rmind newfl = *fl;
457 1.1.1.1.4.2 rmind newfl.l_type = F_UNLCK;
458 1.1.1.1.4.2 rmind
459 1.1.1.1.4.2 rmind oldcred = td->td_ucred;
460 1.1.1.1.4.2 rmind nlm_set_creds_for_lock(td, &newfl);
461 1.1.1.1.4.2 rmind
462 1.1.1.1.4.2 rmind error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
463 1.1.1.1.4.2 rmind FALSE, FALSE);
464 1.1.1.1.4.2 rmind
465 1.1.1.1.4.2 rmind crfree(td->td_ucred);
466 1.1.1.1.4.2 rmind td->td_ucred = oldcred;
467 1.1.1.1.4.2 rmind
468 1.1.1.1.4.2 rmind return (error);
469 1.1.1.1.4.2 rmind }
470 1.1.1.1.4.2 rmind
471 1.1.1.1.4.2 rmind int
472 1.1.1.1.4.2 rmind nlm_reclaim(struct vop_reclaim_args *ap)
473 1.1.1.1.4.2 rmind {
474 1.1.1.1.4.2 rmind
475 1.1.1.1.4.2 rmind nlm_cancel_wait(ap->a_vp);
476 1.1.1.1.4.2 rmind lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
477 1.1.1.1.4.2 rmind return (0);
478 1.1.1.1.4.2 rmind }
479 1.1.1.1.4.2 rmind
480 1.1.1.1.4.2 rmind struct nlm_recovery_context {
481 1.1.1.1.4.2 rmind struct nlm_host *nr_host; /* host we are recovering */
482 1.1.1.1.4.2 rmind int nr_state; /* remote NSM state for recovery */
483 1.1.1.1.4.2 rmind };
484 1.1.1.1.4.2 rmind
485 1.1.1.1.4.2 rmind static int
486 1.1.1.1.4.2 rmind nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
487 1.1.1.1.4.2 rmind {
488 1.1.1.1.4.2 rmind struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
489 1.1.1.1.4.2 rmind struct thread *td = curthread;
490 1.1.1.1.4.2 rmind struct ucred *oldcred;
491 1.1.1.1.4.2 rmind int state, error;
492 1.1.1.1.4.2 rmind
493 1.1.1.1.4.2 rmind /*
494 1.1.1.1.4.2 rmind * If the remote NSM state changes during recovery, the host
495 1.1.1.1.4.2 rmind * must have rebooted a second time. In that case, we must
496 1.1.1.1.4.2 rmind * restart the recovery.
497 1.1.1.1.4.2 rmind */
498 1.1.1.1.4.2 rmind state = nlm_host_get_state(nr->nr_host);
499 1.1.1.1.4.2 rmind if (nr->nr_state != state)
500 1.1.1.1.4.2 rmind return (ERESTART);
501 1.1.1.1.4.2 rmind
502 1.1.1.1.4.2 rmind error = vn_lock(vp, LK_SHARED);
503 1.1.1.1.4.2 rmind if (error)
504 1.1.1.1.4.2 rmind return (error);
505 1.1.1.1.4.2 rmind
506 1.1.1.1.4.2 rmind oldcred = td->td_ucred;
507 1.1.1.1.4.2 rmind nlm_set_creds_for_lock(td, fl);
508 1.1.1.1.4.2 rmind
509 1.1.1.1.4.2 rmind error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
510 1.1.1.1.4.2 rmind TRUE, TRUE);
511 1.1.1.1.4.2 rmind
512 1.1.1.1.4.2 rmind crfree(td->td_ucred);
513 1.1.1.1.4.2 rmind td->td_ucred = oldcred;
514 1.1.1.1.4.2 rmind
515 1.1.1.1.4.2 rmind return (error);
516 1.1.1.1.4.2 rmind }
517 1.1.1.1.4.2 rmind
518 1.1.1.1.4.2 rmind void
519 1.1.1.1.4.2 rmind nlm_client_recovery(struct nlm_host *host)
520 1.1.1.1.4.2 rmind {
521 1.1.1.1.4.2 rmind struct nlm_recovery_context nr;
522 1.1.1.1.4.2 rmind int sysid, error;
523 1.1.1.1.4.2 rmind
524 1.1.1.1.4.2 rmind sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
525 1.1.1.1.4.2 rmind do {
526 1.1.1.1.4.2 rmind nr.nr_host = host;
527 1.1.1.1.4.2 rmind nr.nr_state = nlm_host_get_state(host);
528 1.1.1.1.4.2 rmind error = lf_iteratelocks_sysid(sysid,
529 1.1.1.1.4.2 rmind nlm_client_recover_lock, &nr);
530 1.1.1.1.4.2 rmind } while (error == ERESTART);
531 1.1.1.1.4.2 rmind }
532 1.1.1.1.4.2 rmind
533 1.1.1.1.4.2 rmind static void
534 1.1.1.1.4.2 rmind nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
535 1.1.1.1.4.2 rmind {
536 1.1.1.1.4.2 rmind
537 1.1.1.1.4.2 rmind dst->caller_name = src->caller_name;
538 1.1.1.1.4.2 rmind dst->fh = src->fh;
539 1.1.1.1.4.2 rmind dst->oh = src->oh;
540 1.1.1.1.4.2 rmind dst->svid = src->svid;
541 1.1.1.1.4.2 rmind dst->l_offset = src->l_offset;
542 1.1.1.1.4.2 rmind dst->l_len = src->l_len;
543 1.1.1.1.4.2 rmind }
544 1.1.1.1.4.2 rmind
545 1.1.1.1.4.2 rmind static void
546 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
547 1.1.1.1.4.2 rmind {
548 1.1.1.1.4.2 rmind
549 1.1.1.1.4.2 rmind dst->exclusive = src->exclusive;
550 1.1.1.1.4.2 rmind dst->svid = src->svid;
551 1.1.1.1.4.2 rmind dst->oh = src->oh;
552 1.1.1.1.4.2 rmind dst->l_offset = src->l_offset;
553 1.1.1.1.4.2 rmind dst->l_len = src->l_len;
554 1.1.1.1.4.2 rmind }
555 1.1.1.1.4.2 rmind
556 1.1.1.1.4.2 rmind static void
557 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
558 1.1.1.1.4.2 rmind {
559 1.1.1.1.4.2 rmind dst->cookie = src->cookie;
560 1.1.1.1.4.2 rmind dst->stat.stat = (enum nlm4_stats) src->stat.stat;
561 1.1.1.1.4.2 rmind }
562 1.1.1.1.4.2 rmind
563 1.1.1.1.4.2 rmind static enum clnt_stat
564 1.1.1.1.4.2 rmind nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
565 1.1.1.1.4.2 rmind struct rpc_callextra *ext, struct timeval timo)
566 1.1.1.1.4.2 rmind {
567 1.1.1.1.4.2 rmind if (vers == NLM_VERS4) {
568 1.1.1.1.4.2 rmind return nlm4_test_4(args, res, client, ext, timo);
569 1.1.1.1.4.2 rmind } else {
570 1.1.1.1.4.2 rmind nlm_testargs args1;
571 1.1.1.1.4.2 rmind nlm_testres res1;
572 1.1.1.1.4.2 rmind enum clnt_stat stat;
573 1.1.1.1.4.2 rmind
574 1.1.1.1.4.2 rmind args1.cookie = args->cookie;
575 1.1.1.1.4.2 rmind args1.exclusive = args->exclusive;
576 1.1.1.1.4.2 rmind nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
577 1.1.1.1.4.2 rmind memset(&res1, 0, sizeof(res1));
578 1.1.1.1.4.2 rmind
579 1.1.1.1.4.2 rmind stat = nlm_test_1(&args1, &res1, client, ext, timo);
580 1.1.1.1.4.2 rmind
581 1.1.1.1.4.2 rmind if (stat == RPC_SUCCESS) {
582 1.1.1.1.4.2 rmind res->cookie = res1.cookie;
583 1.1.1.1.4.2 rmind res->stat.stat = (enum nlm4_stats) res1.stat.stat;
584 1.1.1.1.4.2 rmind if (res1.stat.stat == nlm_denied)
585 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_holder(
586 1.1.1.1.4.2 rmind &res->stat.nlm4_testrply_u.holder,
587 1.1.1.1.4.2 rmind &res1.stat.nlm_testrply_u.holder);
588 1.1.1.1.4.2 rmind }
589 1.1.1.1.4.2 rmind
590 1.1.1.1.4.2 rmind return (stat);
591 1.1.1.1.4.2 rmind }
592 1.1.1.1.4.2 rmind }
593 1.1.1.1.4.2 rmind
594 1.1.1.1.4.2 rmind static enum clnt_stat
595 1.1.1.1.4.2 rmind nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
596 1.1.1.1.4.2 rmind struct rpc_callextra *ext, struct timeval timo)
597 1.1.1.1.4.2 rmind {
598 1.1.1.1.4.2 rmind if (vers == NLM_VERS4) {
599 1.1.1.1.4.2 rmind return nlm4_lock_4(args, res, client, ext, timo);
600 1.1.1.1.4.2 rmind } else {
601 1.1.1.1.4.2 rmind nlm_lockargs args1;
602 1.1.1.1.4.2 rmind nlm_res res1;
603 1.1.1.1.4.2 rmind enum clnt_stat stat;
604 1.1.1.1.4.2 rmind
605 1.1.1.1.4.2 rmind args1.cookie = args->cookie;
606 1.1.1.1.4.2 rmind args1.block = args->block;
607 1.1.1.1.4.2 rmind args1.exclusive = args->exclusive;
608 1.1.1.1.4.2 rmind nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
609 1.1.1.1.4.2 rmind args1.reclaim = args->reclaim;
610 1.1.1.1.4.2 rmind args1.state = args->state;
611 1.1.1.1.4.2 rmind memset(&res1, 0, sizeof(res1));
612 1.1.1.1.4.2 rmind
613 1.1.1.1.4.2 rmind stat = nlm_lock_1(&args1, &res1, client, ext, timo);
614 1.1.1.1.4.2 rmind
615 1.1.1.1.4.2 rmind if (stat == RPC_SUCCESS) {
616 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_res(res, &res1);
617 1.1.1.1.4.2 rmind }
618 1.1.1.1.4.2 rmind
619 1.1.1.1.4.2 rmind return (stat);
620 1.1.1.1.4.2 rmind }
621 1.1.1.1.4.2 rmind }
622 1.1.1.1.4.2 rmind
623 1.1.1.1.4.2 rmind static enum clnt_stat
624 1.1.1.1.4.2 rmind nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
625 1.1.1.1.4.2 rmind struct rpc_callextra *ext, struct timeval timo)
626 1.1.1.1.4.2 rmind {
627 1.1.1.1.4.2 rmind if (vers == NLM_VERS4) {
628 1.1.1.1.4.2 rmind return nlm4_cancel_4(args, res, client, ext, timo);
629 1.1.1.1.4.2 rmind } else {
630 1.1.1.1.4.2 rmind nlm_cancargs args1;
631 1.1.1.1.4.2 rmind nlm_res res1;
632 1.1.1.1.4.2 rmind enum clnt_stat stat;
633 1.1.1.1.4.2 rmind
634 1.1.1.1.4.2 rmind args1.cookie = args->cookie;
635 1.1.1.1.4.2 rmind args1.block = args->block;
636 1.1.1.1.4.2 rmind args1.exclusive = args->exclusive;
637 1.1.1.1.4.2 rmind nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
638 1.1.1.1.4.2 rmind memset(&res1, 0, sizeof(res1));
639 1.1.1.1.4.2 rmind
640 1.1.1.1.4.2 rmind stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
641 1.1.1.1.4.2 rmind
642 1.1.1.1.4.2 rmind if (stat == RPC_SUCCESS) {
643 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_res(res, &res1);
644 1.1.1.1.4.2 rmind }
645 1.1.1.1.4.2 rmind
646 1.1.1.1.4.2 rmind return (stat);
647 1.1.1.1.4.2 rmind }
648 1.1.1.1.4.2 rmind }
649 1.1.1.1.4.2 rmind
650 1.1.1.1.4.2 rmind static enum clnt_stat
651 1.1.1.1.4.2 rmind nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
652 1.1.1.1.4.2 rmind struct rpc_callextra *ext, struct timeval timo)
653 1.1.1.1.4.2 rmind {
654 1.1.1.1.4.2 rmind if (vers == NLM_VERS4) {
655 1.1.1.1.4.2 rmind return nlm4_unlock_4(args, res, client, ext, timo);
656 1.1.1.1.4.2 rmind } else {
657 1.1.1.1.4.2 rmind nlm_unlockargs args1;
658 1.1.1.1.4.2 rmind nlm_res res1;
659 1.1.1.1.4.2 rmind enum clnt_stat stat;
660 1.1.1.1.4.2 rmind
661 1.1.1.1.4.2 rmind args1.cookie = args->cookie;
662 1.1.1.1.4.2 rmind nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
663 1.1.1.1.4.2 rmind memset(&res1, 0, sizeof(res1));
664 1.1.1.1.4.2 rmind
665 1.1.1.1.4.2 rmind stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
666 1.1.1.1.4.2 rmind
667 1.1.1.1.4.2 rmind if (stat == RPC_SUCCESS) {
668 1.1.1.1.4.2 rmind nlm_convert_to_nlm4_res(res, &res1);
669 1.1.1.1.4.2 rmind }
670 1.1.1.1.4.2 rmind
671 1.1.1.1.4.2 rmind return (stat);
672 1.1.1.1.4.2 rmind }
673 1.1.1.1.4.2 rmind }
674 1.1.1.1.4.2 rmind
675 1.1.1.1.4.2 rmind /*
676 1.1.1.1.4.2 rmind * Called after a lock request (set or clear) succeeded. We record the
677 1.1.1.1.4.2 rmind * details in the local lock manager. Note that since the remote
678 1.1.1.1.4.2 rmind * server has granted the lock, we can be sure that it doesn't
679 1.1.1.1.4.2 rmind * conflict with any other locks we have in the local lock manager.
680 1.1.1.1.4.2 rmind *
681 1.1.1.1.4.2 rmind * Since it is possible that host may also make NLM client requests to
682 1.1.1.1.4.2 rmind * our NLM server, we use a different sysid value to record our own
683 1.1.1.1.4.2 rmind * client locks.
684 1.1.1.1.4.2 rmind *
685 1.1.1.1.4.2 rmind * Note that since it is possible for us to receive replies from the
686 1.1.1.1.4.2 rmind * server in a different order than the locks were granted (e.g. if
687 1.1.1.1.4.2 rmind * many local threads are contending for the same lock), we must use a
688 1.1.1.1.4.2 rmind * blocking operation when registering with the local lock manager.
689 1.1.1.1.4.2 rmind * We expect that any actual wait will be rare and short hence we
690 1.1.1.1.4.2 rmind * ignore signals for this.
691 1.1.1.1.4.2 rmind */
692 1.1.1.1.4.2 rmind static void
693 1.1.1.1.4.2 rmind nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
694 1.1.1.1.4.2 rmind int svid, int sysid, off_t size)
695 1.1.1.1.4.2 rmind {
696 1.1.1.1.4.2 rmind struct vop_advlockasync_args a;
697 1.1.1.1.4.2 rmind struct flock newfl;
698 1.1.1.1.4.2 rmind int error;
699 1.1.1.1.4.2 rmind
700 1.1.1.1.4.2 rmind a.a_vp = vp;
701 1.1.1.1.4.2 rmind a.a_id = NULL;
702 1.1.1.1.4.2 rmind a.a_op = op;
703 1.1.1.1.4.2 rmind a.a_fl = &newfl;
704 1.1.1.1.4.2 rmind a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
705 1.1.1.1.4.2 rmind a.a_task = NULL;
706 1.1.1.1.4.2 rmind a.a_cookiep = NULL;
707 1.1.1.1.4.2 rmind newfl.l_start = fl->l_start;
708 1.1.1.1.4.2 rmind newfl.l_len = fl->l_len;
709 1.1.1.1.4.2 rmind newfl.l_type = fl->l_type;
710 1.1.1.1.4.2 rmind newfl.l_whence = fl->l_whence;
711 1.1.1.1.4.2 rmind newfl.l_pid = svid;
712 1.1.1.1.4.2 rmind newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
713 1.1.1.1.4.2 rmind
714 1.1.1.1.4.2 rmind error = lf_advlockasync(&a, &vp->v_lockf, size);
715 1.1.1.1.4.2 rmind KASSERT(error == 0 || error == ENOENT,
716 1.1.1.1.4.2 rmind ("Failed to register NFS lock locally - error=%d", error));
717 1.1.1.1.4.2 rmind }
718 1.1.1.1.4.2 rmind
719 1.1.1.1.4.2 rmind static int
720 1.1.1.1.4.2 rmind nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
721 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
722 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
723 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
724 1.1.1.1.4.2 rmind {
725 1.1.1.1.4.2 rmind struct nlm4_lockargs args;
726 1.1.1.1.4.2 rmind char oh_space[32];
727 1.1.1.1.4.2 rmind struct nlm4_res res;
728 1.1.1.1.4.2 rmind u_int xid;
729 1.1.1.1.4.2 rmind CLIENT *client;
730 1.1.1.1.4.2 rmind enum clnt_stat stat;
731 1.1.1.1.4.2 rmind int retry, block, exclusive;
732 1.1.1.1.4.2 rmind void *wait_handle = NULL;
733 1.1.1.1.4.2 rmind int error;
734 1.1.1.1.4.2 rmind
735 1.1.1.1.4.2 rmind memset(&args, 0, sizeof(args));
736 1.1.1.1.4.2 rmind memset(&res, 0, sizeof(res));
737 1.1.1.1.4.2 rmind
738 1.1.1.1.4.2 rmind block = (flags & F_WAIT) ? TRUE : FALSE;
739 1.1.1.1.4.2 rmind exclusive = (fl->l_type == F_WRLCK);
740 1.1.1.1.4.2 rmind
741 1.1.1.1.4.2 rmind error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
742 1.1.1.1.4.2 rmind &args.alock, oh_space);
743 1.1.1.1.4.2 rmind if (error)
744 1.1.1.1.4.2 rmind return (error);
745 1.1.1.1.4.2 rmind args.block = block;
746 1.1.1.1.4.2 rmind args.exclusive = exclusive;
747 1.1.1.1.4.2 rmind args.reclaim = reclaim;
748 1.1.1.1.4.2 rmind args.state = nlm_nsm_state;
749 1.1.1.1.4.2 rmind
750 1.1.1.1.4.2 rmind retry = 5*hz;
751 1.1.1.1.4.2 rmind for (;;) {
752 1.1.1.1.4.2 rmind client = nlm_host_get_rpc(host, FALSE);
753 1.1.1.1.4.2 rmind if (!client)
754 1.1.1.1.4.2 rmind return (ENOLCK); /* XXX retry? */
755 1.1.1.1.4.2 rmind
756 1.1.1.1.4.2 rmind if (block)
757 1.1.1.1.4.2 rmind wait_handle = nlm_register_wait_lock(&args.alock, vp);
758 1.1.1.1.4.2 rmind
759 1.1.1.1.4.2 rmind xid = atomic_fetchadd_int(&nlm_xid, 1);
760 1.1.1.1.4.2 rmind args.cookie.n_len = sizeof(xid);
761 1.1.1.1.4.2 rmind args.cookie.n_bytes = (char*) &xid;
762 1.1.1.1.4.2 rmind
763 1.1.1.1.4.2 rmind stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
764 1.1.1.1.4.2 rmind
765 1.1.1.1.4.2 rmind CLNT_RELEASE(client);
766 1.1.1.1.4.2 rmind
767 1.1.1.1.4.2 rmind if (stat != RPC_SUCCESS) {
768 1.1.1.1.4.2 rmind if (block)
769 1.1.1.1.4.2 rmind nlm_deregister_wait_lock(wait_handle);
770 1.1.1.1.4.2 rmind if (retries) {
771 1.1.1.1.4.2 rmind retries--;
772 1.1.1.1.4.2 rmind continue;
773 1.1.1.1.4.2 rmind }
774 1.1.1.1.4.2 rmind return (EINVAL);
775 1.1.1.1.4.2 rmind }
776 1.1.1.1.4.2 rmind
777 1.1.1.1.4.2 rmind /*
778 1.1.1.1.4.2 rmind * Free res.cookie.
779 1.1.1.1.4.2 rmind */
780 1.1.1.1.4.2 rmind xdr_free((xdrproc_t) xdr_nlm4_res, &res);
781 1.1.1.1.4.2 rmind
782 1.1.1.1.4.2 rmind if (block && res.stat.stat != nlm4_blocked)
783 1.1.1.1.4.2 rmind nlm_deregister_wait_lock(wait_handle);
784 1.1.1.1.4.2 rmind
785 1.1.1.1.4.2 rmind if (res.stat.stat == nlm4_denied_grace_period) {
786 1.1.1.1.4.2 rmind /*
787 1.1.1.1.4.2 rmind * The server has recently rebooted and is
788 1.1.1.1.4.2 rmind * giving old clients a change to reclaim
789 1.1.1.1.4.2 rmind * their locks. Wait for a few seconds and try
790 1.1.1.1.4.2 rmind * again.
791 1.1.1.1.4.2 rmind */
792 1.1.1.1.4.2 rmind error = tsleep(&args, PCATCH, "nlmgrace", retry);
793 1.1.1.1.4.2 rmind if (error && error != EWOULDBLOCK)
794 1.1.1.1.4.2 rmind return (error);
795 1.1.1.1.4.2 rmind retry = 2*retry;
796 1.1.1.1.4.2 rmind if (retry > 30*hz)
797 1.1.1.1.4.2 rmind retry = 30*hz;
798 1.1.1.1.4.2 rmind continue;
799 1.1.1.1.4.2 rmind }
800 1.1.1.1.4.2 rmind
801 1.1.1.1.4.2 rmind if (block && res.stat.stat == nlm4_blocked) {
802 1.1.1.1.4.2 rmind /*
803 1.1.1.1.4.2 rmind * The server should call us back with a
804 1.1.1.1.4.2 rmind * granted message when the lock succeeds. In
805 1.1.1.1.4.2 rmind * order to deal with broken servers, lost
806 1.1.1.1.4.2 rmind * granted messages and server reboots, we
807 1.1.1.1.4.2 rmind * will also re-try every few seconds.
808 1.1.1.1.4.2 rmind */
809 1.1.1.1.4.2 rmind error = nlm_wait_lock(wait_handle, retry);
810 1.1.1.1.4.2 rmind if (error == EWOULDBLOCK) {
811 1.1.1.1.4.2 rmind retry = 2*retry;
812 1.1.1.1.4.2 rmind if (retry > 30*hz)
813 1.1.1.1.4.2 rmind retry = 30*hz;
814 1.1.1.1.4.2 rmind continue;
815 1.1.1.1.4.2 rmind }
816 1.1.1.1.4.2 rmind if (error) {
817 1.1.1.1.4.2 rmind /*
818 1.1.1.1.4.2 rmind * We need to call the server to
819 1.1.1.1.4.2 rmind * cancel our lock request.
820 1.1.1.1.4.2 rmind */
821 1.1.1.1.4.2 rmind nlm4_cancargs cancel;
822 1.1.1.1.4.2 rmind
823 1.1.1.1.4.2 rmind memset(&cancel, 0, sizeof(cancel));
824 1.1.1.1.4.2 rmind
825 1.1.1.1.4.2 rmind xid = atomic_fetchadd_int(&nlm_xid, 1);
826 1.1.1.1.4.2 rmind cancel.cookie.n_len = sizeof(xid);
827 1.1.1.1.4.2 rmind cancel.cookie.n_bytes = (char*) &xid;
828 1.1.1.1.4.2 rmind cancel.block = block;
829 1.1.1.1.4.2 rmind cancel.exclusive = exclusive;
830 1.1.1.1.4.2 rmind cancel.alock = args.alock;
831 1.1.1.1.4.2 rmind
832 1.1.1.1.4.2 rmind do {
833 1.1.1.1.4.2 rmind client = nlm_host_get_rpc(host, FALSE);
834 1.1.1.1.4.2 rmind if (!client)
835 1.1.1.1.4.2 rmind /* XXX retry? */
836 1.1.1.1.4.2 rmind return (ENOLCK);
837 1.1.1.1.4.2 rmind
838 1.1.1.1.4.2 rmind stat = nlm_cancel_rpc(vers, &cancel,
839 1.1.1.1.4.2 rmind &res, client, ext, *timo);
840 1.1.1.1.4.2 rmind
841 1.1.1.1.4.2 rmind CLNT_RELEASE(client);
842 1.1.1.1.4.2 rmind
843 1.1.1.1.4.2 rmind if (stat != RPC_SUCCESS) {
844 1.1.1.1.4.2 rmind /*
845 1.1.1.1.4.2 rmind * We need to cope
846 1.1.1.1.4.2 rmind * with temporary
847 1.1.1.1.4.2 rmind * network partitions
848 1.1.1.1.4.2 rmind * as well as server
849 1.1.1.1.4.2 rmind * reboots. This means
850 1.1.1.1.4.2 rmind * we have to keep
851 1.1.1.1.4.2 rmind * trying to cancel
852 1.1.1.1.4.2 rmind * until the server
853 1.1.1.1.4.2 rmind * wakes up again.
854 1.1.1.1.4.2 rmind */
855 1.1.1.1.4.2 rmind pause("nlmcancel", 10*hz);
856 1.1.1.1.4.2 rmind }
857 1.1.1.1.4.2 rmind } while (stat != RPC_SUCCESS);
858 1.1.1.1.4.2 rmind
859 1.1.1.1.4.2 rmind /*
860 1.1.1.1.4.2 rmind * Free res.cookie.
861 1.1.1.1.4.2 rmind */
862 1.1.1.1.4.2 rmind xdr_free((xdrproc_t) xdr_nlm4_res, &res);
863 1.1.1.1.4.2 rmind
864 1.1.1.1.4.2 rmind switch (res.stat.stat) {
865 1.1.1.1.4.2 rmind case nlm_denied:
866 1.1.1.1.4.2 rmind /*
867 1.1.1.1.4.2 rmind * There was nothing
868 1.1.1.1.4.2 rmind * to cancel. We are
869 1.1.1.1.4.2 rmind * going to go ahead
870 1.1.1.1.4.2 rmind * and assume we got
871 1.1.1.1.4.2 rmind * the lock.
872 1.1.1.1.4.2 rmind */
873 1.1.1.1.4.2 rmind error = 0;
874 1.1.1.1.4.2 rmind break;
875 1.1.1.1.4.2 rmind
876 1.1.1.1.4.2 rmind case nlm4_denied_grace_period:
877 1.1.1.1.4.2 rmind /*
878 1.1.1.1.4.2 rmind * The server has
879 1.1.1.1.4.2 rmind * recently rebooted -
880 1.1.1.1.4.2 rmind * treat this as a
881 1.1.1.1.4.2 rmind * successful
882 1.1.1.1.4.2 rmind * cancellation.
883 1.1.1.1.4.2 rmind */
884 1.1.1.1.4.2 rmind break;
885 1.1.1.1.4.2 rmind
886 1.1.1.1.4.2 rmind case nlm4_granted:
887 1.1.1.1.4.2 rmind /*
888 1.1.1.1.4.2 rmind * We managed to
889 1.1.1.1.4.2 rmind * cancel.
890 1.1.1.1.4.2 rmind */
891 1.1.1.1.4.2 rmind break;
892 1.1.1.1.4.2 rmind
893 1.1.1.1.4.2 rmind default:
894 1.1.1.1.4.2 rmind /*
895 1.1.1.1.4.2 rmind * Broken server
896 1.1.1.1.4.2 rmind * implementation -
897 1.1.1.1.4.2 rmind * can't really do
898 1.1.1.1.4.2 rmind * anything here.
899 1.1.1.1.4.2 rmind */
900 1.1.1.1.4.2 rmind break;
901 1.1.1.1.4.2 rmind }
902 1.1.1.1.4.2 rmind
903 1.1.1.1.4.2 rmind }
904 1.1.1.1.4.2 rmind } else {
905 1.1.1.1.4.2 rmind error = nlm_map_status(res.stat.stat);
906 1.1.1.1.4.2 rmind }
907 1.1.1.1.4.2 rmind
908 1.1.1.1.4.2 rmind if (!error && !reclaim) {
909 1.1.1.1.4.2 rmind nlm_record_lock(vp, op, fl, args.alock.svid,
910 1.1.1.1.4.2 rmind nlm_host_get_sysid(host), size);
911 1.1.1.1.4.2 rmind nlm_host_monitor(host, 0);
912 1.1.1.1.4.2 rmind }
913 1.1.1.1.4.2 rmind
914 1.1.1.1.4.2 rmind return (error);
915 1.1.1.1.4.2 rmind }
916 1.1.1.1.4.2 rmind }
917 1.1.1.1.4.2 rmind
918 1.1.1.1.4.2 rmind static int
919 1.1.1.1.4.2 rmind nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
920 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
921 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
922 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size)
923 1.1.1.1.4.2 rmind {
924 1.1.1.1.4.2 rmind struct nlm4_unlockargs args;
925 1.1.1.1.4.2 rmind char oh_space[32];
926 1.1.1.1.4.2 rmind struct nlm4_res res;
927 1.1.1.1.4.2 rmind u_int xid;
928 1.1.1.1.4.2 rmind CLIENT *client;
929 1.1.1.1.4.2 rmind enum clnt_stat stat;
930 1.1.1.1.4.2 rmind int error;
931 1.1.1.1.4.2 rmind
932 1.1.1.1.4.2 rmind memset(&args, 0, sizeof(args));
933 1.1.1.1.4.2 rmind memset(&res, 0, sizeof(res));
934 1.1.1.1.4.2 rmind
935 1.1.1.1.4.2 rmind error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
936 1.1.1.1.4.2 rmind &args.alock, oh_space);
937 1.1.1.1.4.2 rmind if (error)
938 1.1.1.1.4.2 rmind return (error);
939 1.1.1.1.4.2 rmind
940 1.1.1.1.4.2 rmind for (;;) {
941 1.1.1.1.4.2 rmind client = nlm_host_get_rpc(host, FALSE);
942 1.1.1.1.4.2 rmind if (!client)
943 1.1.1.1.4.2 rmind return (ENOLCK); /* XXX retry? */
944 1.1.1.1.4.2 rmind
945 1.1.1.1.4.2 rmind xid = atomic_fetchadd_int(&nlm_xid, 1);
946 1.1.1.1.4.2 rmind args.cookie.n_len = sizeof(xid);
947 1.1.1.1.4.2 rmind args.cookie.n_bytes = (char*) &xid;
948 1.1.1.1.4.2 rmind
949 1.1.1.1.4.2 rmind stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
950 1.1.1.1.4.2 rmind
951 1.1.1.1.4.2 rmind CLNT_RELEASE(client);
952 1.1.1.1.4.2 rmind
953 1.1.1.1.4.2 rmind if (stat != RPC_SUCCESS) {
954 1.1.1.1.4.2 rmind if (retries) {
955 1.1.1.1.4.2 rmind retries--;
956 1.1.1.1.4.2 rmind continue;
957 1.1.1.1.4.2 rmind }
958 1.1.1.1.4.2 rmind return (EINVAL);
959 1.1.1.1.4.2 rmind }
960 1.1.1.1.4.2 rmind
961 1.1.1.1.4.2 rmind /*
962 1.1.1.1.4.2 rmind * Free res.cookie.
963 1.1.1.1.4.2 rmind */
964 1.1.1.1.4.2 rmind xdr_free((xdrproc_t) xdr_nlm4_res, &res);
965 1.1.1.1.4.2 rmind
966 1.1.1.1.4.2 rmind if (res.stat.stat == nlm4_denied_grace_period) {
967 1.1.1.1.4.2 rmind /*
968 1.1.1.1.4.2 rmind * The server has recently rebooted and is
969 1.1.1.1.4.2 rmind * giving old clients a change to reclaim
970 1.1.1.1.4.2 rmind * their locks. Wait for a few seconds and try
971 1.1.1.1.4.2 rmind * again.
972 1.1.1.1.4.2 rmind */
973 1.1.1.1.4.2 rmind error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
974 1.1.1.1.4.2 rmind if (error && error != EWOULDBLOCK)
975 1.1.1.1.4.2 rmind return (error);
976 1.1.1.1.4.2 rmind continue;
977 1.1.1.1.4.2 rmind }
978 1.1.1.1.4.2 rmind
979 1.1.1.1.4.2 rmind /*
980 1.1.1.1.4.2 rmind * If we are being called via nlm_reclaim (which will
981 1.1.1.1.4.2 rmind * use the F_REMOTE flag), don't record the lock
982 1.1.1.1.4.2 rmind * operation in the local lock manager since the vnode
983 1.1.1.1.4.2 rmind * is going away.
984 1.1.1.1.4.2 rmind */
985 1.1.1.1.4.2 rmind if (!(flags & F_REMOTE))
986 1.1.1.1.4.2 rmind nlm_record_lock(vp, op, fl, args.alock.svid,
987 1.1.1.1.4.2 rmind nlm_host_get_sysid(host), size);
988 1.1.1.1.4.2 rmind
989 1.1.1.1.4.2 rmind return (0);
990 1.1.1.1.4.2 rmind }
991 1.1.1.1.4.2 rmind }
992 1.1.1.1.4.2 rmind
993 1.1.1.1.4.2 rmind static int
994 1.1.1.1.4.2 rmind nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
995 1.1.1.1.4.2 rmind rpcvers_t vers, struct timeval *timo, int retries,
996 1.1.1.1.4.2 rmind struct vnode *vp, int op, struct flock *fl, int flags,
997 1.1.1.1.4.2 rmind int svid, size_t fhlen, void *fh, off_t size)
998 1.1.1.1.4.2 rmind {
999 1.1.1.1.4.2 rmind struct nlm4_testargs args;
1000 1.1.1.1.4.2 rmind char oh_space[32];
1001 1.1.1.1.4.2 rmind struct nlm4_testres res;
1002 1.1.1.1.4.2 rmind u_int xid;
1003 1.1.1.1.4.2 rmind CLIENT *client;
1004 1.1.1.1.4.2 rmind enum clnt_stat stat;
1005 1.1.1.1.4.2 rmind int exclusive;
1006 1.1.1.1.4.2 rmind int error;
1007 1.1.1.1.4.2 rmind
1008 1.1.1.1.4.2 rmind KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1009 1.1.1.1.4.2 rmind
1010 1.1.1.1.4.2 rmind memset(&args, 0, sizeof(args));
1011 1.1.1.1.4.2 rmind memset(&res, 0, sizeof(res));
1012 1.1.1.1.4.2 rmind
1013 1.1.1.1.4.2 rmind exclusive = (fl->l_type == F_WRLCK);
1014 1.1.1.1.4.2 rmind
1015 1.1.1.1.4.2 rmind error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1016 1.1.1.1.4.2 rmind &args.alock, oh_space);
1017 1.1.1.1.4.2 rmind if (error)
1018 1.1.1.1.4.2 rmind return (error);
1019 1.1.1.1.4.2 rmind args.exclusive = exclusive;
1020 1.1.1.1.4.2 rmind
1021 1.1.1.1.4.2 rmind for (;;) {
1022 1.1.1.1.4.2 rmind client = nlm_host_get_rpc(host, FALSE);
1023 1.1.1.1.4.2 rmind if (!client)
1024 1.1.1.1.4.2 rmind return (ENOLCK); /* XXX retry? */
1025 1.1.1.1.4.2 rmind
1026 1.1.1.1.4.2 rmind xid = atomic_fetchadd_int(&nlm_xid, 1);
1027 1.1.1.1.4.2 rmind args.cookie.n_len = sizeof(xid);
1028 1.1.1.1.4.2 rmind args.cookie.n_bytes = (char*) &xid;
1029 1.1.1.1.4.2 rmind
1030 1.1.1.1.4.2 rmind stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1031 1.1.1.1.4.2 rmind
1032 1.1.1.1.4.2 rmind CLNT_RELEASE(client);
1033 1.1.1.1.4.2 rmind
1034 1.1.1.1.4.2 rmind if (stat != RPC_SUCCESS) {
1035 1.1.1.1.4.2 rmind if (retries) {
1036 1.1.1.1.4.2 rmind retries--;
1037 1.1.1.1.4.2 rmind continue;
1038 1.1.1.1.4.2 rmind }
1039 1.1.1.1.4.2 rmind return (EINVAL);
1040 1.1.1.1.4.2 rmind }
1041 1.1.1.1.4.2 rmind
1042 1.1.1.1.4.2 rmind if (res.stat.stat == nlm4_denied_grace_period) {
1043 1.1.1.1.4.2 rmind /*
1044 1.1.1.1.4.2 rmind * The server has recently rebooted and is
1045 1.1.1.1.4.2 rmind * giving old clients a change to reclaim
1046 1.1.1.1.4.2 rmind * their locks. Wait for a few seconds and try
1047 1.1.1.1.4.2 rmind * again.
1048 1.1.1.1.4.2 rmind */
1049 1.1.1.1.4.2 rmind xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1050 1.1.1.1.4.2 rmind error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1051 1.1.1.1.4.2 rmind if (error && error != EWOULDBLOCK)
1052 1.1.1.1.4.2 rmind return (error);
1053 1.1.1.1.4.2 rmind continue;
1054 1.1.1.1.4.2 rmind }
1055 1.1.1.1.4.2 rmind
1056 1.1.1.1.4.2 rmind if (res.stat.stat == nlm4_denied) {
1057 1.1.1.1.4.2 rmind struct nlm4_holder *h =
1058 1.1.1.1.4.2 rmind &res.stat.nlm4_testrply_u.holder;
1059 1.1.1.1.4.2 rmind fl->l_start = h->l_offset;
1060 1.1.1.1.4.2 rmind fl->l_len = h->l_len;
1061 1.1.1.1.4.2 rmind fl->l_pid = h->svid;
1062 1.1.1.1.4.2 rmind if (h->exclusive)
1063 1.1.1.1.4.2 rmind fl->l_type = F_WRLCK;
1064 1.1.1.1.4.2 rmind else
1065 1.1.1.1.4.2 rmind fl->l_type = F_RDLCK;
1066 1.1.1.1.4.2 rmind fl->l_whence = SEEK_SET;
1067 1.1.1.1.4.2 rmind fl->l_sysid = 0;
1068 1.1.1.1.4.2 rmind } else {
1069 1.1.1.1.4.2 rmind fl->l_type = F_UNLCK;
1070 1.1.1.1.4.2 rmind }
1071 1.1.1.1.4.2 rmind
1072 1.1.1.1.4.2 rmind xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1073 1.1.1.1.4.2 rmind
1074 1.1.1.1.4.2 rmind return (0);
1075 1.1.1.1.4.2 rmind }
1076 1.1.1.1.4.2 rmind }
1077 1.1.1.1.4.2 rmind
1078 1.1.1.1.4.2 rmind static int
1079 1.1.1.1.4.2 rmind nlm_map_status(nlm4_stats stat)
1080 1.1.1.1.4.2 rmind {
1081 1.1.1.1.4.2 rmind switch (stat) {
1082 1.1.1.1.4.2 rmind case nlm4_granted:
1083 1.1.1.1.4.2 rmind return (0);
1084 1.1.1.1.4.2 rmind
1085 1.1.1.1.4.2 rmind case nlm4_denied:
1086 1.1.1.1.4.2 rmind return (EAGAIN);
1087 1.1.1.1.4.2 rmind
1088 1.1.1.1.4.2 rmind case nlm4_denied_nolocks:
1089 1.1.1.1.4.2 rmind return (ENOLCK);
1090 1.1.1.1.4.2 rmind
1091 1.1.1.1.4.2 rmind case nlm4_deadlck:
1092 1.1.1.1.4.2 rmind return (EDEADLK);
1093 1.1.1.1.4.2 rmind
1094 1.1.1.1.4.2 rmind case nlm4_rofs:
1095 1.1.1.1.4.2 rmind return (EROFS);
1096 1.1.1.1.4.2 rmind
1097 1.1.1.1.4.2 rmind case nlm4_stale_fh:
1098 1.1.1.1.4.2 rmind return (ESTALE);
1099 1.1.1.1.4.2 rmind
1100 1.1.1.1.4.2 rmind case nlm4_fbig:
1101 1.1.1.1.4.2 rmind return (EFBIG);
1102 1.1.1.1.4.2 rmind
1103 1.1.1.1.4.2 rmind case nlm4_failed:
1104 1.1.1.1.4.2 rmind return (EACCES);
1105 1.1.1.1.4.2 rmind
1106 1.1.1.1.4.2 rmind default:
1107 1.1.1.1.4.2 rmind return (EINVAL);
1108 1.1.1.1.4.2 rmind }
1109 1.1.1.1.4.2 rmind }
1110 1.1.1.1.4.2 rmind
1111 1.1.1.1.4.2 rmind static struct nlm_file_svid *
1112 1.1.1.1.4.2 rmind nlm_find_svid(void *id)
1113 1.1.1.1.4.2 rmind {
1114 1.1.1.1.4.2 rmind struct nlm_file_svid *ns, *newns;
1115 1.1.1.1.4.2 rmind int h;
1116 1.1.1.1.4.2 rmind
1117 1.1.1.1.4.2 rmind h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1118 1.1.1.1.4.2 rmind
1119 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
1120 1.1.1.1.4.2 rmind LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1121 1.1.1.1.4.2 rmind if (ns->ns_id == id) {
1122 1.1.1.1.4.2 rmind ns->ns_refs++;
1123 1.1.1.1.4.2 rmind break;
1124 1.1.1.1.4.2 rmind }
1125 1.1.1.1.4.2 rmind }
1126 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
1127 1.1.1.1.4.2 rmind if (!ns) {
1128 1.1.1.1.4.2 rmind int svid = alloc_unr(nlm_svid_allocator);
1129 1.1.1.1.4.2 rmind newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1130 1.1.1.1.4.2 rmind M_WAITOK);
1131 1.1.1.1.4.2 rmind newns->ns_refs = 1;
1132 1.1.1.1.4.2 rmind newns->ns_id = id;
1133 1.1.1.1.4.2 rmind newns->ns_svid = svid;
1134 1.1.1.1.4.2 rmind newns->ns_ucred = NULL;
1135 1.1.1.1.4.2 rmind newns->ns_active = FALSE;
1136 1.1.1.1.4.2 rmind
1137 1.1.1.1.4.2 rmind /*
1138 1.1.1.1.4.2 rmind * We need to check for a race with some other
1139 1.1.1.1.4.2 rmind * thread allocating a svid for this file.
1140 1.1.1.1.4.2 rmind */
1141 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
1142 1.1.1.1.4.2 rmind LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1143 1.1.1.1.4.2 rmind if (ns->ns_id == id) {
1144 1.1.1.1.4.2 rmind ns->ns_refs++;
1145 1.1.1.1.4.2 rmind break;
1146 1.1.1.1.4.2 rmind }
1147 1.1.1.1.4.2 rmind }
1148 1.1.1.1.4.2 rmind if (ns) {
1149 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
1150 1.1.1.1.4.2 rmind free_unr(nlm_svid_allocator, newns->ns_svid);
1151 1.1.1.1.4.2 rmind free(newns, M_NLM);
1152 1.1.1.1.4.2 rmind } else {
1153 1.1.1.1.4.2 rmind LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1154 1.1.1.1.4.2 rmind ns_link);
1155 1.1.1.1.4.2 rmind ns = newns;
1156 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
1157 1.1.1.1.4.2 rmind }
1158 1.1.1.1.4.2 rmind }
1159 1.1.1.1.4.2 rmind
1160 1.1.1.1.4.2 rmind return (ns);
1161 1.1.1.1.4.2 rmind }
1162 1.1.1.1.4.2 rmind
1163 1.1.1.1.4.2 rmind static void
1164 1.1.1.1.4.2 rmind nlm_free_svid(struct nlm_file_svid *ns)
1165 1.1.1.1.4.2 rmind {
1166 1.1.1.1.4.2 rmind
1167 1.1.1.1.4.2 rmind mtx_lock(&nlm_svid_lock);
1168 1.1.1.1.4.2 rmind ns->ns_refs--;
1169 1.1.1.1.4.2 rmind if (!ns->ns_refs) {
1170 1.1.1.1.4.2 rmind KASSERT(!ns->ns_active, ("Freeing active SVID"));
1171 1.1.1.1.4.2 rmind LIST_REMOVE(ns, ns_link);
1172 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
1173 1.1.1.1.4.2 rmind free_unr(nlm_svid_allocator, ns->ns_svid);
1174 1.1.1.1.4.2 rmind if (ns->ns_ucred)
1175 1.1.1.1.4.2 rmind crfree(ns->ns_ucred);
1176 1.1.1.1.4.2 rmind free(ns, M_NLM);
1177 1.1.1.1.4.2 rmind } else {
1178 1.1.1.1.4.2 rmind mtx_unlock(&nlm_svid_lock);
1179 1.1.1.1.4.2 rmind }
1180 1.1.1.1.4.2 rmind }
1181 1.1.1.1.4.2 rmind
1182 1.1.1.1.4.2 rmind static int
1183 1.1.1.1.4.2 rmind nlm_init_lock(struct flock *fl, int flags, int svid,
1184 1.1.1.1.4.2 rmind rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1185 1.1.1.1.4.2 rmind struct nlm4_lock *lock, char oh_space[32])
1186 1.1.1.1.4.2 rmind {
1187 1.1.1.1.4.2 rmind size_t oh_len;
1188 1.1.1.1.4.2 rmind off_t start, len;
1189 1.1.1.1.4.2 rmind
1190 1.1.1.1.4.2 rmind if (fl->l_whence == SEEK_END) {
1191 1.1.1.1.4.2 rmind if (size > OFF_MAX
1192 1.1.1.1.4.2 rmind || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1193 1.1.1.1.4.2 rmind return (EOVERFLOW);
1194 1.1.1.1.4.2 rmind start = size + fl->l_start;
1195 1.1.1.1.4.2 rmind } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1196 1.1.1.1.4.2 rmind start = fl->l_start;
1197 1.1.1.1.4.2 rmind } else {
1198 1.1.1.1.4.2 rmind return (EINVAL);
1199 1.1.1.1.4.2 rmind }
1200 1.1.1.1.4.2 rmind if (start < 0)
1201 1.1.1.1.4.2 rmind return (EINVAL);
1202 1.1.1.1.4.2 rmind if (fl->l_len < 0) {
1203 1.1.1.1.4.2 rmind len = -fl->l_len;
1204 1.1.1.1.4.2 rmind start -= len;
1205 1.1.1.1.4.2 rmind if (start < 0)
1206 1.1.1.1.4.2 rmind return (EINVAL);
1207 1.1.1.1.4.2 rmind } else {
1208 1.1.1.1.4.2 rmind len = fl->l_len;
1209 1.1.1.1.4.2 rmind }
1210 1.1.1.1.4.2 rmind
1211 1.1.1.1.4.2 rmind if (vers == NLM_VERS) {
1212 1.1.1.1.4.2 rmind /*
1213 1.1.1.1.4.2 rmind * Enforce range limits on V1 locks
1214 1.1.1.1.4.2 rmind */
1215 1.1.1.1.4.2 rmind if (start > 0xffffffffLL || len > 0xffffffffLL)
1216 1.1.1.1.4.2 rmind return (EOVERFLOW);
1217 1.1.1.1.4.2 rmind }
1218 1.1.1.1.4.2 rmind
1219 1.1.1.1.4.2 rmind snprintf(oh_space, 32, "%d@", svid);
1220 1.1.1.1.4.2 rmind oh_len = strlen(oh_space);
1221 1.1.1.1.4.2 rmind getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
1222 1.1.1.1.4.2 rmind oh_len = strlen(oh_space);
1223 1.1.1.1.4.2 rmind
1224 1.1.1.1.4.2 rmind memset(lock, 0, sizeof(*lock));
1225 1.1.1.1.4.2 rmind lock->caller_name = prison0.pr_hostname;
1226 1.1.1.1.4.2 rmind lock->fh.n_len = fhlen;
1227 1.1.1.1.4.2 rmind lock->fh.n_bytes = fh;
1228 1.1.1.1.4.2 rmind lock->oh.n_len = oh_len;
1229 1.1.1.1.4.2 rmind lock->oh.n_bytes = oh_space;
1230 1.1.1.1.4.2 rmind lock->svid = svid;
1231 1.1.1.1.4.2 rmind lock->l_offset = start;
1232 1.1.1.1.4.2 rmind lock->l_len = len;
1233 1.1.1.1.4.2 rmind
1234 1.1.1.1.4.2 rmind return (0);
1235 1.1.1.1.4.2 rmind }
1236