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