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