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>
33 #include <sys/kernel.h>
34 #include <sys/limits.h>
36 #include <sys/lockf.h>
37 #include <sys/malloc.h>
38 #include <sys/mount.h>
39 #include <sys/mutex.h>
41 #include <sys/syslog.h>
42 #include <sys/systm.h>
43 #include <sys/unistd.h>
44 #include <sys/vnode.h>
46 #include <rpc/rpcclnt.h>
47 #include <nfs/nfsproto.h>
48 #include <nfsclient/nfs.h>
49 #include <nfsclient/nfsnode.h>
50 #include <nfsclient/nfsmount.h>
52 #include <nlm/nlm_prot.h>
56 * We need to keep track of the svid values used for F_FLOCK locks.
58 struct nlm_file_svid {
59 int ns_refs; /* thread count + 1 if active */
60 int ns_svid; /* on-the-wire SVID for this file */
61 struct ucred *ns_ucred; /* creds to use for lock recovery */
62 void *ns_id; /* local struct file pointer */
63 bool_t ns_active; /* TRUE if we own a lock */
64 LIST_ENTRY(nlm_file_svid) ns_link;
66 LIST_HEAD(nlm_file_svid_list, nlm_file_svid);
68 #define NLM_SVID_HASH_SIZE 256
69 struct nlm_file_svid_list nlm_file_svids[NLM_SVID_HASH_SIZE];
71 struct mtx nlm_svid_lock;
72 static struct unrhdr *nlm_svid_allocator;
73 static volatile u_int nlm_xid = 1;
75 static int nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
76 rpcvers_t vers, struct timeval *timo, int retries,
77 struct vnode *vp, int op, struct flock *fl, int flags,
78 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim);
79 static int nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
80 rpcvers_t vers, struct timeval *timo, int retries,
81 struct vnode *vp, int op, struct flock *fl, int flags,
82 int svid, size_t fhlen, void *fh, off_t size);
83 static int nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
84 rpcvers_t vers, struct timeval *timo, int retries,
85 struct vnode *vp, int op, struct flock *fl, int flags,
86 int svid, size_t fhlen, void *fh, off_t size);
87 static int nlm_map_status(nlm4_stats stat);
88 static struct nlm_file_svid *nlm_find_svid(void *id);
89 static void nlm_free_svid(struct nlm_file_svid *nf);
90 static int nlm_init_lock(struct flock *fl, int flags, int svid,
91 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
92 struct nlm4_lock *lock, char oh_space[32]);
95 nlm_client_init(void *dummy)
99 mtx_init(&nlm_svid_lock, "NLM svid lock", NULL, MTX_DEF);
100 nlm_svid_allocator = new_unrhdr(PID_MAX + 2, INT_MAX, &nlm_svid_lock);
101 for (i = 0; i < NLM_SVID_HASH_SIZE; i++)
102 LIST_INIT(&nlm_file_svids[i]);
104 SYSINIT(nlm_client_init, SI_SUB_LOCK, SI_ORDER_FIRST, nlm_client_init, NULL);
107 nlm_msg(struct thread *td, const char *server, const char *msg, int error)
111 p = td ? td->td_proc : NULL;
113 tprintf(p, LOG_INFO, "nfs server %s: %s, error %d\n", server,
116 tprintf(p, LOG_INFO, "nfs server %s: %s\n", server, msg);
121 struct nlm_feedback_arg {
123 struct nfsmount *nf_nmp;
127 nlm_down(struct nlm_feedback_arg *nf, struct thread *td,
128 const char *msg, int error)
130 struct nfsmount *nmp = nf->nf_nmp;
134 mtx_lock(&nmp->nm_mtx);
135 if (!(nmp->nm_state & NFSSTA_LOCKTIMEO)) {
136 nmp->nm_state |= NFSSTA_LOCKTIMEO;
137 mtx_unlock(&nmp->nm_mtx);
138 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
141 mtx_unlock(&nmp->nm_mtx);
144 nf->nf_printed = TRUE;
145 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, error);
149 nlm_up(struct nlm_feedback_arg *nf, struct thread *td,
152 struct nfsmount *nmp = nf->nf_nmp;
157 nlm_msg(td, nmp->nm_mountp->mnt_stat.f_mntfromname, msg, 0);
159 mtx_lock(&nmp->nm_mtx);
160 if (nmp->nm_state & NFSSTA_LOCKTIMEO) {
161 nmp->nm_state &= ~NFSSTA_LOCKTIMEO;
162 mtx_unlock(&nmp->nm_mtx);
163 vfs_event_signal(&nmp->nm_mountp->mnt_stat.f_fsid,
166 mtx_unlock(&nmp->nm_mtx);
171 nlm_feedback(int type, int proc, void *arg)
173 struct thread *td = curthread;
174 struct nlm_feedback_arg *nf = (struct nlm_feedback_arg *) arg;
177 case FEEDBACK_REXMIT2:
178 case FEEDBACK_RECONNECT:
179 nlm_down(nf, td, "lockd not responding", 0);
183 nlm_up(nf, td, "lockd is alive again");
190 * NFS advisory byte-level locks.
193 nlm_advlock_internal(struct vnode *vp, void *id, int op, struct flock *fl,
194 int flags, bool_t reclaim, bool_t unlock_vp)
196 struct thread *td = curthread;
197 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;
217 ASSERT_VOP_LOCKED(vp, "nlm_advlock_1");
220 * Push any pending writes to the server and flush our cache
221 * so that if we are contending with another machine for a
222 * file, we get whatever they wrote and vice-versa.
224 if (op == F_SETLK || op == F_UNLCK)
225 nfs_vinvalbuf(vp, V_SAVE, td, 1);
228 nmp = VFSTONFS(vp->v_mount);
231 memcpy(&ss, sa, sa->sa_len);
232 sa = (struct sockaddr *) &ss;
233 mtx_lock(&hostname_mtx);
234 strcpy(servername, nmp->nm_hostname);
235 mtx_unlock(&hostname_mtx);
236 fhlen = np->n_fhsize;
237 memcpy(&fh.fh_bytes, np->n_fhp, fhlen);
238 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
239 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * (1000000 / NFS_HZ);
245 if (nmp->nm_flag & NFSMNT_SOFT)
246 retries = nmp->nm_retry;
254 * We need to switch to mount-point creds so that we can send
255 * packets from a privileged port.
258 td->td_ucred = vp->v_mount->mnt_cred;
260 host = nlm_find_host_by_name(servername, sa, vers);
261 auth = authunix_create(cred);
262 memset(&ext, 0, sizeof(ext));
264 nf.nf_printed = FALSE;
268 ext.rc_feedback = nlm_feedback;
269 ext.rc_feedback_arg = &nf;
272 if (flags & F_FLOCK) {
273 ns = nlm_find_svid(id);
274 KASSERT(fl->l_start == 0 && fl->l_len == 0,
275 ("F_FLOCK lock requests must be whole-file locks"));
278 * Remember the creds used for locking in case
279 * we need to recover the lock later.
281 ns->ns_ucred = crdup(cred);
284 } else if (flags & F_REMOTE) {
286 * If we are recovering after a server restart or
287 * trashing locks on a force unmount, use the same
292 svid = ((struct proc *) id)->p_pid;
297 if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
298 && fl->l_type == F_WRLCK) {
300 * The semantics for flock(2) require that any
301 * shared lock on the file must be released
302 * before an exclusive lock is granted. The
303 * local locking code interprets this by
304 * unlocking the file before sleeping on a
305 * blocked exclusive lock request. We
306 * approximate this by first attempting
307 * non-blocking and if that fails, we unlock
308 * the file and block.
310 error = nlm_setlock(host, &ext, vers, &timo, retries,
311 vp, F_SETLK, fl, flags & ~F_WAIT,
312 svid, fhlen, &fh.fh_bytes, size, reclaim);
313 if (error == EAGAIN) {
314 fl->l_type = F_UNLCK;
315 error = nlm_clearlock(host, &ext, vers, &timo,
316 retries, vp, F_UNLCK, fl, flags,
317 svid, fhlen, &fh.fh_bytes, size);
318 fl->l_type = F_WRLCK;
320 mtx_lock(&nlm_svid_lock);
323 ns->ns_active = FALSE;
325 mtx_unlock(&nlm_svid_lock);
327 error = nlm_setlock(host, &ext, vers,
328 &timo, retries, vp, F_SETLK, fl,
329 flags, svid, fhlen, &fh.fh_bytes,
334 error = nlm_setlock(host, &ext, vers, &timo, retries,
335 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
339 mtx_lock(&nlm_svid_lock);
340 if (!ns->ns_active) {
342 * Add one to the reference count to
343 * hold onto the SVID for the lifetime
344 * of the lock. Note that since
345 * F_FLOCK only supports whole-file
346 * locks, there can only be one active
347 * lock for this SVID.
350 ns->ns_active = TRUE;
352 mtx_unlock(&nlm_svid_lock);
357 error = nlm_clearlock(host, &ext, vers, &timo, retries,
358 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
360 mtx_lock(&nlm_svid_lock);
363 ns->ns_active = FALSE;
365 mtx_unlock(&nlm_svid_lock);
370 error = nlm_getlock(host, &ext, vers, &timo, retries,
371 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
385 nlm_host_release(host);
391 nlm_advlock(struct vop_advlock_args *ap)
394 return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
395 ap->a_flags, FALSE, TRUE));
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.
404 nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
407 struct nlm_file_svid *ns;
412 if (fl->l_pid > PID_MAX) {
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.
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);
428 mtx_unlock(&nlm_svid_lock);
431 * This lock is owned by a process. Get a reference to
434 p = pfind(fl->l_pid);
436 cred = crhold(p->p_ucred);
442 * If we can't find a cred, fall back on the recovery
446 cred = crhold(td->td_ucred);
453 nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
456 struct thread *td = curthread;
457 struct ucred *oldcred;
461 newfl.l_type = F_UNLCK;
463 oldcred = td->td_ucred;
464 nlm_set_creds_for_lock(td, &newfl);
466 error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
469 crfree(td->td_ucred);
470 td->td_ucred = oldcred;
476 nlm_reclaim(struct vop_reclaim_args *ap)
479 nlm_cancel_wait(ap->a_vp);
480 lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
484 struct nlm_recovery_context {
485 struct nlm_host *nr_host; /* host we are recovering */
486 int nr_state; /* remote NSM state for recovery */
490 nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
492 struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
493 struct thread *td = curthread;
494 struct ucred *oldcred;
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.
502 state = nlm_host_get_state(nr->nr_host);
503 if (nr->nr_state != state)
506 error = vn_lock(vp, LK_SHARED);
510 oldcred = td->td_ucred;
511 nlm_set_creds_for_lock(td, fl);
513 error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
516 crfree(td->td_ucred);
517 td->td_ucred = oldcred;
523 nlm_client_recovery(struct nlm_host *host)
525 struct nlm_recovery_context nr;
528 sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(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);
538 nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
541 dst->caller_name = src->caller_name;
544 dst->svid = src->svid;
545 dst->l_offset = src->l_offset;
546 dst->l_len = src->l_len;
550 nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
553 dst->exclusive = src->exclusive;
554 dst->svid = src->svid;
556 dst->l_offset = src->l_offset;
557 dst->l_len = src->l_len;
561 nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
563 dst->cookie = src->cookie;
564 dst->stat.stat = (enum nlm4_stats) src->stat.stat;
567 static enum clnt_stat
568 nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
569 struct rpc_callextra *ext, struct timeval timo)
571 if (vers == NLM_VERS4) {
572 return nlm4_test_4(args, res, client, ext, timo);
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));
583 stat = nlm_test_1(&args1, &res1, client, ext, timo);
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);
598 static enum clnt_stat
599 nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
600 struct rpc_callextra *ext, struct timeval timo)
602 if (vers == NLM_VERS4) {
603 return nlm4_lock_4(args, res, client, ext, timo);
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));
617 stat = nlm_lock_1(&args1, &res1, client, ext, timo);
619 if (stat == RPC_SUCCESS) {
620 nlm_convert_to_nlm4_res(res, &res1);
627 static enum clnt_stat
628 nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
629 struct rpc_callextra *ext, struct timeval timo)
631 if (vers == NLM_VERS4) {
632 return nlm4_cancel_4(args, res, client, ext, timo);
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));
644 stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
646 if (stat == RPC_SUCCESS) {
647 nlm_convert_to_nlm4_res(res, &res1);
654 static enum clnt_stat
655 nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
656 struct rpc_callextra *ext, struct timeval timo)
658 if (vers == NLM_VERS4) {
659 return nlm4_unlock_4(args, res, client, ext, timo);
661 nlm_unlockargs args1;
665 args1.cookie = args->cookie;
666 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
667 memset(&res1, 0, sizeof(res1));
669 stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
671 if (stat == RPC_SUCCESS) {
672 nlm_convert_to_nlm4_res(res, &res1);
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.
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
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.
697 nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
698 int svid, int sysid, off_t size)
700 struct vop_advlockasync_args a;
708 a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
711 newfl.l_start = fl->l_start;
712 newfl.l_len = fl->l_len;
713 newfl.l_type = fl->l_type;
714 newfl.l_whence = fl->l_whence;
716 newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
718 error = lf_advlockasync(&a, &vp->v_lockf, size);
719 KASSERT(error == 0, ("Failed to register NFS lock locally - error=%d",
724 nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
725 rpcvers_t vers, struct timeval *timo, int retries,
726 struct vnode *vp, int op, struct flock *fl, int flags,
727 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
729 struct nlm4_lockargs args;
735 int retry, block, exclusive;
736 void *wait_handle = NULL;
739 memset(&args, 0, sizeof(args));
740 memset(&res, 0, sizeof(res));
742 block = (flags & F_WAIT) ? TRUE : FALSE;
743 exclusive = (fl->l_type == F_WRLCK);
745 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
746 &args.alock, oh_space);
750 args.exclusive = exclusive;
751 args.reclaim = reclaim;
752 args.state = nlm_nsm_state;
756 client = nlm_host_get_rpc(host);
758 return (ENOLCK); /* XXX retry? */
761 wait_handle = nlm_register_wait_lock(&args.alock, vp);
763 xid = atomic_fetchadd_int(&nlm_xid, 1);
764 args.cookie.n_len = sizeof(xid);
765 args.cookie.n_bytes = (char*) &xid;
767 stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
769 CLNT_RELEASE(client);
771 if (stat != RPC_SUCCESS) {
773 nlm_deregister_wait_lock(wait_handle);
784 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
786 if (block && res.stat.stat != nlm4_blocked)
787 nlm_deregister_wait_lock(wait_handle);
789 if (res.stat.stat == nlm4_denied_grace_period) {
791 * The server has recently rebooted and is
792 * giving old clients a change to reclaim
793 * their locks. Wait for a few seconds and try
796 error = tsleep(&args, PCATCH, "nlmgrace", retry);
797 if (error && error != EWOULDBLOCK)
805 if (block && res.stat.stat == nlm4_blocked) {
807 * The server should call us back with a
808 * granted message when the lock succeeds. In
809 * order to deal with broken servers, lost
810 * granted messages and server reboots, we
811 * will also re-try every few seconds.
813 error = nlm_wait_lock(wait_handle, retry);
814 if (error == EWOULDBLOCK) {
822 * We need to call the server to
823 * cancel our lock request.
825 nlm4_cancargs cancel;
827 memset(&cancel, 0, sizeof(cancel));
829 xid = atomic_fetchadd_int(&nlm_xid, 1);
830 cancel.cookie.n_len = sizeof(xid);
831 cancel.cookie.n_bytes = (char*) &xid;
832 cancel.block = block;
833 cancel.exclusive = exclusive;
834 cancel.alock = args.alock;
837 client = nlm_host_get_rpc(host);
842 stat = nlm_cancel_rpc(vers, &cancel,
843 &res, client, ext, *timo);
845 CLNT_RELEASE(client);
847 if (stat != RPC_SUCCESS) {
853 * reboots. This means
859 pause("nlmcancel", 10*hz);
861 } while (stat != RPC_SUCCESS);
866 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
868 switch (res.stat.stat) {
880 case nlm4_denied_grace_period:
883 * recently rebooted -
909 error = nlm_map_status(res.stat.stat);
912 if (!error && !reclaim) {
913 nlm_record_lock(vp, op, fl, args.alock.svid,
914 nlm_host_get_sysid(host), size);
915 nlm_host_monitor(host, 0);
923 nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
924 rpcvers_t vers, struct timeval *timo, int retries,
925 struct vnode *vp, int op, struct flock *fl, int flags,
926 int svid, size_t fhlen, void *fh, off_t size)
928 struct nlm4_unlockargs args;
936 memset(&args, 0, sizeof(args));
937 memset(&res, 0, sizeof(res));
939 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
940 &args.alock, oh_space);
945 client = nlm_host_get_rpc(host);
947 return (ENOLCK); /* XXX retry? */
949 xid = atomic_fetchadd_int(&nlm_xid, 1);
950 args.cookie.n_len = sizeof(xid);
951 args.cookie.n_bytes = (char*) &xid;
953 stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
955 CLNT_RELEASE(client);
957 if (stat != RPC_SUCCESS) {
968 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
970 if (res.stat.stat == nlm4_denied_grace_period) {
972 * The server has recently rebooted and is
973 * giving old clients a change to reclaim
974 * their locks. Wait for a few seconds and try
977 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
978 if (error && error != EWOULDBLOCK)
984 * If we are being called via nlm_reclaim (which will
985 * use the F_REMOTE flag), don't record the lock
986 * operation in the local lock manager since the vnode
989 if (!(flags & F_REMOTE))
990 nlm_record_lock(vp, op, fl, args.alock.svid,
991 nlm_host_get_sysid(host), size);
998 nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
999 rpcvers_t vers, struct timeval *timo, int retries,
1000 struct vnode *vp, int op, struct flock *fl, int flags,
1001 int svid, size_t fhlen, void *fh, off_t size)
1003 struct nlm4_testargs args;
1005 struct nlm4_testres res;
1008 enum clnt_stat stat;
1012 KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1014 memset(&args, 0, sizeof(args));
1015 memset(&res, 0, sizeof(res));
1017 exclusive = (fl->l_type == F_WRLCK);
1019 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1020 &args.alock, oh_space);
1023 args.exclusive = exclusive;
1026 client = nlm_host_get_rpc(host);
1028 return (ENOLCK); /* XXX retry? */
1030 xid = atomic_fetchadd_int(&nlm_xid, 1);
1031 args.cookie.n_len = sizeof(xid);
1032 args.cookie.n_bytes = (char*) &xid;
1034 stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1036 CLNT_RELEASE(client);
1038 if (stat != RPC_SUCCESS) {
1046 if (res.stat.stat == nlm4_denied_grace_period) {
1048 * The server has recently rebooted and is
1049 * giving old clients a change to reclaim
1050 * their locks. Wait for a few seconds and try
1053 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1054 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1055 if (error && error != EWOULDBLOCK)
1060 if (res.stat.stat == nlm4_denied) {
1061 struct nlm4_holder *h =
1062 &res.stat.nlm4_testrply_u.holder;
1063 fl->l_start = h->l_offset;
1064 fl->l_len = h->l_len;
1065 fl->l_pid = h->svid;
1067 fl->l_type = F_WRLCK;
1069 fl->l_type = F_RDLCK;
1070 fl->l_whence = SEEK_SET;
1073 fl->l_type = F_UNLCK;
1076 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1083 nlm_map_status(nlm4_stats stat)
1092 case nlm4_denied_nolocks:
1115 static struct nlm_file_svid *
1116 nlm_find_svid(void *id)
1118 struct nlm_file_svid *ns, *newns;
1121 h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1123 mtx_lock(&nlm_svid_lock);
1124 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1125 if (ns->ns_id == id) {
1130 mtx_unlock(&nlm_svid_lock);
1132 int svid = alloc_unr(nlm_svid_allocator);
1133 newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1137 newns->ns_svid = svid;
1138 newns->ns_ucred = NULL;
1139 newns->ns_active = FALSE;
1142 * We need to check for a race with some other
1143 * thread allocating a svid for this file.
1145 mtx_lock(&nlm_svid_lock);
1146 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1147 if (ns->ns_id == id) {
1153 mtx_unlock(&nlm_svid_lock);
1154 free_unr(nlm_svid_allocator, newns->ns_svid);
1157 LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1160 mtx_unlock(&nlm_svid_lock);
1168 nlm_free_svid(struct nlm_file_svid *ns)
1171 mtx_lock(&nlm_svid_lock);
1174 KASSERT(!ns->ns_active, ("Freeing active SVID"));
1175 LIST_REMOVE(ns, ns_link);
1176 mtx_unlock(&nlm_svid_lock);
1177 free_unr(nlm_svid_allocator, ns->ns_svid);
1179 crfree(ns->ns_ucred);
1182 mtx_unlock(&nlm_svid_lock);
1187 nlm_init_lock(struct flock *fl, int flags, int svid,
1188 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1189 struct nlm4_lock *lock, char oh_space[32])
1194 if (fl->l_whence == SEEK_END) {
1196 || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1198 start = size + fl->l_start;
1199 } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1200 start = fl->l_start;
1206 if (fl->l_len < 0) {
1215 if (vers == NLM_VERS) {
1217 * Enforce range limits on V1 locks
1219 if (start > 0xffffffffLL || len > 0xffffffffLL)
1223 mtx_lock(&hostname_mtx);
1224 snprintf(oh_space, 32, "%d@%s", svid, hostname);
1225 mtx_unlock(&hostname_mtx);
1226 oh_len = strlen(oh_space);
1228 memset(lock, 0, sizeof(*lock));
1229 lock->caller_name = hostname;
1230 lock->fh.n_len = fhlen;
1231 lock->fh.n_bytes = fh;
1232 lock->oh.n_len = oh_len;
1233 lock->oh.n_bytes = oh_space;
1235 lock->l_offset = start;