]> arthur.barton.de Git - netatalk.git/commitdiff
Merge 2-1
authorFrank Lahm <franklahm@googlemail.com>
Thu, 21 Apr 2011 12:00:23 +0000 (14:00 +0200)
committerFrank Lahm <franklahm@googlemail.com>
Thu, 21 Apr 2011 12:00:23 +0000 (14:00 +0200)
1  2 
etc/cnid_dbd/cmd_dbd.c
etc/cnid_dbd/cmd_dbd.h
etc/cnid_dbd/cmd_dbd_scanvol.c
etc/cnid_dbd/dbif.c
etc/cnid_dbd/dbif.h
etc/cnid_dbd/main.c

diff --combined etc/cnid_dbd/cmd_dbd.c
index 2359defde738e2bb9770483c840f27cfe841b1fd,7fd2ce02d4e3ab0484606d83ac83f4cc42a72f84..5493a28059461cb525f49f99ecaa0cbf16dbb15d
  #include <atalk/logger.h>
  #include <atalk/cnid_dbd_private.h>
  #include <atalk/volinfo.h>
+ #include <atalk/util.h>
  #include "cmd_dbd.h"
  #include "dbd.h"
  #include "dbif.h"
  #include "db_param.h"
  
- #define LOCKFILENAME  "lock"
  #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
  
  int nocniddb = 0;               /* Dont open CNID database, only scan filesystem */
 +struct volinfo volinfo; /* needed by pack.c:idxname() */
  volatile sig_atomic_t alarmed;  /* flags for signals */
  int db_locked;                  /* have we got the fcntl lock on lockfile ? */
  
