vice & vol: Cast FDH_SYNC results to void
[openafs.git] / src / vol / namei_ops.c
index 897c5e7..cd749c2 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 <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 */
 #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;
 
 
@@ -137,7 +77,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,44 +95,46 @@ 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.
- * 32-36 - unquifier tag and index into counts array for this vnode. Only
+ * low 32 bits - if a regular file or directory, the vnode; else the type.
+ * 32-36 - uniquifier tag and index into counts array for this vnode. Only
  *         two of the available bits are currently used. The rest are
  *         present in case we ever increase the number of types of volumes
  *         in the volume group.
  * bit 37 : 1  == special, 0 == regular
  */
-#define NAMEI_VNODEMASK    0x00ffffffff
-#define NAMEI_TAGSHIFT     32
-#define NAMEI_INODESPECIAL 0x2000000000
-#define NAMEI_SPECDIR "R"
-#else
+# define NAMEI_VNODEMASK    0x00ffffffff
+# define NAMEI_TAGSHIFT     32
+# define NAMEI_INODESPECIAL 0x2000000000
+# define NAMEI_SPECDIR "R"
+# define NAMEI_SPECDIRC 'R'
+#else /* !AFS_NT40_ENV */
 /* Inode number format:
  * low 26 bits - vnode number - all 1's if volume special file.
  * next 3 bits - tag
  * next 3 bits spare (0's)
  * high 32 bits - uniquifier (regular) or type if spare
  */
-#define NAMEI_VNODEMASK    0x003ffffff
-#define NAMEI_TAGSHIFT     26
-#define NAMEI_UNIQMASK     0xffffffff
-#define NAMEI_UNIQSHIFT    32
-#define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
+# define NAMEI_VNODEMASK    0x003ffffff
+# define NAMEI_TAGSHIFT     26
+# define NAMEI_UNIQMASK     0xffffffff
+# define NAMEI_UNIQSHIFT    32
+# define NAMEI_INODESPECIAL ((Inode)NAMEI_VNODEMASK)
 /* dir1 is the high 8 bits of the 26 bit vnode */
-#define VNO_DIR1(vno) ((vno >> 14) & 0xff)
+# define VNO_DIR1(vno) ((vno >> 14) & 0xff)
 /* dir2 is the next 9 bits */
-#define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
+# define VNO_DIR2(vno) ((vno >> 9) & 0x1ff)
 /* "name" is the low 9 bits of the vnode, the 3 bit tag and the uniq */
-#define NAMEI_SPECDIR "special"
-#endif
+# define NAMEI_SPECDIR "special"
+#endif /* !AFS_NT40_ENV */
 #define NAMEI_TAGMASK      0x7
 #define NAMEI_VNODESPECIAL NAMEI_VNODEMASK
 
@@ -202,12 +147,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);
 
@@ -246,17 +192,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)
 
 
@@ -306,13 +253,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';
@@ -375,16 +323,17 @@ namei_ViceREADME(char *partition)
     int fd;
 
     /* 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) {
+       (void)OS_WRITE(fd, VICE_README, strlen(VICE_README));
+       OS_CLOSE(fd);
     }
     return (errno);
 }
@@ -404,7 +353,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)
@@ -415,9 +364,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;
@@ -438,7 +387,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
@@ -491,7 +440,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';
@@ -508,7 +457,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 */
@@ -562,13 +511,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;
@@ -651,6 +600,7 @@ namei_MakeSpecIno(int volid, int type)
     ino = NAMEI_INODESPECIAL;
 #ifdef AFS_NT40_ENV
     ino |= type;
+    /* tag is always 0 for special */
 #else
     type &= NAMEI_TAGMASK;
     ino |= ((Inode) type) << NAMEI_TAGSHIFT;
@@ -659,32 +609,14 @@ namei_MakeSpecIno(int volid, int type)
     return ino;
 }
 
-/* SetOGM - set owner group and mode bits from parm and tag */
+#ifdef AFS_NT40_ENV
+/* SetOGM */
 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.
- * mode - 2 bits of parm, then lowest = 3 bits of tag.
- */
-    int owner, group, mode;
-
-    owner = parm & 0x7fff;
-    group = (parm >> 15) & 0x7fff;
-    if (fchown(fd, owner, group) < 0)
-       return -1;
-
-    mode = (parm >> 27) & 0x18;
-    mode |= tag & 0x7;
-    if (fchmod(fd, mode) < 0)
-       return -1;
-#endif
-    return 0;
+    return -1;
 }
 
