vol: Blank fake linkHandles
[openafs.git] / src / vol / namei_ops.c
index 7dec3fe..463b66b 100644 (file)
 
 #include <roken.h>
 
+
 #ifdef AFS_NAMEI_ENV
-#include <stdio.h>
-#include <stdlib.h>
-#ifndef AFS_NT40_ENV
-#include <unistd.h>
-#else
+
+#ifdef HAVE_SYS_FILE_H
+# include <sys/file.h>
+#endif
+
+#ifdef AFS_NT40_ENV
 #define DELETE_ZLC
-#include <io.h>
 #include <windows.h>
 #include <winnt.h>
 #include <winbase.h>
-#include <winsock2.h>
-#endif
-#include <errno.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-#ifdef AFS_NT40_ENV
 #include <direct.h>
-#else
-#include <sys/file.h>
-#include <sys/param.h>
 #endif
-#include <dirent.h>
-#include <afs/afs_assert.h>
-#include <string.h>
+
+#include <afs/opr.h>
+#include <rx/rx_queue.h>
+#ifdef AFS_PTHREAD_ENV
+# include <opr/lock.h>
+#endif
 #include <lock.h>
 #include <afs/afsutil.h>
 #include <lwp.h>
 #include "volume_inline.h"
 #include "common.h"
 #include <afs/errors.h>
-#ifdef AFS_NT40_ENV
-#include <afs/errmap_nt.h>
-#endif
 
-/*@+fcnmacros +macrofcndecl@*/
-#ifdef O_LARGEFILE
-#ifdef S_SPLINT_S
-#endif /*S_SPLINT_S */
-#define afs_stat               stat64
-#define afs_fstat              fstat64
 #ifdef AFS_NT40_ENV
-#define afs_open                nt_open
-#else
-#define afs_open               open64
-#endif
-#define afs_fopen              fopen64
-#else /* !O_LARGEFILE */
-#ifdef S_SPLINT_S
-#endif /*S_SPLINT_S */
-#define afs_stat               stat
-#define afs_fstat              fstat
-#ifdef AFS_NT40_ENV
-#define afs_open                nt_open
-#else
-#define afs_open               open
+#include <afs/errmap_nt.h>
 #endif
-#define afs_fopen              fopen
-#endif /* !O_LARGEFILE */
-/*@=fcnmacros =macrofcndecl@*/
 
 #ifndef LOCK_SH
 #define   LOCK_SH   1    /* shared lock */
 #ifdef AFS_SALSRV_ENV
 #include <pthread.h>
 #include <afs/work_queue.h>
-#include <afs/thread_pool.h>
 #include <vol/vol-salvage.h>
 #endif
 
-#if !defined(HAVE_FLOCK) && !defined(AFS_NT40_ENV)
-#include <fcntl.h>
-
-/*
- * This function emulates a subset of flock()
- */
-int
-emul_flock(int fd, int cmd)
-{    struct flock f;
-
-    memset(&f, 0, sizeof (f));
-
-    if (cmd & LOCK_UN)
-        f.l_type = F_UNLCK;
-    if (cmd & LOCK_SH)
-        f.l_type = F_RDLCK;
-    if (cmd & LOCK_EX)
-        f.l_type = F_WRLCK;
-
-    return fcntl(fd, (cmd & LOCK_NB) ? F_SETLK : F_SETLKW, &f);
-}
-
-#define flock(f,c)      emul_flock(f,c)
-#endif
-
 int Testing=0;
 
+static void namei_UnlockLinkCount(FdHandle_t * fdP, Inode ino);
 
 afs_sfsize_t
 namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
@@ -137,7 +82,10 @@ namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
        return -1;
 
     nBytes = FDH_PREAD(fdP, buf, size, offset);
-    FDH_CLOSE(fdP);
+    if (nBytes < 0)
+       FDH_REALLYCLOSE(fdP);
+    else
+       FDH_CLOSE(fdP);
     return nBytes;
 }
 
@@ -152,12 +100,13 @@ namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
        return -1;
 
     nBytes = FDH_PWRITE(fdP, buf, size, offset);
-    FDH_CLOSE(fdP);
+    if (nBytes < 0)
+       FDH_REALLYCLOSE(fdP);
+    else
+       FDH_CLOSE(fdP);
     return nBytes;
 }
 
-
-
 #ifdef AFS_NT40_ENV
 /* Inode number format:
  * low 32 bits - if a regular file or directory, the vnode; else the type.
@@ -203,12 +152,13 @@ namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
                                 * group at once.
                                 */
 
-
+#ifndef AFS_NT40_ENV
 typedef struct {
     int ogm_owner;
     int ogm_group;
     int ogm_mode;
 } namei_ogm_t;
+#endif
 
 static int GetFreeTag(IHandle_t * ih, int vno);
 
@@ -247,17 +197,18 @@ namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
      */
     volutil_PartitionName_r(ih->ih_dev, name->n_base, sizeof(name->n_base));
     offset = VICE_PREFIX_SIZE + (ih->ih_dev > 25 ? 2 : 1);
-    name->n_base[offset] = '/';
+    name->n_base[offset] = OS_DIRSEPC;
     offset++;
     strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
     strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
 }
 #endif
 
-#define addtoname(N, C)                                 \
-do {                                                    \
-    strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path));     \
-    strlcat((N)->n_path, (C), sizeof((N)->n_path));     \
+#define addtoname(N, C)                                         \
+do {                                                            \
+    if ((N)->n_path[strlen((N)->n_path)-1] != OS_DIRSEPC)       \
+        strlcat((N)->n_path, OS_DIRSEP, sizeof((N)->n_path));   \
+    strlcat((N)->n_path, (C), sizeof((N)->n_path));             \
 } while(0)
 
 