@@@ -92,8 -92,6 +93,8 @@@ static struct db_param db_param = 
      NULL,                       /* Volume dirpath */
      1,                          /* bdb logfile autoremove */
      64 * 1024,                  /* bdb cachesize (64 MB) */
 +    DEFAULT_MAXLOCKS,           /* maxlocks */
 +    DEFAULT_MAXLOCKOBJS,        /* maxlockobjs */
      0,                          /* flush_interval */
      0,                          /* flush_frequency */
      0,                          /* usock_file */
      -1,                         /* idle_timeout */
      -1                          /* max_vols */
  };
 -static char dbpath[PATH_MAX];   /* Path to the dbd database */
 +static char dbpath[MAXPATHLEN+1];   /* Path to the dbd database */
  
  /* 
     Provide some logging
@@@ -167,84 -165,6 +168,6 @@@ static void set_signal(void
      }        
  }
  
- /*!
-  * Get lock on db lock file
-  *
-  * @args cmd       (r) !=0: lock, 0: unlock
-  * @args dbpath    (r) path to lockfile, only used on first call,
-  *                     later the stored fd is used
-  * @returns            1 if lock was acquired, 0 if file is already locked, -1 on error
-  */
- int get_lock(int cmd, const char *dbpath)
- {
-     static int lockfd = -1;
-     char lockpath[PATH_MAX];
-     struct flock lock;
-     struct stat st;
-     if (cmd == 0) {
-         if (lockfd == -1)
-             return -1;
-         lock.l_start  = 0;
-         lock.l_whence = SEEK_SET;
-         lock.l_len    = 0;
-         lock.l_type = F_UNLCK;
-         fcntl(lockfd, F_SETLK, &lock);
-         close(lockfd);
-         lockfd = -1;
-         return 0;
-     }
-     if (lockfd == -1) {
-         if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
-             dbd_log( LOGSTD, ".AppleDB pathname too long");
-             return -1;
-         }
-         strncpy(lockpath, dbpath, PATH_MAX - 1);
-         strcat(lockpath, "/");
-         strcat(lockpath, LOCKFILENAME);
-         if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
-             dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
-             return -1;
-         }
-         if ((stat(dbpath, &st)) != 0) {
-             dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno));
-             return -1;
-         }
-         if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
-             dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s", strerror(errno));
-             return -1;
-         }
-     }
-     
-     lock.l_start  = 0;
-     lock.l_whence = SEEK_SET;
-     lock.l_len    = 0;
-     lock.l_type   = F_WRLCK;
-     if (fcntl(lockfd, F_SETLK, &lock) < 0) {
-         if (errno == EACCES || errno == EAGAIN) {
-             if (exclusive) {
-                 dbd_log(LOGSTD, "Database is in use and exlusive was requested");
-                 return -1;
-             };
-             dbd_log(LOGDEBUG, "get_lock: couldn't lock");
-             return 0;
-         } else {
-             dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
-             return -1;
-        }
-     }
-     dbd_log(LOGDEBUG, "get_lock: got lock");    
-     return 1;
- }
  static void usage (void)
  {
      printf("Usage: dbd [-e|-t|-v|-x] -d [-i] | -s [-c|-n]| -r [-c|-f] | -u <path to netatalk volume>\n"
@@@ -298,6 -218,7 +221,6 @@@ int main(int argc, char **argv
      int dump=0, scan=0, rebuild=0, prep_upgrade=0, rebuildindexes=0, dumpindexes=0, force=0;
      dbd_flags_t flags = 0;
      char *volpath;
 -    struct volinfo volinfo;
      int cdir;
  
      if (geteuid() != 0) {
          exit(EXIT_FAILURE);        
      }
  
 +    /* Enuser dbpath is there, create if necessary */
 +    struct stat st;
 +    if (stat(volinfo.v_dbpath, &st) != 0) {
 +        if (errno != ENOENT) {
 +            dbd_log( LOGSTD, "Can't stat dbpath \"%s\": %s", volinfo.v_dbpath, strerror(errno));
 +            exit(EXIT_FAILURE);        
 +        }
 +        if ((mkdir(volinfo.v_dbpath, 0755)) != 0) {
 +            dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
 +            exit(EXIT_FAILURE);
 +        }        
 +    }
 +
      /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
 -    if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
 +    if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > MAXPATHLEN ) {
          dbd_log( LOGSTD, "Volume pathname too long");
          exit(EXIT_FAILURE);        
      }
 -    strncpy(dbpath, volinfo.v_dbpath, PATH_MAX - 9 - 1);
 +    strncpy(dbpath, volinfo.v_dbpath, MAXPATHLEN - strlen("/.AppleDB"));
      strcat(dbpath, "/.AppleDB");
  
      /* Check or create dbpath */
          close(dbdirfd);
      }
  
-     /* Get db lock, which exits if exclusive was requested and it already is locked */
-     if ((db_locked = get_lock(1, dbpath)) == -1)
+     /* Get db lock */
+     if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
          goto exit_failure;
+     if (db_locked != LOCK_EXCL) {
+         /* Couldn't get exclusive lock, try shared lock if -e wasn't requested */
+         if (exclusive) {
+             dbd_log(LOGSTD, "Database is in use and exlusive was requested");
+             goto exit_failure;
+         }
+         if ((db_locked = get_lock(LOCK_SHRD, NULL)) != LOCK_SHRD)
+             goto exit_failure;
+     }
  
      /* Prepare upgrade ? */
      if (prep_upgrade) {
 -        if (dbif_prep_upgrade(dbpath))
 +        if (dbif_env_remove(dbpath))
              goto exit_failure;
          goto exit_success;
      }        
      /* Check if -f is requested and wipe db if yes */
      if ((flags & DBD_FLAGS_FORCE) && rebuild && (volinfo.v_flags & AFPVOL_CACHE)) {
          char cmd[8 + MAXPATHLEN];
-         if ((db_locked = get_lock(0, NULL)) != 0)
+         if ((db_locked = get_lock(LOCK_FREE, NULL)) != 0)
              goto exit_failure;
 -        snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", dbpath);
 +
 +        snprintf(cmd, 8 + MAXPATHLEN, "rm -rf \"%s\"", dbpath);
          dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
          system(cmd);
 +        if ((mkdir(dbpath, 0755)) != 0) {
 +            dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
 +            exit(EXIT_FAILURE);
 +        }
          dbd_log( LOGDEBUG, "Removed old database.");
-         if ((db_locked = get_lock(1, dbpath)) == -1)
+         if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
              goto exit_failure;
      }
  
          
          if (dbif_env_open(dbd,
                            &db_param,
-                           exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
+                           (db_locked == LOCK_EXCL) ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
              dbd_log( LOGSTD, "error opening database!");
              goto exit_failure;
          }
  
-         if (exclusive)
+         if (db_locked == LOCK_EXCL)
              dbd_log( LOGDEBUG, "Finished recovery.");
  
          if (dbif_open(dbd, NULL, rebuildindexes) < 0) {
              dbif_close(dbd);
              goto exit_failure;
          }
 -
 -        if (dbd_stamp(dbd) < 0) {
 -            dbif_close(dbd);
 -            goto exit_failure;
 -        }
      }
  
+     /* Downgrade db lock if not running exclusive */
+     if (!exclusive && (db_locked == LOCK_EXCL)) {
+         if (get_lock(LOCK_UNLOCK, NULL) != 0)
+             goto exit_failure;
+         if (get_lock(LOCK_SHRD, NULL) != LOCK_SHRD)
+             goto exit_failure;
+     }
      /* Now execute given command scan|rebuild|dump */
      if (dump && ! nocniddb) {
          if (dbif_dump(dbd, dumpindexes) < 0) {
diff --combined etc/cnid_dbd/cmd_dbd.h
index 7caf634f5f1ac093a0f818b6bb2063923f417cb0,b2ee649f6d7fa8c1c8d0d487b52d4de4f7537c78..ef7829881b28a851cc335998b6db8dfacdac7da6
@@@ -27,10 -27,11 +27,9 @@@ typedef unsigned int dbd_flags_t
  extern int nocniddb; /* Dont open CNID database, only scan filesystem */
  extern int db_locked; /* have we got the fcntl lock on lockfd ? */
  extern volatile sig_atomic_t alarmed;
 -extern struct volinfo *volinfo;
 -extern char cwdbuf[MAXPATHLEN+1];
  
  extern void dbd_log(enum logtype lt, char *fmt, ...);
  extern int cmd_dbd_scanvol(DBD *dbd, struct volinfo *volinfo, dbd_flags_t flags);
- extern int get_lock(int cmd, const char *dbpath);
  
  /*
    Functions for querying the database which couldn't be reused from the existing
index a91f43716dd196c8a11ba56c8678c501dd8f2c97,abe9688a48693ba130bcab586af464ace0a96750..daa13e413e441622191606f31ae6085f8ce0ed19
  #define ADDIR_OK (addir_ok == 0)
  #define ADFILE_OK (adfile_ok == 0)
  
 -/* These must be accessible for cmd_dbd_* funcs */
 -struct volinfo        *volinfo;
 -char                  cwdbuf[MAXPATHLEN+1];
  
 -/* Some static vars */
 +static struct volinfo *myvolinfo;
 +static char           cwdbuf[MAXPATHLEN+1];
  static DBD            *dbd;
  static DBD            *dbd_rebuild;
  static dbd_flags_t    dbd_flags;
@@@ -83,22 -85,22 +83,22 @@@ static char *utompath(char *upath
      u = upath;
      outlen = strlen(upath);
  
 -    if ((volinfo->v_casefold & AFPVOL_UTOMUPPER))
 +    if ((myvolinfo->v_casefold & AFPVOL_UTOMUPPER))
          flags |= CONV_TOUPPER;
 -    else if ((volinfo->v_casefold & AFPVOL_UTOMLOWER))
 +    else if ((myvolinfo->v_casefold & AFPVOL_UTOMLOWER))
          flags |= CONV_TOLOWER;
  
 -    if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
 +    if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
          flags |= CONV__EILSEQ;
      }
  
      /* convert charsets */
 -    if ((size_t)-1 == ( outlen = convert_charset(volinfo->v_volcharset,
 +    if ((size_t)-1 == ( outlen = convert_charset(myvolinfo->v_volcharset,
                                                   CH_UTF8_MAC,
 -                                                 volinfo->v_maccharset,
 +                                                 myvolinfo->v_maccharset,
                                                   u, outlen, mpath, MAXPATHLEN, &flags)) ) {
          dbd_log( LOGSTD, "Conversion from %s to %s for %s failed.",
 -                 volinfo->v_volcodepage, volinfo->v_maccodepage, u);
 +                 myvolinfo->v_volcodepage, myvolinfo->v_maccodepage, u);
          return NULL;
      }
  
@@@ -124,17 -126,17 +124,17 @@@ static char *mtoupath(char *mpath
      }
  
      /* set conversion flags */
 -    if (!(volinfo->v_flags & AFPVOL_NOHEX))
 +    if (!(myvolinfo->v_flags & AFPVOL_NOHEX))
          flags |= CONV_ESCAPEHEX;
 -    if (!(volinfo->v_flags & AFPVOL_USEDOTS))
 +    if (!(myvolinfo->v_flags & AFPVOL_USEDOTS))
          flags |= CONV_ESCAPEDOTS;
  
 -    if ((volinfo->v_casefold & AFPVOL_MTOUUPPER))
 +    if ((myvolinfo->v_casefold & AFPVOL_MTOUUPPER))
          flags |= CONV_TOUPPER;
 -    else if ((volinfo->v_casefold & AFPVOL_MTOULOWER))
 +    else if ((myvolinfo->v_casefold & AFPVOL_MTOULOWER))
          flags |= CONV_TOLOWER;
  
 -    if ((volinfo->v_flags & AFPVOL_EILSEQ)) {
 +    if ((myvolinfo->v_flags & AFPVOL_EILSEQ)) {
          flags |= CONV__EILSEQ;
      }
  
      outlen = MAXPATHLEN;
  
      if ((size_t)-1 == (outlen = convert_charset(CH_UTF8_MAC,
 -                                                volinfo->v_volcharset,
 -                                                volinfo->v_maccharset,
 +                                                myvolinfo->v_volcharset,
 +                                                myvolinfo->v_maccharset,
                                                  m, inplen, u, outlen, &flags)) ) {
          dbd_log( LOGSTD, "conversion from UTF8-MAC to %s for %s failed.",
 -                 volinfo->v_volcodepage, mpath);
 +                 myvolinfo->v_volcodepage, mpath);
          return NULL;
      }
  
@@@ -222,8 -224,8 +222,8 @@@ static int check_symlink(const char *na
        and can compare it with the currents volume path
      */
      int i = 0;
 -    while (volinfo->v_path[i]) {
 -        if ((pathbuf[i] == 0) || (volinfo->v_path[i] != pathbuf[i])) {
 +    while (myvolinfo->v_path[i]) {
 +        if ((pathbuf[i] == 0) || (myvolinfo->v_path[i] != pathbuf[i])) {
              dbd_log( LOGDEBUG, "extra-share symlink '%s/%s', following", cwdbuf, name);
              return 1;
          }
@@@ -304,7 -306,7 +304,7 @@@ static int check_adfile(const char *fna
      else
          adflags = ADFLAGS_DIR;
  
 -    adname = volinfo->ad_path(fname, adflags);
 +    adname = myvolinfo->ad_path(fname, adflags);
  
      if ((ret = access( adname, F_OK)) != 0) {
          if (errno != ENOENT) {
              return -1;
  
          /* Create ad file */
 -        ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
  
          if ((ret = ad_open_metadata( fname, adflags, O_CREAT, &ad)) != 0) {
              dbd_log( LOGSTD, "Error creating AppleDouble file '%s/%s': %s",
          chmod(adname, st->st_mode);
  #endif
      } else {
 -        ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
          if (ad_open_metadata( fname, adflags, O_RDONLY, &ad) != 0) {
              dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s'", cwdbuf, fname);
              return -1;
@@@ -470,10 -472,10 +470,10 @@@ static int check_addir(int volroot
      }
  
      /* Check for ".Parent" */
 -    if ( (adpar_ok = access(volinfo->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
 +    if ( (adpar_ok = access(myvolinfo->ad_path(".", ADFLAGS_DIR), F_OK)) != 0) {
          if (errno != ENOENT) {
              dbd_log(LOGSTD, "Access error on '%s/%s': %s",
 -                    cwdbuf, volinfo->ad_path(".", ADFLAGS_DIR), strerror(errno));
 +                    cwdbuf, myvolinfo->ad_path(".", ADFLAGS_DIR), strerror(errno));
              return -1;
          }
          dbd_log(LOGSTD, "Missing .AppleDouble/.Parent for '%s'", cwdbuf);
          }
  
          /* Create ad dir and set name */
 -        ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
  
          if (ad_open_metadata( ".", ADFLAGS_DIR, O_CREAT, &ad) != 0) {
              dbd_log( LOGSTD, "Error creating AppleDouble dir in %s: %s", cwdbuf, strerror(errno));
              return -1;
          }
          chown(ADv2_DIRNAME, st.st_uid, st.st_gid);
 -        chown(volinfo->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
 +        chown(myvolinfo->ad_path(".", ADFLAGS_DIR), st.st_uid, st.st_gid);
      }
  
      return 0;
@@@ -532,7 -534,7 +532,7 @@@ static int check_eafile_in_adouble(cons
      char *namep, *namedup = NULL;
  
      /* Check if this is an AFPVOL_EA_AD vol */
 -    if (volinfo->v_vfs_ea == AFPVOL_EA_AD) {
 +    if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD) {
          /* Does the filename contain "::EA" ? */
          namedup = strdup(name);
          if ((namep = strstr(namedup, "::EA")) == NULL) {
@@@ -666,8 -668,6 +666,8 @@@ static int read_addir(void
  /*
    Check CNID for a file/dir, both from db and from ad-file.
    For detailed specs see intro.
 +
 +  @return Correct CNID of object or CNID_INVALID (ie 0) on error
  */
  static cnid_t check_cnid(const char *name, cnid_t did, struct stat *st, int adfile_ok, int adflags)
  {
          cnidcount = 0;
          if (dbif_txn_checkpoint(dbd, 0, 0, 0) < 0) {
              dbd_log(LOGSTD, "Error checkpointing!");
 -            return 0;
 +            return CNID_INVALID;
          }
      }
  
      /* Get CNID from ad-file if volume is using AFPVOL_CACHE */
      ad_cnid = 0;
 -    if ( (volinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
 -        ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +    if ( (myvolinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
 +        ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
          if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
              
              if (dbd_flags & DBD_FLAGS_CLEANUP)
 -                return 0;
 +                return CNID_INVALID;
  
              dbd_log( LOGSTD, "Error opening AppleDouble file for '%s/%s': %s", cwdbuf, name, strerror(errno));
 -            return 0;
 +            return CNID_INVALID;
          }
  
          if (dbd_flags & DBD_FLAGS_FORCE) {
      memset(&rply, 0, sizeof(struct cnid_dbd_rply));
      rqst.did = did;
      rqst.cnid = ad_cnid;
 -    if ( ! (volinfo->v_flags & AFPVOL_NODEV))
 +    if ( ! (myvolinfo->v_flags & AFPVOL_NODEV))
          rqst.dev = st->st_dev;
      rqst.ino = st->st_ino;
      rqst.type = S_ISDIR(st->st_mode)?1:0;
  
      /* Query the database */
      ret = dbd_lookup(dbd, &rqst, &rply, (dbd_flags & DBD_FLAGS_SCAN) ? 1 : 0);
 -    dbif_txn_close(dbd, ret);
 +    if (dbif_txn_close(dbd, ret) != 0)
 +        return CNID_INVALID;
      if (rply.result == CNID_DBD_RES_OK) {
          db_cnid = rply.cnid;
      } else if (rply.result == CNID_DBD_RES_NOTFOUND) {
          if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
              rqst.cnid = db_cnid;
              ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
 -            dbif_txn_close(dbd, ret);
 +            if (dbif_txn_close(dbd, ret) != 0)
 +                return CNID_INVALID;
  
              rqst.cnid = ad_cnid;
              ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
 -            dbif_txn_close(dbd, ret);
 +            if (dbif_txn_close(dbd, ret) != 0)
 +                return CNID_INVALID;
  
              ret = dbd_rebuild_add(dbd, &rqst, &rply);
 -            dbif_txn_close(dbd, ret);
 +            if (dbif_txn_close(dbd, ret) != 0)
 +                return CNID_INVALID;
          }
          return ad_cnid;
      } else if (ad_cnid && (db_cnid == 0)) {
              if (ret == CNID_DBD_RES_OK) {
                  /* Occupied! Choose another, update ad-file */
                  ret = dbd_add(dbd, &rqst, &rply, 1);
 -                dbif_txn_close(dbd, ret);
 +                if (dbif_txn_close(dbd, ret) != 0)
 +                    return CNID_INVALID;
                  db_cnid = rply.cnid;
                  dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
  
 -                if ((volinfo->v_flags & AFPVOL_CACHE)
 +                if ((myvolinfo->v_flags & AFPVOL_CACHE)
                      && ADFILE_OK
                      && ( ! (dbd_flags & DBD_FLAGS_SCAN))) {
                      dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
                              cwdbuf, name, ntohl(db_cnid));
 -                    ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +                    ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
                      if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
                          dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
                                  cwdbuf, name, strerror(errno));
 -                        return 0;
 +                        return CNID_INVALID;
                      }
                      ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
                      ad_flush(&ad);
                      cwdbuf, name, ntohl(ad_cnid));
              rqst.cnid = ad_cnid;
              ret = dbd_rebuild_add(dbd, &rqst, &rply);
 -            dbif_txn_close(dbd, ret);
 +            if (dbif_txn_close(dbd, ret) != 0)
 +                return CNID_INVALID;
          }
          return ad_cnid;
      } else if ((db_cnid == 0) && (ad_cnid == 0)) {
          if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
              /* add to db */
              ret = dbd_add(dbd, &rqst, &rply, 1);
 -            dbif_txn_close(dbd, ret);
 +            if (dbif_txn_close(dbd, ret) != 0)
 +                return CNID_INVALID;
              db_cnid = rply.cnid;
              dbd_log(LOGSTD, "New CNID for '%s/%s': %u", cwdbuf, name, ntohl(db_cnid));
          }
  
      if ((ad_cnid == 0) && db_cnid) {
          /* in db but zeroID in ad-file, write it to ad-file if AFPVOL_CACHE */
 -        if ((volinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
 +        if ((myvolinfo->v_flags & AFPVOL_CACHE) && ADFILE_OK) {
              if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                  dbd_log(LOGSTD, "Writing CNID data for '%s/%s' to AppleDouble file",
                          cwdbuf, name, ntohl(db_cnid));
 -                ad_init(&ad, volinfo->v_adouble, volinfo->v_ad_options);
 +                ad_init(&ad, myvolinfo->v_adouble, myvolinfo->v_ad_options);
                  if (ad_open_metadata( name, adflags, O_RDWR, &ad) != 0) {
                      dbd_log(LOGSTD, "Error opening AppleDouble file for '%s/%s': %s",
                              cwdbuf, name, strerror(errno));
 -                    return 0;
 +                    return CNID_INVALID;
                  }
                  ad_setid( &ad, st->st_dev, st->st_ino, db_cnid, did, stamp);
                  ad_flush(&ad);
          return db_cnid;
      }
  
 -    return 0;
 +    return CNID_INVALID;
  }
  
  /*
@@@ -862,11 -855,6 +862,6 @@@ static int dbd_readdir(int volroot, cni
      struct dirent *ep;
      static struct stat st;      /* Save some stack space */
  
-     /* keep trying to get the lock */
-     if (!db_locked)
-         if ((db_locked = get_lock(1, NULL)) == -1)
-             return -1;
      /* Check again for .AppleDouble folder, check_adfile also checks/creates it */
      if ((addir_ok = check_addir(volroot)) != 0)
          if ( ! (dbd_flags & DBD_FLAGS_SCAN))
                  static uint count = 0;
                  rqst.cnid = rply.cnid;
                  ret = dbd_rebuild_add(dbd_rebuild, &rqst, &rply);
 -                dbif_txn_close(dbd_rebuild, ret);
 +                if (dbif_txn_close(dbd_rebuild, ret) != 0)
 +                    return -1;
                  if (rply.result != CNID_DBD_RES_OK) {
 -                    dbd_log( LOGDEBUG, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
 +                    dbd_log( LOGSTD, "Fatal error adding CNID: %u for '%s/%s' to in-memory rebuild-db",
                               cnid, cwdbuf, ep->d_name);
 -                    longjmp(jmp, 1); /* this jumps back to cmd_dbd_scanvol() */
 +                    return -1;
                  }
                  count++;
                  if (count == 10000) {
          }
  
          /* Check EA files */
 -        if (volinfo->v_vfs_ea == AFPVOL_EA_AD)
 +        if (myvolinfo->v_vfs_ea == AFPVOL_EA_AD)
              check_eafiles(ep->d_name);
  
          /**************************************************************************
@@@ -1053,22 -1040,22 +1048,22 @@@ static int scanvol(struct volinfo *vi, 
      }
  
      /* Make this stuff accessible from all funcs easily */
 -    volinfo = vi;
 +    myvolinfo = vi;
      dbd_flags = flags;
  
      /* Init a fake struct vol with just enough so we can call ea_open and friends */
      volume.v_adouble = AD_VERSION2;
 -    volume.v_vfs_ea = volinfo->v_vfs_ea;
 +    volume.v_vfs_ea = myvolinfo->v_vfs_ea;
      initvol_vfs(&volume);
  
      /* Run with umask 0 */
      umask(0);
  
      /* Remove trailing slash from volume, chdir to vol */
 -    if (volinfo->v_path[strlen(volinfo->v_path) - 1] == '/')
 -        volinfo->v_path[strlen(volinfo->v_path) - 1] = 0;
 -    strcpy(cwdbuf, volinfo->v_path);
 -    chdir(volinfo->v_path);
 +    if (myvolinfo->v_path[strlen(myvolinfo->v_path) - 1] == '/')
 +        myvolinfo->v_path[strlen(myvolinfo->v_path) - 1] = 0;
 +    strcpy(cwdbuf, myvolinfo->v_path);
 +    chdir(myvolinfo->v_path);
  
      /* Start recursion */
      if (dbd_readdir(1, htonl(2)) < 0)  /* 2 = volumeroot CNID */
@@@ -1123,14 -1110,8 +1118,14 @@@ static void delete_orphaned_cnids(DBD *
                      dbd_log(LOGSTD, "Orphaned CNID in database: %u", dbd_cnid);
                      if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                          rqst.cnid = htonl(dbd_cnid);
 -                        ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
 -                        dbif_txn_close(dbd, ret);
 +                        if ((ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID)) == -1) {
 +                            dbd_log(LOGSTD, "Error deleting CNID %u", dbd_cnid);
 +                            (void)dbif_txn_abort(dbd);
 +                            goto cleanup;
 +                        }
 +                        
 +                        if (dbif_txn_close(dbd, ret) != 0)
 +                            return;
                          deleted++;
                      }
                      /* Check if we got a termination signal */
  
          if (dbd_cnid < rebuild_cnid) {
              /* CNID is orphaned -> delete */
 -            dbd_log(LOGSTD, "Orphaned CNID in database: %u.", dbd_cnid);
 +            dbd_log(LOGSTD, "One orphaned CNID in database: %u.", dbd_cnid);
              if ( ! (dbd_flags & DBD_FLAGS_SCAN)) {
                  rqst.cnid = htonl(dbd_cnid);
 -                ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID);
 -                dbif_txn_close(dbd, ret);
 +                if ((ret = dbd_delete(dbd, &rqst, &rply, DBIF_CNID)) == -1) {
 +                    dbd_log(LOGSTD, "Error deleting CNID %u", dbd_cnid);
 +                    (void)dbif_txn_abort(dbd);
 +                    goto cleanup;
 +                }
 +                if (dbif_txn_close(dbd, ret) != 0)
 +                    return;
                  deleted++;
              }
              continue;
          if (dbd_cnid > rebuild_cnid) {
              dbif_idwalk(dbd, NULL, 1); /* Close cursor */
              dbif_idwalk(dbd_rebuild, NULL, 1); /* Close cursor */
 -            dbif_txn_close(dbd, 2);
 -            dbif_txn_close(dbd_rebuild, 2);
 +            (void)dbif_txn_close(dbd, 2);
 +            (void)dbif_txn_close(dbd_rebuild, 2);                
              dbd_log(LOGSTD, "Ghost CNID: %u. This is fatal! Dumping rebuild db:\n", rebuild_cnid);
              dbif_dump(dbd_rebuild, 0);
              dbd_log(LOGSTD, "Send this dump and a `dbd -d ...` dump to the Netatalk Dev team!");
              goto cleanup;
          }
 -    }
 +    } /* while ((dbif_idwalk(dbd, &dbd_cnid, 0)) == 1) */
  
  cleanup:
      dbif_idwalk(dbd, NULL, 1); /* Close cursor */
@@@ -1191,7 -1167,7 +1186,7 @@@ static const char *get_tmpdb_path(void
  /*
    Main func called from cmd_dbd.c
  */
 -int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *volinfo, dbd_flags_t flags)
 +int cmd_dbd_scanvol(DBD *dbd_ref, struct volinfo *vi, dbd_flags_t flags)
  {
      int ret = 0;
      struct db_param db_param = { 0 };
  
      /* Set cachesize for in-memory rebuild db */
      db_param.cachesize = 64 * 1024;         /* 64 MB */
 +    db_param.maxlocks = DEFAULT_MAXLOCKS;
 +    db_param.maxlockobjs = DEFAULT_MAXLOCKOBJS;
      db_param.logfile_autoremove = 1;
  
      /* Make it accessible for all funcs */
      dbd = dbd_ref;
  
      /* We only support unicode volumes ! */
 -    if ( volinfo->v_volcharset != CH_UTF8) {
 -        dbd_log( LOGSTD, "Not a Unicode volume: %s, %u != %u", volinfo->v_volcodepage, volinfo->v_volcharset, CH_UTF8);
 +    if ( vi->v_volcharset != CH_UTF8) {
 +        dbd_log( LOGSTD, "Not a Unicode volume: %s, %u != %u", vi->v_volcodepage, vi->v_volcharset, CH_UTF8);
          return -1;
      }
  
      }
  
      /* scanvol */
 -    if ( (scanvol(volinfo, flags)) != 0) {
 +    if ( (scanvol(vi, flags)) != 0) {
          ret = -1;
          goto exit;
      }
  
  exit:
      if (! nocniddb) {
 -        dbif_txn_close(dbd, 1);
 +        if (dbif_txn_close(dbd, ret == 0 ? 1 : 0) != 0)
 +            ret = -1;
          if (dbd_rebuild)
 -            dbif_txn_close(dbd_rebuild, 1);
 -        if ((flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE))
 +            if (dbif_txn_close(dbd_rebuild, ret == 0 ? 1 : 0) != 0)
 +                ret = -1;
 +        if ((ret == 0) && dbd_rebuild && (flags & DBD_FLAGS_EXCL) && !(flags & DBD_FLAGS_FORCE))
              /* We can only do this in exclusive mode, otherwise we might delete CNIDs added from
                 other clients in between our pass 1 and 2 */
              delete_orphaned_cnids(dbd, dbd_rebuild, flags);
diff --combined etc/cnid_dbd/dbif.c
index 646385cbe267d392e955e8261220c49ff80dc933,c79189c504523d801287b7c94b627b8e62800427..792ebe915a4c8dcf7f5085dea7b8e4ce06dbac3f
  #include <stdio.h>
  #include <errno.h>
  #include <stdlib.h>
 -#ifdef HAVE_SYS_TYPES_H
 -#include <sys/types.h>
 -#endif /* HAVE_SYS_TYPES_H */
  #include <string.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/cdefs.h>
  #include <unistd.h>
 +
 +#include <db.h>
 +
  #include <atalk/logger.h>
  #include <atalk/util.h>
 -#include <db.h>
  
  #include "db_param.h"
  #include "dbif.h"
  
  #define DB_ERRLOGFILE "db_errlog"
  
 -static char *old_dbfiles[] = {"cnid.db", NULL};
 -
 -/* --------------- */
 -static int upgrade_required(const DBD *dbd)
 +/*!
 + * Get the db stamp which is the st_ctime of the file "cnid2.db" and store it in buffer
 + */
 +static int dbif_stamp(DBD *dbd, void *buffer, int size)
  {
 -    int i;
 -    int cwd = -1;
 -    int ret = 0;
 -    int found = 0;
      struct stat st;
 +    int         rc,cwd;
  
 -    if ( ! dbd->db_filename)
 -        /* in memory db */
 -        return 0;
 +    if (size < 8)
 +        return -1;
  
      /* Remember cwd */
      if ((cwd = open(".", O_RDONLY)) < 0) {
      /* chdir to db_envhome */
      if ((chdir(dbd->db_envhome)) != 0) {
          LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
 +        return -1;
 +    }
 +
 +    if ((rc = stat(dbd->db_table[DBIF_CNID].name, &st)) < 0) {
 +        LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
 +        return -1;
 +    }
 +
 +    LOG(log_maxdebug, logtype_cnid,"stamp: %s", asctime(localtime(&st.st_ctime)));
 +
 +    memset(buffer, 0, size);
 +    memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
 +
 +    if ((fchdir(cwd)) != 0) {
 +        LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
 +        return -1;
 +    }
 +
 +    return 0;
 +}
 +
 +/*!
 + * Inititialize rootinfo key (which has CNID 0 as key)
 + *
 + * This also "stamps" the database, which means storing st.st_ctime of the
 + * "cnid2.db" file in the rootinfo data at the DEV offset
 + *
 + * @param dbd      (rw) database handle
 + * @param version  (r)  database version number
 + *
 + * @returns -1 on error, 0 on success
 + */
 +static int dbif_init_rootinfo(DBD *dbd, int version)
 +{
 +    DBT key, data;
 +    uint32_t v;
 +    char buf[ROOTINFO_DATALEN];
 +
 +    LOG(log_debug, logtype_cnid, "Setting CNID database version to %u", version);
 +
 +    v = version;
 +    v = htonl(v);
 +
 +    memset(&key, 0, sizeof(key));
 +    memset(&data, 0, sizeof(data));
 +    key.data = ROOTINFO_KEY;
 +    key.size = ROOTINFO_KEYLEN;
 +    data.data = buf;
 +    data.size = ROOTINFO_DATALEN;
 +
 +    memcpy(buf, ROOTINFO_DATA, ROOTINFO_DATALEN);
 +    memcpy(buf + CNID_DID_OFS, &v, sizeof(v));
 +    if (dbif_stamp(dbd, buf + CNID_DEV_OFS, CNID_DEV_LEN) < 0)
 +        return -1;
 +
 +    if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
 +        return -1;
 +    if (dbif_txn_commit(dbd) != 1) {
 +        LOG(log_error, logtype_cnid, "dbif_init_rootinfo: cant commit txn");
 +        return -1;
 +    }
 +
 +    return 0;
 +}
 +
 +/*!
 + * Return CNID database version number
 + *
 + * Returns version in *version
 + *
 + * @returns -1 on error, 0 if theres no rootinfo key yet, 1 if *version is returned
 + */
 +static int dbif_getversion(DBD *dbd, uint32_t *version)
 +{
 +    DBT key, data;
 +    int ret;
 +
 +    LOG(log_maxdebug, logtype_cnid, "dbif_getversion: reading version info");
 +
 +    *version = -1;
 +    memset(&key, 0, sizeof(key));
 +    memset(&data, 0, sizeof(data));
 +    key.data = ROOTINFO_KEY;
 +    key.size = ROOTINFO_KEYLEN;
 +
 +    switch (dbif_get(dbd, DBIF_CNID, &key, &data, 0)) {
 +    case 1: /* found */
 +        memcpy(version, (char *)data.data + CNID_DID_OFS, sizeof(uint32_t));
 +        *version = ntohl(*version);
 +        LOG(log_debug, logtype_cnid, "CNID database version %u", *version);
 +        ret = 1;
 +        break;
 +    case 0: /* not found */
 +        LOG(log_debug, logtype_cnid, "dbif_getversion: no version info found");
 +        ret = 0;
 +        break;
 +    default:
 +        LOG(log_error, logtype_cnid, "dbif_getversion: database error");
          ret = -1;
 -        goto exit;
 +        break;
      }
  
 -    for (i = 0; old_dbfiles[i] != NULL; i++) {
 -        if ( !(stat(old_dbfiles[i], &st) < 0) ) {
 -            found++;
 -            continue;
 -        }
 -        if (errno != ENOENT) {
 -            LOG(log_error, logtype_cnid, "cnid_open: Checking %s gave %s", old_dbfiles[i], strerror(errno));
 -            found++;
 -        }
 +    return ret;
 +}
 +
 +/*!
 + * Set CNID database version number
 + *
 + * Initializes rootinfo key as neccessary
 + * @returns -1 on error, 0 on success
 + */
 +static int dbif_setversion(DBD *dbd, uint32_t version)
 +{
 +    int ret;
 +    DBT key, data;
 +    uint32_t v;
 +
 +    LOG(log_debug, logtype_cnid, "Setting CNID database version to %u", version);
 +
 +    v = version;
 +    v = htonl(v);
 +
 +    memset(&key, 0, sizeof(key));
 +    memset(&data, 0, sizeof(data));
 +    key.data = ROOTINFO_KEY;
 +    key.size = ROOTINFO_KEYLEN;
 +
 +    if ((ret = dbif_get(dbd, DBIF_CNID, &key, &data, 0)) == -1)
 +        return -1;
 +    if (ret == 0) {
 +        /* No rootinfo key yet, init it */
 +        if (dbif_init_rootinfo(dbd, CNID_VERSION) != 0)
 +            return -1;
 +        /* Now try again */
 +        if (dbif_get(dbd, DBIF_CNID, &key, &data, 0) == -1)
 +            return -1;
      }
 +    memcpy((char *)data.data + CNID_DID_OFS, &v, sizeof(v));
 +    data.size = ROOTINFO_DATALEN;
 +    if (dbif_put(dbd, DBIF_CNID, &key, &data, 0) < 0)
 +        return -1;
  
 -exit:
 -    if (cwd != -1) {
 -        if ((fchdir(cwd)) != 0) {
 -            LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
 -            ret = -1;
 -        }
 -        close(cwd);
 +    return 0;
 +}
 +
 +/*!
 + * Upgrade CNID database versions, initialize rootinfo key as as necessary in dbif_setversion()
 + *
 + * For now this does nothing, as upgrading from ver. 0 to 1 is done in dbif_open
 + */
 +#define UNINTIALIZED_DB UINT32_MAX
 +static int dbif_upgrade(DBD *dbd)
 +{
 +    uint32_t version = CNID_VERSION_UNINTIALIZED_DB;
 +
 +    if (dbif_getversion(dbd, &version) == -1)
 +        return -1;
 +    if (version == CNID_VERSION_UNINTIALIZED_DB) {
 +        version = CNID_VERSION;
 +        if (dbif_setversion(dbd, CNID_VERSION) != 0)
 +            return -1;
      }
 -    return (ret < 0 ? ret : found);
 +
 +    /* 
 +     * Do upgrade stuff ...
 +     */
 +
 +    /* Write current version to database */
 +    if (version != CNID_VERSION) {
 +        if (dbif_setversion(dbd, CNID_VERSION) != 0)
 +            return -1;
 +    }
 +
 +    LOG(log_debug, logtype_cnid, "Finished CNID database version upgrade check");
 +
 +    return 0;
  }
  
  /* --------------- */
@@@ -321,6 -174,127 +321,91 @@@ exit
      return ret;
  }
  
 -/* --------------- */
 -int dbif_stamp(DBD *dbd, void *buffer, int size)
 -{
 -    struct stat st;
 -    int         rc,cwd;
 -
 -    if (size < 8)
 -        return -1;
 -
 -    /* Remember cwd */
 -    if ((cwd = open(".", O_RDONLY)) < 0) {
 -        LOG(log_error, logtype_cnid, "error opening cwd: %s", strerror(errno));
 -        return -1;
 -    }
 -
 -    /* chdir to db_envhome */
 -    if ((chdir(dbd->db_envhome)) != 0) {
 -        LOG(log_error, logtype_cnid, "error chdiring to db_env '%s': %s", dbd->db_envhome, strerror(errno));        
 -        return -1;
 -    }
 -
 -    if ((rc = stat(dbd->db_table[DBIF_CNID].name, &st)) < 0) {
 -        LOG(log_error, logtype_cnid, "error stating database %s: %s", dbd->db_table[DBIF_CNID].name, db_strerror(errno));
 -        return -1;
 -    }
 -    memset(buffer, 0, size);
 -    memcpy(buffer, &st.st_ctime, sizeof(st.st_ctime));
 -
 -    if ((fchdir(cwd)) != 0) {
 -        LOG(log_error, logtype_cnid, "error chdiring back: %s", strerror(errno));        
 -        return -1;
 -    }
 -
 -    return 0;
 -}
 -
+ /*!
+  * Get lock on db lock file
+  *
+  * @args cmd       (r) lock command:
+  *                     LOCK_FREE:   close lockfd
+  *                     LOCK_UNLOCK: unlock lockm keep lockfd open
+  *                     LOCK_EXCL:   F_WRLCK on lockfd
+  *                     LOCK_SHRD:   F_RDLCK on lockfd
+  * @args dbpath    (r) path to lockfile, only used on first call,
+  *                     later the stored fd is used
+  * @returns            LOCK_FREE/LOCK_UNLOCK return 0 on success, -1 on error
+  *                     LOCK_EXCL/LOCK_SHRD return LOCK_EXCL or LOCK_SHRD respectively on
+  *                     success, 0 if the lock couldn't be acquired, -1 on other errors
+  */
+ int get_lock(int cmd, const char *dbpath)
+ {
+     static int lockfd = -1;
+     int ret;
+     char lockpath[PATH_MAX];
+     struct stat st;
+     switch (cmd) {
+     case LOCK_FREE:
+         if (lockfd == -1)
+             return -1;
+         close(lockfd);
+         lockfd = -1;
+         return 0;
+     case LOCK_UNLOCK:
+         if (lockfd == -1)
+             return -1;
+         return unlock(lockfd, 0, SEEK_SET, 0);
+     case LOCK_EXCL:
+     case LOCK_SHRD:
+         if (lockfd == -1) {
+             if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
+                 LOG(log_error, logtype_cnid, ".AppleDB pathname too long");
+                 return -1;
+             }
+             strncpy(lockpath, dbpath, PATH_MAX - 1);
+             strcat(lockpath, "/");
+             strcat(lockpath, LOCKFILENAME);
+             if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
+                 LOG(log_error, logtype_cnid, "Error opening lockfile: %s", strerror(errno));
+                 return -1;
+             }
+             if ((stat(dbpath, &st)) != 0) {
+                 LOG(log_error, logtype_cnid, "Error statting lockfile: %s", strerror(errno));
+                 return -1;
+             }
+             if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
+                 LOG(log_error, logtype_cnid, "Error inheriting lockfile permissions: %s",
+                          strerror(errno));
+                 return -1;
+             }
+         }
+     
+         if (cmd == LOCK_EXCL)
+             ret = write_lock(lockfd, 0, SEEK_SET, 0);
+         else
+             ret = read_lock(lockfd, 0, SEEK_SET, 0);
+         if (ret != 0) {
+             if (cmd == LOCK_SHRD)
+                 LOG(log_error, logtype_cnid, "Volume CNID db is locked, try again...");
+             return 0; 
+         }
+         LOG(log_debug, logtype_cnid, "get_lock: got %s lock",
+             cmd == LOCK_EXCL ? "LOCK_EXCL" : "LOCK_SHRD");    
+         return cmd;
+     default:
+         return -1;
+     } /* switch(cmd) */
+     /* deadc0de, never get here */
+     return -1;
+ }
  /* --------------- */
  DBD *dbif_init(const char *envhome, const char *filename)
  {
      dbd->db_table[DBIF_CNID].name        = "cnid2.db";
      dbd->db_table[DBIF_IDX_DEVINO].name  = "devino.db";
      dbd->db_table[DBIF_IDX_DIDNAME].name = "didname.db";
 +    dbd->db_table[DBIF_IDX_NAME].name    = "name.db";
  
      dbd->db_table[DBIF_CNID].type        = DB_BTREE;
      dbd->db_table[DBIF_IDX_DEVINO].type  = DB_BTREE;
      dbd->db_table[DBIF_IDX_DIDNAME].type = DB_BTREE;
 +    dbd->db_table[DBIF_IDX_NAME].type    = DB_BTREE;
  
      dbd->db_table[DBIF_CNID].openflags        = DB_CREATE;
      dbd->db_table[DBIF_IDX_DEVINO].openflags  = DB_CREATE;
      dbd->db_table[DBIF_IDX_DIDNAME].openflags = DB_CREATE;
 +    dbd->db_table[DBIF_IDX_NAME].openflags    = DB_CREATE;
 +
 +    dbd->db_table[DBIF_IDX_NAME].flags = DB_DUPSORT;
  
      return dbd;
  }
  /* 
     We must open the db_env with an absolute pathname, as `dbd` keeps chdir'ing, which
     breaks e.g. bdb logfile-rotation with relative pathnames.
 -   But still we use relative paths with upgrade_required() and the DB_ERRLOGFILE
 +   But still we use relative paths with DB_ERRLOGFILE
     in order to avoid creating absolute paths by copying. Both have no problem with
     a relative path.
  */
@@@ -379,6 -348,12 +464,6 @@@ int dbif_env_open(DBD *dbd, struct db_p
  {
      int ret;
  
 -    /* Refuse to do anything if this is an old version of the CNID database */
 -    if (upgrade_required(dbd)) {
 -        LOG(log_error, logtype_cnid, "Found version 1 of the CNID database. Please upgrade to version 2");
 -        return -1;
 -    }
 -
      if ((ret = db_env_create(&dbd->db_env, 0))) {
          LOG(log_error, logtype_cnid, "error creating DB environment: %s",
              db_strerror(ret));
          return -1;
      }
  
 +    if ((ret = dbd->db_env->set_lk_max_locks(dbd->db_env, dbp->maxlocks))) {
 +        LOG(log_error, logtype_cnid, "error setting DB environment maxlocks to %i: %s",
 +            10000, db_strerror(ret));
 +        dbd->db_env->close(dbd->db_env, 0);
 +        dbd->db_env = NULL;
 +        return -1;
 +    }
 +
 +    if ((ret = dbd->db_env->set_lk_max_objects(dbd->db_env, dbp->maxlockobjs))) {
 +        LOG(log_error, logtype_cnid, "error setting DB environment max lockobjects to %i: %s",
 +            10000, db_strerror(ret));
 +        dbd->db_env->close(dbd->db_env, 0);
 +        dbd->db_env = NULL;
 +        return -1;
 +    }
 +
      if ((ret = dbd->db_env->open(dbd->db_env, dbd->db_envhome, dbenv_oflags, 0))) {
          LOG(log_error, logtype_cnid, "error opening DB environment after recovery: %s",
              db_strerror(ret));
@@@ -536,7 -495,7 +621,7 @@@ int dbif_open(DBD *dbd, struct db_para
                  LOG(log_error, logtype_cnid, "error forcing checkpoint: %s", db_strerror(ret));
                  return -1;
              }
 -            LOG(log_debug, logtype_cnid, "Finished CNID database upgrade check");
 +            LOG(log_debug, logtype_cnid, "Finished BerkeleyBD upgrade check");
          }
          
          if ((fchdir(cwd)) != 0) {
      }
      if (reindex)
          LOG(log_info, logtype_cnid, "... done.");
 +
 +    if (reindex)
 +        LOG(log_info, logtype_cnid, "Reindexing name index...");
 +
 +    /*
 +     * Upgrading from version 0 to 1 requires adding the name index below which
 +     * must be done by specifying the DB_CREATE flag
 +     */
 +    uint32_t version = CNID_VERSION;
 +    if (dbd->db_envhome && !reindex) {
 +        if (dbif_getversion(dbd, &version) == -1)
 +            return -1;
 +    }
 +
 +    if ((ret = dbd->db_table[0].db->associate(dbd->db_table[0].db, 
 +                                              dbd->db_txn,
 +                                              dbd->db_table[DBIF_IDX_NAME].db, 
 +                                              idxname,
 +                                              (reindex
 +                                               || 
 +                                               ((CNID_VERSION == CNID_VERSION_1) && (version == CNID_VERSION_0)))
 +                                              ? DB_CREATE : 0)) != 0) {
 +        LOG(log_error, logtype_cnid, "Failed to associate name index: %s", db_strerror(ret));
 +        return -1;
 +    }
 +    if (reindex)
 +        LOG(log_info, logtype_cnid, "... done.");
 +
 +    if ((dbd->db_envhome) && ((ret = dbif_upgrade(dbd)) != 0)) {
 +        LOG(log_error, logtype_cnid, "Error upgrading CNID database to version %d", CNID_VERSION);
 +        return -1;
 +    }
      
      return 0;
  }
@@@ -710,7 -637,7 +795,7 @@@ int dbif_close(DBD *dbd
     In order to support silent database upgrades:
     destroy env at cnid_dbd shutdown.
   */
 -int dbif_prep_upgrade(const char *path)
 +int dbif_env_remove(const char *path)
  {
      int ret;
      DBD *dbd;
@@@ -877,10 -804,8 +962,10 @@@ int dbif_del(DBD *dbd, const int dbi, D
                                       key,
                                       flags);
      
 -    if (ret == DB_NOTFOUND || ret == DB_SECONDARY_BAD)
 +    if (ret == DB_NOTFOUND) {
 +        LOG(log_info, logtype_cnid, "key not found");
          return 0;
 +    }
      if (ret) {
          LOG(log_error, logtype_cnid, "error deleting key/value from %s: %s",
              dbd->db_table[dbi].name, db_strerror(ret));
          return 1;
  }
  
 +/*!
 + * Search the database by name
 + *
 + * @param resbuf    (w) buffer for search results CNIDs, maxsize is assumed to be
 + *                      DBD_MAX_SRCH_RSLTS * sizefof(cnid_t)
 + *
 + * @returns -1 on error, 0 when nothing found, else the number of matches
 + */
 +int dbif_search(DBD *dbd, DBT *key, char *resbuf)
 +{
 +    int ret = 0;
 +    int count = 0;
 +    DBC *cursorp = NULL;
 +    DBT pkey, data;
 +    char *cnids = resbuf;
 +    cnid_t cnid;
 +    char *namebkp = key->data;
 +    int namelenbkp = key->size;
 +
 +    memset(&pkey, 0, sizeof(DBT));
 +    memset(&data, 0, sizeof(DBT));
 +
 +    /* Get a cursor */
 +    ret = dbd->db_table[DBIF_IDX_NAME].db->cursor(dbd->db_table[DBIF_IDX_NAME].db,
 +                                                  NULL,
 +                                                  &cursorp,
 +                                                  0);
 +    if (ret != 0) {
 +        LOG(log_error, logtype_cnid, "Couldn't create cursor: %s", db_strerror(ret));
 +        ret = -1;
 +        goto exit;
 +    }
 +
 +    ret = cursorp->pget(cursorp, key, &pkey, &data, DB_SET_RANGE);
 +    while (count < DBD_MAX_SRCH_RSLTS && ret != DB_NOTFOUND) {
 +        if (!((namelenbkp <= key->size) && (strncmp(namebkp, key->data, namelenbkp) == 0)))
 +            break;
 +        count++;
 +        memcpy(cnids, pkey.data, sizeof(cnid_t));
 +        memcpy(&cnid, pkey.data, sizeof(cnid_t));
 +        cnids += sizeof(cnid_t);
 +        LOG(log_debug, logtype_cnid, "match: CNID %" PRIu32, ntohl(cnid));
 +
 +        ret = cursorp->pget(cursorp, key, &pkey, &data, DB_NEXT);
 +    }
 +
 +    ret = count;
 +
 +exit:
 +    if (cursorp != NULL)
 +        cursorp->close(cursorp);
 +    return ret;
 +}
 +
  int dbif_txn_begin(DBD *dbd)
  {
      int ret;
@@@ -1010,27 -881,22 +1095,27 @@@ int dbif_txn_abort(DBD *dbd
     ret = 1 -> commit txn if db_param.txn_frequency
     ret = 0 -> abort txn db_param.txn_frequency -> exit!
     anything else -> exit!
 +
 +   @returns 0 on success (abort or commit), -1 on error
  */
 -void dbif_txn_close(DBD *dbd, int ret)
 +int dbif_txn_close(DBD *dbd, int ret)
  {
      if (ret == 0) {
          if (dbif_txn_abort(dbd) < 0) {
              LOG( log_error, logtype_cnid, "Fatal error aborting transaction. Exiting!");
 -            exit(EXIT_FAILURE);
 +            return -1;
          }
 -    } else if (ret == 1 || ret == 2) {
 +    } else if (ret == 1) {
          ret = dbif_txn_commit(dbd);
          if (  ret < 0) {
              LOG( log_error, logtype_cnid, "Fatal error committing transaction. Exiting!");
 -            exit(EXIT_FAILURE);
 +            return -1;
          }
 -    } else
 -       exit(EXIT_FAILURE);
 +    } else {
 +        return -1;
 +    }
 +
 +    return 0;
  }
  
  int dbif_txn_checkpoint(DBD *dbd, u_int32_t kbyte, u_int32_t min, u_int32_t flags)
@@@ -1094,7 -960,7 +1179,7 @@@ int dbif_copy_rootinfokey(DBD *srcdbd, 
  int dbif_dump(DBD *dbd, int dumpindexes)
  {
      int rc;
 -    uint32_t max = 0, count = 0, cnid, type, did, lastid;
 +    uint32_t max = 0, count = 0, cnid, type, did, lastid, version;
      uint64_t dev, ino;
      time_t stamp;
      DBC *cur;
  
          /* Rootinfo node ? */
          if (cnid == 0) {
 -            memcpy(&stamp, (char *)data.data + 4, sizeof(time_t));
 -            memcpy(&lastid, (char *)data.data + 20, sizeof(cnid_t));
 +            memcpy(&stamp, (char *)data.data + CNID_DEV_OFS, sizeof(time_t));
 +            memcpy(&lastid, (char *)data.data + CNID_TYPE_OFS, CNID_TYPE_LEN);
              lastid = ntohl(lastid);
 +            memcpy(&version, (char *)data.data + CNID_DID_OFS, CNID_DID_LEN);
 +            version = ntohl(version);
 +
              strftime(timebuf, sizeof(timebuf), "%b %d %Y %H:%M:%S", localtime(&stamp));
 -            printf("dbd stamp: 0x%08x (%s), next free CNID: %u\n", (unsigned int)stamp, timebuf, lastid + 1);
 +            printf("CNID db version: %u, dbd stamp: 0x%08x (%s), next free CNID: %u\n",
 +                   version, (unsigned int)stamp, timebuf, lastid + 1);
          } else {
              /* dev */
              memcpy(&dev, (char *)data.data + CNID_DEV_OFS, 8);
diff --combined etc/cnid_dbd/dbif.h
index f5d3759fca5affba4013d724e2b32813b70309dd,1a4a0b8e685dbb227df4cf617b48af6be583a7ee..c936a2b93705daf7b5bf1a8061131f7a3174dd37
  #include <atalk/adouble.h>
  #include "db_param.h"
  
 -#define DBIF_DB_CNT 3
 +#define DBIF_DB_CNT 4
   
  #define DBIF_CNID          0
  #define DBIF_IDX_DEVINO    1
  #define DBIF_IDX_DIDNAME   2
 +#define DBIF_IDX_NAME      3
  
+ /* get_lock cmd and return value */
+ #define LOCKFILENAME  "lock"
+ #define LOCK_FREE          0
+ #define LOCK_UNLOCK        1
+ #define LOCK_EXCL          2
+ #define LOCK_SHRD          3
  /* Structures */
  typedef struct {
      char     *name;
@@@ -82,27 -88,29 +89,29 @@@ typedef struct 
      char     *db_envhome;
      char     *db_filename;
      FILE     *db_errlog;
 -    db_table db_table[3];
 +    db_table db_table[DBIF_DB_CNT];
  } DBD;
  
  /* Functions */
+ extern int get_lock(int cmd, const char *dbpath);
  extern DBD *dbif_init(const char *envhome, const char *dbname);
  extern int dbif_env_open(DBD *dbd, struct db_param *dbp, uint32_t dbenv_oflags);
  extern int dbif_open(DBD *dbd, struct db_param *dbp, int reindex);
  extern int dbif_close(DBD *dbd);
 -extern int dbif_prep_upgrade(const char *path);
 +extern int dbif_env_remove(const char *path);
  
  extern int dbif_get(DBD *, const int, DBT *, DBT *, u_int32_t);
  extern int dbif_pget(DBD *, const int, DBT *, DBT *, DBT *, u_int32_t);
  extern int dbif_put(DBD *, const int, DBT *, DBT *, u_int32_t);
  extern int dbif_del(DBD *, const int, DBT *, u_int32_t);
  extern int dbif_count(DBD *, const int, u_int32_t *);
 -extern int dbif_stamp(DBD *, void *, int);
 +extern int dbif_search(DBD *dbd, DBT *key, char *resbuf);
  extern int dbif_copy_rootinfokey(DBD *srcdbd, DBD *destdbd);
  extern int dbif_txn_begin(DBD *);
  extern int dbif_txn_commit(DBD *);
  extern int dbif_txn_abort(DBD *);
 -extern void dbif_txn_close(DBD *dbd, int ret); /* Switch between commit+abort */
 +extern int dbif_txn_close(DBD *dbd, int ret); /* Switch between commit+abort */
  extern int dbif_txn_checkpoint(DBD *, u_int32_t, u_int32_t, u_int32_t);
  
  extern int dbif_dump(DBD *dbd, int dumpindexes);
diff --combined etc/cnid_dbd/main.c
index 9b9d963134c4a02b5feee5bc0f248ef2e689c1bc,bf71b72d5945e8a63772d2da4639e3c204490197..dbb55b4e2dfe6af3b061896fbb1330975350c660
@@@ -1,4 -1,6 +1,4 @@@
  /*
 - * $Id: main.c,v 1.16 2009-11-25 14:59:15 franklahm Exp $
 - *
   * Copyright (C) Joerg Lenneis 2003
   * Copyright (c) Frank Lahm 2009
   * All Rights Reserved.  See COPYING.
  #include <netatalk/endian.h>
  #include <atalk/cnid_dbd_private.h>
  #include <atalk/logger.h>
 -#include <atalk/util.h>
 +#include <atalk/volinfo.h>
  
  #include "db_param.h"
  #include "dbif.h"
  #include "dbd.h"
  #include "comm.h"
  
- #define LOCKFILENAME  "lock"
  /* 
     Note: DB_INIT_LOCK is here so we can run the db_* utilities while netatalk is running.
     It's a likey performance hit, but it might we worth it.
   */
  #define DBOPTIONS (DB_CREATE | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_LOCK | DB_INIT_TXN)
  
 +/* Global, needed by pack.c:idxname() */
 +struct volinfo volinfo;
 +
  static DBD *dbd;
  static int exit_sig = 0;
  static int db_locked;
@@@ -74,57 -71,6 +72,6 @@@ static void block_sigs_onoff(int block
      return;
  }
  
- /*!
-  * Get lock on db lock file
-  *
-  * @args cmd       (r) !=0: lock, 0: unlock
-  * @returns            1 if lock was acquired, 0 if file is already locked, -1 on error
-  */
- static int get_lock(int cmd)
- {
-     static int lockfd = -1;
-     struct flock lock;
-     if (cmd == 0) {
-         if (lockfd == -1)
-             return -1;
-         lock.l_start  = 0;
-         lock.l_whence = SEEK_SET;
-         lock.l_len    = 0;
-         lock.l_type = F_UNLCK;
-         fcntl(lockfd, F_SETLK, &lock);
-         close(lockfd);
-         lockfd = -1;
-         return 0;
-     }
-     if (lockfd == -1) {
-         if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) {
-             LOG(log_error, logtype_cnid, "get_lock: error opening lockfile: %s", strerror(errno));
-             return -1;
-         }
-     }
-     lock.l_start  = 0;
-     lock.l_whence = SEEK_SET;
-     lock.l_len    = 0;
-     lock.l_type   = F_WRLCK;
-     if (fcntl(lockfd, F_SETLK, &lock) < 0) {
-         if (errno == EACCES || errno == EAGAIN) {
-             LOG(log_debug, logtype_cnid, "get_lock: couldn't lock");
-             return 0;
-         } else {
-             LOG(log_error, logtype_cnid, "get_lock: fcntl F_WRLCK lockfile: %s", strerror(errno));
-             return -1;
-         }
-     }
-     LOG(log_debug, logtype_cnid, "get_lock: got lock");
-     return 1;
- }
  /*
    The dbd_XXX and comm_XXX functions all obey the same protocol for return values:
  
@@@ -171,20 -117,6 +118,6 @@@ static int loop(struct db_param *dbp
          dbp->flush_interval, timebuf);
  
      while (1) {
-         /*
-          * If we haven't got the lock yet, get it now.
-          * Prevents a race with dbd:
-          *   1. no cnid_dbd running
-          *   2. dbd -r starts, gets the lock
-          *   3. cnid_dbd starts, doesn't get lock, doesn't run recovery, all is fine
-          *   4. dbd from (2) finishes, drops lock
-          *   5. anothet dbd but this time with -re is started which
-          *      - succeeds getting the lock
-          *      - runs recovery => this kills (3)
-          */
-         if (!db_locked)
-             if ((db_locked = get_lock(1)) == -1)
-                 return -1;
          timeout = min(time_next_flush, time_last_rqst +dbp->idle_timeout);
          if (timeout > now)
              timeout -= now;
              case CNID_DBD_OP_REBUILD_ADD:
                  ret = dbd_rebuild_add(dbd, &rqst, &rply);
                  break;
 +            case CNID_DBD_OP_SEARCH:
 +                ret = dbd_search(dbd, &rqst, &rply);
 +                break;
              default:
                  LOG(log_error, logtype_cnid, "loop: unknown op %d", rqst.op);
                  ret = -1;
                  break;
              }
 -            
 +
              if ((cret = comm_snd(&rply)) < 0 || ret < 0) {
                  dbif_txn_abort(dbd);
                  return -1;
@@@ -351,7 -280,7 +284,7 @@@ int main(int argc, char *argv[]
      struct db_param *dbp;
      int err = 0;
      int ctrlfd, clntfd;
 -    char *dir, *logconfig;
 +    char *logconfig;
  
      set_processname("cnid_dbd");
  
          exit(1);
      }
  
 -    dir = argv[1];
      ctrlfd = atoi(argv[2]);
      clntfd = atoi(argv[3]);
      logconfig = strdup(argv[4]);
      setuplog(logconfig);
  
 -    switch_to_user(dir);
 +    /* Load .volinfo file */
 +    if (loadvolinfo(argv[1], &volinfo) == -1) {
 +        LOG(log_error, logtype_cnid, "Cant load volinfo for \"%s\"", argv[1]);
 +        exit(EXIT_FAILURE);
 +    }
 +    /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
 +    char dbpath[MAXPATHLEN+1];
 +    if ((strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > MAXPATHLEN ) {
 +        LOG(log_error, logtype_cnid, "CNID db pathname too long: \"%s\"", volinfo.v_dbpath);
 +        exit(EXIT_FAILURE);
 +    }
 +    strncpy(dbpath, volinfo.v_dbpath, MAXPATHLEN - strlen("/.AppleDB"));
 +    strcat(dbpath, "/.AppleDB");
 +
 +    if (vol_load_charsets(&volinfo) == -1) {
 +        LOG(log_error, logtype_cnid, "Error loading charsets!");
 +        exit(EXIT_FAILURE);
 +    }
 +    LOG(log_debug, logtype_cnid, "db dir: \"%s\"", dbpath);
 +
 +    switch_to_user(dbpath);
  
-     /* Before we do anything else, check if there is an instance of cnid_dbd
-        running already and silently exit if yes. */
-     if ((db_locked = get_lock(1)) == -1) {
+     /* Get db lock */
+     if ((db_locked = get_lock(LOCK_EXCL, dir)) == -1) {
+         LOG(log_error, logtype_cnid, "main: fatal db lock error");
          exit(1);
      }
+     if (db_locked != LOCK_EXCL) {
+         /* Couldn't get exclusive lock, try shared lock  */
+         if ((db_locked = get_lock(LOCK_SHRD, NULL)) != LOCK_SHRD) {
+             LOG(log_error, logtype_cnid, "main: fatal db lock error");
+             exit(1);
+         }
+     }
  
 -    LOG(log_info, logtype_cnid, "Startup, DB dir %s", dir);
 -
      set_signal();
  
      /* SIGINT and SIGTERM are always off, unless we are in pselect */
      block_sigs_onoff(1);
  
 -    if ((dbp = db_param_read(dir, CNID_DBD)) == NULL)
 +    if ((dbp = db_param_read(dbpath)) == NULL)
          exit(1);
      LOG(log_maxdebug, logtype_cnid, "Finished parsing db_param config file");
  
 -    if (NULL == (dbd = dbif_init(".", "cnid2.db")))
 +    if (NULL == (dbd = dbif_init(dbpath, "cnid2.db")))
          exit(2);
  
      /* Only recover if we got the lock */
      if (dbif_env_open(dbd,
                        dbp,
-                       db_locked ? DBOPTIONS | DB_RECOVER : DBOPTIONS) < 0)
+                       (db_locked == LOCK_EXCL) ? DBOPTIONS | DB_RECOVER : DBOPTIONS) < 0)
          exit(2); /* FIXME: same exit code as failure for dbif_open() */
      LOG(log_debug, logtype_cnid, "Finished initializing BerkeleyDB environment");
  
      }
      LOG(log_debug, logtype_cnid, "Finished opening BerkeleyDB databases");
  
 -    if (dbd_stamp(dbd) < 0) {
 -        dbif_close(dbd);
 -        exit(5);
 -    }
 -    LOG(log_maxdebug, logtype_cnid, "Finished checking database stamp");
 -
+     /* Downgrade db lock  */
+     if (db_locked == LOCK_EXCL) {
+         if (get_lock(LOCK_UNLOCK, NULL) != 0) {
+             dbif_close(dbd);
+             exit(2);
+         }
+         if (get_lock(LOCK_SHRD, NULL) != LOCK_SHRD) {
+             dbif_close(dbd);
+             exit(2);
+         }
+     }
      if (comm_init(dbp, ctrlfd, clntfd) < 0) {
          dbif_close(dbd);
          exit(3);
      if (dbif_close(dbd) < 0)
          err++;
  
 -    if (dbif_prep_upgrade(dir) < 0)
 +    if (dbif_env_remove(dbpath) < 0)
          err++;
  
-     (void)get_lock(0);
      if (err)
          exit(4);
      else if (exit_sig)