namei: Set inconsistent linktable linkCount to 0
[openafs.git] / src / vol / namei_ops.c
index cb69e91..4e845b6 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>
-#include <sys/types.h>
-#include <sys/stat.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>
@@ -56,6 +49,7 @@
 #include "volume_inline.h"
 #include "common.h"
 #include <afs/errors.h>
+
 #ifdef AFS_NT40_ENV
 #include <afs/errmap_nt.h>
 #endif
 #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;
 
 
@@ -113,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;
 }
 
@@ -128,7 +100,10 @@ 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;
 }
 
@@ -353,12 +328,13 @@ 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);
+    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));
@@ -382,7 +358,7 @@ namei_CreateDataDirectories(namei_t * name, int *created)
     int i;
 
     *created = 0;
-    afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%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)
@@ -477,7 +453,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 .. */
@@ -540,7 +516,7 @@ namei_RemoveDataDirectories(namei_t * name)
     char tmp[256];
     int i;
 
-    afs_snprintf(tmp, 256, "%s" OS_DIRSEP "%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);
@@ -623,7 +599,7 @@ 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;
@@ -647,26 +623,76 @@ SetOGM(FD_t fd, int parm, int tag)
 }
 
 static int
-CheckOGM(namei_t *name, FdHandle_t *fdP, int p1)
+SetWinOGM(FD_t fd, int p1, int p2)
 {
-    WIN32_FIND_DATA info;
-    HANDLE dirH;
+    BOOL code;
+    FILETIME ftime;
 
-    dirH =
-       FindFirstFileEx(name->n_path, FindExInfoStandard, &info,
-                       FindExSearchNameMatch, NULL,
-                       FIND_FIRST_EX_CASE_SENSITIVE);
+    ftime.dwHighDateTime = p1;
+    ftime.dwLowDateTime = p2;
 
-    if (!dirH)
-       return -1;          /* Can't get info, leave alone */
+    code = SetFileTime(fd, &ftime, NULL /*access*/, NULL /*write*/);
+    if (!code)
+       return -1;
+    return 0;
+}
 
-    FindClose(dirH);
+static int
+GetWinOGM(FD_t fd, int *p1, int *p2)
+{
+    BOOL code;
+    FILETIME ftime;
 
-    if (info.ftCreationTime.dwHighDateTime != (unsigned int)p1)
+    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
@@ -701,21 +727,89 @@ GetOGMFromStat(struct afs_stat_st *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_st status;
-    int parm, tag;
     if (afs_fstat(fdP->fd_fd, &status) < 0)
        return -1;
+    GetOGMFromStat(&status, parm, tag);
+    return 0;
+}
+
+static int
+CheckOGM(FdHandle_t *fdP, int p1)
+{
+    int parm, tag;
 
-    GetOGMFromStat(&status, &parm, &tag);
+    if (GetOGM(fdP, &parm, &tag) < 0)
+       return -1;
     if (parm != p1)
        return -1;
 
     return 0;
 }
+
+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. */
 
 /* Derive the name and create it O_EXCL. If that fails we have an error.
@@ -733,7 +827,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     FdHandle_t *fdP;
     FdHandle_t tfd;
     int type, tag;
-    FILETIME ftime;
+    int ogm_p1, ogm_p2;
     char *p;
     b32_string_t str1;
 
@@ -753,8 +847,8 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
         * p3 - type
         * p4 - parent volume id
         */
-        ftime.dwHighDateTime = p1;
-        ftime.dwLowDateTime = p2;
+        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);
@@ -775,8 +869,8 @@ 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);
@@ -784,11 +878,11 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     p++;
     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_TRUNC | O_EXCL, 0666);
+        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_TRUNC | O_EXCL, 0666);
+                    fd = OS_OPEN((char *)&name.n_path, O_CREAT | O_RDWR | O_EXCL, 0666);
             }
         }
 
@@ -805,7 +899,7 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     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;
         }
@@ -858,7 +952,8 @@ bad:
 }
 #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,