-#ifdef AFS_NT40_ENV
 static int
 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
 {
@@ -706,10 +638,33 @@ CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
 
     return 0;
 }
-#else
+#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)
+{
+/*
+ * owner - low 15 bits of parm.
+ * group - next 15 bits of parm.
+ * mode - 2 bits of parm, then lowest = 3 bits of tag.
+ */
+    int owner, group, mode;
+
+    owner = parm & 0x7fff;
+    group = (parm >> 15) & 0x7fff;
+    if (fchown(fd, owner, group) < 0)
+       return -1;
+
+    mode = (parm >> 27) & 0x18;
+    mode |= tag & 0x7;
+    if (fchmod(fd, mode) < 0)
+       return -1;
+    return 0;
+}
+
 /* 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;
@@ -719,7 +674,7 @@ GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
 static int
 CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
 {
-    struct afs_stat status;
+    struct afs_stat_st status;
     int parm, tag;
     if (afs_fstat(fdP->fd_fd, &status) < 0)
        return -1;
@@ -730,7 +685,7 @@ CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
 
     return 0;
 }
-#endif
+#endif /* !AFS_NT40_ENV */
 
 int big_vno = 0;               /* Just in case we ever do 64 bit vnodes. */
 
@@ -748,7 +703,7 @@ 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;
+    int type, tag;
     FILETIME ftime;
     char *p;
     b32_string_t str1;
@@ -762,16 +717,16 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
        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;
+       type = p3;
        tmp.ih_vid = p4;        /* Use parent volume id, where this file will be. */
        tmp.ih_ino = namei_MakeSpecIno(p1, p3);
     } else {
@@ -798,20 +753,27 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     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)) {
@@ -821,7 +783,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
     if (!code) {
-        if (p2 != -1) {
+        if (p2 != INODESPECIAL) {
             if (fd == INVALID_FD) {
                 errno = ENOENT;
                 code = nt_unlink((char *)&name.n_path);
@@ -835,7 +797,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. */
@@ -846,10 +808,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);
@@ -863,14 +825,14 @@ 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)
 {
     namei_t name;
-    int fd = -1;
+    int fd = INVALID_FD;
     int code = 0;
     int created_dir = 0;
     IHandle_t tmp;
@@ -929,26 +891,26 @@ 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) {
+    if (p2 == (afs_uint32)-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;
@@ -957,11 +919,11 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
   bad:
-    if (fd >= 0)
-       close(fd);
+    if (fd != INVALID_FD)
+       OS_CLOSE(fd);
 
 
-    if (code || (fd < 0)) {
+    if (code || (fd == INVALID_FD)) {
        if (p2 != -1) {
            fdP = IH_OPEN(lh);
            if (fdP) {
@@ -970,7 +932,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
            }
        }
     }
-    return (code || (fd < 0)) ? (Inode) - 1 : tmp.ih_ino;
+    return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
 }
 #endif
 
@@ -983,7 +945,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;
 }
 
@@ -1039,7 +1001,7 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
            }
 
            if (count > 0) {
-               FDH_REALLYCLOSE(fdP);
+               FDH_CLOSE(fdP);
                IH_RELEASE(tmp);
                return 0;
            }
@@ -1050,6 +1012,11 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
                /* 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);
            }
        }
@@ -1076,7 +1043,7 @@ 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",
+           Log("Warning: Lost ref on ihandle dev %d vid %d ino %lld\n",
                th->ih_dev, th->ih_vid, (afs_int64)th->ih_ino);
            IH_RELEASE(th);
 
@@ -1153,7 +1120,7 @@ namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
     namei_HandleToName(&nameLink, hLink);
     namei_HandleToName(&nameTarget, hTarget);
 
-    unlink(nameLink.n_path);
+    OS_UNLINK(nameLink.n_path);
     code = link(nameTarget.n_path, nameLink.n_path);
     return code;
 }
@@ -1161,10 +1128,11 @@ 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);
@@ -1179,16 +1147,16 @@ 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);
-           unlink(path);
+           OS_CLOSE(fd);
+           OS_UNLINK(path);
            FDH_CLOSE(fdP);
            return ENOMEM;
        }
@@ -1198,18 +1166,18 @@ 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)
            code = EIO;
        else {
-           unlink(name.n_path);
+           OS_UNLINK(name.n_path);
            code = rename(path, name.n_path);
        }
     }
@@ -1409,6 +1377,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) {
@@ -1512,9 +1483,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:
@@ -1564,7 +1535,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;
 
@@ -1582,9 +1553,9 @@ static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
                       unsigned int volid);
 static int DecodeVolumeName(char *name, unsigned int *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);
@@ -1600,11 +1571,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;
 }
 
 
@@ -1612,7 +1583,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"
@@ -1650,13 +1621,12 @@ VerifyDirPerms(char *path)
  *                for this.
  */
 int
-ListViceInodes(char *devname, char *mountedOn, FILE *inodeFile,
+ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
               int (*judgeInode) (struct ViceInodeInfo * info, afs_uint32 vid, void *rock),
               afs_uint32 singleVolumeNumber, int *forcep, int forceR, char *wpath,
               void *rock)
 {
     int ninodes;
-    struct afs_stat status;
 
     *forcep = 0; /* no need to salvage until further notice */
 
@@ -1668,18 +1638,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;
     }
@@ -1687,13 +1653,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;
@@ -1724,9 +1686,9 @@ 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,
+                  FD_t fp,
                   int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                   afs_uint32 singleVolumeNumber, void *rock)
 {
@@ -1777,8 +1739,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))) {
@@ -1841,9 +1803,9 @@ _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,
+                      FD_t fp,
                       int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                       int singleVolumeNumber,
                       void *rock)
