2 * Copyright (c) 2002-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved.
25 * Redistribution and use in source and binary forms, with or without
26 * modification, are permitted provided that the following conditions
28 * 1. Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * 2. Redistributions in binary form must reproduce the above copyright
31 * notice, this list of conditions and the following disclaimer in the
32 * documentation and/or other materials provided with the distribution.
33 * 3. Berkeley Software Design Inc's name may not be used to endorse or
34 * promote products derived from this software without specific prior
37 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
38 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
41 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * from BSDI nfs_lock.c,v 2.4 1998/12/14 23:49:56 jch Exp
52 #include <sys/cdefs.h>
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h> /* for hz */
57 #include <sys/file_internal.h>
58 #include <sys/malloc.h>
59 #include <sys/lockf.h> /* for hz */ /* Must come after sys/malloc.h */
60 #include <sys/kpi_mbuf.h>
61 #include <sys/mount_internal.h>
62 #include <sys/proc_internal.h> /* for p_start */
63 #include <sys/kauth.h>
64 #include <sys/resourcevar.h>
65 #include <sys/socket.h>
66 #include <sys/unistd.h>
68 #include <sys/vnode_internal.h>
70 #include <kern/thread.h>
72 #include <machine/limits.h>
76 #include <nfs/rpcv2.h>
77 #include <nfs/nfsproto.h>
79 #include <nfs/nfsmount.h>
80 #include <nfs/nfsnode.h>
81 #include <nfs/nfs_lock.h>
83 #define OFF_MAX QUAD_MAX
86 * globals for managing the lockd fifo
88 vnode_t nfslockdvnode
= 0;
89 int nfslockdwaiting
= 0;
90 time_t nfslockdstarttimeout
= 0;
91 int nfslockdfifolock
= 0;
92 #define NFSLOCKDFIFOLOCK_LOCKED 1
93 #define NFSLOCKDFIFOLOCK_WANT 2
96 * pending lock request messages are kept in this queue which is
97 * kept sorted by transaction ID (xid).
99 uint64_t nfs_lockxid
= 0;
100 LOCKD_MSG_QUEUE nfs_pendlockq
;
103 * This structure is used to identify processes which have acquired NFS locks.
104 * Knowing which processes have ever acquired locks allows us to short-circuit
105 * unlock requests for processes that have never had an NFS file lock. Thus
106 * avoiding a costly and unnecessary lockd request.
108 struct nfs_lock_pid
{
109 TAILQ_ENTRY(nfs_lock_pid
) lp_lru
; /* LRU list */
110 LIST_ENTRY(nfs_lock_pid
) lp_hash
; /* hash chain */
111 int lp_valid
; /* valid entry? */
112 int lp_time
; /* last time seen valid */
113 pid_t lp_pid
; /* The process ID. */
114 struct timeval lp_pid_start
; /* Start time of process id */
117 #define NFS_LOCK_PID_HASH_SIZE 64 // XXX tune me
118 #define NFS_LOCK_PID_HASH(pid) \
119 (&nfs_lock_pid_hash_tbl[(pid) & nfs_lock_pid_hash])
120 LIST_HEAD(, nfs_lock_pid
) *nfs_lock_pid_hash_tbl
;
121 TAILQ_HEAD(, nfs_lock_pid
) nfs_lock_pid_lru
;
122 u_long nfs_lock_pid_hash
;
123 int nfs_lock_pid_lock
;
127 * initialize global nfs lock state
132 TAILQ_INIT(&nfs_pendlockq
);
133 nfs_lock_pid_lock
= 0;
134 nfs_lock_pid_hash_tbl
= hashinit(NFS_LOCK_PID_HASH_SIZE
,
135 M_TEMP
, &nfs_lock_pid_hash
);
136 TAILQ_INIT(&nfs_lock_pid_lru
);
140 * insert a lock request message into the pending queue
143 nfs_lockdmsg_enqueue(LOCKD_MSG_REQUEST
*msgreq
)
145 LOCKD_MSG_REQUEST
*mr
;
147 mr
= TAILQ_LAST(&nfs_pendlockq
, nfs_lock_msg_queue
);
148 if (!mr
|| (msgreq
->lmr_msg
.lm_xid
> mr
->lmr_msg
.lm_xid
)) {
149 /* fast path: empty queue or new largest xid */
150 TAILQ_INSERT_TAIL(&nfs_pendlockq
, msgreq
, lmr_next
);
153 /* slow path: need to walk list to find insertion point */
154 while (mr
&& (msgreq
->lmr_msg
.lm_xid
> mr
->lmr_msg
.lm_xid
)) {
155 mr
= TAILQ_PREV(mr
, nfs_lock_msg_queue
, lmr_next
);
158 TAILQ_INSERT_AFTER(&nfs_pendlockq
, mr
, msgreq
, lmr_next
);
160 TAILQ_INSERT_HEAD(&nfs_pendlockq
, msgreq
, lmr_next
);
165 * remove a lock request message from the pending queue
168 nfs_lockdmsg_dequeue(LOCKD_MSG_REQUEST
*msgreq
)
170 TAILQ_REMOVE(&nfs_pendlockq
, msgreq
, lmr_next
);
174 * find a pending lock request message by xid
176 * We search from the head of the list assuming that the message we're
177 * looking for is for an older request (because we have an answer to it).
178 * This assumes that lock request will be answered primarily in FIFO order.
179 * However, this may not be the case if there are blocked requests. We may
180 * want to move blocked requests to a separate queue (but that'll complicate
181 * duplicate xid checking).
183 static inline LOCKD_MSG_REQUEST
*
184 nfs_lockdmsg_find_by_xid(uint64_t lockxid
)
186 LOCKD_MSG_REQUEST
*mr
;
188 TAILQ_FOREACH(mr
, &nfs_pendlockq
, lmr_next
) {
189 if (mr
->lmr_msg
.lm_xid
== lockxid
)
191 if (mr
->lmr_msg
.lm_xid
> lockxid
)
198 * Because we can't depend on nlm_granted messages containing the same
199 * cookie we sent with the original lock request, we need code test if
200 * an nlm_granted answer matches the lock request. We also need code
201 * that can find a lockd message based solely on the nlm_granted answer.
205 * compare lockd message to answer
207 * returns 0 on equality and 1 if different
210 nfs_lockdmsg_compare_to_answer(LOCKD_MSG_REQUEST
*msgreq
, struct lockd_ans
*ansp
)
212 if (!(ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
))
214 if (msgreq
->lmr_msg
.lm_fl
.l_pid
!= ansp
->la_pid
)
216 if (msgreq
->lmr_msg
.lm_fl
.l_start
!= ansp
->la_start
)
218 if (msgreq
->lmr_msg
.lm_fl
.l_len
!= ansp
->la_len
)
220 if (msgreq
->lmr_msg
.lm_fh_len
!= ansp
->la_fh_len
)
222 if (bcmp(msgreq
->lmr_msg
.lm_fh
, ansp
->la_fh
, ansp
->la_fh_len
))
228 * find a pending lock request message based on the lock info provided
229 * in the lockd_ans/nlm_granted data. We need this because we can't
230 * depend on nlm_granted messages containing the same cookie we sent
231 * with the original lock request.
233 * We search from the head of the list assuming that the message we're
234 * looking for is for an older request (because we have an answer to it).
235 * This assumes that lock request will be answered primarily in FIFO order.
236 * However, this may not be the case if there are blocked requests. We may
237 * want to move blocked requests to a separate queue (but that'll complicate
238 * duplicate xid checking).
240 static inline LOCKD_MSG_REQUEST
*
241 nfs_lockdmsg_find_by_answer(struct lockd_ans
*ansp
)
243 LOCKD_MSG_REQUEST
*mr
;
245 if (!(ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
))
247 TAILQ_FOREACH(mr
, &nfs_pendlockq
, lmr_next
) {
248 if (!nfs_lockdmsg_compare_to_answer(mr
, ansp
))
255 * return the next unique lock request transaction ID
257 static inline uint64_t
258 nfs_lockxid_get(void)
260 LOCKD_MSG_REQUEST
*mr
;
262 /* derive initial lock xid from system time */
265 * Note: it's OK if this code inits nfs_lockxid to 0 (for example,
266 * due to a broken clock) because we immediately increment it
267 * and we guarantee to never use xid 0. So, nfs_lockxid should only
268 * ever be 0 the first time this function is called.
272 nfs_lockxid
= (uint64_t)tv
.tv_sec
<< 12;
275 /* make sure we get a unique xid */
277 /* Skip zero xid if it should ever happen. */
278 if (++nfs_lockxid
== 0)
280 if (!(mr
= TAILQ_LAST(&nfs_pendlockq
, nfs_lock_msg_queue
)) ||
281 (mr
->lmr_msg
.lm_xid
< nfs_lockxid
)) {
282 /* fast path: empty queue or new largest xid */
285 /* check if xid is already in use */
286 } while (nfs_lockdmsg_find_by_xid(nfs_lockxid
));
293 * Check the nfs_lock_pid hash table for an entry and, if requested,
294 * add the entry if it is not found.
296 * (Also, if adding, try to clean up some stale entries.)
299 nfs_lock_pid_check(proc_t p
, int addflag
, vnode_t vp
)
301 struct nfs_lock_pid
*lp
, *lplru
, *lplru_next
;
308 if (nfs_lock_pid_lock
) {
309 struct nfsmount
*nmp
= VFSTONFS(vnode_mount(vp
));
310 while (nfs_lock_pid_lock
) {
311 nfs_lock_pid_lock
= -1;
312 tsleep(&nfs_lock_pid_lock
, PCATCH
, "nfslockpid", 0);
313 if ((error
= nfs_sigintr(nmp
, NULL
, p
)))
318 nfs_lock_pid_lock
= 1;
320 /* Search hash chain */
322 lp
= NFS_LOCK_PID_HASH(proc_pid(p
))->lh_first
;
323 for (; lp
!= NULL
; lp
= lp
->lp_hash
.le_next
)
324 if (lp
->lp_pid
== proc_pid(p
)) {
326 if (timevalcmp(&lp
->lp_pid_start
, &p
->p_stats
->p_start
, ==)) {
327 /* ...and it's valid */
328 /* move to tail of LRU */
329 TAILQ_REMOVE(&nfs_lock_pid_lru
, lp
, lp_lru
);
331 lp
->lp_time
= now
.tv_sec
;
332 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lp
, lp_lru
);
336 /* ...but it's no longer valid */
337 /* remove from hash, invalidate, and move to lru head */
338 LIST_REMOVE(lp
, lp_hash
);
340 TAILQ_REMOVE(&nfs_lock_pid_lru
, lp
, lp_lru
);
341 TAILQ_INSERT_HEAD(&nfs_lock_pid_lru
, lp
, lp_lru
);
346 /* if we didn't find it (valid) and we've been asked to add it */
347 if ((error
== ENOENT
) && addflag
) {
348 /* scan lru list for invalid, stale entries to reuse/free */
351 for (lplru
= TAILQ_FIRST(&nfs_lock_pid_lru
); lplru
; lplru
= lplru_next
) {
352 lplru_next
= TAILQ_NEXT(lplru
, lp_lru
);
353 if (lplru
->lp_valid
&& (lplru
->lp_time
>= (now
.tv_sec
- 2))) {
355 * If the oldest LRU entry is relatively new, then don't
356 * bother scanning any further.
360 /* remove entry from LRU, and check if it's still in use */
361 TAILQ_REMOVE(&nfs_lock_pid_lru
, lplru
, lp_lru
);
362 if (!lplru
->lp_valid
|| !(plru
= pfind(lplru
->lp_pid
)) ||
363 timevalcmp(&lplru
->lp_pid_start
, &plru
->p_stats
->p_start
, !=)) {
364 /* no longer in use */
365 LIST_REMOVE(lplru
, lp_hash
);
367 /* we'll reuse this one */
370 /* we can free this one */
375 lplru
->lp_time
= now
.tv_sec
;
376 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lplru
, lp_lru
);
378 /* don't check too many entries at once */
383 /* we need to allocate a new one */
384 MALLOC(lp
, struct nfs_lock_pid
*, sizeof(struct nfs_lock_pid
),
385 M_TEMP
, M_WAITOK
| M_ZERO
);
390 /* (re)initialize nfs_lock_pid info */
391 lp
->lp_pid
= proc_pid(p
);
392 lp
->lp_pid_start
= p
->p_stats
->p_start
;
393 /* insert pid in hash */
394 LIST_INSERT_HEAD(NFS_LOCK_PID_HASH(lp
->lp_pid
), lp
, lp_hash
);
396 lp
->lp_time
= now
.tv_sec
;
397 TAILQ_INSERT_TAIL(&nfs_lock_pid_lru
, lp
, lp_lru
);
403 if (nfs_lock_pid_lock
< 0) {
404 nfs_lock_pid_lock
= 0;
405 wakeup(&nfs_lock_pid_lock
);
407 nfs_lock_pid_lock
= 0;
415 * NFS advisory byte-level locks.
418 nfs_dolock(struct vnop_advlock_args
*ap
)
419 /* struct vnop_advlock_args {
420 struct vnodeop_desc *a_desc;
426 vfs_context_t a_context;
429 LOCKD_MSG_REQUEST msgreq
;
436 struct nfsmount
*nmp
;
437 struct nfs_vattr nvattr
;
440 int timeo
, endtime
, lastmsg
, wentdown
= 0;
444 struct sockaddr
*saddr
;
446 p
= vfs_context_proc(ap
->a_context
);
447 cred
= vfs_context_ucred(ap
->a_context
);
453 nmp
= VFSTONFS(vnode_mount(vp
));
456 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
)
460 * The NLM protocol doesn't allow the server to return an error
461 * on ranges, so we do it. Pre LFS (Large File Summit)
462 * standards required EINVAL for the range errors. More recent
463 * standards use EOVERFLOW, but their EINVAL wording still
464 * encompasses these errors.
465 * Any code sensitive to this is either:
466 * 1) written pre-LFS and so can handle only EINVAL, or
467 * 2) written post-LFS and thus ought to be tolerant of pre-LFS
469 * Since returning EOVERFLOW certainly breaks 1), we return EINVAL.
471 if (fl
->l_whence
!= SEEK_END
) {
472 if ((fl
->l_whence
!= SEEK_CUR
&& fl
->l_whence
!= SEEK_SET
) ||
474 (fl
->l_len
> 0 && fl
->l_len
- 1 > OFF_MAX
- fl
->l_start
) ||
475 (fl
->l_len
< 0 && fl
->l_start
+ fl
->l_len
< 0))
479 * If daemon is running take a ref on its fifo vnode
481 if (!(wvp
= nfslockdvnode
)) {
482 if (!nfslockdwaiting
&& !nfslockdstarttimeout
)
485 * Don't wake lock daemon if it hasn't been started yet and
486 * this is an unlock request (since we couldn't possibly
487 * actually have a lock on the file). This could be an
488 * uninformed unlock request due to closef()'s behavior of doing
489 * unlocks on all files if a process has had a lock on ANY file.
491 if (!nfslockdvnode
&& (fl
->l_type
== F_UNLCK
))
494 if (nfslockdwaiting
) {
495 /* wake up lock daemon */
496 nfslockdstarttimeout
= now
.tv_sec
+ 60;
497 (void)wakeup((void *)&nfslockdwaiting
);
499 /* wait on nfslockdvnode for a while to allow daemon to start */
500 while (!nfslockdvnode
&& (now
.tv_sec
< nfslockdstarttimeout
)) {
501 error
= tsleep((void *)&nfslockdvnode
, PCATCH
| PUSER
, "lockdstart", 2*hz
);
502 if (error
&& (error
!= EWOULDBLOCK
))
504 /* check that we still have our mount... */
505 /* ...and that we still support locks */
506 nmp
= VFSTONFS(vnode_mount(vp
));
509 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
)
516 * check for nfslockdvnode
517 * If it hasn't started by now, there's a problem.
519 if (!(wvp
= nfslockdvnode
))
522 error
= vnode_getwithref(wvp
);
525 error
= vnode_ref(wvp
);
532 * Need to check if this process has successfully acquired an NFS lock before.
533 * If not, and this is an unlock request we can simply return success here.
535 lockpidcheck
= nfs_lock_pid_check(p
, 0, vp
);
537 if (lockpidcheck
!= ENOENT
) {
540 return (lockpidcheck
);
542 if (ap
->a_op
== F_UNLCK
) {
550 * The NFS Lock Manager protocol doesn't directly handle
551 * negative lengths or SEEK_END, so we need to normalize
552 * things here where we have all the info.
553 * (Note: SEEK_CUR is already adjusted for at this point)
555 /* Convert the flock structure into a start and end. */
556 switch (fl
->l_whence
) {
560 * Caller is responsible for adding any necessary offset
561 * to fl->l_start when SEEK_CUR is used.
566 /* need to flush, and refetch attributes to make */
567 /* sure we have the correct end of file offset */
568 if (np
->n_flag
& NMODIFIED
) {
570 error
= nfs_vinvalbuf(vp
, V_SAVE
, cred
, p
, 1);
579 error
= nfs_getattr(vp
, &nvattr
, cred
, p
);
585 start
= np
->n_size
+ fl
->l_start
;
594 else if (fl
->l_len
> 0)
595 end
= start
+ fl
->l_len
- 1;
596 else { /* l_len is negative */
606 ((start
>= 0x80000000) || (end
>= 0x80000000))) {
613 * Fill in the information structure.
615 msgreq
.lmr_answered
= 0;
616 msgreq
.lmr_errno
= 0;
617 msgreq
.lmr_saved_errno
= 0;
618 msg
= &msgreq
.lmr_msg
;
619 msg
->lm_version
= LOCKD_MSG_VERSION
;
623 msg
->lm_fl
.l_start
= start
;
625 msg
->lm_fl
.l_len
= end
- start
+ 1;
626 msg
->lm_fl
.l_pid
= proc_pid(p
);
628 if (ap
->a_flags
& F_WAIT
)
629 msg
->lm_flags
|= LOCKD_MSG_BLOCK
;
630 if (ap
->a_op
== F_GETLK
)
631 msg
->lm_flags
|= LOCKD_MSG_TEST
;
633 nmp
= VFSTONFS(vnode_mount(vp
));
640 saddr
= mbuf_data(nmp
->nm_nam
);
641 bcopy(saddr
, &msg
->lm_addr
, min(sizeof msg
->lm_addr
, saddr
->sa_len
));
642 msg
->lm_fh_len
= NFS_ISV3(vp
) ? VTONFS(vp
)->n_fhsize
: NFSX_V2FH
;
643 bcopy(VTONFS(vp
)->n_fhp
, msg
->lm_fh
, msg
->lm_fh_len
);
645 msg
->lm_flags
|= LOCKD_MSG_NFSV3
;
646 cru2x(cred
, &msg
->lm_cred
);
649 lastmsg
= now
.tv_sec
- ((nmp
->nm_tprintf_delay
) - (nmp
->nm_tprintf_initial_delay
));
651 fmode
= FFLAGS(O_WRONLY
);
652 if ((error
= VNOP_OPEN(wvp
, fmode
, ap
->a_context
))) {
661 /* allocate unique xid */
662 msg
->lm_xid
= nfs_lockxid_get();
663 nfs_lockdmsg_enqueue(&msgreq
);
666 #define IO_NOMACCHECK 0;
667 ioflg
= IO_UNIT
| IO_NOMACCHECK
;
670 while (nfslockdfifolock
& NFSLOCKDFIFOLOCK_LOCKED
) {
671 nfslockdfifolock
|= NFSLOCKDFIFOLOCK_WANT
;
672 error
= tsleep((void *)&nfslockdfifolock
,
673 PCATCH
| PUSER
, "lockdfifo", 20*hz
);
679 nfslockdfifolock
|= NFSLOCKDFIFOLOCK_LOCKED
;
681 error
= vn_rdwr(UIO_WRITE
, wvp
, (caddr_t
)msg
, sizeof(*msg
), 0,
682 UIO_SYSSPACE32
, ioflg
, proc_ucred(kernproc
), NULL
, p
);
684 nfslockdfifolock
&= ~NFSLOCKDFIFOLOCK_LOCKED
;
685 if (nfslockdfifolock
& NFSLOCKDFIFOLOCK_WANT
) {
686 nfslockdfifolock
&= ~NFSLOCKDFIFOLOCK_WANT
;
687 wakeup((void *)&nfslockdfifolock
);
690 if (error
&& (((ioflg
& IO_NDELAY
) == 0) || error
!= EAGAIN
)) {
695 * Always wait for an answer. Not waiting for unlocks could
696 * cause a lock to be left if the unlock request gets dropped.
700 * Retry if it takes too long to get a response.
702 * The timeout numbers were picked out of thin air... they start
703 * at 2 and double each timeout with a max of 60 seconds.
705 * In order to maintain responsiveness, we pass a small timeout
706 * to tsleep and calculate the timeouts ourselves. This allows
707 * us to pick up on mount changes quicker.
713 endtime
= now
.tv_sec
+ timeo
/hz
;
715 endtime
= now
.tv_sec
+ 1;
716 while (now
.tv_sec
< endtime
) {
717 error
= tsleep((void *)&msgreq
, PCATCH
| PUSER
, "lockd", 2*hz
);
718 if (msgreq
.lmr_answered
) {
720 * Note: it's possible to have a lock granted at
721 * essentially the same time that we get interrupted.
722 * Since the lock may be granted, we can't return an
723 * error from this request or we might not unlock the
724 * lock that's been granted.
729 if (error
!= EWOULDBLOCK
)
731 /* check that we still have our mount... */
732 /* ...and that we still support locks */
733 nmp
= VFSTONFS(vnode_mount(vp
));
734 if (!nmp
|| (nmp
->nm_flag
& NFSMNT_NOLOCKS
))
737 * If the mount is hung and we've requested not to hang
738 * on remote filesystems, then bail now.
740 if ((p
!= NULL
) && ((proc_noremotehang(p
)) != 0) &&
741 ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_LOCKTIMEO
)) != 0)) {
742 if (fl
->l_type
== F_UNLCK
)
743 printf("nfs_dolock: aborting unlock request "
744 "due to timeout (noremotehang)\n");
751 /* check that we still have our mount... */
752 nmp
= VFSTONFS(vnode_mount(vp
));
754 if (error
== EWOULDBLOCK
)
758 /* ...and that we still support locks */
759 if (nmp
->nm_flag
& NFSMNT_NOLOCKS
) {
760 if (error
== EWOULDBLOCK
)
764 if ((error
== ENOTSUP
) &&
765 (nmp
->nm_state
& NFSSTA_LOCKSWORK
)) {
767 * We have evidence that locks work, yet lockd
768 * returned ENOTSUP. This is probably because
769 * it was unable to contact the server's lockd to
770 * send it the request.
772 * Because we know locks work, we'll consider
773 * this failure to be a timeout.
777 if (error
!= EWOULDBLOCK
) {
779 * We're going to bail on this request.
780 * If we were a blocked lock request, send a cancel.
782 if ((msgreq
.lmr_errno
== EINPROGRESS
) &&
783 !(msg
->lm_flags
& LOCKD_MSG_CANCEL
)) {
784 /* set this request up as a cancel */
785 msg
->lm_flags
|= LOCKD_MSG_CANCEL
;
786 nfs_lockdmsg_dequeue(&msgreq
);
787 msg
->lm_xid
= nfs_lockxid_get();
788 nfs_lockdmsg_enqueue(&msgreq
);
789 msgreq
.lmr_saved_errno
= error
;
790 msgreq
.lmr_errno
= 0;
791 msgreq
.lmr_answered
= 0;
794 /* send cancel request */
801 * If the mount is hung and we've requested not to hang
802 * on remote filesystems, then bail now.
804 if ((p
!= NULL
) && ((proc_noremotehang(p
)) != 0) &&
805 ((nmp
->nm_state
& (NFSSTA_TIMEO
|NFSSTA_LOCKTIMEO
)) != 0)) {
806 if (fl
->l_type
== F_UNLCK
)
807 printf("nfs_dolock: aborting unlock request "
808 "due to timeout (noremotehang)\n");
812 /* warn if we're not getting any response */
814 if ((msgreq
.lmr_errno
!= EINPROGRESS
) &&
815 (nmp
->nm_tprintf_initial_delay
!= 0) &&
816 ((lastmsg
+ nmp
->nm_tprintf_delay
) < now
.tv_sec
)) {
817 lastmsg
= now
.tv_sec
;
818 nfs_down(nmp
, p
, 0, NFSSTA_LOCKTIMEO
, "lockd not responding");
821 if (msgreq
.lmr_errno
== EINPROGRESS
) {
823 * We've got a blocked lock request that we are
824 * going to retry. First, we'll want to try to
825 * send a cancel for the previous request.
827 * Clear errno so if we don't get a response
828 * to the resend we'll call nfs_down().
829 * Also reset timeout because we'll expect a
830 * quick response to the cancel/resend (even if
831 * it is NLM_BLOCKED).
833 msg
->lm_flags
|= LOCKD_MSG_CANCEL
;
834 nfs_lockdmsg_dequeue(&msgreq
);
835 msg
->lm_xid
= nfs_lockxid_get();
836 nfs_lockdmsg_enqueue(&msgreq
);
837 msgreq
.lmr_saved_errno
= msgreq
.lmr_errno
;
838 msgreq
.lmr_errno
= 0;
839 msgreq
.lmr_answered
= 0;
841 /* send cancel then resend request */
845 * We timed out, so we will rewrite the request
846 * to the fifo, but only if it isn't already full.
856 /* we got a reponse, so the server's lockd is OK */
857 nfs_up(VFSTONFS(vnode_mount(vp
)), p
, NFSSTA_LOCKTIMEO
,
858 wentdown
? "lockd alive again" : NULL
);
861 if (msgreq
.lmr_errno
== EINPROGRESS
) {
862 /* got NLM_BLOCKED response */
863 /* need to wait for NLM_GRANTED */
865 msgreq
.lmr_answered
= 0;
866 goto wait_for_granted
;
869 if ((msg
->lm_flags
& LOCKD_MSG_CANCEL
) &&
870 (msgreq
.lmr_saved_errno
== EINPROGRESS
)) {
872 * We just got a successful reply to the
873 * cancel of the previous blocked lock request.
874 * Now, go ahead and resend the request.
876 msg
->lm_flags
&= ~LOCKD_MSG_CANCEL
;
877 nfs_lockdmsg_dequeue(&msgreq
);
878 msg
->lm_xid
= nfs_lockxid_get();
879 nfs_lockdmsg_enqueue(&msgreq
);
880 msgreq
.lmr_saved_errno
= 0;
881 msgreq
.lmr_errno
= 0;
882 msgreq
.lmr_answered
= 0;
888 if ((msg
->lm_flags
& LOCKD_MSG_TEST
) && msgreq
.lmr_errno
== 0) {
889 if (msg
->lm_fl
.l_type
!= F_UNLCK
) {
890 fl
->l_type
= msg
->lm_fl
.l_type
;
891 fl
->l_pid
= msg
->lm_fl
.l_pid
;
892 fl
->l_start
= msg
->lm_fl
.l_start
;
893 fl
->l_len
= msg
->lm_fl
.l_len
;
894 fl
->l_whence
= SEEK_SET
;
896 fl
->l_type
= F_UNLCK
;
901 * If the blocked lock request was cancelled.
902 * Restore the error condition from when we
903 * originally bailed on the request.
905 if (msg
->lm_flags
& LOCKD_MSG_CANCEL
) {
906 msg
->lm_flags
&= ~LOCKD_MSG_CANCEL
;
907 error
= msgreq
.lmr_saved_errno
;
909 error
= msgreq
.lmr_errno
;
912 /* record that NFS file locking has worked on this mount */
913 nmp
= VFSTONFS(vnode_mount(vp
));
914 if (nmp
&& !(nmp
->nm_state
& NFSSTA_LOCKSWORK
))
915 nmp
->nm_state
|= NFSSTA_LOCKSWORK
;
917 * If we successfully acquired a lock, make sure this pid
918 * is in the nfs_lock_pid hash table so we know we can't
919 * short-circuit unlock requests.
921 if ((lockpidcheck
== ENOENT
) &&
922 ((ap
->a_op
== F_SETLK
) || (ap
->a_op
== F_SETLKW
)))
923 nfs_lock_pid_check(p
, 1, vp
);
929 nfs_lockdmsg_dequeue(&msgreq
);
931 error1
= VNOP_CLOSE(wvp
, FWRITE
, ap
->a_context
);
934 /* prefer any previous 'error' to our vn_close 'error1'. */
935 return (error
!= 0 ? error
: error1
);
940 * NFS advisory byte-level locks answer from the lock daemon.
943 nfslockdans(proc_t p
, struct lockd_ans
*ansp
)
945 LOCKD_MSG_REQUEST
*msgreq
;
948 /* Let root make this call. */
949 error
= proc_suser(p
);
953 /* the version should match, or we're out of sync */
954 if (ansp
->la_version
!= LOCKD_ANS_VERSION
)
957 /* try to find the lockd message by transaction id (cookie) */
958 msgreq
= nfs_lockdmsg_find_by_xid(ansp
->la_xid
);
959 if (ansp
->la_flags
& LOCKD_ANS_GRANTED
) {
961 * We can't depend on the granted message having our cookie,
962 * so we check the answer against the lockd message found.
963 * If no message was found or it doesn't match the answer,
964 * we look for the lockd message by the answer's lock info.
966 if (!msgreq
|| nfs_lockdmsg_compare_to_answer(msgreq
, ansp
))
967 msgreq
= nfs_lockdmsg_find_by_answer(ansp
);
969 * We need to make sure this request isn't being cancelled
970 * If it is, we don't want to accept the granted message.
972 if (msgreq
&& (msgreq
->lmr_msg
.lm_flags
& LOCKD_MSG_CANCEL
))
978 msgreq
->lmr_errno
= ansp
->la_errno
;
979 if ((msgreq
->lmr_msg
.lm_flags
& LOCKD_MSG_TEST
) && msgreq
->lmr_errno
== 0) {
980 if (ansp
->la_flags
& LOCKD_ANS_LOCK_INFO
) {
981 if (ansp
->la_flags
& LOCKD_ANS_LOCK_EXCL
)
982 msgreq
->lmr_msg
.lm_fl
.l_type
= F_WRLCK
;
984 msgreq
->lmr_msg
.lm_fl
.l_type
= F_RDLCK
;
985 msgreq
->lmr_msg
.lm_fl
.l_pid
= ansp
->la_pid
;
986 msgreq
->lmr_msg
.lm_fl
.l_start
= ansp
->la_start
;
987 msgreq
->lmr_msg
.lm_fl
.l_len
= ansp
->la_len
;
989 msgreq
->lmr_msg
.lm_fl
.l_type
= F_UNLCK
;
993 msgreq
->lmr_answered
= 1;
994 (void)wakeup((void *)msgreq
);
1001 * NFS advisory byte-level locks: fifo file# from the lock daemon.
1004 nfslockdfd(proc_t p
, int fd
)
1009 error
= proc_suser(p
);
1015 error
= file_vnode(fd
, &vp
);
1018 error
= vnode_getwithref(vp
);
1021 error
= vnode_ref(vp
);
1027 oldvp
= nfslockdvnode
;
1032 (void)wakeup((void *)&nfslockdvnode
);
1041 * lock daemon waiting for lock request
1044 nfslockdwait(proc_t p
)
1048 error
= proc_suser(p
);
1051 if (nfslockdwaiting
|| nfslockdvnode
)
1054 nfslockdstarttimeout
= 0;
1055 nfslockdwaiting
= 1;
1056 tsleep((void *)&nfslockdwaiting
, PCATCH
| PUSER
, "lockd", 0);
1057 nfslockdwaiting
= 0;