* etc/afpd/unix.c: MFH: Fix afpd sharing NFSv3 mounts (thanks to
Pierre Beyssac <beyssac@enst.fr>)
+ * libatalk/cnid/*.[ch]: MFH: Sync CNID DB code with HEAD.
+
2001-12-03 joe c <marcus@marcuscom.com>
* etc/afpd/*.[ch], libatalk/cnid/*.[ch]: Big commit to clean up code
with astyle (readable code is hackable code). Also committed a fix
/*
- * $Id: cnid_add.c,v 1.14.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_add.c,v 1.14.2.3 2001-12-15 06:35:28 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
}
+ if (0) {
retry:
- t.tv_usec = rand() % 1000000;
+ t.tv_usec = rand() % 1000000;
#ifdef DEBUG
- syslog(LOG_INFO, "cnid_add: Hitting MAX_ABORTS, sleeping");
+ syslog(LOG_INFO, "cnid_add: Hitting MAX_ABORTS, sleeping");
#endif
- (void)select(0, NULL, NULL, NULL, &t);
+ (void)select(0, NULL, NULL, NULL, &t);
+ }
if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
syslog(LOG_ERR, "cnid_add: Failed to begin transaction: %s",
db_strerror(rc));
/*
- * $Id: cnid_close.c,v 1.12.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_close.c,v 1.12.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/* Flush the transaction log and delete the log file if we can. */
if ((db->lockfd > -1) && ((db->flags & CNIDFLAG_DB_RO) == 0)) {
struct flock lock;
- char wd[MAXPATHLEN + 1];
-
- /* Save the current working directory so we can restore it
- * when we're done. */
- getcwd(wd, MAXPATHLEN);
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0;
if (fcntl(db->lockfd, F_SETLK, &lock) == 0) {
char **list, **first;
+ int cfd = -1;
+
+ if ((cfd = open(db->close_file, O_RDWR | O_CREAT, 0666)) > -1) {
+
+ /* Checkpoint the databases until we can checkpoint no
+ * more. */
+ rc = txn_checkpoint(db->dbenv, 0, 0, 0);
+ while (rc == DB_INCOMPLETE) {
+ rc = txn_checkpoint(db->dbenv, 0, 0, 0);
+ }
- chdir(db->dbenv->db_log_dir ? db->dbenv->db_log_dir : db->dbenv->db_home);
#if DB_VERSION_MINOR > 2
- if ((rc = log_archive(db->dbenv, &list, 0)) != 0) {
+ if ((rc = log_archive(db->dbenv, &list, DB_ARCH_LOG | DB_ARCH_ABS)) != 0) {
#else /* DB_VERSION_MINOR < 2 */
- if ((rc = log_archive(db->dbenv, &list, 0, NULL)) != 0) {
+ if ((rc = log_archive(db->dbenv, &list, DB_ARCH_LOG | DB_ARCH_ABS, NULL)) != 0) {
#endif /* DB_VERSION_MINOR */
- syslog(LOG_ERR, "cnid_close: Unable to archive logfiles: %s",
- db_strerror(rc));
- }
+ syslog(LOG_ERR, "cnid_close: Unable to archive logfiles: %s",
+ db_strerror(rc));
+ }
- if (list != NULL) {
- for (first = list; *list != NULL; ++list) {
- if ((rc = remove(*list)) != 0) {
- syslog(LOG_INFO, "cnid_close: failed to remove %s: %s",
- *list, strerror(rc));
+ if (list != NULL) {
+ for (first = list; *list != NULL; ++list) {
+ if ((rc = remove(*list)) != 0) {
+#ifdef DEBUG
+ syslog(LOG_INFO, "cnid_close: failed to remove %s: %s",
+ *list, strerror(rc));
+#endif
+ }
}
+ free(first);
}
- free(first);
+ (void)remove(db->close_file);
+ close(cfd);
+ }
+ else {
+ syslog(LOG_ERR, "cnid_close: Failed to open database closing lock file: %s", strerror(errno));
}
- chdir(wd);
}
}
/*
- * $Id: cnid_delete.c,v 1.9.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_delete.c,v 1.9.2.3 2001-12-15 06:35:28 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
CNID_private *db;
DBT key, data;
DB_TXN *tid;
- int rc;
+ int rc, found = 0;
if (!(db = CNID) || !id || (db->flags & CNIDFLAG_DB_RO)) {
return -1;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
+ /* Get from ain CNID database. */
+ key.data = (cnid_t *)&id;
+ key.size = sizeof(id);
+ while (!found) {
+ rc = db->db_cnid->get(db->db_cnid, NULL, &key, &data, 0);
+ switch (rc) {
+ case 0:
+ found = 1;
+ break;
+ case DB_LOCK_DEADLOCK:
+ break;
+ case DB_NOTFOUND:
+#ifdef DEBUG
+ syslog(LOG_INFO, "cnid_delete: CNID %u not in database",
+ ntohl(id));
+#endif
+ return 0;
+ default:
+ syslog(LOG_ERR, "cnid_delete: Unable to delete entry: %s",
+ db_strerror(rc));
+ return rc;
+ }
+ }
+
retry:
if ((rc = txn_begin(db->dbenv, NULL, &tid, 0)) != 0) {
syslog(LOG_ERR, "cnid_delete: Failed to begin transaction: %s",
return rc;
}
- /* Get from ain CNID database. */
+ /* Now delete from the main CNID database. */
key.data = (cnid_t *)&id;
key.size = sizeof(id);
- if ((rc = db->db_cnid->get(db->db_cnid, tid, &key, &data, 0))) {
+ if ((rc = db->db_cnid->del(db->db_cnid, tid, &key, 0))) {
int ret;
if ((ret = txn_abort(tid)) != 0) {
syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
switch (rc) {
case DB_LOCK_DEADLOCK:
goto retry;
- case DB_NOTFOUND:
- syslog(LOG_INFO, "cnid_delete: CNID %u not in database",
- ntohl(id));
- return 0;
default:
- syslog(LOG_ERR, "cnid_delete: Unable to delete entry: %s",
- db_strerror(rc));
- return rc;
+ goto abort_err;
}
}
/* Get data from the did/name database.
* TODO Also handle did/macname, did/shortname, and did/longname. */
-
key.data = (char *)data.data + CNID_DEVINO_LEN;
key.size = data.size - CNID_DEVINO_LEN;
if ((rc = db->db_didname->del(db->db_didname, tid, &key, 0))) {
}
}
- /* Now delete from the main CNID database. */
- key.data = (cnid_t *)&id;
- key.size = sizeof(id);
- if ((rc = db->db_cnid->del(db->db_cnid, tid, &key, 0))) {
- int ret;
- if ((ret = txn_abort(tid)) != 0) {
- syslog(LOG_ERR, "cnid_delete: txn_abort: %s", db_strerror(ret));
- return ret;
- }
- switch (rc) {
- case DB_LOCK_DEADLOCK:
- goto retry;
- default:
- goto abort_err;
- }
- }
-
#ifdef DEBUG
syslog(LOG_INFO, "cnid_delete: Deleting CNID %u", ntohl(id));
#endif
/*
- * $Id: cnid_get.c,v 1.9.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_get.c,v 1.9.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/*
- * $Id: cnid_lookup.c,v 1.9.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_lookup.c,v 1.9.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/*
- * $Id: cnid_nextid.c,v 1.6.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_nextid.c,v 1.6.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef unused
/*
- * $Id: cnid_open.c,v 1.19.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_open.c,v 1.19.2.3 2001-12-15 06:35:28 jmarcus Exp $
*
* Copyright (c) 1999. Adrian Sun (asun@zoology.washington.edu)
* All Rights Reserved. See COPYRIGHT.
#include <sys/param.h>
#include <sys/stat.h>
#include <syslog.h>
+#ifdef HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif /* HAVE_SYS_TIME_H */
#include <db.h>
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif /* ! MIN */
-#define DBHOME ".AppleDB"
-#define DBCNID "cnid.db"
-#define DBDEVINO "devino.db"
-#define DBDIDNAME "didname.db" /* did/full name mapping */
-#define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
-#define DBMACNAME "macname.db" /* did/31 mapping */
-#define DBLONGNAME "longname.db" /* did/unicode mapping */
-#define DBLOCKFILE "cnid.lock"
+#define DBHOME ".AppleDB"
+#define DBCNID "cnid.db"
+#define DBDEVINO "devino.db"
+#define DBDIDNAME "didname.db" /* did/full name mapping */
+#define DBSHORTNAME "shortname.db" /* did/8+3 mapping */
+#define DBMACNAME "macname.db" /* did/31 mapping */
+#define DBLONGNAME "longname.db" /* did/unicode mapping */
+#define DBLOCKFILE "cnid.lock"
+#define DBRECOVERFILE "cnid.dbrecover"
+#define DBCLOSEFILE "cnid.close"
#define DBHOMELEN 8
#define DBLEN 10
#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
DB_INIT_LOG | DB_INIT_TXN)
#else /* DB_VERSION_MINOR < 1 */
-/*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
-DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)*/
#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
-DB_INIT_LOG | DB_INIT_TXN)
+DB_INIT_LOG | DB_INIT_TXN | DB_TXN_NOSYNC)
+/*#define DBOPTIONS (DB_CREATE | DB_INIT_MPOOL | DB_INIT_LOCK | \
+DB_INIT_LOG | DB_INIT_TXN)*/
#endif /* DB_VERSION_MINOR */
+/* Let's try and use the random deadlock decider if available. This adds
+ * a bit of entropy to the mix that might be beneficial. If random isn't
+ * available, we'll decide deadlocks by kicking off the youngest process.
+ * If we can't do that, then let DB3 use its default deadlock detector. */
+#ifdef DB_LOCK_RANDOM
+#define DEAD_LOCK_DETECT DB_LOCK_RANDOM
+#elif defined DB_LOCK_YOUNGEST
+#define DEAD_LOCK_DETECT DB_LOCK_YOUNGEST
+#else /* DB_LOCK_RANDOM */
+#define DEAD_LOCK_DETECT DB_LOCK_DEFAULT
+#endif /* DB_LOCK_RANDOM */
+
#define MAXITER 0xFFFF /* maximum number of simultaneously open CNID
* databases. */
static int have_lock = 0;
void *cnid_open(const char *dir) {
- struct stat st;
+ struct stat st, rsb, csb;
struct flock lock;
char path[MAXPATHLEN + 1];
+ char recover_file[MAXPATHLEN + 1];
CNID_private *db;
DBT key, data;
DB_TXN *tid;
u_int32_t DBEXTRAS = 0;
int open_flag, len;
- int rc;
+ int rc, rfd = -1;
if (!dir) {
return NULL;
goto fail_adouble;
}
+ /* Make sure cnid.lock goes in .AppleDB. */
+ strcat(path, "/");
+ len++;
+
+ strcpy(db->close_file, path);
+ strcat(db->close_file, DBCLOSEFILE);
+
+ /* Check to make sure that a client isn't in the process of closing
+ * the database environment. To do this, select on the close file. */
+ while(stat(db->close_file, &csb) == 0) {
+ struct timeval ct;
+ ct.tv_sec = 1;
+ ct.tv_usec = 0;
+ (void)select(0, NULL, NULL, NULL, &ct);
+ }
+
+ strcpy(recover_file, path);
+ strcat(recover_file, DBRECOVERFILE);
+
/* Search for a byte lock. This allows us to cleanup the log files
* at cnid_close() in a clean fashion.
*
lock.l_len = 1;
while (fcntl(db->lockfd, F_SETLK, &lock) < 0) {
if (++lock.l_start > MAXITER) {
- syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
+ syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup for database environment %s lock (lock failed)", path);
close(db->lockfd);
db->lockfd = -1;
break;
}
}
else {
- syslog(LOG_INFO, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
+ syslog(LOG_ERR, "cnid_open: Cannot establish logfile cleanup lock for database environment %s (open() failed)", path);
}
+ /* Create a file to represent database recovery. While this file
+ * exists, the database is being recovered, and all other clients will
+ * select until recovery is complete, and this file goes away. */
if (!have_lock && db->lockfd > -1 && lock.l_start == 0) {
- /* We test to see if we have exclusive database access. If we do, we
- * will open the database with the DB_RECOVER flag.
- */
-#ifdef DEBUG
- syslog(LOG_INFO, "cnid_open: Opening database environment %s with DB_RECOVER flag", path);
-#endif
- DBEXTRAS |= DB_RECOVER;
- have_lock = 1;
+ if (stat(recover_file, &rsb) == 0) {
+ (void)remove(recover_file);
+ }
+ if ((rfd = open(recover_file, O_RDWR | O_CREAT, 0666)) > -1) {
+ DBEXTRAS |= DB_RECOVER;
+ have_lock = 1;
+ }
+ }
+ else if (!have_lock) {
+ while (stat(recover_file, &rsb) == 0) {
+ struct timeval rt;
+ rt.tv_sec = 1;
+ rt.tv_usec = 0;
+ (void)select(0, NULL, NULL, NULL, &rt);
+ }
}
-
path[len + DBHOMELEN] = '\0';
open_flag = DB_CREATE;
}
/* Setup internal deadlock detection. */
- if ((rc = db->dbenv->set_lk_detect(db->dbenv, DB_LOCK_DEFAULT)) != 0) {
+ if ((rc = db->dbenv->set_lk_detect(db->dbenv, DEAD_LOCK_DETECT)) != 0) {
syslog(LOG_ERR, "cnid_open: set_lk_detect: %s", db_strerror(rc));
goto fail_lock;
}
#if DB_VERSION_MINOR > 1
/* Take care of setting the DB_TXN_NOSYNC flag in db3 > 3.1.x. */
- /* if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
- syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
- goto fail_lock;
- }*/
+ if ((rc = db->dbenv->set_flags(db->dbenv, DB_TXN_NOSYNC, 1)) != 0) {
+ syslog(LOG_ERR, "cnid_open: set_flags: %s", db_strerror(rc));
+ goto fail_lock;
+ }
#endif /* DB_VERSION_MINOR > 1 */
/* Open the database environment. */
syslog(LOG_INFO, "cnid_open: Obtained read-only database environment %s", path);
}
+ /* If we have the recovery lock, close the file, remove it, so other
+ * clients can proceed opening the DB environment. */
+ if (rfd > -1) {
+ (void)remove(recover_file);
+ switch(errno) {
+ case 0:
+ case ENOENT:
+ break;
+ default:
+ syslog(LOG_ERR, "cnid_open: Unable to remove %s: %s",
+ recover_file, strerror(errno));
+ }
+ close(rfd);
+ rfd = -1;
+ }
+
/* did/name reverse mapping. We use a BTree for this one. */
if ((rc = db_create(&db->db_didname, db->dbenv, 0)) != 0) {
syslog(LOG_ERR, "cnid_open: Failed to create did/name database: %s",
if (db->lockfd > -1) {
close(db->lockfd);
}
+ if (rfd > -1) {
+ (void)remove(recover_file);
+ close(rfd);
+ }
fail_adouble:
/*
- * $Id: cnid_private.h,v 1.3.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_private.h,v 1.3.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifndef LIBATALK_CNID_PRIVATE_H
#endif /* EXTENDED_DB */
DB_ENV* dbenv;
int lockfd, flags;
+ char close_file[MAXPATHLEN + 1];
} CNID_private;
/* on-disk data format (in network byte order where appropriate) --
/*
- * $Id: cnid_resolve.c,v 1.8.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_resolve.c,v 1.8.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
/*
- * $Id: cnid_update.c,v 1.12.2.2 2001-12-03 15:53:39 jmarcus Exp $
+ * $Id: cnid_update.c,v 1.12.2.3 2001-12-15 06:35:28 jmarcus Exp $
*/
#ifdef HAVE_CONFIG_H
switch (rc) {
case DB_LOCK_DEADLOCK:
goto retry;
+ case DB_NOTFOUND:
+ /* Silently fail here. We're allowed to do this since this CNID
+ * might have been deleted out from under us, or someone has
+ * called cnid_lookup then cnid_update (which is redundant). */
+ return 0;
default:
goto update_err;
}