/*
+ * $Id: ad_lock.c,v 1.6 2002-11-14 17:15:22 srittau Exp $
+ *
* Copyright (c) 1998,1999 Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT for more information.
*
#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif /* HAVE_UNISTD_H */
+#ifdef HAVE_FCNTL_H
#include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
#include <errno.h>
#include <atalk/adouble.h>
#define LOCK_RSRC_WR (1)
#define LOCK_DATA_RD (2)
#define LOCK_DATA_WR (3)
+
+#define LOCK_RSRC_DRD (4)
+#define LOCK_RSRC_DWR (5)
+#define LOCK_DATA_DRD (6)
+#define LOCK_DATA_DWR (7)
+
+#define LOCK_RSRC_NONE (8)
+#define LOCK_DATA_NONE (9)
+
+/* --------------
+ translate a data fork lock to an offset
+*/
+
+static int df2off(int off)
+{
+int start = off;
+ if (off == AD_FILELOCK_OPEN_WR)
+ start = LOCK_DATA_WR;
+ else if (off == AD_FILELOCK_OPEN_RD)
+ start = LOCK_DATA_RD;
+ else if (off == AD_FILELOCK_DENY_RD)
+ start = LOCK_DATA_DRD;
+ else if (off == AD_FILELOCK_DENY_WR)
+ start = LOCK_DATA_DWR;
+ else if (off == AD_FILELOCK_OPEN_NONE)
+ start = LOCK_DATA_NONE;
+ return start;
+}
+
+/* --------------
+ translate a resource fork lock to an offset
+*/
+
+static int hf2off(int off)
+{
+int start = off;
+ if (off == AD_FILELOCK_OPEN_WR)
+ start = LOCK_RSRC_WR;
+ else if (off == AD_FILELOCK_OPEN_RD)
+ start = LOCK_RSRC_RD;
+ else if (off == AD_FILELOCK_DENY_RD)
+ start = LOCK_RSRC_DRD;
+ else if (off == AD_FILELOCK_DENY_WR)
+ start = LOCK_RSRC_DWR;
+ else if (off == AD_FILELOCK_OPEN_NONE)
+ start = LOCK_RSRC_NONE;
+ return start;
+}
+
int ad_fcntl_lock(struct adouble *ad, const u_int32_t eid, const int type,
const off_t off, const size_t len, const int user)
{
lock.l_start = off;
if (eid == ADEID_DFORK) {
- if ((type & ADLOCK_FILELOCK) && (ad_hfileno(ad) != -1)) {
- adf = &ad->ad_hf;
- if (off == AD_FILELOCK_WR)
- lock.l_start = LOCK_DATA_WR;
- else if (off == AD_FILELOCK_RD)
- lock.l_start = LOCK_DATA_RD;
- } else
- adf = &ad->ad_df;
-
+ adf = &ad->ad_df;
+ if ((type & ADLOCK_FILELOCK)) {
+ if (ad_hfileno(ad) != -1) {
+ lock.l_start = df2off(off);
+ adf = &ad->ad_hf;
+ }
+ }
} else { /* rfork */
adf = &ad->ad_hf;
- if (type & ADLOCK_FILELOCK) {
- if (off == AD_FILELOCK_WR)
- lock.l_start = LOCK_RSRC_WR;
- else if (off == AD_FILELOCK_RD)
- lock.l_start = LOCK_RSRC_RD;
- } else
+ if (type & ADLOCK_FILELOCK)
+ lock.l_start = hf2off(off);
+ else
lock.l_start += ad_getentryoff(ad, eid);
}
return -1;
}
+/* -------------------------
+ we are using lock as tristate variable
+
+ we have a lock ==> 1
+ no ==> 0
+ error ==> -1
+
+*/
+int ad_testlock(struct adouble *ad, int eid, const off_t off)
+{
+ struct flock lock;
+ struct ad_fd *adf;
+ adf_lock_t *plock;
+ int i;
+
+ lock.l_start = off;
+ if (eid == ADEID_DFORK) {
+ adf = &ad->ad_df;
+ if ((ad_hfileno(ad) != -1)) {
+ adf = &ad->ad_hf;
+ lock.l_start = df2off(off);
+ }
+ } else { /* rfork */
+ adf = &ad->ad_hf;
+ lock.l_start = hf2off(off);
+ }
+
+ plock = adf->adf_lock;
+ /* Does we have a lock? */
+ lock.l_whence = SEEK_SET;
+ lock.l_len = 1;
+ for (i = 0; i < adf->adf_lockcount; i++) {
+ if (OVERLAP(lock.l_start, 1, plock[i].lock.l_start, plock[i].lock.l_len))
+ return 1; /* */
+ }
+ /* Does another process have a lock?
+ FIXME F_GETLK ?
+ */
+ lock.l_type = (ad_getoflags(ad, eid) & O_RDWR) ?F_WRLCK : F_RDLCK;
+
+ if (fcntl(adf->adf_fd, F_SETLK, &lock) < 0) {
+ return (errno == EACCES || errno == EAGAIN)?1:-1;
+ }
+
+ lock.l_type = F_UNLCK;
+ return fcntl(adf->adf_fd, F_SETLK, &lock);
+}
+/* -------------------------
+*/
/* with temp locks, we don't need to distinguish within the same
* process as everything is single-threaded. in addition, if
* multi-threading gets added, it will only be in a few areas. */
adf = &ad->ad_df;
} else {
adf = &ad->ad_hf;
- lock.l_start += ad_getentryoff(ad, eid);
+ /* if ADLOCK_FILELOCK we want a lock from offset 0
+ * it's used when deleting a file:
+ * in open we put read locks on meta datas
+ * in delete a write locks on the whole file
+ * so if the file is open by somebody else it fails
+ */
+ if (!(type & ADLOCK_FILELOCK))
+ lock.l_start += ad_getentryoff(ad, eid);
}
lock.l_type = XLATE_FCNTL_LOCK(type & ADLOCK_MASK);
lock.l_whence = SEEK_SET;
adf_unlock(&ad->ad_hf, ad->ad_hf.adf_fd, user);
}
}
-
-/* byte-range locks. ad_lock is used by afp_bytelock and afp_openfork
- * to establish locks. both ad_lock and ad_tmplock take 0, 0, 0 to
- * signify locking of the entire file. in the absence of working
- * byte-range locks, this will default to file-wide flock-style locks.
- */
-int ad_flock_lock(struct adouble *ad, const u_int32_t eid, const int type,
- const off_t off, const size_t len, const int user)
-{
- int err, lock_type;
-
- lock_type = XLATE_FLOCK(type & ADLOCK_MASK);
- if (eid == ADEID_DFORK) {
- if ((err = flock(ad_dfileno(ad), lock_type | LOCK_NB)) == 0)
- ad->ad_df.adf_lockcount = lock_type;
- } else if ((err = flock(ad_hfileno(ad), lock_type | LOCK_NB)) == 0)
- ad->ad_hf.adf_lockcount = lock_type;
-
- if (err) {
- if ((EWOULDBLOCK != EAGAIN) && (errno == EWOULDBLOCK))
- errno = EAGAIN;
- return -1;
- }
-
- return 0;
-}
-
-/* ad_tmplock is used by afpd to lock actual read/write operations.
- * it saves the current lock state before attempting to lock to prevent
- * mixups. if byte-locks don't exist, it will lock the entire file with
- * an flock. we can be a little smart here by just upgrading/downgrading
- * locks. */
-int ad_flock_tmplock(struct adouble *ad, const u_int32_t eid, const int type,
- const off_t off, const size_t len)
-{
- int fd, oldlock, lock_type;
-
- if (eid == ADEID_DFORK) {
- oldlock = ad->ad_df.adf_lockcount;
- fd = ad_dfileno(ad);
- } else {
- oldlock = ad->ad_hf.adf_lockcount;
- fd = ad_hfileno(ad);
- }
-
- /* if we already have a write lock, we don't need to do anything */
- if (oldlock == LOCK_EX) {
- return 0;
- }
-
- /* if we have a read lock, upgrade it if necessary */
- lock_type = XLATE_FLOCK(type & ADLOCK_MASK);
- if (oldlock == LOCK_SH) {
- if (lock_type == LOCK_EX)
- return flock(fd, LOCK_EX | LOCK_NB);
- else if (lock_type == LOCK_UN) /* reset it */
- return flock(fd, LOCK_SH | LOCK_NB);
- else /* do nothing */
- return 0;
- }
-
- /* if we don't already have a lock, just do it. */
- return flock(fd, lock_type | LOCK_NB);
-}
-
-