+        FD_t *afd, Inode *ainode)
 {
     namei_t name;
     int fd = INVALID_FD;
@@ -920,12 +1015,12 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
     namei_HandleToName(&name, &tmp);
-    fd = OS_OPEN(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 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 = OS_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 == INVALID_FD)
                goto bad;
@@ -948,10 +1043,6 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
     }
 
   bad:
-    if (fd != INVALID_FD)
-       OS_CLOSE(fd);
-
-
     if (code || (fd == INVALID_FD)) {
        if (p2 != -1) {
            fdP = IH_OPEN(lh);
@@ -961,7 +1052,56 @@ namei_icreate(IHandle_t * lh, char *part, afs_uint32 p1, afs_uint32 p2, afs_uint
            }
        }
     }
-    return (code || (fd == INVALID_FD)) ? (Inode) - 1 : tmp.ih_ino;
+
+    *afd = fd;
+    *ainode = tmp.ih_ino;
+
+    return code;
+}
+
+Inode
+namei_icreate(IHandle_t * lh, char *part,
+              afs_uint32 p1, afs_uint32 p2, afs_uint32 p3, afs_uint32 p4)
+{
+    Inode ino = 0;
+    int fd = INVALID_FD;
+    int code;
+
+    code = icreate(lh, part, p1, p2, p3, p4, &fd, &ino);
+    if (fd != INVALID_FD) {
+       close(fd);
+    }
+    return (code || (fd == INVALID_FD)) ? (Inode) - 1 : 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)
+{
+    Inode ino = 0;
+    int fd = INVALID_FD;
+    int code;
+    IHandle_t *ihP;
+    FdHandle_t *fdP;
+
+    code = icreate(lh, part, p1, p2, p3, p4, &fd, &ino);
+    if (fd == INVALID_FD) {
+       return NULL;
+    }
+    if (code) {
+       close(fd);
+       return NULL;
+    }
+
+    IH_INIT(ihP, dev, p1, ino);
+    fdP = ih_attachfd(ihP, fd);
+    if (!fdP) {
+       close(fd);
+    } else {
+       FDH_CLOSE(fdP);
+    }
+
+    return ihP;
 }
 #endif
 
@@ -1006,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);
@@ -1030,7 +1170,7 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
            }
 
            if (count > 0) {
-               FDH_REALLYCLOSE(fdP);
+               FDH_CLOSE(fdP);
                IH_RELEASE(tmp);
                return 0;
            }
@@ -1041,6 +1181,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);
            }
        }
@@ -1067,8 +1212,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;
@@ -1171,8 +1316,8 @@ 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 = OS_OPEN(path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 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;
@@ -1357,8 +1502,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
@@ -1401,6 +1546,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) {
@@ -1504,9 +1652,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:
@@ -1556,7 +1704,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;
 
@@ -1571,15 +1719,15 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
 
 /* 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);
+                      VolumeId volid);
+static int DecodeVolumeName(char *name, VolumeId *vid);
 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
                                int (*write_fun) (FD_t,
                                                  struct ViceInodeInfo *,
                                                  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
@@ -1596,7 +1744,7 @@ WriteInodeInfo(FD_t fp, struct ViceInodeInfo *info, char *dir, char *name)
 {
     size_t n;
     n = OS_WRITE(fp, info, sizeof(*info));
-    return (n == 1) ? 0 : -2;
+    return (n == sizeof(*info)) ? 0 : -2;
 }
 
 
@@ -1643,8 +1791,8 @@ VerifyDirPerms(char *path)
  */
 int
 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,
+              int (*judgeInode) (struct ViceInodeInfo * info, VolumeId vid, void *rock),
+              VolumeId singleVolumeNumber, int *forcep, int forceR, char *wpath,
               void *rock)
 {
     int ninodes;
@@ -1674,7 +1822,7 @@ ListViceInodes(char *devname, char *mountedOn, FD_t inodeFile,
     /*
      * Paranoia:  check that the file is really the right size
      */
-    if (OS_SIZE(inodeFile) * 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) OS_SIZE(inodeFile),
            (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
@@ -1710,8 +1858,8 @@ namei_ListAFSFiles(char *dev,
                   int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
                   FD_t fp,
-                  int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                  afs_uint32 singleVolumeNumber, void *rock)
+                  int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                  VolumeId singleVolumeNumber, void *rock)
 {
     IHandle_t ih;
     namei_t name;
@@ -1760,8 +1908,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))) {
@@ -1827,42 +1975,38 @@ _namei_examine_special(char * path1,
                       int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                        char *),
                       FD_t fp,
-                      int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                      int singleVolumeNumber,
+                      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) {
        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);
