2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
5 * Authors: Doug Rabson <dfr@rabson.org>
6 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/fcntl.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/lockf.h>
40 #include <sys/malloc.h>
42 #include <sys/mount.h>
43 #include <sys/mutex.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>
51 #include <nfs/nfsproto.h>
52 #include <nfsclient/nfs.h>
53 #include <nfsclient/nfsmount.h>
55 #include <nlm/nlm_prot.h>
59 * We need to keep track of the svid values used for F_FLOCK locks.
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;
69 LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
71 #define NLM_SVID_HASH_SIZE 256
72 struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
74 struct mtx nlm_svid_lock;
75 static struct unrhdr *nlm_svid_allocator;
76 static volatile u_int nlm_xid = 1;
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]);
98 nlm_client_init(void *dummy)
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]);
108 SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
111 nlm_msg(struct thread *td, const char *server, const char *msg, int error)
115 p = td ? td->td_proc : NULL;
117 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
120 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
125 struct nlm_feedback_arg {
127 struct nfsmount *nf_nmp;
131 nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
132 const char *msg, int error)
134 struct nfsmount *nmp = nf->nf_nmp;
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,
145 mtx_unlock(&nmp->nm_mtx);
148 nf->nf_printed = TRUE;
149 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
153 nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
156 struct nfsmount *nmp = nf->nf_nmp;
161 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
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,
170 mtx_unlock(&nmp->nm_mtx);
175 nlm_feedback(int type, int proc, void *arg)
177 struct thread *td = curthread;
178 struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
181 case FEEDBACK_REXMIT2:
182 case FEEDBACK_RECONNECT:
183 nlm_down(nf, td, "lockd not responding", 0);
187 nlm_up(nf, td, "lockd is alive again");
194 * NFS advisory byte-level locks.
197 nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
198 int flags, bool_t reclaim, bool_t unlock_vp)
200 struct thread *td = curthread;
201 struct nfsmount *nmp;
206 struct sockaddr_storage ss;
211 struct nlm_host *host;
212 struct rpc_callextra ext;
213 struct nlm_feedback_arg nf;
215 struct ucred *cred, *cred1;
216 struct nlm_file_svid *ns;
221 ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
223 servername = malloc(MNAMELEN, M_TEMP, M_WAITOK); /* XXXKIB vp locked */
224 nmp = VFSTONFS(vp->v_mount);
226 * Push any pending writes to the server and flush our cache
227 * so that if we are contending with another machine for a
228 * file, we get whatever they wrote and vice-versa.
230 if (op == F_SETLK || op == F_UNLCK)
231 nmp->nm_vinvalbuf(vp, V_SAVE, td, 1);
233 strcpy(servername, nmp->nm_hostname);
234 nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo);
235 sa = (struct sockaddr *) &ss;
241 if (nmp->nm_flag & NFSMNT_SOFT)
242 retries = nmp->nm_retry;
247 * We need to switch to mount-point creds so that we can send
248 * packets from a privileged port. Reference mnt_cred and
249 * switch to them before unlocking the vnode, since mount
250 * point could be unmounted right after unlock.
253 td->td_ucred = vp->v_mount->mnt_cred;
254 crhold(td->td_ucred);
258 host = nlm_find_host_by_name(servername, sa, vers);
259 auth = authunix_create(cred);
260 memset(&ext, 0, sizeof(ext));
262 nf.nf_printed = FALSE;
266 ext.rc_feedback = nlm_feedback;
267 ext.rc_feedback_arg = &nf;
268 ext.rc_timers = NULL;
271 if (flags & F_FLOCK) {
272 ns = nlm_find_svid(id);
273 KASSERT(fl->l_start == 0 && fl->l_len == 0,
274 ("F_FLOCK lock requests must be whole-file locks"));
277 * Remember the creds used for locking in case
278 * we need to recover the lock later.
280 ns->ns_ucred = crdup(cred);
283 } else if (flags & F_REMOTE) {
285 * If we are recovering after a server restart or
286 * trashing locks on a force unmount, use the same
291 svid = ((struct proc *) id)->p_pid;
296 if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
297 && fl->l_type == F_WRLCK) {
299 * The semantics for flock(2) require that any
300 * shared lock on the file must be released
301 * before an exclusive lock is granted. The
302 * local locking code interprets this by
303 * unlocking the file before sleeping on a
304 * blocked exclusive lock request. We
305 * approximate this by first attempting
306 * non-blocking and if that fails, we unlock
307 * the file and block.
309 error = nlm_setlock(host, &ext, vers, &timo, retries,
310 vp, F_SETLK, fl, flags & ~F_WAIT,
311 svid, fhlen, &fh.fh_bytes, size, reclaim);
312 if (error == EAGAIN) {
313 fl->l_type = F_UNLCK;
314 error = nlm_clearlock(host, &ext, vers, &timo,
315 retries, vp, F_UNLCK, fl, flags,
316 svid, fhlen, &fh.fh_bytes, size);
317 fl->l_type = F_WRLCK;
319 mtx_lock(&nlm_svid_lock);
322 ns->ns_active = FALSE;
324 mtx_unlock(&nlm_svid_lock);
326 error = nlm_setlock(host, &ext, vers,
327 &timo, retries, vp, F_SETLK, fl,
328 flags, svid, fhlen, &fh.fh_bytes,
333 error = nlm_setlock(host, &ext, vers, &timo, retries,
334 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
338 mtx_lock(&nlm_svid_lock);
339 if (!ns->ns_active) {
341 * Add one to the reference count to
342 * hold onto the SVID for the lifetime
343 * of the lock. Note that since
344 * F_FLOCK only supports whole-file
345 * locks, there can only be one active
346 * lock for this SVID.
349 ns->ns_active = TRUE;
351 mtx_unlock(&nlm_svid_lock);
356 error = nlm_clearlock(host, &ext, vers, &timo, retries,
357 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
359 mtx_lock(&nlm_svid_lock);
362 ns->ns_active = FALSE;
364 mtx_unlock(&nlm_svid_lock);
369 error = nlm_getlock(host, &ext, vers, &timo, retries,
370 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
381 cred1 = td->td_ucred;
386 nlm_host_release(host);
387 free(servername, M_TEMP);
392 nlm_advlock(struct vop_advlock_args *ap)
395 return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
396 ap->a_flags, FALSE, TRUE));
400 * Set the creds of td to the creds of the given lock's owner. The new
401 * creds reference count will be incremented via crhold. The caller is
402 * responsible for calling crfree and restoring td's original creds.
405 nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
408 struct nlm_file_svid *ns;
413 if (fl->l_pid > PID_MAX) {
415 * If this was originally a F_FLOCK-style lock, we
416 * recorded the creds used when it was originally
417 * locked in the nlm_file_svid structure.
419 mtx_lock(&nlm_svid_lock);
420 for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
421 for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
422 ns = LIST_NEXT(ns, ns_link)) {
423 if (ns->ns_svid == fl->l_pid) {
424 cred = crhold(ns->ns_ucred);
429 mtx_unlock(&nlm_svid_lock);
432 * This lock is owned by a process. Get a reference to
435 p = pfind(fl->l_pid);
437 cred = crhold(p->p_ucred);
443 * If we can't find a cred, fall back on the recovery
447 cred = crhold(td->td_ucred);
454 nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
457 struct thread *td = curthread;
458 struct ucred *oldcred;
462 newfl.l_type = F_UNLCK;
464 oldcred = td->td_ucred;
465 nlm_set_creds_for_lock(td, &newfl);
467 error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
470 crfree(td->td_ucred);
471 td->td_ucred = oldcred;
477 nlm_reclaim(struct vop_reclaim_args *ap)
480 nlm_cancel_wait(ap->a_vp);
481 lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
485 struct nlm_recovery_context {
486 struct nlm_host *nr_host; /* host we are recovering */
487 int nr_state; /* remote NSM state for recovery */
491 nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
493 struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
494 struct thread *td = curthread;
495 struct ucred *oldcred;
499 * If the remote NSM state changes during recovery, the host
500 * must have rebooted a second time. In that case, we must
501 * restart the recovery.
503 state = nlm_host_get_state(nr->nr_host);
504 if (nr->nr_state != state)
507 error = vn_lock(vp, LK_SHARED);
511 oldcred = td->td_ucred;
512 nlm_set_creds_for_lock(td, fl);
514 error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
517 crfree(td->td_ucred);
518 td->td_ucred = oldcred;
524 nlm_client_recovery(struct nlm_host *host)
526 struct nlm_recovery_context nr;
529 sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
532 nr.nr_state = nlm_host_get_state(host);
533 error = lf_iteratelocks_sysid(sysid,
534 nlm_client_recover_lock, &nr);
535 } while (error == ERESTART);
539 nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
542 dst->caller_name = src->caller_name;
545 dst->svid = src->svid;
546 dst->l_offset = src->l_offset;
547 dst->l_len = src->l_len;
551 nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
554 dst->exclusive = src->exclusive;
555 dst->svid = src->svid;
557 dst->l_offset = src->l_offset;
558 dst->l_len = src->l_len;
562 nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
564 dst->cookie = src->cookie;
565 dst->stat.stat = (enum nlm4_stats) src->stat.stat;
568 static enum clnt_stat
569 nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
570 struct rpc_callextra *ext, struct timeval timo)
572 if (vers == NLM_VERS4) {
573 return nlm4_test_4(args, res, client, ext, timo);
579 args1.cookie = args->cookie;
580 args1.exclusive = args->exclusive;
581 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
582 memset(&res1, 0, sizeof(res1));
584 stat = nlm_test_1(&args1, &res1, client, ext, timo);
586 if (stat == RPC_SUCCESS) {
587 res->cookie = res1.cookie;
588 res->stat.stat = (enum nlm4_stats) res1.stat.stat;
589 if (res1.stat.stat == nlm_denied)
590 nlm_convert_to_nlm4_holder(
591 &res->stat.nlm4_testrply_u.holder,
592 &res1.stat.nlm_testrply_u.holder);
599 static enum clnt_stat
600 nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
601 struct rpc_callextra *ext, struct timeval timo)
603 if (vers == NLM_VERS4) {
604 return nlm4_lock_4(args, res, client, ext, timo);
610 args1.cookie = args->cookie;
611 args1.block = args->block;
612 args1.exclusive = args->exclusive;
613 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
614 args1.reclaim = args->reclaim;
615 args1.state = args->state;
616 memset(&res1, 0, sizeof(res1));
618 stat = nlm_lock_1(&args1, &res1, client, ext, timo);
620 if (stat == RPC_SUCCESS) {
621 nlm_convert_to_nlm4_res(res, &res1);
628 static enum clnt_stat
629 nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
630 struct rpc_callextra *ext, struct timeval timo)
632 if (vers == NLM_VERS4) {
633 return nlm4_cancel_4(args, res, client, ext, timo);
639 args1.cookie = args->cookie;
640 args1.block = args->block;
641 args1.exclusive = args->exclusive;
642 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
643 memset(&res1, 0, sizeof(res1));
645 stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
647 if (stat == RPC_SUCCESS) {
648 nlm_convert_to_nlm4_res(res, &res1);
655 static enum clnt_stat
656 nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
657 struct rpc_callextra *ext, struct timeval timo)
659 if (vers == NLM_VERS4) {
660 return nlm4_unlock_4(args, res, client, ext, timo);
662 nlm_unlockargs args1;
666 args1.cookie = args->cookie;
667 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
668 memset(&res1, 0, sizeof(res1));
670 stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
672 if (stat == RPC_SUCCESS) {
673 nlm_convert_to_nlm4_res(res, &res1);
681 * Called after a lock request (set or clear) succeeded. We record the
682 * details in the local lock manager. Note that since the remote
683 * server has granted the lock, we can be sure that it doesn't
684 * conflict with any other locks we have in the local lock manager.
686 * Since it is possible that host may also make NLM client requests to
687 * our NLM server, we use a different sysid value to record our own
690 * Note that since it is possible for us to receive replies from the
691 * server in a different order than the locks were granted (e.g. if
692 * many local threads are contending for the same lock), we must use a
693 * blocking operation when registering with the local lock manager.
694 * We expect that any actual wait will be rare and short hence we
695 * ignore signals for this.
698 nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
699 int svid, int sysid, off_t size)
701 struct vop_advlockasync_args a;
704 int error, stops_deferred;
710 a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
713 newfl.l_start = fl->l_start;
714 newfl.l_len = fl->l_len;
715 newfl.l_type = fl->l_type;
716 newfl.l_whence = fl->l_whence;
718 newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
721 error = lf_advlockasync(&a, &vp->v_lockf, size);
722 if (error == EDEADLK) {
724 * Locks are associated with the processes and
725 * not with threads. Suppose we have two
726 * threads A1 A2 in one process, A1 locked
727 * file f1, A2 is locking file f2, and A1 is
728 * unlocking f1. Then remote server may
729 * already unlocked f1, while local still not
730 * yet scheduled A1 to make the call to local
731 * advlock manager. The process B owns lock on
732 * f2 and issued the lock on f1. Remote would
733 * grant B the request on f1, but local would
738 stops_deferred = sigdeferstop(SIGDEFERSTOP_OFF);
740 thread_suspend_check(0);
742 sigallowstop(stops_deferred);
743 } else if (error == EINTR) {
745 * lf_purgelocks() might wake up the lock
746 * waiter and removed our lock graph edges.
747 * There is no sense in re-trying recording
748 * the lock to the local manager after
756 KASSERT(error == 0 || error == ENOENT,
757 ("Failed to register NFS lock locally - error=%d", error));
761 nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
762 rpcvers_t vers, struct timeval *timo, int retries,
763 struct vnode *vp, int op, struct flock *fl, int flags,
764 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
766 struct nlm4_lockargs args;
772 int retry, block, exclusive;
773 void *wait_handle = NULL;
776 memset(&args, 0, sizeof(args));
777 memset(&res, 0, sizeof(res));
779 block = (flags & F_WAIT) ? TRUE : FALSE;
780 exclusive = (fl->l_type == F_WRLCK);
782 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
783 &args.alock, oh_space);
787 args.exclusive = exclusive;
788 args.reclaim = reclaim;
789 args.state = nlm_nsm_state;
793 client = nlm_host_get_rpc(host, FALSE);
795 return (ENOLCK); /* XXX retry? */
798 wait_handle = nlm_register_wait_lock(&args.alock, vp);
800 xid = atomic_fetchadd_int(&nlm_xid, 1);
801 args.cookie.n_len = sizeof(xid);
802 args.cookie.n_bytes = (char*) &xid;
804 stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
806 CLNT_RELEASE(client);
808 if (stat != RPC_SUCCESS) {
810 nlm_deregister_wait_lock(wait_handle);
821 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
823 if (block && res.stat.stat != nlm4_blocked)
824 nlm_deregister_wait_lock(wait_handle);
826 if (res.stat.stat == nlm4_denied_grace_period) {
828 * The server has recently rebooted and is
829 * giving old clients a change to reclaim
830 * their locks. Wait for a few seconds and try
833 error = tsleep(&args, PCATCH, "nlmgrace", retry);
834 if (error && error != EWOULDBLOCK)
842 if (block && res.stat.stat == nlm4_blocked) {
844 * The server should call us back with a
845 * granted message when the lock succeeds. In
846 * order to deal with broken servers, lost
847 * granted messages and server reboots, we
848 * will also re-try every few seconds.
850 error = nlm_wait_lock(wait_handle, retry);
851 if (error == EWOULDBLOCK) {
859 * We need to call the server to
860 * cancel our lock request.
862 nlm4_cancargs cancel;
864 memset(&cancel, 0, sizeof(cancel));
866 xid = atomic_fetchadd_int(&nlm_xid, 1);
867 cancel.cookie.n_len = sizeof(xid);
868 cancel.cookie.n_bytes = (char*) &xid;
869 cancel.block = block;
870 cancel.exclusive = exclusive;
871 cancel.alock = args.alock;
874 client = nlm_host_get_rpc(host, FALSE);
879 stat = nlm_cancel_rpc(vers, &cancel,
880 &res, client, ext, *timo);
882 CLNT_RELEASE(client);
884 if (stat != RPC_SUCCESS) {
890 * reboots. This means
896 pause("nlmcancel", 10*hz);
898 } while (stat != RPC_SUCCESS);
903 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
905 switch (res.stat.stat) {
917 case nlm4_denied_grace_period:
920 * recently rebooted -
946 error = nlm_map_status(res.stat.stat);
949 if (!error && !reclaim) {
950 nlm_record_lock(vp, op, fl, args.alock.svid,
951 nlm_host_get_sysid(host), size);
952 nlm_host_monitor(host, 0);
960 nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
961 rpcvers_t vers, struct timeval *timo, int retries,
962 struct vnode *vp, int op, struct flock *fl, int flags,
963 int svid, size_t fhlen, void *fh, off_t size)
965 struct nlm4_unlockargs args;
973 memset(&args, 0, sizeof(args));
974 memset(&res, 0, sizeof(res));
976 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
977 &args.alock, oh_space);
982 client = nlm_host_get_rpc(host, FALSE);
984 return (ENOLCK); /* XXX retry? */
986 xid = atomic_fetchadd_int(&nlm_xid, 1);
987 args.cookie.n_len = sizeof(xid);
988 args.cookie.n_bytes = (char*) &xid;
990 stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
992 CLNT_RELEASE(client);
994 if (stat != RPC_SUCCESS) {
1005 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
1007 if (res.stat.stat == nlm4_denied_grace_period) {
1009 * The server has recently rebooted and is
1010 * giving old clients a change to reclaim
1011 * their locks. Wait for a few seconds and try
1014 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1015 if (error && error != EWOULDBLOCK)
1021 * If we are being called via nlm_reclaim (which will
1022 * use the F_REMOTE flag), don't record the lock
1023 * operation in the local lock manager since the vnode
1026 if (!(flags & F_REMOTE))
1027 nlm_record_lock(vp, op, fl, args.alock.svid,
1028 nlm_host_get_sysid(host), size);
1035 nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
1036 rpcvers_t vers, struct timeval *timo, int retries,
1037 struct vnode *vp, int op, struct flock *fl, int flags,
1038 int svid, size_t fhlen, void *fh, off_t size)
1040 struct nlm4_testargs args;
1042 struct nlm4_testres res;
1045 enum clnt_stat stat;
1049 KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1051 memset(&args, 0, sizeof(args));
1052 memset(&res, 0, sizeof(res));
1054 exclusive = (fl->l_type == F_WRLCK);
1056 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1057 &args.alock, oh_space);
1060 args.exclusive = exclusive;
1063 client = nlm_host_get_rpc(host, FALSE);
1065 return (ENOLCK); /* XXX retry? */
1067 xid = atomic_fetchadd_int(&nlm_xid, 1);
1068 args.cookie.n_len = sizeof(xid);
1069 args.cookie.n_bytes = (char*) &xid;
1071 stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1073 CLNT_RELEASE(client);
1075 if (stat != RPC_SUCCESS) {
1083 if (res.stat.stat == nlm4_denied_grace_period) {
1085 * The server has recently rebooted and is
1086 * giving old clients a change to reclaim
1087 * their locks. Wait for a few seconds and try
1090 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1091 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1092 if (error && error != EWOULDBLOCK)
1097 if (res.stat.stat == nlm4_denied) {
1098 struct nlm4_holder *h =
1099 &res.stat.nlm4_testrply_u.holder;
1100 fl->l_start = h->l_offset;
1101 fl->l_len = h->l_len;
1102 fl->l_pid = h->svid;
1104 fl->l_type = F_WRLCK;
1106 fl->l_type = F_RDLCK;
1107 fl->l_whence = SEEK_SET;
1110 fl->l_type = F_UNLCK;
1113 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1120 nlm_map_status(nlm4_stats stat)
1129 case nlm4_denied_nolocks:
1152 static struct nlm_file_svid *
1153 nlm_find_svid(void *id)
1155 struct nlm_file_svid *ns, *newns;
1158 h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1160 mtx_lock(&nlm_svid_lock);
1161 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1162 if (ns->ns_id == id) {
1167 mtx_unlock(&nlm_svid_lock);
1169 int svid = alloc_unr(nlm_svid_allocator);
1170 newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1174 newns->ns_svid = svid;
1175 newns->ns_ucred = NULL;
1176 newns->ns_active = FALSE;
1179 * We need to check for a race with some other
1180 * thread allocating a svid for this file.
1182 mtx_lock(&nlm_svid_lock);
1183 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1184 if (ns->ns_id == id) {
1190 mtx_unlock(&nlm_svid_lock);
1191 free_unr(nlm_svid_allocator, newns->ns_svid);
1194 LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1197 mtx_unlock(&nlm_svid_lock);
1205 nlm_free_svid(struct nlm_file_svid *ns)
1208 mtx_lock(&nlm_svid_lock);
1211 KASSERT(!ns->ns_active, ("Freeing active SVID"));
1212 LIST_REMOVE(ns, ns_link);
1213 mtx_unlock(&nlm_svid_lock);
1214 free_unr(nlm_svid_allocator, ns->ns_svid);
1216 crfree(ns->ns_ucred);
1219 mtx_unlock(&nlm_svid_lock);
1224 nlm_init_lock(struct flock *fl, int flags, int svid,
1225 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1226 struct nlm4_lock *lock, char oh_space[32])
1231 if (fl->l_whence == SEEK_END) {
1233 || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1235 start = size + fl->l_start;
1236 } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1237 start = fl->l_start;
1243 if (fl->l_len < 0) {
1252 if (vers == NLM_VERS) {
1254 * Enforce range limits on V1 locks
1256 if (start > 0xffffffffLL || len > 0xffffffffLL)
1260 snprintf(oh_space, 32, "%d@", svid);
1261 oh_len = strlen(oh_space);
1262 getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
1263 oh_len = strlen(oh_space);
1265 memset(lock, 0, sizeof(*lock));
1266 lock->caller_name = prison0.pr_hostname;
1267 lock->fh.n_len = fhlen;
1268 lock->fh.n_bytes = fh;
1269 lock->oh.n_len = oh_len;
1270 lock->oh.n_bytes = oh_space;
1272 lock->l_offset = start;