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");
221 * Push any pending writes to the server and flush our cache
222 * so that if we are contending with another machine for a
223 * file, we get whatever they wrote and vice-versa.
225 if (op == F_SETLK || op == F_UNLCK)
226 nfs_vinvalbuf(vp, V_SAVE, td, 1);
228 nmp = VFSTONFS(vp->v_mount);
229 strcpy(servername, nmp->nm_hostname);
230 nmp->nm_getinfo(vp, fh.fh_bytes, &fhlen, &ss, &is_v3, &size);
231 sa = (struct sockaddr *) &ss;
232 timo.tv_sec = nmp->nm_timeo / NFS_HZ;
233 timo.tv_usec = (nmp->nm_timeo % NFS_HZ) * (1000000 / NFS_HZ);
239 if (nmp->nm_flag & NFSMNT_SOFT)
240 retries = nmp->nm_retry;
248 * We need to switch to mount-point creds so that we can send
249 * packets from a privileged port.
252 td->td_ucred = vp->v_mount->mnt_cred;
254 host = nlm_find_host_by_name(servername, sa, vers);
255 auth = authunix_create(cred);
256 memset(&ext, 0, sizeof(ext));
258 nf.nf_printed = FALSE;
262 ext.rc_feedback = nlm_feedback;
263 ext.rc_feedback_arg = &nf;
264 ext.rc_timers = NULL;
267 if (flags & F_FLOCK) {
268 ns = nlm_find_svid(id);
269 KASSERT(fl->l_start == 0 && fl->l_len == 0,
270 ("F_FLOCK lock requests must be whole-file locks"));
273 * Remember the creds used for locking in case
274 * we need to recover the lock later.
276 ns->ns_ucred = crdup(cred);
279 } else if (flags & F_REMOTE) {
281 * If we are recovering after a server restart or
282 * trashing locks on a force unmount, use the same
287 svid = ((struct proc *) id)->p_pid;
292 if ((flags & (F_FLOCK|F_WAIT)) == (F_FLOCK|F_WAIT)
293 && fl->l_type == F_WRLCK) {
295 * The semantics for flock(2) require that any
296 * shared lock on the file must be released
297 * before an exclusive lock is granted. The
298 * local locking code interprets this by
299 * unlocking the file before sleeping on a
300 * blocked exclusive lock request. We
301 * approximate this by first attempting
302 * non-blocking and if that fails, we unlock
303 * the file and block.
305 error = nlm_setlock(host, &ext, vers, &timo, retries,
306 vp, F_SETLK, fl, flags & ~F_WAIT,
307 svid, fhlen, &fh.fh_bytes, size, reclaim);
308 if (error == EAGAIN) {
309 fl->l_type = F_UNLCK;
310 error = nlm_clearlock(host, &ext, vers, &timo,
311 retries, vp, F_UNLCK, fl, flags,
312 svid, fhlen, &fh.fh_bytes, size);
313 fl->l_type = F_WRLCK;
315 mtx_lock(&nlm_svid_lock);
318 ns->ns_active = FALSE;
320 mtx_unlock(&nlm_svid_lock);
322 error = nlm_setlock(host, &ext, vers,
323 &timo, retries, vp, F_SETLK, fl,
324 flags, svid, fhlen, &fh.fh_bytes,
329 error = nlm_setlock(host, &ext, vers, &timo, retries,
330 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes,
334 mtx_lock(&nlm_svid_lock);
335 if (!ns->ns_active) {
337 * Add one to the reference count to
338 * hold onto the SVID for the lifetime
339 * of the lock. Note that since
340 * F_FLOCK only supports whole-file
341 * locks, there can only be one active
342 * lock for this SVID.
345 ns->ns_active = TRUE;
347 mtx_unlock(&nlm_svid_lock);
352 error = nlm_clearlock(host, &ext, vers, &timo, retries,
353 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
355 mtx_lock(&nlm_svid_lock);
358 ns->ns_active = FALSE;
360 mtx_unlock(&nlm_svid_lock);
365 error = nlm_getlock(host, &ext, vers, &timo, retries,
366 vp, op, fl, flags, svid, fhlen, &fh.fh_bytes, size);
380 nlm_host_release(host);
386 nlm_advlock(struct vop_advlock_args *ap)
389 return (nlm_advlock_internal(ap->a_vp, ap->a_id, ap->a_op, ap->a_fl,
390 ap->a_flags, FALSE, TRUE));
394 * Set the creds of td to the creds of the given lock's owner. The new
395 * creds reference count will be incremented via crhold. The caller is
396 * responsible for calling crfree and restoring td's original creds.
399 nlm_set_creds_for_lock(struct thread *td, struct flock *fl)
402 struct nlm_file_svid *ns;
407 if (fl->l_pid > PID_MAX) {
409 * If this was originally a F_FLOCK-style lock, we
410 * recorded the creds used when it was originally
411 * locked in the nlm_file_svid structure.
413 mtx_lock(&nlm_svid_lock);
414 for (i = 0; i < NLM_SVID_HASH_SIZE; i++) {
415 for (ns = LIST_FIRST(&nlm_file_svids[i]); ns;
416 ns = LIST_NEXT(ns, ns_link)) {
417 if (ns->ns_svid == fl->l_pid) {
418 cred = crhold(ns->ns_ucred);
423 mtx_unlock(&nlm_svid_lock);
426 * This lock is owned by a process. Get a reference to
429 p = pfind(fl->l_pid);
431 cred = crhold(p->p_ucred);
437 * If we can't find a cred, fall back on the recovery
441 cred = crhold(td->td_ucred);
448 nlm_reclaim_free_lock(struct vnode *vp, struct flock *fl, void *arg)
451 struct thread *td = curthread;
452 struct ucred *oldcred;
456 newfl.l_type = F_UNLCK;
458 oldcred = td->td_ucred;
459 nlm_set_creds_for_lock(td, &newfl);
461 error = nlm_advlock_internal(vp, NULL, F_UNLCK, &newfl, F_REMOTE,
464 crfree(td->td_ucred);
465 td->td_ucred = oldcred;
471 nlm_reclaim(struct vop_reclaim_args *ap)
474 nlm_cancel_wait(ap->a_vp);
475 lf_iteratelocks_vnode(ap->a_vp, nlm_reclaim_free_lock, NULL);
479 struct nlm_recovery_context {
480 struct nlm_host *nr_host; /* host we are recovering */
481 int nr_state; /* remote NSM state for recovery */
485 nlm_client_recover_lock(struct vnode *vp, struct flock *fl, void *arg)
487 struct nlm_recovery_context *nr = (struct nlm_recovery_context *) arg;
488 struct thread *td = curthread;
489 struct ucred *oldcred;
493 * If the remote NSM state changes during recovery, the host
494 * must have rebooted a second time. In that case, we must
495 * restart the recovery.
497 state = nlm_host_get_state(nr->nr_host);
498 if (nr->nr_state != state)
501 error = vn_lock(vp, LK_SHARED);
505 oldcred = td->td_ucred;
506 nlm_set_creds_for_lock(td, fl);
508 error = nlm_advlock_internal(vp, NULL, F_SETLK, fl, F_REMOTE,
511 crfree(td->td_ucred);
512 td->td_ucred = oldcred;
518 nlm_client_recovery(struct nlm_host *host)
520 struct nlm_recovery_context nr;
523 sysid = NLM_SYSID_CLIENT | nlm_host_get_sysid(host);
526 nr.nr_state = nlm_host_get_state(host);
527 error = lf_iteratelocks_sysid(sysid,
528 nlm_client_recover_lock, &nr);
529 } while (error == ERESTART);
533 nlm_convert_to_nlm_lock(struct nlm_lock *dst, struct nlm4_lock *src)
536 dst->caller_name = src->caller_name;
539 dst->svid = src->svid;
540 dst->l_offset = src->l_offset;
541 dst->l_len = src->l_len;
545 nlm_convert_to_nlm4_holder(struct nlm4_holder *dst, struct nlm_holder *src)
548 dst->exclusive = src->exclusive;
549 dst->svid = src->svid;
551 dst->l_offset = src->l_offset;
552 dst->l_len = src->l_len;
556 nlm_convert_to_nlm4_res(struct nlm4_res *dst, struct nlm_res *src)
558 dst->cookie = src->cookie;
559 dst->stat.stat = (enum nlm4_stats) src->stat.stat;
562 static enum clnt_stat
563 nlm_test_rpc(rpcvers_t vers, nlm4_testargs *args, nlm4_testres *res, CLIENT *client,
564 struct rpc_callextra *ext, struct timeval timo)
566 if (vers == NLM_VERS4) {
567 return nlm4_test_4(args, res, client, ext, timo);
573 args1.cookie = args->cookie;
574 args1.exclusive = args->exclusive;
575 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
576 memset(&res1, 0, sizeof(res1));
578 stat = nlm_test_1(&args1, &res1, client, ext, timo);
580 if (stat == RPC_SUCCESS) {
581 res->cookie = res1.cookie;
582 res->stat.stat = (enum nlm4_stats) res1.stat.stat;
583 if (res1.stat.stat == nlm_denied)
584 nlm_convert_to_nlm4_holder(
585 &res->stat.nlm4_testrply_u.holder,
586 &res1.stat.nlm_testrply_u.holder);
593 static enum clnt_stat
594 nlm_lock_rpc(rpcvers_t vers, nlm4_lockargs *args, nlm4_res *res, CLIENT *client,
595 struct rpc_callextra *ext, struct timeval timo)
597 if (vers == NLM_VERS4) {
598 return nlm4_lock_4(args, res, client, ext, timo);
604 args1.cookie = args->cookie;
605 args1.block = args->block;
606 args1.exclusive = args->exclusive;
607 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
608 args1.reclaim = args->reclaim;
609 args1.state = args->state;
610 memset(&res1, 0, sizeof(res1));
612 stat = nlm_lock_1(&args1, &res1, client, ext, timo);
614 if (stat == RPC_SUCCESS) {
615 nlm_convert_to_nlm4_res(res, &res1);
622 static enum clnt_stat
623 nlm_cancel_rpc(rpcvers_t vers, nlm4_cancargs *args, nlm4_res *res, CLIENT *client,
624 struct rpc_callextra *ext, struct timeval timo)
626 if (vers == NLM_VERS4) {
627 return nlm4_cancel_4(args, res, client, ext, timo);
633 args1.cookie = args->cookie;
634 args1.block = args->block;
635 args1.exclusive = args->exclusive;
636 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
637 memset(&res1, 0, sizeof(res1));
639 stat = nlm_cancel_1(&args1, &res1, client, ext, timo);
641 if (stat == RPC_SUCCESS) {
642 nlm_convert_to_nlm4_res(res, &res1);
649 static enum clnt_stat
650 nlm_unlock_rpc(rpcvers_t vers, nlm4_unlockargs *args, nlm4_res *res, CLIENT *client,
651 struct rpc_callextra *ext, struct timeval timo)
653 if (vers == NLM_VERS4) {
654 return nlm4_unlock_4(args, res, client, ext, timo);
656 nlm_unlockargs args1;
660 args1.cookie = args->cookie;
661 nlm_convert_to_nlm_lock(&args1.alock, &args->alock);
662 memset(&res1, 0, sizeof(res1));
664 stat = nlm_unlock_1(&args1, &res1, client, ext, timo);
666 if (stat == RPC_SUCCESS) {
667 nlm_convert_to_nlm4_res(res, &res1);
675 * Called after a lock request (set or clear) succeeded. We record the
676 * details in the local lock manager. Note that since the remote
677 * server has granted the lock, we can be sure that it doesn't
678 * conflict with any other locks we have in the local lock manager.
680 * Since it is possible that host may also make NLM client requests to
681 * our NLM server, we use a different sysid value to record our own
684 * Note that since it is possible for us to receive replies from the
685 * server in a different order than the locks were granted (e.g. if
686 * many local threads are contending for the same lock), we must use a
687 * blocking operation when registering with the local lock manager.
688 * We expect that any actual wait will be rare and short hence we
689 * ignore signals for this.
692 nlm_record_lock(struct vnode *vp, int op, struct flock *fl,
693 int svid, int sysid, off_t size)
695 struct vop_advlockasync_args a;
703 a.a_flags = F_REMOTE|F_WAIT|F_NOINTR;
706 newfl.l_start = fl->l_start;
707 newfl.l_len = fl->l_len;
708 newfl.l_type = fl->l_type;
709 newfl.l_whence = fl->l_whence;
711 newfl.l_sysid = NLM_SYSID_CLIENT | sysid;
713 error = lf_advlockasync(&a, &vp->v_lockf, size);
714 KASSERT(error == 0 || error == ENOENT,
715 ("Failed to register NFS lock locally - error=%d", error));
719 nlm_setlock(struct nlm_host *host, struct rpc_callextra *ext,
720 rpcvers_t vers, struct timeval *timo, int retries,
721 struct vnode *vp, int op, struct flock *fl, int flags,
722 int svid, size_t fhlen, void *fh, off_t size, bool_t reclaim)
724 struct nlm4_lockargs args;
730 int retry, block, exclusive;
731 void *wait_handle = NULL;
734 memset(&args, 0, sizeof(args));
735 memset(&res, 0, sizeof(res));
737 block = (flags & F_WAIT) ? TRUE : FALSE;
738 exclusive = (fl->l_type == F_WRLCK);
740 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
741 &args.alock, oh_space);
745 args.exclusive = exclusive;
746 args.reclaim = reclaim;
747 args.state = nlm_nsm_state;
751 client = nlm_host_get_rpc(host, FALSE);
753 return (ENOLCK); /* XXX retry? */
756 wait_handle = nlm_register_wait_lock(&args.alock, vp);
758 xid = atomic_fetchadd_int(&nlm_xid, 1);
759 args.cookie.n_len = sizeof(xid);
760 args.cookie.n_bytes = (char*) &xid;
762 stat = nlm_lock_rpc(vers, &args, &res, client, ext, *timo);
764 CLNT_RELEASE(client);
766 if (stat != RPC_SUCCESS) {
768 nlm_deregister_wait_lock(wait_handle);
779 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
781 if (block && res.stat.stat != nlm4_blocked)
782 nlm_deregister_wait_lock(wait_handle);
784 if (res.stat.stat == nlm4_denied_grace_period) {
786 * The server has recently rebooted and is
787 * giving old clients a change to reclaim
788 * their locks. Wait for a few seconds and try
791 error = tsleep(&args, PCATCH, "nlmgrace", retry);
792 if (error && error != EWOULDBLOCK)
800 if (block && res.stat.stat == nlm4_blocked) {
802 * The server should call us back with a
803 * granted message when the lock succeeds. In
804 * order to deal with broken servers, lost
805 * granted messages and server reboots, we
806 * will also re-try every few seconds.
808 error = nlm_wait_lock(wait_handle, retry);
809 if (error == EWOULDBLOCK) {
817 * We need to call the server to
818 * cancel our lock request.
820 nlm4_cancargs cancel;
822 memset(&cancel, 0, sizeof(cancel));
824 xid = atomic_fetchadd_int(&nlm_xid, 1);
825 cancel.cookie.n_len = sizeof(xid);
826 cancel.cookie.n_bytes = (char*) &xid;
827 cancel.block = block;
828 cancel.exclusive = exclusive;
829 cancel.alock = args.alock;
832 client = nlm_host_get_rpc(host, FALSE);
837 stat = nlm_cancel_rpc(vers, &cancel,
838 &res, client, ext, *timo);
840 CLNT_RELEASE(client);
842 if (stat != RPC_SUCCESS) {
848 * reboots. This means
854 pause("nlmcancel", 10*hz);
856 } while (stat != RPC_SUCCESS);
861 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
863 switch (res.stat.stat) {
875 case nlm4_denied_grace_period:
878 * recently rebooted -
904 error = nlm_map_status(res.stat.stat);
907 if (!error && !reclaim) {
908 nlm_record_lock(vp, op, fl, args.alock.svid,
909 nlm_host_get_sysid(host), size);
910 nlm_host_monitor(host, 0);
918 nlm_clearlock(struct nlm_host *host, struct rpc_callextra *ext,
919 rpcvers_t vers, struct timeval *timo, int retries,
920 struct vnode *vp, int op, struct flock *fl, int flags,
921 int svid, size_t fhlen, void *fh, off_t size)
923 struct nlm4_unlockargs args;
931 memset(&args, 0, sizeof(args));
932 memset(&res, 0, sizeof(res));
934 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
935 &args.alock, oh_space);
940 client = nlm_host_get_rpc(host, FALSE);
942 return (ENOLCK); /* XXX retry? */
944 xid = atomic_fetchadd_int(&nlm_xid, 1);
945 args.cookie.n_len = sizeof(xid);
946 args.cookie.n_bytes = (char*) &xid;
948 stat = nlm_unlock_rpc(vers, &args, &res, client, ext, *timo);
950 CLNT_RELEASE(client);
952 if (stat != RPC_SUCCESS) {
963 xdr_free((xdrproc_t) xdr_nlm4_res, &res);
965 if (res.stat.stat == nlm4_denied_grace_period) {
967 * The server has recently rebooted and is
968 * giving old clients a change to reclaim
969 * their locks. Wait for a few seconds and try
972 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
973 if (error && error != EWOULDBLOCK)
979 * If we are being called via nlm_reclaim (which will
980 * use the F_REMOTE flag), don't record the lock
981 * operation in the local lock manager since the vnode
984 if (!(flags & F_REMOTE))
985 nlm_record_lock(vp, op, fl, args.alock.svid,
986 nlm_host_get_sysid(host), size);
993 nlm_getlock(struct nlm_host *host, struct rpc_callextra *ext,
994 rpcvers_t vers, struct timeval *timo, int retries,
995 struct vnode *vp, int op, struct flock *fl, int flags,
996 int svid, size_t fhlen, void *fh, off_t size)
998 struct nlm4_testargs args;
1000 struct nlm4_testres res;
1003 enum clnt_stat stat;
1007 KASSERT(!(flags & F_FLOCK), ("unexpected F_FLOCK for F_GETLK"));
1009 memset(&args, 0, sizeof(args));
1010 memset(&res, 0, sizeof(res));
1012 exclusive = (fl->l_type == F_WRLCK);
1014 error = nlm_init_lock(fl, flags, svid, vers, fhlen, fh, size,
1015 &args.alock, oh_space);
1018 args.exclusive = exclusive;
1021 client = nlm_host_get_rpc(host, FALSE);
1023 return (ENOLCK); /* XXX retry? */
1025 xid = atomic_fetchadd_int(&nlm_xid, 1);
1026 args.cookie.n_len = sizeof(xid);
1027 args.cookie.n_bytes = (char*) &xid;
1029 stat = nlm_test_rpc(vers, &args, &res, client, ext, *timo);
1031 CLNT_RELEASE(client);
1033 if (stat != RPC_SUCCESS) {
1041 if (res.stat.stat == nlm4_denied_grace_period) {
1043 * The server has recently rebooted and is
1044 * giving old clients a change to reclaim
1045 * their locks. Wait for a few seconds and try
1048 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1049 error = tsleep(&args, PCATCH, "nlmgrace", 5*hz);
1050 if (error && error != EWOULDBLOCK)
1055 if (res.stat.stat == nlm4_denied) {
1056 struct nlm4_holder *h =
1057 &res.stat.nlm4_testrply_u.holder;
1058 fl->l_start = h->l_offset;
1059 fl->l_len = h->l_len;
1060 fl->l_pid = h->svid;
1062 fl->l_type = F_WRLCK;
1064 fl->l_type = F_RDLCK;
1065 fl->l_whence = SEEK_SET;
1068 fl->l_type = F_UNLCK;
1071 xdr_free((xdrproc_t) xdr_nlm4_testres, &res);
1078 nlm_map_status(nlm4_stats stat)
1087 case nlm4_denied_nolocks:
1110 static struct nlm_file_svid *
1111 nlm_find_svid(void *id)
1113 struct nlm_file_svid *ns, *newns;
1116 h = (((uintptr_t) id) >> 7) % NLM_SVID_HASH_SIZE;
1118 mtx_lock(&nlm_svid_lock);
1119 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1120 if (ns->ns_id == id) {
1125 mtx_unlock(&nlm_svid_lock);
1127 int svid = alloc_unr(nlm_svid_allocator);
1128 newns = malloc(sizeof(struct nlm_file_svid), M_NLM,
1132 newns->ns_svid = svid;
1133 newns->ns_ucred = NULL;
1134 newns->ns_active = FALSE;
1137 * We need to check for a race with some other
1138 * thread allocating a svid for this file.
1140 mtx_lock(&nlm_svid_lock);
1141 LIST_FOREACH(ns, &nlm_file_svids[h], ns_link) {
1142 if (ns->ns_id == id) {
1148 mtx_unlock(&nlm_svid_lock);
1149 free_unr(nlm_svid_allocator, newns->ns_svid);
1152 LIST_INSERT_HEAD(&nlm_file_svids[h], newns,
1155 mtx_unlock(&nlm_svid_lock);
1163 nlm_free_svid(struct nlm_file_svid *ns)
1166 mtx_lock(&nlm_svid_lock);
1169 KASSERT(!ns->ns_active, ("Freeing active SVID"));
1170 LIST_REMOVE(ns, ns_link);
1171 mtx_unlock(&nlm_svid_lock);
1172 free_unr(nlm_svid_allocator, ns->ns_svid);
1174 crfree(ns->ns_ucred);
1177 mtx_unlock(&nlm_svid_lock);
1182 nlm_init_lock(struct flock *fl, int flags, int svid,
1183 rpcvers_t vers, size_t fhlen, void *fh, off_t size,
1184 struct nlm4_lock *lock, char oh_space[32])
1189 if (fl->l_whence == SEEK_END) {
1191 || (fl->l_start > 0 && size > OFF_MAX - fl->l_start))
1193 start = size + fl->l_start;
1194 } else if (fl->l_whence == SEEK_SET || fl->l_whence == SEEK_CUR) {
1195 start = fl->l_start;
1201 if (fl->l_len < 0) {
1210 if (vers == NLM_VERS) {
1212 * Enforce range limits on V1 locks
1214 if (start > 0xffffffffLL || len > 0xffffffffLL)
1218 snprintf(oh_space, 32, "%d@", svid);
1219 oh_len = strlen(oh_space);
1220 getcredhostname(NULL, oh_space + oh_len, 32 - oh_len);
1221 oh_len = strlen(oh_space);
1223 memset(lock, 0, sizeof(*lock));
1224 lock->caller_name = prison0.pr_hostname;
1225 lock->fh.n_len = fhlen;
1226 lock->fh.n_bytes = fh;
1227 lock->oh.n_len = oh_len;
1228 lock->oh.n_bytes = oh_space;
1230 lock->l_offset = start;