2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. Berkeley Software Design Inc's name may not be used to endorse or
40 * promote products derived from this software without specific prior
43 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
58 #include <sys/cdefs.h>
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/fcntl.h>
62 #include <sys/kernel.h> /* for hz */
63 #include <sys/file_internal.h>
64 #include <sys/malloc.h>
65 #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
66 #include <sys/kpi_mbuf.h>
67 #include <sys/mount_internal.h>
68 #include <sys/proc_internal.h> /* for p_start */
69 #include <sys/kauth.h>
70 #include <sys/resourcevar.h>
71 #include <sys/socket.h>
72 #include <sys/unistd.h>
74 #include <sys/vnode_internal.h>
76 #include <kern/thread.h>
78 #include <machine/limits.h>
82 #include <nfs/rpcv2.h>
83 #include <nfs/nfsproto.h>
85 #include <nfs/nfsmount.h>
86 #include <nfs/nfsnode.h>
87 #include <nfs/nfs_lock.h>
89 #define OFF_MAX QUAD_MAX
92 * globals for managing the lockd fifo
94 vnode_t nfslockdvnode
= 0;
95 int nfslockdwaiting
= 0;
96 time_t nfslockdstarttimeout
= 0;
97 int nfslockdfifolock
= 0;
98 #define NFSLOCKDFIFOLOCK_LOCKED 1
99 #define NFSLOCKDFIFOLOCK_WANT 2
102 * pending lock request messages are kept in this queue which is
103 * kept sorted by transaction ID (xid).
105 uint64_t nfs_lockxid
= 0;
106 LOCKD_MSG_QUEUE nfs_pendlockq
;
109 * This structure is used to identify processes which have acquired NFS locks.
110 * Knowing which processes have ever acquired locks allows us to short-circuit
111 * unlock requests for processes that have never had an NFS file lock. Thus
112 * avoiding a costly and unnecessary lockd request.
114 struct nfs_lock_pid
{
115 TAILQ_ENTRY(nfs_lock_pid
) lp_lru
; /* LRU list */
116 LIST_ENTRY(nfs_lock_pid
) lp_hash
; /* hash chain */
117 int lp_valid
; /* valid entry? */
118 int lp_time
; /* last time seen valid */
119 pid_t lp_pid
; /* The process ID. */
120 struct timeval lp_pid_start
; /* Start time of process id */
123 #define NFS_LOCK_PID_HASH_SIZE 64 // XXX tune me
124 #define NFS_LOCK_PID_HASH(pid) \
125 (&nfs_lock_pid_hash_tbl[(pid) & nfs_lock_pid_hash])
126 LIST_HEAD(, nfs_lock_pid
) *nfs_lock_pid_hash_tbl
;
127 TAILQ_HEAD(, nfs_lock_pid
) nfs_lock_pid_lru
;
128 u_long nfs_lock_pid_hash
;
129 int nfs_lock_pid_lock
;
133 * initialize global nfs lock state
138 TAILQ_INIT(&nfs_pendlockq
);
139 nfs_lock_pid_lock
= 0;
140 nfs_lock_pid_hash_tbl
= hashinit(NFS_LOCK_PID_HASH_SIZE
,
141 M_TEMP
, &nfs_lock_pid_hash
);
142 TAILQ_INIT(&nfs_lock_pid_lru
);
146 * insert a lock request message into the pending queue
149 nfs_lockdmsg_enqueue(LOCKD_MSG_REQUEST
*msgreq
)
151 LOCKD_MSG_REQUEST
*mr
;
153 mr
= TAILQ_LAST(&nfs_pendlockq
, nfs_lock_msg_queue
);
154 if (!mr
|| (msgreq
->lmr_msg
.lm_xid
> mr
->lmr_msg
.lm_xid
)) {
155 /* fast path: empty queue or new largest xid */
156 TAILQ_INSERT_TAIL(&nfs_pendlockq
, msgreq
, lmr_next
);
159 /* slow path: need to walk list to find insertion point */
160 while (mr
&& (msgreq
->lmr_msg
.lm_xid
> mr
->lmr_msg
.lm_xid
)) {
161 mr
= TAILQ_PREV(mr
, nfs_lock_msg_queue
, lmr_next
);
164 TAILQ_INSERT_AFTER(&nfs_pendlockq
, mr
, msgreq
, lmr_next
);
166 TAILQ_INSERT_HEAD(&nfs_pendlockq
, msgreq
, lmr_next
);
171 * remove a lock request message from the pending queue
174 nfs_lockdmsg_dequeue(LOCKD_MSG_REQUEST
*msgreq
)
176 TAILQ_REMOVE(&nfs_pendlockq
, msgreq
, lmr_next
);
180 * find a pending lock request message by xid
182 * We search from the head of the list assuming that the message we're
183 * looking for is for an older request (because we have an answer to it).
184 * This assumes that lock request will be answered primarily in FIFO order.
185 * However, this may not be the case if there are blocked requests. We may
186 * want to move blocked requests to a separate queue (but that'll complicate
187 * duplicate xid checking).
189 static inline LOCKD_MSG_REQUEST
*
190 nfs_lockdmsg_find_by_xid(uint64_t lockxid
)
192 LOCKD_MSG_REQUEST
*mr
;
194 TAILQ_FOREACH(mr
, &nfs_pendlockq
, lmr_next
) {
195 if (mr
->lmr_msg
.lm_xid
== lockxid
)
197 if (mr
->lmr_msg
.lm_xid
> lockxid
)
204 * Because we can't depend on nlm_granted messages containing the same
205 * cookie we sent with the original lock request, we need code test if
206 * an nlm_granted answer matches the lock request. We also need code
207 * that can find a lockd message based solely on the nlm_granted answer.
211 * compare lockd message to answer
213 * returns 0 on equality and 1 if different
216 nfs_lockdmsg_compare_to_answer(LOCKD_MSG_REQUEST
*msgreq
, struct lockd_ans
*ansp
)
218 if (!(ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
))
220 if (msgreq
->lmr_msg
.lm_fl
.l_pid
!= ansp
->la_pid
)
222 if (msgreq
->lmr_msg
.lm_fl
.l_start
!= ansp
->la_start
)
224 if (msgreq
->lmr_msg
.lm_fl
.l_len
!= ansp
->la_len
)
226 if (msgreq
->lmr_msg
.lm_fh_len
!= ansp
->la_fh_len
)
228 if (bcmp(msgreq
->lmr_msg
.lm_fh
, ansp
->la_fh
, ansp
->la_fh_len
))
234 * find a pending lock request message based on the lock info provided
235 * in the lockd_ans/nlm_granted data. We need this because we can't
236 * depend on nlm_granted messages containing the same cookie we sent
237 * with the original lock request.
239 * We search from the head of the list assuming that the message we're
240 * looking for is for an older request (because we have an answer to it).
241 * This assumes that lock request will be answered primarily in FIFO order.
242 * However, this may not be the case if there are blocked requests. We may
243 * want to move blocked requests to a separate queue (but that'll complicate
244 * duplicate xid checking).
246 static inline LOCKD_MSG_REQUEST
*
247 nfs_lockdmsg_find_by_answer(struct lockd_ans
*ansp
)
249 LOCKD_MSG_REQUEST
*mr
;
251 if (!(ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
))
253 TAILQ_FOREACH(mr
, &nfs_pendlockq
, lmr_next
) {
254 if (!nfs_lockdmsg_compare_to_answer(mr
, ansp
))
261 * return the next unique lock request transaction ID
263 static inline uint64_t
264 nfs_lockxid_get(void)
266 LOCKD_MSG_REQUEST
*mr
;
268 /* derive initial lock xid from system time */
271 * Note: it's OK if this code inits nfs_lockxid to 0 (for example,
272 * due to a broken clock) because we immediately increment it
273 * and we guarantee to never use xid 0. So, nfs_lockxid should only
274 * ever be 0 the first time this function is called.
278 nfs_lockxid
= (uint64_t)tv
.tv_sec
<< 12;
281 /* make sure we get a unique xid */
283 /* Skip zero xid if it should ever happen. */
284 if (++nfs_lockxid
== 0)
286 if (!(mr
= TAILQ_LAST(&nfs_pendlockq
, nfs_lock_msg_queue
)) ||
287 (mr
->lmr_msg
.lm_xid
< nfs_lockxid
)) {
288 /* fast path: empty queue or new largest xid */
291 /* check if xid is already in use */
292 } while (nfs_lockdmsg_find_by_xid(nfs_lockxid
));
299 * Check the nfs_lock_pid hash table for an entry and, if requested,
300 * add the entry if it is not found.
302 * (Also, if adding, try to clean up some stale entries.)
305 nfs_lock_pid_check(proc_t p
, int addflag
, vnode_t vp
)
307 struct nfs_lock_pid
*lp
, *lplru
, *lplru_next
;
314 if (nfs_lock_pid_lock
) {
315 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
316 while (nfs_lock_pid_lock
) {
317 nfs_lock_pid_lock
= -1;
318 tsleep(&nfs_lock_pid_lock
, PCATCH
, "nfslockpid", 0);
319 if ((error
= nfs_sigintr(nmp
, NULL
, p
)))
324 nfs_lock_pid_lock
= 1;
326 /* Search hash chain */
328 lp
= NFS_LOCK_PID_HASH(proc_pid(p
))->lh_first
;
329 for (; lp
!= NULL
; lp
= lp
->lp_hash
.le_next
)
330 if (lp
->lp_pid
== proc_pid(p
)) {
332 if (timevalcmp(&lp
->lp_pid_start
, &p
->p_stats
->p_start
, ==)) {
333 /* ...and it's valid */
334 /* move to tail of LRU */
335 TAILQ_REMOVE(&nfs_lock_pid_lru
, lp
, lp_lru
);
337 lp
->lp_time
= now
.tv_sec
;
338 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lp
, lp_lru
);
342 /* ...but it's no longer valid */
343 /* remove from hash, invalidate, and move to lru head */
344 LIST_REMOVE(lp
, lp_hash
);
346 TAILQ_REMOVE(&nfs_lock_pid_lru
, lp
, lp_lru
);
347 TAILQ_INSERT_HEAD(&nfs_lock_pid_lru
, lp
, lp_lru
);
352 /* if we didn't find it (valid) and we've been asked to add it */
353 if ((error
== ENOENT
) && addflag
) {
354 /* scan lru list for invalid, stale entries to reuse/free */
357 for (lplru
= TAILQ_FIRST(&nfs_lock_pid_lru
); lplru
; lplru
= lplru_next
) {
358 lplru_next
= TAILQ_NEXT(lplru
, lp_lru
);
359 if (lplru
->lp_valid
&& (lplru
->lp_time
>= (now
.tv_sec
- 2))) {
361 * If the oldest LRU entry is relatively new, then don't
362 * bother scanning any further.
366 /* remove entry from LRU, and check if it's still in use */
367 TAILQ_REMOVE(&nfs_lock_pid_lru
, lplru
, lp_lru
);
368 if (!lplru
->lp_valid
|| !(plru
= pfind(lplru
->lp_pid
)) ||
369 timevalcmp(&lplru
->lp_pid_start
, &plru
->p_stats
->p_start
, !=)) {
370 /* no longer in use */
371 LIST_REMOVE(lplru
, lp_hash
);
373 /* we'll reuse this one */
376 /* we can free this one */
381 lplru
->lp_time
= now
.tv_sec
;
382 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lplru
, lp_lru
);
384 /* don't check too many entries at once */
389 /* we need to allocate a new one */
390 MALLOC(lp
, struct nfs_lock_pid
*, sizeof(struct nfs_lock_pid
),
391 M_TEMP
, M_WAITOK
| M_ZERO
);
396 /* (re)initialize nfs_lock_pid info */
397 lp
->lp_pid
= proc_pid(p
);
398 lp
->lp_pid_start
= p
->p_stats
->p_start
;
399 /* insert pid in hash */
400 LIST_INSERT_HEAD(NFS_LOCK_PID_HASH(lp
->lp_pid
), lp
, lp_hash
);
402 lp
->lp_time
= now
.tv_sec
;
403 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lp
, lp_lru
);
409 if (nfs_lock_pid_lock
< 0) {
410 nfs_lock_pid_lock
= 0;
411 wakeup(&nfs_lock_pid_lock
);
413 nfs_lock_pid_lock
= 0;
421 * NFS advisory byte-level locks.
424 nfs_dolock(struct vnop_advlock_args
*ap
)
425 /* struct vnop_advlock_args {
426 struct vnodeop_desc *a_desc;
432 vfs_context_t a_context;
435 LOCKD_MSG_REQUEST msgreq
;
442 struct nfsmount
*nmp
;
443 struct nfs_vattr nvattr
;
446 int timeo
, endtime
, lastmsg
, wentdown
= 0;
450 struct sockaddr
*saddr
;
452 p
= vfs_context_proc(ap
->a_context
);
453 cred
= vfs_context_ucred(ap
->a_context
);
459 nmp
= VFSTONFS(vnode_mount(vp
));
462 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
)
466 * The NLM protocol doesn't allow the server to return an error
467 * on ranges, so we do it. Pre LFS (Large File Summit)
468 * standards required EINVAL for the range errors. More recent
469 * standards use EOVERFLOW, but their EINVAL wording still
470 * encompasses these errors.
471 * Any code sensitive to this is either:
472 * 1) written pre-LFS and so can handle only EINVAL, or
473 * 2) written post-LFS and thus ought to be tolerant of pre-LFS
475 * Since returning EOVERFLOW certainly breaks 1), we return EINVAL.
477 if (fl
->l_whence
!= SEEK_END
) {
478 if ((fl
->l_whence
!= SEEK_CUR
&& fl
->l_whence
!= SEEK_SET
) ||
480 (fl
->l_len
> 0 && fl
->l_len
- 1 > OFF_MAX
- fl
->l_start
) ||
481 (fl
->l_len
< 0 && fl
->l_start
+ fl
->l_len
< 0))
485 * If daemon is running take a ref on its fifo vnode
487 if (!(wvp
= nfslockdvnode
)) {
488 if (!nfslockdwaiting
&& !nfslockdstarttimeout
)
491 * Don't wake lock daemon if it hasn't been started yet and
492 * this is an unlock request (since we couldn't possibly
493 * actually have a lock on the file). This could be an
494 * uninformed unlock request due to closef()'s behavior of doing
495 * unlocks on all files if a process has had a lock on ANY file.
497 if (!nfslockdvnode
&& (fl
->l_type
== F_UNLCK
))
500 if (nfslockdwaiting
) {
501 /* wake up lock daemon */
502 nfslockdstarttimeout
= now
.tv_sec
+ 60;
503 (void)wakeup((void *)&nfslockdwaiting
);
505 /* wait on nfslockdvnode for a while to allow daemon to start */
506 while (!nfslockdvnode
&& (now
.tv_sec
< nfslockdstarttimeout
)) {
507 error
= tsleep((void *)&nfslockdvnode
, PCATCH
| PUSER
, "lockdstart", 2*hz
);
508 if (error
&& (error
!= EWOULDBLOCK
))
510 /* check that we still have our mount... */
511 /* ...and that we still support locks */
512 nmp
= VFSTONFS(vnode_mount(vp
));
515 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
)
522 * check for nfslockdvnode
523 * If it hasn't started by now, there's a problem.
525 if (!(wvp
= nfslockdvnode
))
528 error
= vnode_getwithref(wvp
);
531 error
= vnode_ref(wvp
);
538 * Need to check if this process has successfully acquired an NFS lock before.
539 * If not, and this is an unlock request we can simply return success here.
541 lockpidcheck
= nfs_lock_pid_check(p
, 0, vp
);
543 if (lockpidcheck
!= ENOENT
) {
546 return (lockpidcheck
);
548 if (ap
->a_op
== F_UNLCK
) {
556 * The NFS Lock Manager protocol doesn't directly handle
557 * negative lengths or SEEK_END, so we need to normalize
558 * things here where we have all the info.
559 * (Note: SEEK_CUR is already adjusted for at this point)
561 /* Convert the flock structure into a start and end. */
562 switch (fl
->l_whence
) {
566 * Caller is responsible for adding any necessary offset
567 * to fl->l_start when SEEK_CUR is used.
572 /* need to flush, and refetch attributes to make */
573 /* sure we have the correct end of file offset */
574 if (np
->n_flag
& NMODIFIED
) {
576 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
585 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
591 start
= np
->n_size
+ fl
->l_start
;
600 else if (fl
->l_len
> 0)
601 end
= start
+ fl
->l_len
- 1;
602 else { /* l_len is negative */
612 ((start
>= 0x80000000) || (end
>= 0x80000000))) {
619 * Fill in the information structure.
621 msgreq
.lmr_answered
= 0;
622 msgreq
.lmr_errno
= 0;
623 msgreq
.lmr_saved_errno
= 0;
624 msg
= &msgreq
.lmr_msg
;
625 msg
->lm_version
= LOCKD_MSG_VERSION
;
629 msg
->lm_fl
.l_start
= start
;
631 msg
->lm_fl
.l_len
= end
- start
+ 1;
632 msg
->lm_fl
.l_pid
= proc_pid(p
);
634 if (ap
->a_flags
& F_WAIT
)
635 msg
->lm_flags
|= LOCKD_MSG_BLOCK
;
636 if (ap
->a_op
== F_GETLK
)
637 msg
->lm_flags
|= LOCKD_MSG_TEST
;
639 nmp
= VFSTONFS(vnode_mount(vp
));
646 saddr
= mbuf_data(nmp
->nm_nam
);
647 bcopy(saddr
, &msg
->lm_addr
, min(sizeof msg
->lm_addr
, saddr
->sa_len
));
648 msg
->lm_fh_len
= NFS_ISV3(vp
) ? VTONFS(vp
)->n_fhsize
: NFSX_V2FH
;
649 bcopy(VTONFS(vp
)->n_fhp
, msg
->lm_fh
, msg
->lm_fh_len
);
651 msg
->lm_flags
|= LOCKD_MSG_NFSV3
;
652 cru2x(cred
, &msg
->lm_cred
);
655 lastmsg
= now
.tv_sec
- ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
657 fmode
= FFLAGS(O_WRONLY
);
658 if ((error
= VNOP_OPEN(wvp
, fmode
, ap
->a_context
))) {
667 /* allocate unique xid */
668 msg
->lm_xid
= nfs_lockxid_get();
669 nfs_lockdmsg_enqueue(&msgreq
);
672 #define IO_NOMACCHECK 0;
673 ioflg
= IO_UNIT
| IO_NOMACCHECK
;
676 while (nfslockdfifolock
& NFSLOCKDFIFOLOCK_LOCKED
) {
677 nfslockdfifolock
|= NFSLOCKDFIFOLOCK_WANT
;
678 error
= tsleep((void *)&nfslockdfifolock
,
679 PCATCH
| PUSER
, "lockdfifo", 20*hz
);
685 nfslockdfifolock
|= NFSLOCKDFIFOLOCK_LOCKED
;
687 error
= vn_rdwr(UIO_WRITE
, wvp
, (caddr_t
)msg
, sizeof(*msg
), 0,
688 UIO_SYSSPACE32
, ioflg
, proc_ucred(kernproc
), NULL
, p
);
690 nfslockdfifolock
&= ~NFSLOCKDFIFOLOCK_LOCKED
;
691 if (nfslockdfifolock
& NFSLOCKDFIFOLOCK_WANT
) {
692 nfslockdfifolock
&= ~NFSLOCKDFIFOLOCK_WANT
;
693 wakeup((void *)&nfslockdfifolock
);
696 if (error
&& (((ioflg
& IO_NDELAY
) == 0) || error
!= EAGAIN
)) {
701 * Always wait for an answer. Not waiting for unlocks could
702 * cause a lock to be left if the unlock request gets dropped.
706 * Retry if it takes too long to get a response.
708 * The timeout numbers were picked out of thin air... they start
709 * at 2 and double each timeout with a max of 60 seconds.
711 * In order to maintain responsiveness, we pass a small timeout
712 * to tsleep and calculate the timeouts ourselves. This allows
713 * us to pick up on mount changes quicker.
719 endtime
= now
.tv_sec
+ timeo
/hz
;
721 endtime
= now
.tv_sec
+ 1;
722 while (now
.tv_sec
< endtime
) {
723 error
= tsleep((void *)&msgreq
, PCATCH
| PUSER
, "lockd", 2*hz
);
724 if (msgreq
.lmr_answered
) {
726 * Note: it's possible to have a lock granted at
727 * essentially the same time that we get interrupted.
728 * Since the lock may be granted, we can't return an
729 * error from this request or we might not unlock the
730 * lock that's been granted.
735 if (error
!= EWOULDBLOCK
)
737 /* check that we still have our mount... */
738 /* ...and that we still support locks */
739 nmp
= VFSTONFS(vnode_mount(vp
));
740 if (!nmp
|| (nmp
->nm_flag
& NFSMNT_NOLOCKS
))
743 * If the mount is hung and we've requested not to hang
744 * on remote filesystems, then bail now.
746 if ((p
!= NULL
) && ((proc_noremotehang(p
)) != 0) &&
747 ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_LOCKTIMEO
)) != 0)) {
748 if (fl
->l_type
== F_UNLCK
)
749 printf("nfs_dolock: aborting unlock request "
750 "due to timeout (noremotehang)\n");
757 /* check that we still have our mount... */
758 nmp
= VFSTONFS(vnode_mount(vp
));
760 if (error
== EWOULDBLOCK
)
764 /* ...and that we still support locks */
765 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
) {
766 if (error
== EWOULDBLOCK
)
770 if ((error
== ENOTSUP
) &&
771 (nmp
->nm_state
& NFSSTA_LOCKSWORK
)) {
773 * We have evidence that locks work, yet lockd
774 * returned ENOTSUP. This is probably because
775 * it was unable to contact the server's lockd to
776 * send it the request.
778 * Because we know locks work, we'll consider
779 * this failure to be a timeout.
783 if (error
!= EWOULDBLOCK
) {
785 * We're going to bail on this request.
786 * If we were a blocked lock request, send a cancel.
788 if ((msgreq
.lmr_errno
== EINPROGRESS
) &&
789 !(msg
->lm_flags
& LOCKD_MSG_CANCEL
)) {
790 /* set this request up as a cancel */
791 msg
->lm_flags
|= LOCKD_MSG_CANCEL
;
792 nfs_lockdmsg_dequeue(&msgreq
);
793 msg
->lm_xid
= nfs_lockxid_get();
794 nfs_lockdmsg_enqueue(&msgreq
);
795 msgreq
.lmr_saved_errno
= error
;
796 msgreq
.lmr_errno
= 0;
797 msgreq
.lmr_answered
= 0;
800 /* send cancel request */
807 * If the mount is hung and we've requested not to hang
808 * on remote filesystems, then bail now.
810 if ((p
!= NULL
) && ((proc_noremotehang(p
)) != 0) &&
811 ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_LOCKTIMEO
)) != 0)) {
812 if (fl
->l_type
== F_UNLCK
)
813 printf("nfs_dolock: aborting unlock request "
814 "due to timeout (noremotehang)\n");
818 /* warn if we're not getting any response */
820 if ((msgreq
.lmr_errno
!= EINPROGRESS
) &&
821 (nmp
->nm_tprintf_initial_delay
!= 0) &&
822 ((lastmsg
+ nmp
->nm_tprintf_delay
) < now
.tv_sec
)) {
823 lastmsg
= now
.tv_sec
;
824 nfs_down(nmp
, p
, 0, NFSSTA_LOCKTIMEO
, "lockd not responding");
827 if (msgreq
.lmr_errno
== EINPROGRESS
) {
829 * We've got a blocked lock request that we are
830 * going to retry. First, we'll want to try to
831 * send a cancel for the previous request.
833 * Clear errno so if we don't get a response
834 * to the resend we'll call nfs_down().
835 * Also reset timeout because we'll expect a
836 * quick response to the cancel/resend (even if
837 * it is NLM_BLOCKED).
839 msg
->lm_flags
|= LOCKD_MSG_CANCEL
;
840 nfs_lockdmsg_dequeue(&msgreq
);
841 msg
->lm_xid
= nfs_lockxid_get();
842 nfs_lockdmsg_enqueue(&msgreq
);
843 msgreq
.lmr_saved_errno
= msgreq
.lmr_errno
;
844 msgreq
.lmr_errno
= 0;
845 msgreq
.lmr_answered
= 0;
847 /* send cancel then resend request */
851 * We timed out, so we will rewrite the request
852 * to the fifo, but only if it isn't already full.
862 /* we got a reponse, so the server's lockd is OK */
863 nfs_up(VFSTONFS(vnode_mount(vp
)), p
, NFSSTA_LOCKTIMEO
,
864 wentdown
? "lockd alive again" : NULL
);
867 if (msgreq
.lmr_errno
== EINPROGRESS
) {
868 /* got NLM_BLOCKED response */
869 /* need to wait for NLM_GRANTED */
871 msgreq
.lmr_answered
= 0;
872 goto wait_for_granted
;
875 if ((msg
->lm_flags
& LOCKD_MSG_CANCEL
) &&
876 (msgreq
.lmr_saved_errno
== EINPROGRESS
)) {
878 * We just got a successful reply to the
879 * cancel of the previous blocked lock request.
880 * Now, go ahead and resend the request.
882 msg
->lm_flags
&= ~LOCKD_MSG_CANCEL
;
883 nfs_lockdmsg_dequeue(&msgreq
);
884 msg
->lm_xid
= nfs_lockxid_get();
885 nfs_lockdmsg_enqueue(&msgreq
);
886 msgreq
.lmr_saved_errno
= 0;
887 msgreq
.lmr_errno
= 0;
888 msgreq
.lmr_answered
= 0;
894 if ((msg
->lm_flags
& LOCKD_MSG_TEST
) && msgreq
.lmr_errno
== 0) {
895 if (msg
->lm_fl
.l_type
!= F_UNLCK
) {
896 fl
->l_type
= msg
->lm_fl
.l_type
;
897 fl
->l_pid
= msg
->lm_fl
.l_pid
;
898 fl
->l_start
= msg
->lm_fl
.l_start
;
899 fl
->l_len
= msg
->lm_fl
.l_len
;
900 fl
->l_whence
= SEEK_SET
;
902 fl
->l_type
= F_UNLCK
;
907 * If the blocked lock request was cancelled.
908 * Restore the error condition from when we
909 * originally bailed on the request.
911 if (msg
->lm_flags
& LOCKD_MSG_CANCEL
) {
912 msg
->lm_flags
&= ~LOCKD_MSG_CANCEL
;
913 error
= msgreq
.lmr_saved_errno
;
915 error
= msgreq
.lmr_errno
;
918 /* record that NFS file locking has worked on this mount */
919 nmp
= VFSTONFS(vnode_mount(vp
));
920 if (nmp
&& !(nmp
->nm_state
& NFSSTA_LOCKSWORK
))
921 nmp
->nm_state
|= NFSSTA_LOCKSWORK
;
923 * If we successfully acquired a lock, make sure this pid
924 * is in the nfs_lock_pid hash table so we know we can't
925 * short-circuit unlock requests.
927 if ((lockpidcheck
== ENOENT
) &&
928 ((ap
->a_op
== F_SETLK
) || (ap
->a_op
== F_SETLKW
)))
929 nfs_lock_pid_check(p
, 1, vp
);
935 nfs_lockdmsg_dequeue(&msgreq
);
937 error1
= VNOP_CLOSE(wvp
, FWRITE
, ap
->a_context
);
940 /* prefer any previous 'error' to our vn_close 'error1'. */
941 return (error
!= 0 ? error
: error1
);
946 * NFS advisory byte-level locks answer from the lock daemon.
949 nfslockdans(proc_t p
, struct lockd_ans
*ansp
)
951 LOCKD_MSG_REQUEST
*msgreq
;
954 /* Let root make this call. */
955 error
= proc_suser(p
);
959 /* the version should match, or we're out of sync */
960 if (ansp
->la_version
!= LOCKD_ANS_VERSION
)
963 /* try to find the lockd message by transaction id (cookie) */
964 msgreq
= nfs_lockdmsg_find_by_xid(ansp
->la_xid
);
965 if (ansp
->la_flags
& LOCKD_ANS_GRANTED
) {
967 * We can't depend on the granted message having our cookie,
968 * so we check the answer against the lockd message found.
969 * If no message was found or it doesn't match the answer,
970 * we look for the lockd message by the answer's lock info.
972 if (!msgreq
|| nfs_lockdmsg_compare_to_answer(msgreq
, ansp
))
973 msgreq
= nfs_lockdmsg_find_by_answer(ansp
);
975 * We need to make sure this request isn't being cancelled
976 * If it is, we don't want to accept the granted message.
978 if (msgreq
&& (msgreq
->lmr_msg
.lm_flags
& LOCKD_MSG_CANCEL
))
984 msgreq
->lmr_errno
= ansp
->la_errno
;
985 if ((msgreq
->lmr_msg
.lm_flags
& LOCKD_MSG_TEST
) && msgreq
->lmr_errno
== 0) {
986 if (ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
) {
987 if (ansp
->la_flags
& LOCKD_ANS_LOCK_EXCL
)
988 msgreq
->lmr_msg
.lm_fl
.l_type
= F_WRLCK
;
990 msgreq
->lmr_msg
.lm_fl
.l_type
= F_RDLCK
;
991 msgreq
->lmr_msg
.lm_fl
.l_pid
= ansp
->la_pid
;
992 msgreq
->lmr_msg
.lm_fl
.l_start
= ansp
->la_start
;
993 msgreq
->lmr_msg
.lm_fl
.l_len
= ansp
->la_len
;
995 msgreq
->lmr_msg
.lm_fl
.l_type
= F_UNLCK
;
999 msgreq
->lmr_answered
= 1;
1000 (void)wakeup((void *)msgreq
);
1007 * NFS advisory byte-level locks: fifo file# from the lock daemon.
1010 nfslockdfd(proc_t p
, int fd
)
1015 error
= proc_suser(p
);
1021 error
= file_vnode(fd
, &vp
);
1024 error
= vnode_getwithref(vp
);
1027 error
= vnode_ref(vp
);
1033 oldvp
= nfslockdvnode
;
1038 (void)wakeup((void *)&nfslockdvnode
);
1047 * lock daemon waiting for lock request
1050 nfslockdwait(proc_t p
)
1054 error
= proc_suser(p
);
1057 if (nfslockdwaiting
|| nfslockdvnode
)
1060 nfslockdstarttimeout
= 0;
1061 nfslockdwaiting
= 1;
1062 tsleep((void *)&nfslockdwaiting
, PCATCH
| PUSER
, "lockd", 0);
1063 nfslockdwaiting
= 0;