+       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);
@@ -1870,9 +2014,12 @@ _namei_examine_special(char * path1,
 
     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;
        }
     }
 
@@ -1916,8 +2063,8 @@ _namei_examine_reg(char * path3,
                   int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                    char *),
                   FD_t fp,
-                  int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                  int singleVolumeNumber,
+                  int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                  VolumeId singleVolumeNumber,
                   void *rock)
 {
     int ret = 0;
@@ -1952,9 +2099,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;
        }
     }
 
@@ -1984,8 +2134,8 @@ struct listsubdirs_work_node {
     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
@@ -2154,7 +2304,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);
 }
 
 /**
@@ -2168,9 +2318,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);
 }
 
 /**
@@ -2311,8 +2461,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                     int (*writeFun) (FD_t, struct ViceInodeInfo *, char *,
                                      char *),
                     FD_t fp,
-                    int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
-                    afs_uint32 singleVolumeNumber, void *rock)
+                    int (*judgeFun) (struct ViceInodeInfo *, VolumeId, void *),
+                    VolumeId singleVolumeNumber, void *rock)
 {
     int code = 0, ret = 0;
     IHandle_t myIH = *dirIH;
@@ -2346,7 +2496,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) {
@@ -2416,8 +2566,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 */
@@ -2438,7 +2588,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))) {
@@ -2446,12 +2597,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" OS_DIRSEP "%s", path1,
-                                dp1->d_name);
+                   snprintf(path3, sizeof(path3), "%s" OS_DIRSEP "%s",
+                            path1, dp1->d_name);
 #endif
                    dirp3 = opendir(path3);
                    if (dirp3) {
@@ -2543,7 +2694,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];
@@ -2563,7 +2714,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;
@@ -2581,7 +2732,7 @@ DecodeVolumeName(char *name, unsigned int *vid)
 #ifdef AFS_NT40_ENV
 static int
 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-           unsigned int volid)
+           VolumeId volid)
 {
     char fpath[512];
     int tag, vno;
@@ -2592,7 +2743,7 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     FdHandle_t linkHandle;
     char dirl;
 
-    afs_snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
+    snprintf(fpath, sizeof(fpath), "%s" OS_DIRSEP "%s", dpath, name);
 
     dirH = FindFirstFileEx(fpath, FindExInfoStandard, &data,
                           FindExSearchNameMatch, NULL,
@@ -2657,14 +2808,14 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
 #else
 static int
 DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-           unsigned int volid)
+           VolumeId volid)
 {
     char fpath[512];
     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;
@@ -2677,14 +2828,17 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
     if (strcmp(name, check))
        return -1;
 
-    GetOGMFromStat(&status, &parm, &tag);
     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);
@@ -2703,15 +2857,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, &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;
     }
@@ -2773,7 +2927,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
@@ -2796,6 +2950,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 */
@@ -2825,6 +2981,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);
@@ -2885,7 +3054,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;
        }
@@ -2907,8 +3076,8 @@ 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);
+    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",
@@ -2918,7 +3087,7 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
     }
     t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
     namei_HandleToName(&n, &t_ih);
-    fd2 = OS_OPEN(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 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);
@@ -2938,8 +3107,8 @@ 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);
+    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);
@@ -2957,8 +3126,8 @@ namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 
     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);
+    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);
@@ -3019,7 +3188,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;
 }
@@ -3041,13 +3210,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) {