2 * Copyright (c) 2008 Isilon Inc http://www.isilon.com/
3 * Authors: Doug Rabson <dfr@rabson.org>
4 * Developed with Red Inc: Alfred Perlstein <alfred@freebsd.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
37 #include <sys/lockf.h>
38 #include <sys/malloc.h>
40 #include <sys/mount.h>
41 #include <sys/mutex.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
45 #include <sys/systm.h>
46 #include <sys/unistd.h>
47 #include <sys/vnode.h>
49 #include <nfs/nfsproto.h>
50 #include <nfsclient/nfs.h>
51 #include <nfsclient/nfsmount.h>
53 #include <nlm/nlm_prot.h>
57 * We need to keep track of the svid values used for F_FLOCK locks.
59 struct nlm_file_svid {
60 int ns_refs; /* thread count + 1 if active */
61 int ns_svid; /* on-the-wire SVID for this file */
62 struct ucred *ns_ucred; /* creds to use for lock recovery */
63 void *ns_id; /* local struct file pointer */
64 bool_t ns_active; /* TRUE if we own a lock */
65 LIST_ENTRY(nlm_file_svid) ns_link;
67 LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
69 #define NLM_SVID_HASH_SIZE 256
70 struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
72 struct mtx nlm_svid_lock;
73 static struct unrhdr *nlm_svid_allocator;
74 static volatile u_int nlm_xid = 1;
76 static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
77 rpcvers_t vers, struct timeval *timo, int retries,
78 struct vnode *vp, int op, struct flock *fl, int flags,
79 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim);
80 static int nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
81 rpcvers_t vers, struct timeval *timo, int retries,
82 struct vnode *vp, int op, struct flock *fl, int flags,
83 int svid, size_t fhlen, void *fh, off_t size);
84 static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
85 rpcvers_t vers, struct timeval *timo, int retries,
86 struct vnode *vp, int op, struct flock *fl, int flags,
87 int svid, size_t fhlen, void *fh, off_t size);
88 static int nlm_map_status(nlm4_stats stat);
89 static struct nlm_file_svid *nlm_find_svid(void *id);
90 static void nlm_free_svid(struct nlm_file_svid *nf);
91 static int nlm_init_lock(struct flock *fl, int flags, int svid,
92 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
93 struct nlm4_lock *lock, char oh_space[32]);
96 nlm_client_init(void *dummy)
100 mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF);
101 nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock);
102 for (i = 0; i < NLM_SVID_HASH_SIZE; i++)
103 LIST_INIT(&nlm_file_svids[i]);
105 SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
108 nlm_msg(struct thread *td, const char *server, const char *msg, int error)
112 p = td ? td->td_proc : NULL;
114 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
117 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
122 struct nlm_feedback_arg {
124 struct nfsmount *nf_nmp;
128 nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
129 const char *msg, int error)
131 struct nfsmount *nmp = nf->nf_nmp;
135 mtx_lock(&nmp->nm_mtx);
136 if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
137 nmp->nm_state |= NFSSTA_LOCKTIMEO;
138 mtx_unlock(&nmp->nm_mtx);
139 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
142 mtx_unlock(&nmp->nm_mtx);
145 nf->nf_printed = TRUE;
146 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
150 nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
153 struct nfsmount *nmp = nf->nf_nmp;
158 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
160 mtx_lock(&nmp->nm_mtx);
161 if (nmp->nm_state & NFSSTA_LOCKTIMEO) {
162 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
163 mtx_unlock(&nmp->nm_mtx);
164 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
167 mtx_unlock(&nmp->nm_mtx);
172 nlm_feedback(int type, int proc, void *arg)
174 struct thread *td = curthread;
175 struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
178 case FEEDBACK_REXMIT2:
179 case FEEDBACK_RECONNECT:
180 nlm_down(nf, td, "lockd not responding", 0);
184 nlm_up(nf, td, "lockd is alive again");
191 * NFS advisory byte-level locks.
194 nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
195 int flags, bool_t reclaim, bool_t unlock_vp)
197 struct thread *td = curthread;
198 struct nfsmount *nmp;
203 struct sockaddr_storage ss;
204 char servername[MNAMELEN];
208 struct nlm_host *host;
209 struct rpc_callextra ext;
210 struct nlm_feedback_arg nf;
213 struct nlm_file_svid *ns;
218 ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
220 nmp = VFSTONFS(vp->v_mount);
222 * Push any pending writes to the server and flush our cache
223 * so that if we are contending with another machine for a
224 * file, we get whatever they wrote and vice-versa.
226 if (op == F_SETLK || op == F_UNLCK)
227 nmp->nm_vinvalbuf(vp, V_SAVE, td, 1);
229 strcpy(servername, nmp->nm_hostname);
230 nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size, &timo);
231 sa = (struct sockaddr *) &ss;
237 if (nmp->nm_flag & NFSMNT_SOFT)
238 retries = nmp->nm_retry;
246 * We need to switch to mount-point creds so that we can send
247 * packets from a privileged port.
250 td->td_ucred = vp->v_mount->mnt_cred;
252 host = nlm_find_host_by_name(servername, sa, vers);
253 auth = authunix_create(cred);
254 memset(&ext, 0, sizeof(ext));
256 nf.nf_printed = FALSE;
260 ext.rc_feedback = nlm_feedback;
261 ext.rc_feedback_arg = &nf;
262 ext.rc_timers = NULL;
265 if (flags & F_FLOCK) {
266 ns = nlm_find_svid(id);
267 KASSERT(fl->l_start == 0 && fl->l_len == 0,
268 ("F_FLOCK lock requests must be whole-file locks"));
271 * Remember the creds used for locking in case
272 * we need to recover the lock later.
274 ns->ns_ucred = crdup(cred);
277 } else if (flags & F_REMOTE) {
279 * If we are recovering after a server restart or
280 * trashing locks on a force unmount, use the same
285 svid = ((struct proc *) id)->p_pid;
290 if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
291 && fl->l_type == F_WRLCK) {
293 * The semantics for flock(2) require that any
294 * shared lock on the file must be released
295 * before an exclusive lock is granted. The
296 * local locking code interprets this by
297 * unlocking the file before sleeping on a
298 * blocked exclusive lock request. We
299 * approximate this by first attempting
300 * non-blocking and if that fails, we unlock
301 * the file and block.
303 error = nlm_setlock(host, &ext, vers, &timo, retries,
304 vp, F_SETLK, fl, flags & ~F_WAIT,
305 svid, fhlen, &fh.fh_bytes, size, reclaim);
306 if (error == EAGAIN) {
307 fl->l_type = F_UNLCK;
308 error = nlm_clearlock(host, &ext, vers, &timo,
309 retries, vp, F_UNLCK, fl, flags,
310 svid, fhlen, &fh.fh_bytes, size);
311 fl->l_type = F_WRLCK;
313 mtx_lock(&nlm_svid_lock);
316 ns->ns_active = FALSE;
318 mtx_unlock(&nlm_svid_lock);
320 error = nlm_setlock(host, &ext, vers,
321 &timo, retries, vp, F_SETLK, fl,
322 flags, svid, fhlen, &fh.fh_bytes,
327 error = nlm_setlock(host, &ext, vers, &timo, retries,
328 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
332 mtx_lock(&nlm_svid_lock);
333 if (!ns->ns_active) {
335 * Add one to the reference count to
336 * hold onto the SVID for the lifetime
337 * of the lock. Note that since
338 * F_FLOCK only supports whole-file
339 * locks, there can only be one active
340 * lock for this SVID.
343 ns->ns_active = TRUE;
345 mtx_unlock(&nlm_svid_lock);
350 error = nlm_clearlock(host, &ext, vers, &timo, retries,
351 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
353 mtx_lock(&nlm_svid_lock);
356 ns->ns_active = FALSE;
358 mtx_unlock(&nlm_svid_lock);
363 error = nlm_getlock(host, &ext, vers, &timo, retries,
364 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
378 nlm_host_release(host);
384 nlm_advlock(struct vop_advlock_args *ap)
387 return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
388 ap->a_flags, FALSE, TRUE));
392 * Set the creds of td to the creds of the given lock's owner. The new
393 * creds reference count will be incremented via crhold. The caller is
394 * responsible for calling crfree and restoring td's original creds.
397 nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
400 struct nlm_file_svid *ns;
405 if (fl->l_pid > PID_MAX) {
407 * If this was originally a F_FLOCK-style lock, we
408 * recorded the creds used when it was originally
409 * locked in the nlm_file_svid structure.
411 mtx_lock(&nlm_svid_lock);
412 for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
413 for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
414 ns = LIST_NEXT(ns, ns_link)) {
415 if (ns->ns_svid == fl->l_pid) {
416 cred = crhold(ns->ns_ucred);
421 mtx_unlock(&nlm_svid_lock);
424 * This lock is owned by a process. Get a reference to
427 p = pfind(fl->l_pid);
429 cred = crhold(p->p_ucred);
435 * If we can't find a cred, fall back on the recovery
439 cred = crhold(td->td_ucred);
446 nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
449 struct thread *td = curthread;
450 struct ucred *oldcred;
454 newfl.l_type = F_UNLCK;
456 oldcred = td->td_ucred;
457 nlm_set_creds_for_lock(td, &newfl);
459 error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
462 crfree(td->td_ucred);
463 td->td_ucred = oldcred;
469 nlm_reclaim(struct vop_reclaim_args *ap)
472 nlm_cancel_wait(ap->a_vp);
473 lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
477 struct nlm_recovery_context {
478 struct nlm_host *nr_host; /* host we are recovering */
479 int nr_state; /* remote NSM state for recovery */
483 nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
485 struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
486 struct thread *td = curthread;
487 struct ucred *oldcred;
491 * If the remote NSM state changes during recovery, the host
492 * must have rebooted a second time. In that case, we must
493 * restart the recovery.
495 state = nlm_host_get_state(nr->nr_host);
496 if (nr->nr_state != state)
499 error = vn_lock(vp, LK_SHARED);
503 oldcred = td->td_ucred;
504 nlm_set_creds_for_lock(td, fl);
506 error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
509 crfree(td->td_ucred);
510 td->td_ucred = oldcred;
516 nlm_client_recovery(struct nlm_host *host)
518 struct nlm_recovery_context nr;
521 sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
524 nr.nr_state = nlm_host_get_state(host);
525 error = lf_iteratelocks_sysid(sysid,
526 nlm_client_recover_lock, &nr);
527 } while (error == ERESTART);
531 nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
534 dst->caller_name = src->caller_name;
537 dst->svid = src->svid;
538 dst->l_offset = src->l_offset;
539 dst->l_len = src->l_len;
543 nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
546 dst->exclusive = src->exclusive;
547 dst->svid = src->svid;
549 dst->l_offset = src->l_offset;
550 dst->l_len = src->l_len;
554 nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
556 dst->cookie = src->cookie;
557 dst->stat.stat = (enum nlm4_stats) src->stat.stat;
560 static enum clnt_stat
561 nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
562 struct rpc_callextra *ext, struct timeval timo)
564 if (vers == NLM_VERS4) {
565 return nlm4_test_4(args, res, client, ext, timo);
571 args1.cookie = args->cookie;
572 args1.exclusive = args->exclusive;
573 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
574 memset(&res1, 0, sizeof(res1));
576 stat = nlm_test_1(&args1, &res1, client, ext, timo);
578 if (stat == RPC_SUCCESS) {
579 res->cookie = res1.cookie;
580 res->stat.stat = (enum nlm4_stats) res1.stat.stat;
581 if (res1.stat.stat == nlm_denied)
582 nlm_convert_to_nlm4_holder(
583 &res->stat.nlm4_testrply_u.holder,
584 &res1.stat.nlm_testrply_u.holder);
591 static enum clnt_stat
592 nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
593 struct rpc_callextra *ext, struct timeval timo)
595 if (vers == NLM_VERS4) {
596 return nlm4_lock_4(args, res, client, ext, timo);
602 args1.cookie = args->cookie;
603 args1.block = args->block;
604 args1.exclusive = args->exclusive;
605 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
606 args1.reclaim = args->reclaim;
607 args1.state = args->state;
608 memset(&res1, 0, sizeof(res1));
610 stat = nlm_lock_1(&args1, &res1, client, ext, timo);
612 if (stat == RPC_SUCCESS) {
613 nlm_convert_to_nlm4_res(res, &res1);
620 static enum clnt_stat
621 nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
622 struct rpc_callextra *ext, struct timeval timo)
624 if (vers == NLM_VERS4) {
625 return nlm4_cancel_4(args, res, client, ext, timo);
631 args1.cookie = args->cookie;
632 args1.block = args->block;
633 args1.exclusive = args->exclusive;
634 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
635 memset(&res1, 0, sizeof(res1));
637 stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
639 if (stat == RPC_SUCCESS) {
640 nlm_convert_to_nlm4_res(res, &res1);
647 static enum clnt_stat
648 nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
649 struct rpc_callextra *ext, struct timeval timo)
651 if (vers == NLM_VERS4) {
652 return nlm4_unlock_4(args, res, client, ext, timo);
654 nlm_unlockargs args1;
658 args1.cookie = args->cookie;
659 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
660 memset(&res1, 0, sizeof(res1));
662 stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
664 if (stat == RPC_SUCCESS) {
665 nlm_convert_to_nlm4_res(res, &res1);
673 * Called after a lock request (set or clear) succeeded. We record the
674 * details in the local lock manager. Note that since the remote
675 * server has granted the lock, we can be sure that it doesn't
676 * conflict with any other locks we have in the local lock manager.
678 * Since it is possible that host may also make NLM client requests to
679 * our NLM server, we use a different sysid value to record our own
682 * Note that since it is possible for us to receive replies from the
683 * server in a different order than the locks were granted (e.g. if
684 * many local threads are contending for the same lock), we must use a
685 * blocking operation when registering with the local lock manager.
686 * We expect that any actual wait will be rare and short hence we
687 * ignore signals for this.
690 nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
691 int svid, int sysid, off_t size)
693 struct vop_advlockasync_args a;
701 a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
704 newfl.l_start = fl->l_start;
705 newfl.l_len = fl->l_len;
706 newfl.l_type = fl->l_type;
707 newfl.l_whence = fl->l_whence;
709 newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
711 error = lf_advlockasync(&a, &vp->v_lockf, size);
712 KASSERT(error == 0 || error == ENOENT,
713 ("Failed to register NFS lock locally - error=%d", error));
717 nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
718 rpcvers_t vers, struct timeval *timo, int retries,
719 struct vnode *vp, int op, struct flock *fl, int flags,
720 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
722 struct nlm4_lockargs args;
728 int retry, block, exclusive;
729 void *wait_handle = NULL;
732 memset(&args, 0, sizeof(args));
733 memset(&res, 0, sizeof(res));
735 block = (flags & F_WAIT) ? TRUE : FALSE;
736 exclusive = (fl->l_type == F_WRLCK);
738 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
739 &args.alock, oh_space);
743 args.exclusive = exclusive;
744 args.reclaim = reclaim;
745 args.state = nlm_nsm_state;
749 client = nlm_host_get_rpc(host, FALSE);
751 return (ENOLCK); /* XXX retry? */
754 wait_handle = nlm_register_wait_lock(&args.alock, vp);
756 xid = atomic_fetchadd_int(&nlm_xid, 1);
757 args.cookie.n_len = sizeof(xid);
758 args.cookie.n_bytes = (char*) &xid;
760 stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
762 CLNT_RELEASE(client);
764 if (stat != RPC_SUCCESS) {
766 nlm_deregister_wait_lock(wait_handle);
777 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
779 if (block && res.stat.stat != nlm4_blocked)
780 nlm_deregister_wait_lock(wait_handle);
782 if (res.stat.stat == nlm4_denied_grace_period) {
784 * The server has recently rebooted and is
785 * giving old clients a change to reclaim
786 * their locks. Wait for a few seconds and try
789 error = tsleep(&args, PCATCH, "nlmgrace", retry);
790 if (error && error != EWOULDBLOCK)
798 if (block && res.stat.stat == nlm4_blocked) {
800 * The server should call us back with a
801 * granted message when the lock succeeds. In
802 * order to deal with broken servers, lost
803 * granted messages and server reboots, we
804 * will also re-try every few seconds.
806 error = nlm_wait_lock(wait_handle, retry);
807 if (error == EWOULDBLOCK) {
815 * We need to call the server to
816 * cancel our lock request.
818 nlm4_cancargs cancel;
820 memset(&cancel, 0, sizeof(cancel));
822 xid = atomic_fetchadd_int(&nlm_xid, 1);
823 cancel.cookie.n_len = sizeof(xid);
824 cancel.cookie.n_bytes = (char*) &xid;
825 cancel.block = block;
826 cancel.exclusive = exclusive;
827 cancel.alock = args.alock;
830 client = nlm_host_get_rpc(host, FALSE);
835 stat = nlm_cancel_rpc(vers, &cancel,
836 &res, client, ext, *timo);
838 CLNT_RELEASE(client);
840 if (stat != RPC_SUCCESS) {
846 * reboots. This means
852 pause("nlmcancel", 10*hz);
854 } while (stat != RPC_SUCCESS);
859 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
861 switch (res.stat.stat) {
873 case nlm4_denied_grace_period:
876 * recently rebooted -
902 error = nlm_map_status(res.stat.stat);
905 if (!error && !reclaim) {
906 nlm_record_lock(vp, op, fl, args.alock.svid,
907 nlm_host_get_sysid(host), size);
908 nlm_host_monitor(host, 0);
916 nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
917 rpcvers_t vers, struct timeval *timo, int retries,
918 struct vnode *vp, int op, struct flock *fl, int flags,
919 int svid, size_t fhlen, void *fh, off_t size)
921 struct nlm4_unlockargs args;
929 memset(&args, 0, sizeof(args));
930 memset(&res, 0, sizeof(res));
932 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
933 &args.alock, oh_space);
938 client = nlm_host_get_rpc(host, FALSE);
940 return (ENOLCK); /* XXX retry? */
942 xid = atomic_fetchadd_int(&nlm_xid, 1);
943 args.cookie.n_len = sizeof(xid);
944 args.cookie.n_bytes = (char*) &xid;
946 stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
948 CLNT_RELEASE(client);
950 if (stat != RPC_SUCCESS) {
961 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
963 if (res.stat.stat == nlm4_denied_grace_period) {
965 * The server has recently rebooted and is
966 * giving old clients a change to reclaim
967 * their locks. Wait for a few seconds and try
970 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
971 if (error && error != EWOULDBLOCK)
977 * If we are being called via nlm_reclaim (which will
978 * use the F_REMOTE flag), don't record the lock
979 * operation in the local lock manager since the vnode
982 if (!(flags & F_REMOTE))
983 nlm_record_lock(vp, op, fl, args.alock.svid,
984 nlm_host_get_sysid(host), size);
991 nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
992 rpcvers_t vers, struct timeval *timo, int retries,
993 struct vnode *vp, int op, struct flock *fl, int flags,
994 int svid, size_t fhlen, void *fh, off_t size)
996 struct nlm4_testargs args;
998 struct nlm4_testres res;
1001 enum clnt_stat stat;
1005 KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1007 memset(&args, 0, sizeof(args));
1008 memset(&res, 0, sizeof(res));
1010 exclusive = (fl->l_type == F_WRLCK);
1012 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1013 &args.alock, oh_space);
1016 args.exclusive = exclusive;
1019 client = nlm_host_get_rpc(host, FALSE);
1021 return (ENOLCK); /* XXX retry? */
1023 xid = atomic_fetchadd_int(&nlm_xid, 1);
1024 args.cookie.n_len = sizeof(xid);
1025 args.cookie.n_bytes = (char*) &xid;
1027 stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1029 CLNT_RELEASE(client);
1031 if (stat != RPC_SUCCESS) {
1039 if (res.stat.stat == nlm4_denied_grace_period) {
1041 * The server has recently rebooted and is
1042 * giving old clients a change to reclaim
1043 * their locks. Wait for a few seconds and try
1046 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1047 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1048 if (error && error != EWOULDBLOCK)
1053 if (res.stat.stat == nlm4_denied) {
1054 struct nlm4_holder *h =
1055 &res.stat.nlm4_testrply_u.holder;
1056 fl->l_start = h->l_offset;
1057 fl->l_len = h->l_len;
1058 fl->l_pid = h->svid;
1060 fl->l_type = F_WRLCK;
1062 fl->l_type = F_RDLCK;
1063 fl->l_whence = SEEK_SET;
1066 fl->l_type = F_UNLCK;
1069 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1076 nlm_map_status(nlm4_stats stat)
1085 case nlm4_denied_nolocks:
1108 static struct nlm_file_svid *
1109 nlm_find_svid(void *id)
1111 struct nlm_file_svid *ns, *newns;
1114 h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1116 mtx_lock(&nlm_svid_lock);
1117 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1118 if (ns->ns_id == id) {
1123 mtx_unlock(&nlm_svid_lock);
1125 int svid = alloc_unr(nlm_svid_allocator);
1126 newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1130 newns->ns_svid = svid;
1131 newns->ns_ucred = NULL;
1132 newns->ns_active = FALSE;
1135 * We need to check for a race with some other
1136 * thread allocating a svid for this file.
1138 mtx_lock(&nlm_svid_lock);
1139 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1140 if (ns->ns_id == id) {
1146 mtx_unlock(&nlm_svid_lock);
1147 free_unr(nlm_svid_allocator, newns->ns_svid);
1150 LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1153 mtx_unlock(&nlm_svid_lock);
1161 nlm_free_svid(struct nlm_file_svid *ns)
1164 mtx_lock(&nlm_svid_lock);
1167 KASSERT(!ns->ns_active, ("Freeing active SVID"));
1168 LIST_REMOVE(ns, ns_link);
1169 mtx_unlock(&nlm_svid_lock);
1170 free_unr(nlm_svid_allocator, ns->ns_svid);
1172 crfree(ns->ns_ucred);
1175 mtx_unlock(&nlm_svid_lock);
1180 nlm_init_lock(struct flock *fl, int flags, int svid,
1181 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1182 struct nlm4_lock *lock, char oh_space[32])
1187 if (fl->l_whence == SEEK_END) {
1189 || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1191 start = size + fl->l_start;
1192 } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1193 start = fl->l_start;
1199 if (fl->l_len < 0) {
1208 if (vers == NLM_VERS) {
1210 * Enforce range limits on V1 locks
1212 if (start > 0xffffffffLL || len > 0xffffffffLL)
1216 snprintf(oh_space, 32, "%d@", svid);
1217 oh_len = strlen(oh_space);
1218 getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
1219 oh_len = strlen(oh_space);
1221 memset(lock, 0, sizeof(*lock));
1222 lock->caller_name = prison0.pr_hostname;
1223 lock->fh.n_len = fhlen;
1224 lock->fh.n_bytes = fh;
1225 lock->oh.n_len = oh_len;
1226 lock->oh.n_bytes = oh_space;
1228 lock->l_offset = start;