@@ -1878,18 +1840,21 @@ _namei_examine_special(char * path1,
     } 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;
        }
     }
 
@@ -1900,7 +1865,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.
@@ -1930,9 +1895,9 @@ _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,
+                  FD_t fp,
                   int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                   int singleVolumeNumber,
                   void *rock)
@@ -1940,7 +1905,7 @@ _namei_examine_reg(char * path3,
     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) {
@@ -1957,7 +1922,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,
@@ -1968,9 +1934,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;
        }
     }
 
@@ -1994,10 +1963,10 @@ 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 *);
@@ -2324,9 +2293,9 @@ _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,
+                    FD_t fp,
                     int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
                     afs_uint32 singleVolumeNumber, void *rock)
 {
@@ -2334,8 +2303,9 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
     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
@@ -2349,11 +2319,6 @@ 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
 
     namei_HandleToVolDir(&name, &myIH);
     strlcpy(path1, name.n_path, sizeof(path1));
@@ -2458,7 +2423,8 @@ 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);
+           snprintf(path2, sizeof(path2), "%s" OS_DIRSEP "%s",
+                    path1, dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -2466,12 +2432,12 @@ 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);
+                   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);
+                   snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                            path1, dp1->d_name);
 #endif
                    dirp3 = opendir(path3);
                    if (dirp3) {
@@ -2484,7 +2450,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;
@@ -2498,7 +2466,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;
@@ -2608,7 +2578,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     FdHandle_t linkHandle;
     char dirl;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s\\%s", dpath, name);
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
                           FindExSearchNameMatch, NULL,
@@ -2626,26 +2596,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);
        }
@@ -2654,11 +2624,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 {
@@ -2676,11 +2646,11 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
            unsigned int volid)
 {
     char fpath[512];
-    struct afs_stat status;
+    struct afs_stat_st status;
     int parm, tag;
     lb64_string_t check;
 
-    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;
@@ -2724,7 +2694,7 @@ convertVolumeInfo(FD_t fdr, FD_t fdw, afs_uint32 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),
@@ -2739,11 +2709,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),
@@ -2909,10 +2893,10 @@ 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) {
+    snprintf(oldpath, sizeof oldpath, "%s" OS_DIRSEP "%s", dir_name,
+            infoName);
+    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;
@@ -2920,8 +2904,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;
@@ -2940,10 +2924,10 @@ 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) {
+    snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+            smallName);
+    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,15 +2938,15 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
 #else
     link(newpath, n.n_path);
-    unlink(newpath);
+    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) {
+    snprintf(newpath, sizeof newpath, "%s" OS_DIRSEP "%s", dir_name,
+            largeName);
+    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 +2957,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     MoveFileEx(n.n_path, newpath, MOVEFILE_WRITE_THROUGH);
 #else
     link(newpath, n.n_path);
-    unlink(newpath);
+    OS_UNLINK(newpath);
 #endif
 
     OS_UNLINK(oldpath);
@@ -3021,7 +3005,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;
 }
@@ -3064,7 +3048,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);
 
@@ -3081,7 +3065,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) {