@@ -307,13 +258,14 @@ void
 namei_HandleToName(namei_t * name, IHandle_t * ih)
 {
     int vno = (int)(ih->ih_ino & NAMEI_VNODEMASK);
+    int special = (ih->ih_ino & NAMEI_INODESPECIAL)?1:0;
     int tag = (int)((ih->ih_ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
     b32_string_t str1;
     char *namep;
     namei_HandleToVolDir(name, ih);
 
-    if (vno == NAMEI_VNODESPECIAL) {
-       name->n_dir[0] = 'R';
+    if (special) {
+       name->n_dir[0] = NAMEI_SPECDIRC;
     } else {
        if (vno & 0x1)
             name->n_dir[0] = 'Q';
@@ -373,19 +325,24 @@ int
 namei_ViceREADME(char *partition)
 {
     char filename[32];
-    int fd;
+    int fd, len, e = 0;
 
     /* Create the inode directory if we're starting for the first time */
-    (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
-                      INODEDIR);
+    snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s", partition,
+            INODEDIR);
     mkdir(filename, 0700);
 
-    (void)afs_snprintf(filename, sizeof filename, "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
-                       partition, INODEDIR);
-    fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
-    if (fd >= 0) {
-       (void)write(fd, VICE_README, strlen(VICE_README));
-       close(fd);
+    snprintf(filename, sizeof filename,
+            "%s" OS_DIRSEP "%s" OS_DIRSEP "README",
+             partition, INODEDIR);
+    fd = OS_OPEN(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
+    if (fd != INVALID_FD) {
+       len = strlen(VICE_README);
+       if (OS_WRITE(fd, VICE_README, len) != len)
+           e = errno;
+       OS_CLOSE(fd);
+       if (e)
+           errno = e;
     }
     return (errno);
 }
@@ -405,7 +362,7 @@ namei_CreateDataDirectories(namei_t * name, int *created)
     int i;
 
     *created = 0;
-    afs_snprintf(tmp, 256, "%s\\%s", name->n_drive, name->n_voldir);
+    snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
 
     if (mkdir(tmp) < 0) {
         if (errno != EEXIST)
@@ -416,9 +373,9 @@ namei_CreateDataDirectories(namei_t * name, int *created)
     s = tmp;
     s += strlen(tmp);
 
-    *s++ = '\\';
+    *s++ = OS_DIRSEPC;
     *(s + 1) = '\0';
-    for (i = 'A'; i <= 'R'; i++) {
+    for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
         *s = (char)i;
         if (mkdir(tmp) < 0 && errno != EEXIST)
             return -1;
@@ -439,7 +396,7 @@ do { \
 
 #define create_nextdir(A) \
 do { \
-        strcat(tmp, "/"); strcat(tmp, A); create_dir();  \
+        strcat(tmp, OS_DIRSEP); strcat(tmp, A); create_dir();  \
 } while(0)
 
 static int
@@ -492,7 +449,7 @@ delTree(char *root, char *tree, int *errp)
 
     if (*tree) {
        /* delete the children first */
-       cp = strchr(tree, '/');
+       cp = strchr(tree, OS_DIRSEPC);
        if (cp) {
            delTree(root, cp + 1, errp);
            *cp = '\0';
@@ -500,7 +457,7 @@ delTree(char *root, char *tree, int *errp)
            cp = tree + strlen(tree);   /* move cp to the end of string tree */
 
        /* now delete all entries in this dir */
-       if ((ds = opendir(root)) != (DIR *) NULL) {
+       if ((ds = opendir(root)) != NULL) {
            errno = 0;
            while ((dirp = readdir(ds))) {
                /* ignore . and .. */
@@ -509,7 +466,7 @@ delTree(char *root, char *tree, int *errp)
                /* since root is big enough, we reuse the space to
                 * concatenate the dirname to the current tree
                 */
-               strcat(root, "/");
+               strcat(root, OS_DIRSEP);
                strcat(root, dirp->d_name);
                if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
                    /* delete this subtree */
@@ -563,13 +520,13 @@ namei_RemoveDataDirectories(namei_t * name)
     char tmp[256];
     int i;
 
-    afs_snprintf(tmp, 256, "%s\\%s", name->n_drive, name->n_voldir);
+    snprintf(tmp, 256, "%s" OS_DIRSEP "%s", name->n_drive, name->n_voldir);
 
     path = tmp;
     path += strlen(path);
-    *path++ = '\\';
+    *path++ = OS_DIRSEPC;
     *(path + 1) = '\0';
-    for (i = 'A'; i <= 'R'; i++) {
+    for (i = 'A'; i <= NAMEI_SPECDIRC; i++) {
         *path = (char)i;
         if (rmdir(name->n_path) < 0 && errno != ENOENT)
             code = -1;
@@ -646,12 +603,13 @@ namei_RemoveDataDirectories(namei_t * name)
  * types, but if we get that far, this could should be dead by then.
  */
 Inode
-namei_MakeSpecIno(int volid, int type)
+namei_MakeSpecIno(VolumeId volid, int type)
 {
     Inode ino;
     ino = NAMEI_INODESPECIAL;
 #ifdef AFS_NT40_ENV
     ino |= type;
+    /* tag is always 0 for special */
 #else
     type &= NAMEI_TAGMASK;
     ino |= ((Inode) type) << NAMEI_TAGSHIFT;
@@ -660,11 +618,90 @@ namei_MakeSpecIno(int volid, int type)
     return ino;
 }
 
+#ifdef AFS_NT40_ENV
+/* SetOGM */
+static int
+SetOGM(FD_t fd, int parm, int tag)
+{
+    return -1;
+}
+
+static int
+SetWinOGM(FD_t fd, int p1, int p2)
+{
+    BOOL code;
+    FILETIME ftime;
+
+    ftime.dwHighDateTime = p1;
+    ftime.dwLowDateTime = p2;
+
+    code = SetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
+    if (!code)
+       return -1;
+    return 0;
+}
+
+static int
+GetWinOGM(FD_t fd, int *p1, int *p2)
+{
+    BOOL code;
+    FILETIME ftime;
+
+    code = GetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
+    if (!code)
+       return -1;
+
+    *p1 = ftime.dwHighDateTime;
+    *p2 = ftime.dwLowDateTime;
+
+    return 0;
+}
+
+static int
+CheckOGM(FdHandle_t *fdP, int p1)
+{
+    int ogm_p1, ogm_p2;
+
+    if (GetWinOGM(fdP->fd_fd, &ogm_p1, &ogm_p2)) {
+       return -1;
+    }
+
+    if (ogm_p1 != p1) {
+       return -1;
+    }
+
+    return 0;
+}
+
+static int
+FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+    Inode ino = fdP->fd_ih->ih_ino;
+    VnodeId vno = NAMEI_VNODESPECIAL, ogm_vno;
+    int ogm_volid;
+
+    if (GetWinOGM(fdP->fd_fd, &ogm_volid, &ogm_vno)) {
+       return -1;
+    }
+
+    /* the only thing we can check is the vnode number; for the volid we have
+     * nothing else to compare against */
+    if (vno != ogm_vno) {
+       if (check) {
+           return -1;
+       }
+       if (SetWinOGM(fdP->fd_fd, ogm_volid, vno)) {
+           return -1;
+       }
+    }
+    return 0;
+}
+
+#else /* AFS_NT40_ENV */
 /* SetOGM - set owner group and mode bits from parm and tag */
 static int
 SetOGM(FD_t fd, int parm, int tag)
 {
-#ifndef AFS_NT40_ENV
 /*
  * owner - low 15 bits of parm.
  * group - next 15 bits of parm.
@@ -681,36 +718,12 @@ SetOGM(FD_t fd, int parm, int tag)
     mode |= tag & 0x7;
     if (fchmod(fd, mode) < 0)
        return -1;
-#endif
     return 0;
 }
 
-#ifdef AFS_NT40_ENV
-static int
-CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
-{
-    WIN32_FIND_DATA info;
-    HANDLE dirH;
-
-    dirH =
-       FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
-                       FindExSearchNameMatch, NULL,
-                       FIND_FIRST_EX_CASE_SENSITIVE);
-
-    if (!dirH)
-       return -1;          /* Can't get info, leave alone */
-
-    FindClose(dirH);
-
-    if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
-       return -1;
-
-    return 0;
-}
-#else
 /* GetOGM - get parm and tag from owner, group and mode bits. */
 static void
-GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
+GetOGMFromStat(struct afs_stat_st *status, int *parm, int *tag)
 {
     *parm = status->st_uid | (status->st_gid << 15);
     *parm |= (status->st_mode & 0x18) << 27;
@@ -718,20 +731,88 @@ GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
 }
 
 static int
-CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
+GetOGM(FdHandle_t *fdP, int *parm, int *tag)
 {
-    struct afs_stat status;
-    int parm, tag;
+    struct afs_stat_st status;
     if (afs_fstat(fdP->fd_fd, &status) < 0)
        return -1;
+    GetOGMFromStat(&status, parm, tag);
+    return 0;
+}
 
-    GetOGMFromStat(&status, &parm, &tag);
+static int
+CheckOGM(FdHandle_t *fdP, int p1)
+{
+    int parm, tag;
+
+    if (GetOGM(fdP, &parm, &tag) < 0)
+       return -1;
     if (parm != p1)
        return -1;
 
     return 0;
 }
-#endif
+
+static int
+FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+    int inode_volid, ogm_volid;
+    int inode_type, ogm_type;
+    Inode ino = fdP->fd_ih->ih_ino;
+
+    inode_volid = ((ino >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
+    inode_type = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
+
+    if (GetOGM(fdP, &ogm_volid, &ogm_type) < 0) {
+       Log("Error retrieving OGM info\n");
+       return -1;
+    }
+
+    if (inode_volid != ogm_volid || inode_type != ogm_type) {
+       Log("%sIncorrect OGM data (ino: vol %u type %d) (ogm: vol %u type %d)\n",
+           check?"":"Fixing ", inode_volid, inode_type, ogm_volid, ogm_type);
+
+       if (check) {
+           return -1;
+       }
+
+       if (SetOGM(fdP->fd_fd, inode_volid, inode_type) < 0) {
+           Log("Error setting OGM data\n");
+           return -1;
+       }
+    }
+    return 0;
+}
+
+#endif /* !AFS_NT40_ENV */
+
+/**
+ * Check/fix the OGM data for an inode
+ *
+ * @param[in] fdP   Open file handle for the inode to check
+ * @param[in] check 1 to just check the OGM data, and return an error if it
+ *                  is incorrect. 0 to fix the OGM data if it is incorrect.
+ *
+ * @pre fdP must be for a special inode
+ *
+ * @return status
+ *  @retval 0 success
+ *  @retval -1 error
+ */
+int
+namei_FixSpecialOGM(FdHandle_t *fdP, int check)
+{
+    int vnode;
+    Inode ino = fdP->fd_ih->ih_ino;
+
+    vnode = (int)(ino & NAMEI_VNODEMASK);
+    if (vnode != NAMEI_VNODESPECIAL) {
+       Log("FixSpecialOGM: non-special vnode %u\n", vnode);
+       return -1;
+    }
+
+    return FixSpecialOGM(fdP, check);
+}
 
 int big_vno = 0;               /* Just in case we ever do 64 bit vnodes. */
 
@@ -749,30 +830,32 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     IHandle_t tmp;
     FdHandle_t *fdP;
     FdHandle_t tfd;
-    int tag, i;
-    FILETIME ftime;
+    int type, tag;
+    int ogm_p1, ogm_p2;
     char *p;
     b32_string_t str1;
 
     memset((void *)&tmp, 0, sizeof(IHandle_t));
     memset(&tfd, 0, sizeof(FdHandle_t));
 
+    ih_PkgDefaults();
+
     tmp.ih_dev = nt_DriveToDev(part);
     if (tmp.ih_dev == -1) {
        errno = EINVAL;
        return -1;
     }
 
-    if (p2 == -1) {
+    if (p2 == INODESPECIAL) {
        /* Parameters for special file:
         * p1 - volume id - goes into owner/group/mode
-        * p2 - vnode == -1
+        * p2 - vnode == INODESPECIAL
         * p3 - type
         * p4 - parent volume id
         */
-        ftime.dwHighDateTime = p1;
-        ftime.dwLowDateTime = p2;
-       tag = p3;
+        ogm_p1 = p1;
+        ogm_p2 = p2;
+       type = p3;
        tmp.ih_vid = p4;        /* Use parent volume id, where this file will be. */
        tmp.ih_ino = namei_MakeSpecIno(p1, p3);
     } else {
@@ -792,37 +875,44 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
 
        tmp.ih_vid = p1;
        tmp.ih_ino = (Inode) p2;
-       ftime.dwHighDateTime = p3;
-        ftime.dwLowDateTime = p4;
+       ogm_p1 = p3;
+        ogm_p2 = p4;
     }
 
     namei_HandleToName(&name, &tmp);
     p = strrchr((char *)&name.n_path, '.');
     p++;
-    for (i = 0; i < NAMEI_MAXVOLS; i++) {
-        *p = *int_to_base32(str1, i);
-        fd = nt_open((char *)&name.n_path, O_CREAT | O_RDWR | O_TRUNC | O_EXCL, 0666);
+    for (tag = 0; tag < NAMEI_MAXVOLS; tag++) {
+        *p = *int_to_base32(str1, tag);
+        fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
+        if (fd == INVALID_FD) {
+            if (errno == ENOTDIR || errno == ENOENT) {
+                if (namei_CreateDataDirectories(&name, &created_dir) == 0)
+                    fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
+            }
+        }
+
         if (fd != INVALID_FD)
             break;
-        if (p2 == -1 && p3 == VI_LINKTABLE)
+        if (p2 == INODESPECIAL && p3 == VI_LINKTABLE)
             break;
     }
     if (fd == INVALID_FD) {
         code = -1;
         goto bad;
     }
-    tmp.ih_ino &= ~((Inode) NAMEI_TAGMASK << NAMEI_TAGSHIFT);
-    tmp.ih_ino |= ((Inode) i << NAMEI_TAGSHIFT);
+    tmp.ih_ino &= ~(((Inode) NAMEI_TAGMASK) << NAMEI_TAGSHIFT);
+    tmp.ih_ino |= (((Inode) tag) << NAMEI_TAGSHIFT);
 
     if (!code) {
-        if (!SetFileTime((HANDLE) fd, &ftime, NULL, NULL)) {
+        if (SetWinOGM(fd, ogm_p1, ogm_p2)) {
            errno = OS_ERROR(EBADF);
             code = -1;
         }
     }
 
     if (!code) {
-        if (p2 != -1) {
+        if (p2 != INODESPECIAL) {
             if (fd == INVALID_FD) {
                 errno = ENOENT;
                 code = nt_unlink((char *)&name.n_path);
@@ -836,7 +926,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
             }
             code = namei_SetLinkCount(fdP, tmp.ih_ino, 1, 0);
             FDH_CLOSE(fdP);
-        } else if (p2 == -1 && p3 == VI_LINKTABLE) {
+        } else if (p2 == INODESPECIAL && p3 == VI_LINKTABLE) {
             if (fd == INVALID_FD)
                 goto bad;
             /* hack at tmp to setup for set link count call. */
@@ -847,10 +937,10 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
 
 bad:
     if (fd != INVALID_FD)
-        nt_close(fd);
+        OS_CLOSE(fd);
 
     if (code || (fd == INVALID_FD)) {
-       if (p2 != -1) {
+       if (p2 != INODESPECIAL) {
             fdP = IH_OPEN(lh);
             if (fdP) {
                 namei_SetLinkCount(fdP, tmp.ih_ino, 0, 0);
@@ -864,24 +954,26 @@ bad:
            errno = save_errno;
        }
     }
-    return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
+    return (code || (fd == INVALID_FD)) ? (Inode) -1 : tmp.ih_ino;
 }
-#else
+#else /* !AFS_NT40_ENV */
 Inode
-namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4,
+        IHandle_t **a_ih)
 {
     namei_t name;
-    int fd = -1;
+    int fd = INVALID_FD;
     int code = 0;
     int created_dir = 0;
     IHandle_t tmp;
+    IHandle_t *realh = NULL;
     FdHandle_t *fdP;
-    FdHandle_t tfd;
     int tag;
     int ogm_parm;
 
     memset((void *)&tmp, 0, sizeof(IHandle_t));
-    memset(&tfd, 0, sizeof(FdHandle_t));
+
+    ih_PkgDefaults();
 
     tmp.ih_dev = volutil_GetPartitionID(part);
     if (tmp.ih_dev == -1) {
@@ -930,39 +1022,40 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
     namei_HandleToName(&name, &tmp);
-    fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
-    if (fd < 0) {
+    fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
+    if (fd == INVALID_FD) {
        if (errno == ENOTDIR || errno == ENOENT) {
            if (namei_CreateDataDirectories(&name, &created_dir) < 0)
                goto bad;
-           fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
+           fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_RDWR,
                          0);
-           if (fd < 0)
+           if (fd == INVALID_FD)
                goto bad;
        } else {
            goto bad;
        }
     }
     if (SetOGM(fd, ogm_parm, tag) < 0) {
-       close(fd);
-       fd = -1;
+       OS_CLOSE(fd);
+       fd = INVALID_FD;
        goto bad;
     }
 
-    if (p2 == -1 && p3 == VI_LINKTABLE) {
-       /* hack at tmp to setup for set link count call. */
-       memset((void *)&tfd, 0, sizeof(FdHandle_t));    /* minimalistic still, but a little cleaner */
-       tfd.fd_ih = &tmp;
-       tfd.fd_fd = fd;
-       code = namei_SetLinkCount(&tfd, (Inode) 0, 1, 0);
-    }
+    IH_INIT(realh, tmp.ih_dev, tmp.ih_vid, tmp.ih_ino);
+    fdP = ih_attachfd(realh, fd);
 
-  bad:
-    if (fd >= 0)
-       close(fd);
+    /* ih_attachfd can only return NULL if we give it an invalid fd; our fd
+     * must be valid by this point. */
+    opr_Assert(fdP);
 
+    if (p2 == (afs_uint32)-1 && p3 == VI_LINKTABLE) {
+       code = namei_SetLinkCount(fdP, (Inode) 0, 1, 0);
+    }
 
-    if (code || (fd < 0)) {
+    FDH_CLOSE(fdP);
+
+  bad:
+    if (code || (fd == INVALID_FD)) {
        if (p2 != -1) {
            fdP = IH_OPEN(lh);
            if (fdP) {
@@ -970,8 +1063,45 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
                FDH_CLOSE(fdP);
            }
        }
+       IH_RELEASE(realh);
     }
-    return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
+
+    *a_ih = realh;
+
+    return code;
+}
+
+Inode
+namei_icreate(IHandle_t * lh, char *part,
+              afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+    Inode ino;
+    IHandle_t *ihP = NULL;
+    int code;
+
+    code = icreate(lh, part, p1, p2, p3, p4, &ihP);
+    if (code || !ihP) {
+       opr_Assert(!ihP);
+       ino = -1;
+    } else {
+       ino = ihP->ih_ino;
+       IH_RELEASE(ihP);
+    }
+    return ino;
+}
+
+IHandle_t *
+namei_icreate_init(IHandle_t * lh, int dev, char *part,
+                   afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+    int code;
+    IHandle_t *ihP = NULL;
+
+    code = icreate(lh, part, p1, p2, p3, p4, &ihP);
+    if (code) {
+       opr_Assert(!ihP);
+    }
+    return ihP;
 }
 #endif
 
@@ -984,7 +1114,7 @@ namei_iopen(IHandle_t * h)
 
     /* Convert handle to file name. */
     namei_HandleToName(&name, h);
-    fd = afs_open((char *)&name.n_path, O_RDWR, 0666);
+    fd = OS_OPEN((char *)&name.n_path, O_RDWR, 0666);
     return fd;
 }
 
@@ -1016,7 +1146,7 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
            return -1;
        }
 
-       if (CheckOGM(&name, fdP, p1) < 0) {
+       if (CheckOGM(fdP, p1) < 0) {
            FDH_REALLYCLOSE(fdP);
            IH_RELEASE(tmp);
            errno = OS_ERROR(EINVAL);
@@ -1032,30 +1162,47 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
            }
 
            count--;
-           if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) <
-               0) {
-               FDH_REALLYCLOSE(fdP);
-               IH_RELEASE(tmp);
-               return -1;
-           }
-
            if (count > 0) {
-               FDH_REALLYCLOSE(fdP);
+               /* if our count is non-zero, we just set our new linkcount and
+                * return. But if our count is 0, don't bother updating the
+                * linktable, since we're about to delete the link table,
+                * below. */
+               if (namei_SetLinkCount(fdP, (Inode) 0, count < 0 ? 0 : count, 1) < 0) {
+                   FDH_REALLYCLOSE(fdP);
+                   IH_RELEASE(tmp);
+                   return -1;
+               }
+
+               FDH_CLOSE(fdP);
                IH_RELEASE(tmp);
                return 0;
            }
+
+           namei_UnlockLinkCount(fdP, (Inode) 0);
        }
 
+       /* We should IH_REALLYCLOSE right before deleting the special file from
+        * disk, to ensure that somebody else cannot create a special inode,
+        * then IH_OPEN that special inode and get back a cached fd for the
+        * file we are deleting here (instead of an fd for the file they just
+        * created). */
+       IH_REALLYCLOSE(tmp);
+       FDH_REALLYCLOSE(fdP);
+       IH_RELEASE(tmp);
+
        if ((code = OS_UNLINK(name.n_path)) == 0) {
            if (type == VI_LINKTABLE) {
                /* Try to remove directory. If it fails, that's ok.
                 * Salvage will clean up.
                 */
+               char *slash = strrchr(name.n_path, OS_DIRSEPC);
+               if (slash) {
+                   /* avoid an rmdir() on the file we just unlinked */
+                   *slash = '\0';
+               }
                (void)namei_RemoveDataDirectories(&name);
            }
        }
-       FDH_REALLYCLOSE(fdP);
-       IH_RELEASE(tmp);
     } else {
        /* Get a file descriptor handle for this Inode */
        fdP = IH_OPEN(ih);
@@ -1077,8 +1224,8 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
        } else {
            IHandle_t *th;
            IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
-           Log("Warning: Lost ref on ihandle dev %d vid %d ino %" AFS_INT64_FMT "\n",
-               th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
+           Log("Warning: Lost ref on ihandle dev %d vid %" AFS_VOLID_FMT " ino %lld\n",
+               th->ih_dev, afs_printable_VolumeId_lu(th->ih_vid), (afs_int64)th->ih_ino);
            IH_RELEASE(th);
 
            /* If we're less than 0, someone presumably unlinked;
@@ -1162,17 +1309,18 @@ namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
 int
 namei_copy_on_write(IHandle_t *h)
 {
-    afs_int32 fd, code = 0;
+    afs_int32 code = 0;
+    FD_t fd;
     namei_t name;
     FdHandle_t *fdP;
-    struct afs_stat tstat;
+    struct afs_stat_st tstat;
     afs_foff_t offset;
 
     namei_HandleToName(&name, h);
     if (afs_stat(name.n_path, &tstat) < 0)
        return EIO;
     if (tstat.st_nlink > 1) {                   /* do a copy on write */
-       char path[259];
+       char path[NAMEI_PATH_LEN + 4];
        char *buf;
        afs_size_t size;
        ssize_t tlen;
@@ -1180,15 +1328,15 @@ namei_copy_on_write(IHandle_t *h)
        fdP = IH_OPEN(h);
        if (!fdP)
            return EIO;
-       afs_snprintf(path, sizeof(path), "%s-tmp", name.n_path);
-       fd = afs_open(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
-       if (fd < 0) {
+       snprintf(path, sizeof(path), "%s-tmp", name.n_path);
+       fd = OS_OPEN(path, O_CREAT | O_EXCL | O_RDWR, 0);
+       if (fd == INVALID_FD) {
            FDH_CLOSE(fdP);
            return EIO;
        }
        buf = malloc(8192);
        if (!buf) {
-           close(fd);
+           OS_CLOSE(fd);
            OS_UNLINK(path);
            FDH_CLOSE(fdP);
            return ENOMEM;
@@ -1199,12 +1347,12 @@ namei_copy_on_write(IHandle_t *h)
            tlen = size > 8192 ? 8192 : size;
            if (FDH_PREAD(fdP, buf, tlen, offset) != tlen)
                break;
-           if (write(fd, buf, tlen) != tlen)
+           if (OS_WRITE(fd, buf, tlen) != tlen)
                break;
            size -= tlen;
            offset += tlen;
        }
-       close(fd);
+       OS_CLOSE(fd);
        FDH_REALLYCLOSE(fdP);
        free(buf);
        if (size)
@@ -1366,8 +1514,8 @@ namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
 #ifdef AFS_PTHREAD_ENV
 /* XXX do static initializers work for WINNT/pthread? */
 pthread_mutex_t _namei_glc_lock = PTHREAD_MUTEX_INITIALIZER;
-#define NAMEI_GLC_LOCK MUTEX_ENTER(&_namei_glc_lock)
-#define NAMEI_GLC_UNLOCK MUTEX_EXIT(&_namei_glc_lock)
+#define NAMEI_GLC_LOCK opr_mutex_enter(&_namei_glc_lock)
+#define NAMEI_GLC_UNLOCK opr_mutex_exit(&_namei_glc_lock)
 #else /* !AFS_PTHREAD_ENV */
 #define NAMEI_GLC_LOCK
 #define NAMEI_GLC_UNLOCK
@@ -1410,6 +1558,9 @@ namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite
     }
 
     rc = FDH_PREAD(h, (char*)&row, sizeof(row), offset);
+    if (rc == -1)
+       goto bad_getLinkByte;
+
     if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
         return 1;
     if (rc == 0 && fixup) {
@@ -1424,7 +1575,10 @@ namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite
            NAMEI_GLC_UNLOCK;
            goto bad_getLinkByte;
        }
-        FDH_TRUNC(h, offset+sizeof(row));
+        if (FDH_TRUNC(h, offset+sizeof(row))) {
+           NAMEI_GLC_UNLOCK;
+           goto bad_getLinkByte;
+       }
         row = 1 << index;
        rc = FDH_PWRITE(h, (char *)&row, sizeof(row), offset);
        NAMEI_GLC_UNLOCK;
@@ -1459,12 +1613,6 @@ namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite
     return -1;
 }
 
-int
-namei_SetNonZLC(FdHandle_t * h, Inode ino)
-{
-    return namei_GetLinkCount(h, ino, 0, 1, 0);
-}
-
 /* Return a free column index for this vnode. */
 static int
 GetFreeTag(IHandle_t * ih, int vno)
@@ -1481,14 +1629,14 @@ GetFreeTag(IHandle_t * ih, int vno)
     if (fdP == NULL)
        return -1;
 
+    offset = (vno << LINKTABLE_SHIFT) + 8;     /* * 2 + sizeof stamp */
+
     /* Only one manipulates at a time. */
     if (FDH_LOCKFILE(fdP, offset) != 0) {
        FDH_REALLYCLOSE(fdP);
        return -1;
     }
 
-    offset = (vno << LINKTABLE_SHIFT) + 8;     /* * 2 + sizeof stamp */
-
     nBytes = FDH_PREAD(fdP, (char *)&row, sizeof(row), offset);
     if (nBytes != sizeof(row)) {
        if (nBytes != 0)
@@ -1513,9 +1661,9 @@ GetFreeTag(IHandle_t * ih, int vno)
     if (FDH_PWRITE(fdP, (char *)&row, sizeof(row), offset) != sizeof(row)) {
        goto badGetFreeTag;
     }
-    FDH_SYNC(fdP);
+    (void)FDH_SYNC(fdP);
     FDH_UNLOCKFILE(fdP, offset);
-    FDH_REALLYCLOSE(fdP);
+    FDH_CLOSE(fdP);
     return col;
 
   badGetFreeTag:
@@ -1565,7 +1713,7 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
        errno = OS_ERROR(EBADF);
        goto bad_SetLinkCount;
     }
-    FDH_SYNC(fdP);
+    (void)FDH_SYNC(fdP);
 
     nBytes = 0;
 
@@ -1577,18 +1725,29 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
     return (int)nBytes;
 }
 
+static void
+namei_UnlockLinkCount(FdHandle_t * fdP, Inode ino)
+{
+    afs_foff_t offset;
+    int index;
+
+    namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
+
+    FDH_UNLOCKFILE(fdP, offset);
+}
+
 
 /* ListViceInodes - write inode data to a results file. */
 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-                      unsigned int volid);
-static int DecodeVolumeName(char *name, unsigned int *vid);
+                      IHandle_t *myIH);
+static int DecodeVolumeName(char *name, VolumeId *vid);
 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
-                               int (*write_fun) (FILE *,
+                               int (*write_fun) (FD_t,
                                                  struct ViceInodeInfo *,
-                                                 char *, char *), FILE * fp,
+                                                 char *, char *), FD_t fp,
                                int (*judgeFun) (struct ViceInodeInfo *,
-                                                afs_uint32 vid, void *),
-                               afs_uint32 singleVolumeNumber, void *rock);
+                                                VolumeId vid, void *),
+                               VolumeId singleVolumeNumber, void *rock);
 
 
 /* WriteInodeInfo
@@ -1601,11 +1760,11 @@ static int namei_ListAFSSubDirs(IHandle_t * dirIH,
  * can use the same inode reading code.
  */
 static int
-WriteInodeInfo(FILE * fp, struct ViceInodeInfo *info, char *dir, char *name)
+WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
 {
     size_t n;
-    n = fwrite(info, sizeof(*info), 1, fp);
-    return (n == 1) ? 0 : -2;
+    n = OS_WRITE(fp, info, sizeof(*info));
+    return (n == sizeof(*info)) ? 0 : -2;
 }
 
 
@@ -1613,7 +1772,7 @@ int mode_errors;          /* Number of errors found in mode bits on directories. */
 void
 VerifyDirPerms(char *path)
 {
-    struct afs_stat status;
+    struct afs_stat_st status;
 
     if (afs_stat(path, &status) < 0) {
        Log("Unable to stat %s. Please manually verify mode bits for this"
@@ -1651,13 +1810,12 @@ VerifyDirPerms(char *path)
  *                for this.
  */
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
-              int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
-              afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
+              int (*judgeInode) (struct ViceInodeInfo * info, VolumeId vid, void *rock),
+              VolumeId singleVolumeNumber, int *forcep, int forceR, char *wpath,
               void *rock)
 {
     int ninodes;
-    struct afs_stat status;
 
     *forcep = 0; /* no need to salvage until further notice */
 
@@ -1669,18 +1827,14 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
        namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
                           singleVolumeNumber, rock);
 
-    if (!inodeFile)
+    if (inodeFile == INVALID_FD)
        return ninodes;
 
     if (ninodes < 0) {
        return ninodes;
     }
 
-    if (fflush(inodeFile) == EOF) {
-       Log("Unable to successfully flush inode file for %s\n", mountedOn);
-       return -2;
-    }
-    if (fsync(fileno(inodeFile)) == -1) {
+    if (OS_SYNC(inodeFile) == -1) {
        Log("Unable to successfully fsync inode file for %s\n", mountedOn);
        return -2;
     }
@@ -1688,13 +1842,9 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
     /*
      * Paranoia:  check that the file is really the right size
      */
-    if (afs_fstat(fileno(inodeFile), &status) == -1) {
-       Log("Unable to successfully stat inode file for %s\n", mountedOn);
-       return -2;
-    }
-    if (status.st_size != ninodes * sizeof(struct ViceInodeInfo)) {
+    if (OS_SIZE(inodeFile) != ninodes * sizeof(struct ViceInodeInfo)) {
        Log("Wrong size (%d instead of %lu) in inode file for %s\n",
-           (int) status.st_size,
+           (int) OS_SIZE(inodeFile),
            (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
            mountedOn);
        return -2;
@@ -1725,11 +1875,11 @@ ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
  */
 int
 namei_ListAFSFiles(char *dev,
-                  int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                  int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
-                  FILE * fp,
-                  int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                  afs_uint32 singleVolumeNumber, void *rock)
+                  FD_t fp,
+                  int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                  VolumeId singleVolumeNumber, void *rock)
 {
     IHandle_t ih;
     namei_t name;
@@ -1778,8 +1928,8 @@ namei_ListAFSFiles(char *dev,
 #else
            if (*dp1->d_name == '.')
                continue;
-           afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
-                        dp1->d_name);
+           snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", name.n_path,
+                    dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -1842,55 +1992,54 @@ _namei_examine_special(char * path1,
                       char * dname,
                       IHandle_t * myIH,
                       FdHandle_t * linkHandle,
-                      int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                      int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                        char *),
-                      FILE * fp,
-                      int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                      int singleVolumeNumber,
+                      FD_t fp,
+                      int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                      VolumeId singleVolumeNumber,
                       void *rock)
 {
     int ret = 0;
     struct ViceInodeInfo info;
-    afs_uint32 inode_vgid;
 
-    if (DecodeInode(path1, dname, &info, myIH->ih_vid) < 0) {
+    if (DecodeInode(path1, dname, &info, myIH) < 0) {
        ret = 0;
        goto error;
     }
 
-#ifdef AFS_NT40_ENV
-    inode_vgid = myIH->ih_vid;
-#else
-    inode_vgid = (info.inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK;
-#endif
-
     if (info.u.param[2] != VI_LINKTABLE) {
        info.linkCount = 1;
-    } else if ((info.u.param[0] != myIH->ih_vid) ||
-              (inode_vgid != myIH->ih_vid)) {
+    } else if (info.u.param[0] != myIH->ih_vid) {
        /* VGID encoded in linktable filename and/or OGM data isn't
         * consistent with VGID encoded in namei path */
        Log("namei_ListAFSSubDirs: warning: inconsistent linktable "
            "filename \"%s" OS_DIRSEP "%s\"; salvager will delete it "
-           "(dir_vgid=%u, inode_vgid=%u, ogm_vgid=%u)\n",
-           path1, dname, myIH->ih_vid,
-           (unsigned int)inode_vgid,
-           info.u.param[0]);
+           "(dir_vgid=%" AFS_VOLID_FMT ", inode_vgid=%" AFS_VOLID_FMT ")\n",
+           path1, dname, afs_printable_VolumeId_lu(myIH->ih_vid),
+           afs_printable_VolumeId_lu(info.u.param[0]));
+       /* We need to set the linkCount to _something_, so linkCount
+        * doesn't just contain stack garbage. Set it to 0, so in case
+        * the salvager or whatever our caller is does try to process
+        * this like a normal file, we won't try to INC or DEC it. */
+       info.linkCount = 0;
     } else {
        char path2[512];
        /* Open this handle */
-       (void)afs_snprintf(path2, sizeof(path2),
-                          "%s" OS_DIRSEP "%s", path1, dname);
-       linkHandle->fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
+       snprintf(path2, sizeof(path2),
+                "%s" OS_DIRSEP "%s", path1, dname);
+       linkHandle->fd_fd = OS_OPEN(path2, Testing ? O_RDONLY : O_RDWR, 0666);
        info.linkCount =
            namei_GetLinkCount(linkHandle, (Inode) 0, 1, 1, Testing);
     }
 
     if (!judgeFun ||
        (*judgeFun) (&info, singleVolumeNumber, rock)) {
-       ret = 1;
-        if ((*writeFun) (fp, &info, path1, dname) < 0) {
+       ret = (*writeFun) (fp, &info, path1, dname);
+       if (ret < 0) {
+           Log("_namei_examine_special: writeFun returned %d\n", ret);
            ret = -1;
+       } else {
+           ret = 1;
        }
     }
 
@@ -1901,7 +2050,7 @@ _namei_examine_special(char * path1,
 /**
  * examine a namei file.
  *
- * @param[in] path1               volume special directory path
+ * @param[in] path3               volume special directory path
  * @param[in] dname               directory entry name
  * @param[in] myIH                inode handle to volume directory
  * @param[in] linkHandle          namei link count fd handle.
@@ -1931,20 +2080,20 @@ _namei_examine_reg(char * path3,
                   char * dname,
                   IHandle_t * myIH,
                   FdHandle_t * linkHandle,
-                  int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                  int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
-                  FILE * fp,
-                  int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                  int singleVolumeNumber,
+                  FD_t fp,
+                  int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                  VolumeId singleVolumeNumber,
                   void *rock)
 {
     int ret = 0;
     struct ViceInodeInfo info;
 #ifdef DELETE_ZLC
-    int i; /* Windows-only (one level hash dir) */
+    int dirl; /* Windows-only (one level hash dir) */
 #endif
 
-    if (DecodeInode(path3, dname, &info, myIH->ih_vid) < 0) {
+    if (DecodeInode(path3, dname, &info, myIH) < 0) {
        goto error;
     }
 
@@ -1958,7 +2107,8 @@ _namei_examine_reg(char * path3,
        /* defer -- the AddToZLCDeleteList() interface is not MT-safe */
        ret = -2;
 #else /* !AFS_SALSRV_ENV */
-       AddToZLCDeleteList((char)i, dname);
+        dirl = path3[strlen(path3)-1];
+       AddToZLCDeleteList((char)dirl, dname);
 #endif /* !AFS_SALSRV_ENV */
 #else /* !DELETE_ZLC */
        Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path3,
@@ -1969,9 +2119,12 @@ _namei_examine_reg(char * path3,
 
     if (!judgeFun ||
        (*judgeFun) (&info, singleVolumeNumber, rock)) {
-       ret = 1;
-        if ((*writeFun) (fp, &info, path3, dname) < 0) {
+       ret = (*writeFun) (fp, &info, path3, dname);
+       if (ret < 0) {
+           Log("_namei_examine_reg: writeFun returned %d\n", ret);
            ret = -1;
+       } else {
+           ret = 1;
        }
     }
 
@@ -1995,14 +2148,14 @@ struct listsubdirs_work_node {
                                          *   inode, this will be pointed at the
                                          *   link table
                                          */
-    FILE * fp;                          /**< file pointer for writeFun */
+    FD_t fp;                            /**< file pointer for writeFun */
 
     /** function which will write inode metadata to fp */
-    int (*writeFun) (FILE *, struct ViceInodeInfo *, char *, char *);
+    int (*writeFun) (FD_t, struct ViceInodeInfo *, char *, char *);
 
     /** inode filter function */
-    int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *);
-    int singleVolumeNumber;             /**< volume id filter */
+    int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *);
+    VolumeId singleVolumeNumber;             /**< volume id filter */
     void * rock;                        /**< pointer passed to writeFun and judgeFun */
     int code;                           /**< return code from examine function */
     int special;                        /**< asserted when this is a volume
@@ -2171,7 +2324,7 @@ static pthread_key_t wq_key;
 static void
 _namei_wq_keycreate(void)
 {
-    osi_Assert(pthread_key_create(&wq_key, NULL) == 0);
+    opr_Verify(pthread_key_create(&wq_key, NULL) == 0);
 }
 
 /**
@@ -2185,9 +2338,9 @@ _namei_wq_keycreate(void)
 void
 namei_SetWorkQueue(struct afs_work_queue *wq)
 {
-    osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
+    opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
 
-    osi_Assert(pthread_setspecific(wq_key, wq) == 0);
+    opr_Verify(pthread_setspecific(wq_key, wq) == 0);
 }
 
 /**
@@ -2325,18 +2478,19 @@ _namei_examine_file_spawn(const struct listsubdirs_work_node *work,
  */
 static int
 namei_ListAFSSubDirs(IHandle_t * dirIH,
-                    int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
+                    int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                      char *),
-                    FILE * fp,
-                    int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                    afs_uint32 singleVolumeNumber, void *rock)
+                    FD_t fp,
+                    int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                    VolumeId singleVolumeNumber, void *rock)
 {
     int code = 0, ret = 0;
     IHandle_t myIH = *dirIH;
     namei_t name;
     char path1[512], path3[512];
-    DIR *dirp1, *dirp2, *dirp3;
+    DIR *dirp1, *dirp3;
 #ifndef AFS_NT40_ENV
+    DIR *dirp2;
     struct dirent *dp2;
     char path2[512];
 #endif
@@ -2350,11 +2504,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
     int wq_up = 0;
     struct rx_queue resultlist;
 #endif
-#ifdef DELETE_ZLC
-    int i;
-    static void AddToZLCDeleteList(char dir, char *name);
-    static void DeleteZLCFiles(char *path);
-#endif
+
+    memset(&linkHandle, 0, sizeof(linkHandle));
 
     namei_HandleToVolDir(&name, &myIH);
     strlcpy(path1, name.n_path, sizeof(path1));
@@ -2367,7 +2518,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 
     linkHandle.fd_fd = INVALID_FD;
 #ifdef AFS_SALSRV_ENV
-    osi_Assert(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
+    opr_Verify(pthread_once(&wq_once, _namei_wq_keycreate) == 0);
 
     wq = pthread_getspecific(wq_key);
     if (!wq) {
@@ -2437,8 +2588,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 #endif
 
     if (linkHandle.fd_fd == INVALID_FD) {
-       Log("namei_ListAFSSubDirs: warning: VG %u does not have a link table; "
-           "salvager will recreate it.\n", dirIH->ih_vid);
+       Log("namei_ListAFSSubDirs: warning: VG %" AFS_VOLID_FMT " does not have a link table; "
+           "salvager will recreate it.\n", afs_printable_VolumeId_lu(dirIH->ih_vid));
     }
 
     /* Now run through all the other subdirs */
@@ -2459,7 +2610,14 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 
 #ifndef AFS_NT40_ENV /* This level missing on Windows */
            /* Now we've got a next level subdir. */
-           afs_snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s", path1, dp1->d_name);
+           code = snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
+                           path1, dp1->d_name);
+           if (code < 0 || code >= sizeof(path2)) {
+               /* error, or truncated */
+               closedir(dirp1);
+               ret = -1;
+               goto error;
+           }
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -2467,13 +2625,22 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                        continue;
 
                    /* Now we've got to the actual data */
-                   afs_snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s", path2,
-                                dp2->d_name);
+                   code = snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                                   path2, dp2->d_name);
 #else
                    /* Now we've got to the actual data */
-                   afs_snprintf(path3, sizeof(path3), "%s\\%s", path1,
-                                dp1->d_name);
+                   code = snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                                   path1, dp1->d_name);
+#endif
+                   if (code < 0 || code >= sizeof(path3)) {
+                       /* error, or truncated */
+#ifndef AFS_NT40_ENV
+                       closedir(dirp2);
 #endif
+                       closedir(dirp1);
+                       ret = -1;
+                       goto error;
+                   }
                    dirp3 = opendir(path3);
                    if (dirp3) {
                        while ((dp3 = readdir(dirp3))) {
@@ -2485,7 +2652,9 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 #ifdef AFS_SALSRV_ENV
                            if (error) {
                                closedir(dirp3);
+#ifndef AFS_NT40_ENV
                                closedir(dirp2);
+#endif
                                closedir(dirp1);
                                ret = -1;
                                goto error;
@@ -2499,7 +2668,9 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                            switch (code) {
                            case -1:
                                closedir(dirp3);
+#ifndef AFS_NT40_ENV
                                closedir(dirp2);
+#endif
                                closedir(dirp1);
                                ret = -1;
                                goto error;
@@ -2560,7 +2731,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 
 #ifdef AFS_NT40_ENV
 static int
-DecodeVolumeName(char *name, unsigned int *vid)
+DecodeVolumeName(char *name, VolumeId *vid)
 {
     /* Name begins with "Vol_" and ends with .data.  See nt_HandleToVolDir() */
     char stmp[32];
@@ -2580,7 +2751,7 @@ DecodeVolumeName(char *name, unsigned int *vid)
 }
 #else
 static int
-DecodeVolumeName(char *name, unsigned int *vid)
+DecodeVolumeName(char *name, VolumeId *vid)
 {
     if (strlen(name) < 1)
        return -1;
@@ -2598,7 +2769,7 @@ DecodeVolumeName(char *name, unsigned int *vid)
 #ifdef AFS_NT40_ENV
 static int
 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-           unsigned int volid)
+           IHandle_t *myIH)
 {
     char fpath[512];
     int tag, vno;
@@ -2608,8 +2779,11 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     char stmp[16];
     FdHandle_t linkHandle;
     char dirl;
+    VolumeId volid = myIH->ih_vid;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s\\%s", dpath, name);
+    memset(&linkHandle, 0, sizeof(linkHandle));
+
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
                           FindExSearchNameMatch, NULL,
@@ -2627,26 +2801,26 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
         return -1;
 
     *t = '\0';
-    vno = base32_to_int(s);
+    vno = base32_to_int(s);     /* type for special files */
     tag = base32_to_int(t+1);
-    info->inodeNumber = (Inode) tag << NAMEI_TAGSHIFT;
-    info->inodeNumber |= (Inode) vno;
+    info->inodeNumber = ((Inode) tag) << NAMEI_TAGSHIFT;
+    info->inodeNumber |= vno;
     info->byteCount = data.nFileSizeLow;
 
     dirl = dpath[strlen(dpath)-1];
-    if (dirl == 'R') { /* Special inode. */
+    if (dirl == NAMEI_SPECDIRC) { /* Special inode. */
        info->inodeNumber |= NAMEI_INODESPECIAL;
        info->u.param[0] = data.ftCreationTime.dwHighDateTime;
        info->u.param[1] = data.ftCreationTime.dwLowDateTime;
-       info->u.param[2] = tag;
+       info->u.param[2] = vno; /* type */
        info->u.param[3] = volid;
-       if (tag != VI_LINKTABLE)
+       if (vno != VI_LINKTABLE)
            info->linkCount = 1;
        else {
            /* Open this handle */
            char lpath[1024];
-           (void)sprintf(lpath, "%s\\%s", fpath, data.cFileName);
-           linkHandle.fd_fd = nt_open(lpath, O_RDONLY, 0666);
+           (void)sprintf(lpath, "%s" OS_DIRSEP "%s", fpath, data.cFileName);
+           linkHandle.fd_fd = OS_OPEN(lpath, O_RDONLY, 0666);
            info->linkCount =
                namei_GetLinkCount(&linkHandle, (Inode) 0, 0, 0, 0);
        }
@@ -2655,11 +2829,11 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
            namei_GetLinkCount(&linkHandle, info->inodeNumber, 0, 0, 0);
        if (info->linkCount == 0) {
 #ifdef DELETE_ZLC
-           Log("Found 0 link count file %s\\%s, deleting it.\n",
+           Log("Found 0 link count file %s" OS_DIRSEP "%s, deleting it.\n",
                fpath, data.cFileName);
            AddToZLCDeleteList(dirl, data.cFileName);
 #else
-           Log("Found 0 link count file %s\\%s.\n", path,
+           Log("Found 0 link count file %s" OS_DIRSEP "%s.\n", path,
                data.cFileName);
 #endif
        } else {
@@ -2674,14 +2848,18 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
 #else
 static int
 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-           unsigned int volid)
+           IHandle_t *myIH)
 {
     char fpath[512];
-    struct afs_stat status;
+    struct afs_stat_st status;
+    struct afs_stat_st checkstatus;
     int parm, tag;
     lb64_string_t check;
+    VolumeId volid = myIH->ih_vid;
+    IHandle_t tmpih;
+    namei_t nameiname;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     if (afs_stat(fpath, &status) < 0) {
        return -1;
@@ -2694,14 +2872,47 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     if (strcmp(name, check))
        return -1;
 
-    GetOGMFromStat(&status, &parm, &tag);
+    /* Check if the _full_ path is correct, to ensure we can actually open this
+     * file later. Otherwise, the salvager can choke. */
+    memset(&tmpih, 0, sizeof(tmpih));
+    tmpih.ih_dev = myIH->ih_dev;
+    tmpih.ih_vid = myIH->ih_vid;
+    tmpih.ih_ino = info->inodeNumber;
+    namei_HandleToName(&nameiname, &tmpih);
+    if ((afs_stat(nameiname.n_path, &checkstatus) < 0) ||
+        checkstatus.st_ino != status.st_ino ||
+        checkstatus.st_size != status.st_size) {
+       static int logged;
+       /* log something for this case, since this means the filename looks
+        * like a valid inode, but it's just in the wrong place. That's pretty
+        * strange. */
+       if (!logged) {
+           logged = 1;
+           Log("Note:\n");
+           Log("  Seemingly-misplaced files have been found, which I am\n");
+           Log("  ignoring for now. If you cannot salvage the relevant volume,\n");
+           Log("  you may try manually moving them to their correct location.\n");
+           Log("  If the relevant volume seems fine, and these files do not\n");
+           Log("  appear to contain important data, you can probably manually\n");
+           Log("  delete them, or leave them alone. Contact your local OpenAFS\n");
+           Log("  expert if you are unsure.\n");
+       }
+       Log("Ignoring misplaced file in volume group %u: %s (should be %s)\n",
+           (unsigned)myIH->ih_vid, fpath, nameiname.n_path);
+       return -1;
+    }
+
     if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
+       parm = ((info->inodeNumber >> NAMEI_UNIQSHIFT) & NAMEI_UNIQMASK);
+       tag = (int)((info->inodeNumber >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
+
        /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
        info->u.param[0] = parm;
        info->u.param[1] = -1;
        info->u.param[2] = tag;
        info->u.param[3] = volid;
     } else {
+       GetOGMFromStat(&status, &parm, &tag);
        /* p1 - vid, p2 - vno, p3 - uniq, p4 - dv */
        info->u.param[0] = volid;
        info->u.param[1] = (int)(info->inodeNumber & NAMEI_VNODEMASK);
@@ -2720,15 +2931,15 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
 
 #ifdef FSSYNC_BUILD_CLIENT
 static afs_int32
-convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
+convertVolumeInfo(FD_t fdr, FD_t fdw, VolumeId vid)
 {
     struct VolumeDiskData vd;
     char *p;
 
-    if (OS_READ(fdr, (char *)&vd, sizeof(struct VolumeDiskData)) !=
+    if (OS_READ(fdr, &vd, sizeof(struct VolumeDiskData)) !=
        sizeof(struct VolumeDiskData)) {
-       Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
-           afs_printable_uint32_lu(vid),
+       Log("1 convertVolumeInfo: read failed for %" AFS_VOLID_FMT " with code %d\n",
+           afs_printable_VolumeId_lu(vid),
            errno);
        return -1;
     }
@@ -2740,11 +2951,25 @@ convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
     vd.inUse = 0;
     vd.uniquifier += 5000;     /* just in case there are still file copies from
                                 * the old RW volume around */
+
+    /* For ROs, the copyDate contains the time that the RO volume was actually
+     * created, and the creationDate just contains the last time the RO was
+     * copied from the RW data. So, make the new RW creationDate more accurate
+     * by setting it to copyDate, if copyDate is older. Since, we know the
+     * volume is at least as old as copyDate. */
+    if (vd.copyDate < vd.creationDate) {
+       vd.creationDate = vd.copyDate;
+    } else {
+       /* If copyDate is newer, just make copyDate and creationDate the same,
+        * for consistency with other RWs */
+       vd.copyDate = vd.creationDate;
+    }
+
     p = strrchr(vd.name, '.');
     if (p && !strcmp(p, ".readonly")) {
        memset(p, 0, 9);
     }
-    if (OS_WRITE(fdw, (char *)&vd, sizeof(struct VolumeDiskData)) !=
+    if (OS_WRITE(fdw, &vd, sizeof(struct VolumeDiskData)) !=
        sizeof(struct VolumeDiskData)) {
        Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
            afs_printable_uint32_lu(vid),
@@ -2776,7 +3001,7 @@ convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 vid)
  */
 
 int
-namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
+namei_ConvertROtoRWvolume(char *pname, VolumeId volumeId)
 {
     int code = 0;
 #ifdef FSSYNC_BUILD_CLIENT
@@ -2799,6 +3024,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     struct DiskPartition64 *partP;
     struct ViceInodeInfo info;
     struct VolumeDiskHeader h;
+    char *rwpart, *rwname;
+    Error ec;
 # ifdef AFS_DEMAND_ATTACH_FS
     int locktype = 0;
 # endif /* AFS_DEMAND_ATTACH_FS */
@@ -2828,6 +3055,19 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
        goto done;
     }
 
+    /* check for existing RW on any partition on this server; */
+    /* if found, return EXDEV - invalid cross-device link */
+    VOL_LOCK;
+    VGetVolumePath(&ec, h.parent, &rwpart, &rwname);
+    if (ec == 0) {
+       Log("1 namei_ConvertROtoRWvolume: RW volume %lu already exists on server partition %s.\n",
+           afs_printable_uint32_lu(h.parent), rwpart);
+       code = EXDEV;
+       VOL_UNLOCK;
+       goto done;
+    }
+    VOL_UNLOCK;
+
     FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
 
     ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
@@ -2850,7 +3090,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
        if (*dp->d_name == '.')
            continue;
 #endif
-       if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
+       if (DecodeInode(dir_name, dp->d_name, &info, ih) < 0) {
            Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s" OS_DIRSEP "%s\n",
                dir_name, dp->d_name);
            closedir(dirp);
@@ -2888,7 +3128,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
            largeSeen = 1;
        } else {
            closedir(dirp);
-           Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s" OS_DIRSEP "%s\n", info.u.param[2], dir_name, dp->d_name);
+           Log("1 namei_ConvertROtoRWvolume: unknown type %u of special file found : %s" OS_DIRSEP "%s\n", info.u.param[2], dir_name, dp->d_name);
            code = -1;
            goto done;
        }
@@ -2910,10 +3150,15 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     t_ih.ih_dev = ih->ih_dev;
     t_ih.ih_vid = ih->ih_vid;
 
-    (void)afs_snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
-                      infoName);
-    fd = afs_open(oldpath, O_RDWR, 0);
-    if (fd < 0) {
+    code = snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
+                   infoName);
+    if (code < 0 || code >= sizeof(oldpath)) {
+       /* error, or truncated */
+       code = -1;
+       goto done;
+    }
+    fd = OS_OPEN(oldpath, O_RDWR, 0);
+    if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
            oldpath);
        code = -1;
@@ -2921,8 +3166,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     }
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
     namei_HandleToName(&n, &t_ih);
-    fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
-    if (fd2 < 0) {
+    fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_RDWR, 0);
+    if (fd2 == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
        OS_CLOSE(fd);
        code = -1;
@@ -2941,10 +3186,15 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
     namei_HandleToName(&n, &t_ih);
-    (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
-                      smallName);
-    fd = afs_open(newpath, O_RDWR, 0);
-    if (fd < 0) {
+    code = snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+                   smallName);
+    if (code < 0 || code >= sizeof(newpath)) {
+       /* error, or truncated */
+       code = -1;
+       goto done;
+    }
+    fd = OS_OPEN(newpath, O_RDWR, 0);
+    if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
        code = -1;
        goto done;
@@ -2954,16 +3204,25 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 #ifdef AFS_NT40_ENV
     MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
 #else
-    link(newpath, n.n_path);
+    if (link(newpath, n.n_path)) {
+       Log("1 namei_ConvertROtoRWvolume: could not move SmallIndex file: %s\n", n.n_path);
+       code = -1;
+       goto done;
+    }
     OS_UNLINK(newpath);
 #endif
 
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
     namei_HandleToName(&n, &t_ih);
-    (void)afs_snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
-                      largeName);
-    fd = afs_open(newpath, O_RDWR, 0);
-    if (fd < 0) {
+    code = snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+                   largeName);
+    if (code < 0 || code >= sizeof(newpath)) {
+       /* error, or truncated */
+       code = -1;
+       goto done;
+    }
+    fd = OS_OPEN(newpath, O_RDWR, 0);
+    if (fd == INVALID_FD) {
        Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
        code = -1;
        goto done;
@@ -2973,7 +3232,11 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 #ifdef AFS_NT40_ENV
     MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
 #else
-    link(newpath, n.n_path);
+    if (link(newpath, n.n_path)) {
+       Log("1 namei_ConvertROtoRWvolume: could not move LargeIndex file: %s\n", n.n_path);
+       code = -1;
+       goto done;
+    }
     OS_UNLINK(newpath);
 #endif
 
@@ -3000,6 +3263,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
     FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
 
+    code = 0;
  done:
 # ifdef AFS_DEMAND_ATTACH_FS
     if (locktype) {
@@ -3022,7 +3286,7 @@ PrintInode(char *s, Inode ino)
     if (!s)
        s = result;
 
-    (void)afs_snprintf(s, sizeof(afs_ino_str_t), "%" AFS_UINT64_FMT, (afs_uintmax_t) ino);
+    snprintf(s, sizeof(afs_ino_str_t), "%llu", (afs_uintmax_t) ino);
 
     return s;
 }
@@ -3044,13 +3308,13 @@ static zlcList_t *zlcCur = NULL;
 static void
 AddToZLCDeleteList(char dir, char *name)
 {
-    osi_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
+    opr_Assert(strlen(name) <= MAX_ZLC_NAMELEN - 3);
 
     if (!zlcCur || zlcCur->zlc_n >= MAX_ZLC_NAMES) {
        if (zlcCur && zlcCur->zlc_next)
            zlcCur = zlcCur->zlc_next;
        else {
-           zlcList_t *tmp = (zlcList_t *) malloc(sizeof(zlcList_t));
+           zlcList_t *tmp = malloc(sizeof(zlcList_t));
            if (!tmp)
                return;
            if (!zlcAnchor) {
@@ -3065,7 +3329,7 @@ AddToZLCDeleteList(char dir, char *name)
     }
 
     if (dir)
-       (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c\\%s", dir, name);
+       (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%c" OS_DIRSEP "%s", dir, name);
     else
        (void)sprintf(zlcCur->zlc_names[zlcCur->zlc_n], "%s", name);
 
@@ -3082,7 +3346,7 @@ DeleteZLCFiles(char *path)
     for (z = zlcAnchor; z; z = z->zlc_next) {
        for (i = 0; i < z->zlc_n; i++) {
            if (path)
-               (void)sprintf(fname, "%s\\%s", path, z->zlc_names[i]);
+               (void)sprintf(fname, "%s" OS_DIRSEP "%s", path, z->zlc_names[i]);
            else
                (void)sprintf(fname, "%s", z->zlc_names[i]);
            if (namei_unlink(fname) < 0) {
@@ -3111,4 +3375,37 @@ FreeZLCList(void)
 }
 #endif
 
+/**
+ * Remove empty directories associated with the volume received
+ * as an argument.
+ *
+ * @param[in] pname    vice partition path
+ * @param[in] vid      volume id
+ *
+ * @return 0 on success
+ */
+int
+namei_RemoveDirectories(char *pname, afs_int32 vid)
+{
+    IHandle_t dirIH;
+    namei_t name;
+
+    memset(&dirIH, 0, sizeof(dirIH));
+
+    dirIH.ih_vid = vid;
+
+#ifdef AFS_NT40_ENV
+    dirIH.ih_dev = nt_DriveToDev(pname);
+#else
+    dirIH.ih_dev = volutil_GetPartitionID(pname);
+#endif
+    if (dirIH.ih_dev == -1) {
+       return -1;
+    }
+
+    namei_HandleToVolDir(&name, &dirIH);
+
+    return namei_RemoveDataDirectories(&name);
+}
+
 #endif /* AFS_NAMEI_ENV */