2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
15 #include <afsconfig.h>
16 #include "afs/param.h"
19 #include "afs/sysincludes.h" /* Standard vendor system headers */
20 #include "afsincludes.h" /* Afs-based standard headers */
21 #include "afs/afs_stats.h" /* statistics */
22 #include "afs/afs_cbqueue.h"
23 #include "afs/nfsclient.h"
24 #include "afs/afs_osidnlc.h"
25 #include "afs/unified_afs.h"
31 /* Static prototypes */
32 static int HandleGetLock(register struct vcache *avc,
33 register struct AFS_FLOCK *af,
34 register struct vrequest *areq, int clid);
35 static int GetFlockCount(struct vcache *avc, struct vrequest *areq);
36 static int lockIdcmp2(struct AFS_FLOCK *flock1, struct vcache *vp,
37 register struct SimpleLocks *alp, int onlymine,
39 static void DoLockWarning(void);
41 /* int clid; * non-zero on SGI, OSF, SunOS, Darwin, xBSD ** XXX ptr type */
43 #if defined(AFS_SUN5_ENV)
45 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
47 proc_t *procp = ttoproc(curthread);
52 slp->pid = procp->p_pid;
54 slp->sysid = procp->p_sysid;
55 slp->pid = procp->p_epid;
60 flock->l_pid = procp->p_pid;
62 flock->l_sysid = procp->p_sysid;
63 flock->l_pid = procp->p_epid;
67 #elif defined(AFS_SGI_ENV)
69 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
71 # if defined(AFS_SGI65_ENV)
73 get_current_flid(&flid);
75 afs_proc_t *procp = OSI_GET_CURRENT_PROCP();
80 slp->sysid = flid.fl_sysid;
82 slp->sysid = OSI_GET_CURRENT_SYSID();
87 flock->l_sysid = flid.fl_sysid;
89 flock->l_sysid = OSI_GET_CURRENT_SYSID();
94 #elif defined(AFS_AIX_ENV)
96 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
98 # if !defined(AFS_AIX32_ENV)
99 afs_proc_t *procp = u.u_procp;
103 # if defined(AFS_AIX41_ENV)
106 # elif defined(AFS_AIX32_ENV)
107 slp->sysid = u.u_sysid;
110 slp->sysid = procp->p_sysid;
111 slp->pid = prcop->p_epid;
114 # if defined(AFS_AIX41_ENV)
116 flock->l_pid = getpid();
117 # elif defined(AFS_AIX32_ENV)
118 flock->l_sysid = u.u_sysid;
119 flock->l_pid = u.u_epid;
121 flock->l_sysid = procp->p_sysid;
122 flock->l_pid = procp->p_epid;
125 #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
127 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
135 #elif defined(AFS_LINUX20_ENV) || defined(AFS_HPUX_ENV)
137 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
142 flock->l_pid = getpid();
147 lockIdSet(struct AFS_FLOCK *flock, struct SimpleLocks *slp, int clid)
150 slp->pid = u.u_procp->p_pid;
152 flock->l_pid = u.u_procp->p_pid;
157 /* return 1 (true) if specified flock does not match alp (if
158 * specified), or any of the slp structs (if alp == 0)
160 /* I'm not sure that the comparsion of flock->pid to p_ppid
161 * is correct. Should that be a comparision of alp (or slp) ->pid
162 * to p_ppid? Especially in the context of the lower loop, where
163 * the repeated comparison doesn't make much sense...
165 /* onlymine - don't match any locks which are held by my parent */
166 /* clid - only irix 6.5 */
169 lockIdcmp2(struct AFS_FLOCK *flock1, struct vcache *vp,
170 register struct SimpleLocks *alp, int onlymine, int clid)
172 register struct SimpleLocks *slp;
173 #if defined(AFS_SUN5_ENV)
174 register proc_t *procp = ttoproc(curthread);
176 #if !defined(AFS_AIX41_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_SGI65_ENV) && !defined(AFS_DARWIN_ENV) && !defined(AFS_XBSD_ENV)
178 afs_proc_t *procp = curprocp;
179 #else /* AFS_SGI64_ENV */
180 afs_proc_t *procp = u.u_procp;
181 #endif /* AFS_SGI64_ENV */
186 #if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV) || defined(AFS_SGI_ENV)
187 if (flock1->l_sysid != alp->sysid) {
191 if ((flock1->l_pid == alp->pid) ||
192 #if defined(AFS_AIX41_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_HPUX_ENV)
193 (!onlymine && (flock1->l_pid == getppid()))
195 #if defined(AFS_SGI65_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
196 /* XXX check this. used to be *only* irix for some reason. */
197 (!onlymine && (flock1->l_pid == clid))
199 (!onlymine && (flock1->l_pid == procp->p_ppid))
208 for (slp = vp->slocks; slp; slp = slp->next) {
209 #if defined(AFS_HAVE_FLOCK_SYSID)
210 if (flock1->l_sysid != slp->sysid) {
214 if (flock1->l_pid == slp->pid) {
218 return (1); /* failure */
222 /* we don't send multiple read flocks to the server, but rather just count
223 them up ourselves. Of course, multiple write locks are incompatible.
225 Note that we should always try to release a lock, even if we have
226 a network problem sending the release command through, since often
227 a lock is released on a close call, when the user can't retry anyway.
229 After we remove it from our structure, the lock will no longer be
230 kept alive, and the server should time it out within a few minutes.
232 94.04.13 add "force" parameter. If a child explicitly unlocks a
233 file, I guess we'll permit it. however, we don't want simple,
234 innocent closes by children to unlock files in the parent process.
236 If called when disconnected support is unabled, the discon_lock must
239 /* clid - nonzero on sgi sunos osf1 only */
241 HandleFlock(register struct vcache *avc, int acom, struct vrequest *areq,
242 pid_t clid, int onlymine)
245 struct SimpleLocks *slp, *tlp, **slpp;
247 struct AFSVolSync tsync;
249 struct AFS_FLOCK flock;
251 AFS_STATCNT(HandleFlock);
252 code = 0; /* default when we don't make any network calls */
253 lockIdSet(&flock, NULL, clid);
255 #if defined(AFS_SGI_ENV)
256 osi_Assert(valusema(&avc->vc_rwlock) <= 0);
257 osi_Assert(OSI_GET_LOCKID() == avc->vc_rwlockid);
259 ObtainWriteLock(&avc->lock, 118);
260 if (acom & LOCK_UN) {
265 /* If the lock is held exclusive, then only the owning process
266 * or a child can unlock it. Use pid and ppid because they are
267 * unique identifiers.
269 if ((avc->flockCount < 0) && (getpid() != avc->ownslock)) {
271 if (onlymine || (getppid() != avc->ownslock)) {
273 if (onlymine || (u.u_procp->p_ppid != avc->ownslock)) {
275 ReleaseWriteLock(&avc->lock);
280 if (lockIdcmp2(&flock, avc, NULL, onlymine, clid)) {
281 ReleaseWriteLock(&avc->lock);
287 if (avc->flockCount == 0) {
288 ReleaseWriteLock(&avc->lock);
292 /* unlock the lock */
293 if (avc->flockCount > 0) {
295 for (slp = *slpp; slp;) {
296 if (!lockIdcmp2(&flock, avc, slp, onlymine, clid)) {
298 tlp = *slpp = slp->next;
299 osi_FreeSmallSpace(slp);
306 } else if (avc->flockCount == -1) {
307 afs_StoreAllSegments(avc, areq, AFS_ASYNC); /* fsync file early */
309 /* And remove the (only) exclusive lock entry from the list... */
310 osi_FreeSmallSpace(avc->slocks);
313 if (avc->flockCount == 0) {
314 if (!AFS_IS_DISCONNECTED) {
316 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
318 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_RELEASELOCK);
320 code = RXAFS_ReleaseLock(tc->id, (struct AFSFid *)
321 &avc->f.fid.Fid, &tsync);
327 (tc, code, &avc->f.fid, areq,
328 AFS_STATS_FS_RPCIDX_RELEASELOCK, SHARED_LOCK, NULL));
330 /*printf("Network is dooooooowwwwwwwnnnnnnn\n");*/
335 while (1) { /* set a new lock */
337 * Upgrading from shared locks to Exclusive and vice versa
338 * is a bit tricky and we don't really support it yet. But
339 * we try to support the common used one which is upgrade
340 * a shared lock to an exclusive for the same process...
342 if ((avc->flockCount > 0 && (acom & LOCK_EX))
343 || (avc->flockCount == -1 && (acom & LOCK_SH))) {
345 * Upgrading from shared locks to an exclusive one:
346 * For now if all the shared locks belong to the
347 * same process then we unlock them on the server
348 * and proceed with the upgrade. Unless we change the
349 * server's locking interface impl we prohibit from
350 * unlocking other processes's shared locks...
351 * Upgrading from an exclusive lock to a shared one:
352 * Again only allowed to be done by the same process.
355 for (slp = *slpp; slp;) {
357 (&flock, avc, slp, 1 /*!onlymine */ , clid)) {
362 tlp = *slpp = slp->next;
363 osi_FreeSmallSpace(slp);
371 if (!code && avc->flockCount == 0) {
372 if (!AFS_IS_DISCONNECTED) {
374 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
377 (AFS_STATS_FS_RPCIDX_RELEASELOCK);
380 RXAFS_ReleaseLock(tc->id,
381 (struct AFSFid *)&avc->
388 (tc, code, &avc->f.fid, areq,
389 AFS_STATS_FS_RPCIDX_RELEASELOCK, SHARED_LOCK,
393 } else if (avc->flockCount == -1 && (acom & LOCK_EX)) {
394 if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
400 /* compatible here, decide if needs to go to file server. If
401 * we've already got the file locked (and thus read-locked, since
402 * we've already checked for compatibility), we shouldn't send
403 * the call through to the server again */
404 if (avc->flockCount == 0) {
405 /* we're the first on our block, send the call through */
406 lockType = ((acom & LOCK_EX) ? LockWrite : LockRead);
407 if (!AFS_IS_DISCONNECTED) {
409 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
411 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_SETLOCK);
413 code = RXAFS_SetLock(tc->id, (struct AFSFid *)
414 &avc->f.fid.Fid, lockType,
421 (tc, code, &avc->f.fid, areq,
422 AFS_STATS_FS_RPCIDX_SETLOCK, SHARED_LOCK,
425 /* XXX - Should probably try and log this when we're
426 * XXX - running with logging enabled. But it's horrid
428 code = 0; /* pretend we worked - ick!!! */
430 code = 0; /* otherwise, pretend things worked */
433 slp = (struct SimpleLocks *)
434 osi_AllocSmallSpace(sizeof(struct SimpleLocks));
435 if (acom & LOCK_EX) {
440 /* Record unique id of process owning exclusive lock. */
441 avc->ownslock = getpid();
444 slp->type = LockWrite;
447 avc->flockCount = -1;
449 slp->type = LockRead;
450 slp->next = avc->slocks;
455 lockIdSet(&flock, slp, clid);
458 /* now, if we got EWOULDBLOCK, and we're supposed to wait, we do */
459 if (((code == EWOULDBLOCK) || (code == EAGAIN) ||
460 (code == UAEWOULDBLOCK) || (code == UAEAGAIN))
461 && !(acom & LOCK_NB)) {
462 /* sleep for a second, allowing interrupts */
463 ReleaseWriteLock(&avc->lock);
464 #if defined(AFS_SGI_ENV)
465 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
467 code = afs_osi_Wait(1000, NULL, 1);
468 #if defined(AFS_SGI_ENV)
469 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
471 ObtainWriteLock(&avc->lock, 120);
473 code = EINTR; /* return this if ^C typed */
480 ReleaseWriteLock(&avc->lock);
481 code = afs_CheckCode(code, areq, 1); /* defeat a buggy AIX optimization */
486 /* warn a user that a lock has been ignored */
487 afs_int32 lastWarnTime = 0; /* this is used elsewhere */
491 register afs_int32 now;
494 AFS_STATCNT(DoLockWarning);
495 /* check if we've already warned someone recently */
496 if (now < lastWarnTime + 120)
499 /* otherwise, it is time to nag the user */
501 #ifdef AFS_LINUX26_ENV
503 ("afs: byte-range locks only enforced for processes on this machine.\n");
506 ("afs: byte-range lock/unlock ignored; make sure no one else is running this program.\n");
511 #if defined(AFS_SGI_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)
512 int afs_lockctl(struct vcache * avc, struct AFS_FLOCK * af, int acmd,
513 afs_ucred_t * acred, pid_t clid)
516 int afs_lockctl(struct vcache * avc, struct AFS_FLOCK * af, int acmd,
520 struct vrequest treq;
522 struct afs_fakestat_state fakestate;
524 AFS_STATCNT(afs_lockctl);
525 if ((code = afs_InitReq(&treq, acred)))
527 afs_InitFakeStat(&fakestate);
531 code = afs_EvalFakeStat(&avc, &fakestate, &treq);
535 #if (defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)) && !defined(AFS_SUN58_ENV)
536 if ((acmd == F_GETLK) || (acmd == F_RGETLK)) {
538 if (acmd == F_GETLK) {
540 if (af->l_type == F_UNLCK) {
544 code = HandleGetLock(avc, af, &treq, clid);
545 code = afs_CheckCode(code, &treq, 2); /* defeat buggy AIX optimz */
547 } else if ((acmd == F_SETLK) || (acmd == F_SETLKW)
548 #if (defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)) && !defined(AFS_SUN58_ENV)
549 || (acmd == F_RSETLK) || (acmd == F_RSETLKW)) {
553 /* this next check is safer when left out, but more applications work
554 * with it in. However, they fail in race conditions. The question is
555 * what to do for people who don't have source to their application;
556 * this way at least, they can get work done */
557 #ifdef AFS_LINUX24_ENV
558 if (af->l_len == OFFSET_MAX)
559 af->l_len = 0; /* since some systems indicate it as EOF */
561 if (af->l_len == 0x7fffffff)
562 af->l_len = 0; /* since some systems indicate it as EOF */
563 #ifdef AFS_LINUX_64BIT_KERNEL
564 if (af->l_len == LONG_MAX)
565 af->l_len = 0; /* since some systems indicate it as EOF */
568 /* Java VMs ask for l_len=(long)-1 regardless of OS/CPU; bottom 32 bits
569 * sometimes get masked off by OS */
570 if ((sizeof(af->l_len) == 8) && (af->l_len == 0x7ffffffffffffffeLL))
572 /* next line makes byte range locks always succeed,
573 * even when they should block */
574 if (af->l_whence != 0 || af->l_start != 0 || af->l_len != 0) {
579 /* otherwise we can turn this into a whole-file flock */
580 if (af->l_type == F_RDLCK)
582 else if (af->l_type == F_WRLCK)
584 else if (af->l_type == F_UNLCK)
587 afs_PutFakeStat(&fakestate);
588 return EINVAL; /* unknown lock type */
590 if (((acmd == F_SETLK)
591 #if (defined(AFS_SGI_ENV) || defined(AFS_SUN5_ENV)) && !defined(AFS_SUN58_ENV)
592 || (acmd == F_RSETLK)
594 ) && code != LOCK_UN)
595 code |= LOCK_NB; /* non-blocking, s.v.p. */
596 #if defined(AFS_DARWIN_ENV)
597 code = HandleFlock(avc, code, &treq, clid, 0 /*!onlymine */ );
598 #elif defined(AFS_SGI_ENV)
599 AFS_RWLOCK((vnode_t *) avc, VRWLOCK_WRITE);
600 code = HandleFlock(avc, code, &treq, clid, 0 /*!onlymine */ );
601 AFS_RWUNLOCK((vnode_t *) avc, VRWLOCK_WRITE);
603 code = HandleFlock(avc, code, &treq, 0, 0 /*!onlymine */ );
605 code = afs_CheckCode(code, &treq, 3); /* defeat AIX -O bug */
610 afs_PutFakeStat(&fakestate);
617 * Get a description of the first lock which would
618 * block the lock specified. If the specified lock
619 * would succeed, fill in the lock structure with 'F_UNLCK'.
621 * To do that, we have to ask the server for the lock
623 * 1. The file is not locked by this machine.
624 * 2. Asking for write lock, and only the current
625 * PID has the file read locked.
628 HandleGetLock(register struct vcache *avc, register struct AFS_FLOCK *af,
629 register struct vrequest *areq, int clid)
631 register afs_int32 code;
632 struct AFS_FLOCK flock;
634 lockIdSet(&flock, NULL, clid);
636 ObtainWriteLock(&avc->lock, 122);
637 if (avc->flockCount == 0) {
639 * We don't know ourselves, so ask the server. Unfortunately, we
640 * don't know the pid. Not even the server knows the pid. Besides,
641 * the process with the lock is on another machine
643 code = GetFlockCount(avc, areq);
644 if (code == 0 || (af->l_type == F_RDLCK && code > 0)) {
645 af->l_type = F_UNLCK;
649 af->l_type = F_RDLCK;
651 af->l_type = F_WRLCK;
654 #if defined(AFS_HAVE_FLOCK_SYSID)
660 if (af->l_type == F_RDLCK) {
662 * We want a read lock. If there are only
663 * read locks, or we are the one with the
664 * write lock, say it is unlocked.
666 if (avc->flockCount > 0 || /* only read locks */
667 !lockIdcmp2(&flock, avc, NULL, 1, clid)) {
668 af->l_type = F_UNLCK;
672 /* one write lock, but who? */
673 af->l_type = F_WRLCK; /* not us, so lock would block */
674 if (avc->slocks) { /* we know who, so tell */
675 af->l_pid = avc->slocks->pid;
676 #if defined(AFS_HAVE_FLOCK_SYSID)
677 af->l_sysid = avc->slocks->sysid;
680 af->l_pid = 0; /* XXX can't happen?? */
681 #if defined(AFS_HAVE_FLOCK_SYSID)
689 * Ok, we want a write lock. If there is a write lock
690 * already, and it is not this process, we fail.
692 if (avc->flockCount < 0) {
693 if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
694 af->l_type = F_WRLCK;
696 af->l_pid = avc->slocks->pid;
697 #if defined(AFS_HAVE_FLOCK_SYSID)
698 af->l_sysid = avc->slocks->sysid;
701 af->l_pid = 0; /* XXX can't happen?? */
702 #if defined(AFS_HAVE_FLOCK_SYSID)
708 /* we are the one with the write lock */
709 af->l_type = F_UNLCK;
714 * Want a write lock, and we know there are read locks.
715 * If there is more than one, or it isn't us, we cannot lock.
717 if ((avc->flockCount > 1)
718 || lockIdcmp2(&flock, avc, NULL, 1, clid)) {
719 struct SimpleLocks *slp;
721 af->l_type = F_RDLCK;
723 #if defined(AFS_HAVE_FLOCK_SYSID)
726 /* find a pid that isn't our own */
727 for (slp = avc->slocks; slp; slp = slp->next) {
728 if (lockIdcmp2(&flock, NULL, slp, 1, clid)) {
729 af->l_pid = slp->pid;
730 #if defined(AFS_HAVE_FLOCK_SYSID)
731 af->l_sysid = avc->slocks->sysid;
740 * Ok, we want a write lock. If there is a write lock
741 * already, and it is not this process, we fail.
743 if (avc->flockCount < 0) {
744 if (lockIdcmp2(&flock, avc, NULL, 1, clid)) {
745 af->l_type = F_WRLCK;
747 af->l_pid = avc->slocks->pid;
748 #if defined(AFS_HAVE_FLOCK_SYSID)
749 af->l_sysid = avc->slocks->sysid;
752 af->l_pid = 0; /* XXX can't happen?? */
753 #if defined(AFS_HAVE_FLOCK_SYSID)
759 /* we are the one with the write lock */
760 af->l_type = F_UNLCK;
765 * Want a write lock, and we know there are read locks.
766 * If there is more than one, or it isn't us, we cannot lock.
768 if ((avc->flockCount > 1)
769 || lockIdcmp2(&flock, avc, NULL, 1, clid)) {
770 struct SimpleLocks *slp;
771 af->l_type = F_RDLCK;
773 #if defined(AFS_HAVE_FLOCK_SYSID)
776 /* find a pid that isn't our own */
777 for (slp = avc->slocks; slp; slp = slp->next) {
778 if (lockIdcmp2(&flock, NULL, slp, 1, clid)) {
779 af->l_pid = slp->pid;
780 #if defined(AFS_HAVE_FLOCK_SYSID)
781 af->l_sysid = avc->slocks->sysid;
790 * Want a write lock, and there is just one read lock, and it
791 * is this process with a read lock. Ask the server if there
792 * are any more processes with the file locked.
794 code = GetFlockCount(avc, areq);
795 if (code == 0 || code == 1) {
796 af->l_type = F_UNLCK;
800 af->l_type = F_RDLCK;
802 af->l_type = F_WRLCK;
804 #if defined(AFS_HAVE_FLOCK_SYSID)
811 af->l_len = 0; /* to end of file */
814 ReleaseWriteLock(&avc->lock);
818 /* Get the 'flock' count from the server. This comes back in a 'spare'
819 * field from a GetStatus RPC. If we have any problems with the RPC,
820 * we lie and say the file is unlocked. If we ask any 'old' fileservers,
821 * the spare field will be a zero, saying the file is unlocked. This is
822 * OK, as a further 'lock' request will do the right thing.
825 GetFlockCount(struct vcache *avc, struct vrequest *areq)
827 register struct afs_conn *tc;
828 register afs_int32 code;
829 struct AFSFetchStatus OutStatus;
830 struct AFSCallBack CallBack;
831 struct AFSVolSync tsync;
834 temp = areq->flags & O_NONBLOCK;
835 areq->flags |= O_NONBLOCK;
837 /* If we're disconnected, lie and say that we've got no locks. Ick */
838 if (AFS_IS_DISCONNECTED)
842 tc = afs_Conn(&avc->f.fid, areq, SHARED_LOCK);
844 XSTATS_START_TIME(AFS_STATS_FS_RPCIDX_FETCHSTATUS);
847 RXAFS_FetchStatus(tc->id, (struct AFSFid *)&avc->f.fid.Fid,
848 &OutStatus, &CallBack, &tsync);
854 (tc, code, &avc->f.fid, areq, AFS_STATS_FS_RPCIDX_FETCHSTATUS,
858 areq->flags &= ~O_NONBLOCK;
861 return (0); /* failed, say it is 'unlocked' */
863 return ((int)OutStatus.lockCount);