namei decodevolume allow low volume ids
[openafs.git] / src / vol / namei_ops.c
index dfa9718..b4e0649 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
-RCSID
-    ("$Header$");
 
 #ifdef AFS_NAMEI_ENV
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/stat.h>
@@ -27,10 +26,7 @@ RCSID
 #include <sys/file.h>
 #include <sys/param.h>
 #include <lock.h>
-#ifdef AFS_AIX_ENV
-#include <sys/lockf.h>
-#endif
-#ifdef AFS_SUN5_ENV
+#if defined(AFS_SUN5_ENV) || defined(AFS_HPUX_ENV)
 #include <unistd.h>
 #endif
 #include <afs/afsutil.h>
@@ -43,16 +39,72 @@ RCSID
 #include "viceinode.h"
 #include "voldefs.h"
 #include "partition.h"
+#include "fssync.h"
+#include "volume_inline.h"
+#include "common.h"
 #include <afs/errors.h>
 
-/*@printflike@*/ extern void Log(const char *format, ...);
+/*@+fcnmacros +macrofcndecl@*/
+#ifdef O_LARGEFILE
+#ifdef S_SPLINT_S
+extern off64_t afs_lseek(int FD, off64_t O, int F);
+#endif /*S_SPLINT_S */
+#define afs_lseek(FD, O, F)    lseek64(FD, (off64_t)(O), F)
+#define afs_stat               stat64
+#define afs_fstat              fstat64
+#define afs_open               open64
+#define afs_fopen              fopen64
+#else /* !O_LARGEFILE */
+#ifdef S_SPLINT_S
+extern off_t afs_lseek(int FD, off_t O, int F);
+#endif /*S_SPLINT_S */
+#define afs_lseek(FD, O, F)    lseek(FD, (off_t)(O), F)
+#define afs_stat               stat
+#define afs_fstat              fstat
+#define afs_open               open
+#define afs_fopen              fopen
+#endif /* !O_LARGEFILE */
+/*@=fcnmacros =macrofcndecl@*/
+
+#ifndef LOCK_SH
+#define   LOCK_SH   1    /* shared lock */
+#define   LOCK_EX   2    /* exclusive lock */
+#define   LOCK_NB   4    /* don't block when locking */
+#define   LOCK_UN   8    /* unlock */
+#endif
+
+#ifndef HAVE_FLOCK
+#include <fcntl.h>
 
-extern char *volutil_PartitionName_r(int volid, char *buf, int buflen);
+/*
+ * This function emulates a subset of flock()
+ */
+int 
+emul_flock(int fd, int cmd)
+{    struct flock f;
 
-int
-namei_iread(IHandle_t * h, int offset, char *buf, int size)
+    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;
+
+
+afs_sfsize_t
+namei_iread(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
 {
-    int nBytes;
+    afs_sfsize_t nBytes;
     FdHandle_t *fdP;
 
     fdP = IH_OPEN(h);
@@ -69,10 +121,10 @@ namei_iread(IHandle_t * h, int offset, char *buf, int size)
     return nBytes;
 }
 
-int
-namei_iwrite(IHandle_t * h, int offset, char *buf, int size)
+afs_sfsize_t
+namei_iwrite(IHandle_t * h, afs_foff_t offset, char *buf, afs_fsize_t size)
 {
-    int nBytes;
+    afs_sfsize_t nBytes;
     FdHandle_t *fdP;
 
     fdP = IH_OPEN(h);
@@ -127,7 +179,8 @@ typedef struct {
     int ogm_mode;
 } namei_ogm_t;
 
-int namei_SetLinkCount(FdHandle_t * h, Inode ino, int count, int locked);
+static int namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite);
+
 static int GetFreeTag(IHandle_t * ih, int vno);
 
 /* namei_HandleToInodeDir
@@ -140,22 +193,30 @@ static int GetFreeTag(IHandle_t * ih, int vno);
 static void
 namei_HandleToInodeDir(namei_t * name, IHandle_t * ih)
 {
-    char *tmp = name->n_base;
+    size_t offset;
 
     memset(name, '\0', sizeof(*name));
 
-    (void)volutil_PartitionName_r(ih->ih_dev, tmp, NAMEI_LCOMP_LEN);
-    tmp += VICE_PREFIX_SIZE;
-    tmp += ih->ih_dev > 25 ? 2 : 1;
-    *tmp = '/';
-    tmp++;
-    (void)strcpy(tmp, INODEDIR);
-    (void)strcpy(name->n_path, name->n_base);
+    /*
+     * Add the /vicepXX string to the start of name->n_base and then calculate
+     * offset as the number of bytes we know we added.
+     *
+     * FIXME: This embeds knowledge of the vice partition naming scheme and
+     * mapping from device numbers.  There needs to be an API that tells us
+     * this offset.
+     */
+    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] = '/';
+    offset++;
+    strlcpy(name->n_base + offset, INODEDIR, sizeof(name->n_base) - offset);
+    strlcpy(name->n_path, name->n_base, sizeof(name->n_path));
 }
 
-#define addtoname(N, C) \
-do { \
-        strcat((N)->n_path, "/"); strcat((N)->n_path, C); \
+#define addtoname(N, C)                                 \
+do {                                                    \
+    strlcat((N)->n_path, "/", sizeof((N)->n_path));     \
+    strlcat((N)->n_path, (C), sizeof((N)->n_path));     \
 } while(0)
 
 
@@ -166,10 +227,10 @@ namei_HandleToVolDir(namei_t * name, IHandle_t * ih)
 
     namei_HandleToInodeDir(name, ih);
     (void)int32_to_flipbase64(tmp, (int64_t) (ih->ih_vid & 0xff));
-    (void)strcpy(name->n_voldir1, tmp);
+    strlcpy(name->n_voldir1, tmp, sizeof(name->n_voldir1));
     addtoname(name, name->n_voldir1);
     (void)int32_to_flipbase64(tmp, (int64_t) ih->ih_vid);
-    (void)strcpy(name->n_voldir2, tmp);
+    strlcpy(name->n_voldir2, tmp, sizeof(name->n_voldir2));
     addtoname(name, name->n_voldir2);
 }
 
@@ -187,19 +248,19 @@ namei_HandleToName(namei_t * name, IHandle_t * ih)
     namei_HandleToVolDir(name, ih);
 
     if (vno == NAMEI_VNODESPECIAL) {
-       (void)strcpy(name->n_dir1, NAMEI_SPECDIR);
+       strlcpy(name->n_dir1, NAMEI_SPECDIR, sizeof(name->n_dir1));
        addtoname(name, name->n_dir1);
        name->n_dir2[0] = '\0';
     } else {
        (void)int32_to_flipbase64(str, VNO_DIR1(vno));
-       (void)strcpy(name->n_dir1, str);
+       strlcpy(name->n_dir1, str, sizeof(name->n_dir1));
        addtoname(name, name->n_dir1);
        (void)int32_to_flipbase64(str, VNO_DIR2(vno));
-       (void)strcpy(name->n_dir2, str);
+       strlcpy(name->n_dir2, str, sizeof(name->n_dir2));
        addtoname(name, name->n_dir2);
     }
     (void)int64_to_flipbase64(str, (int64_t) ih->ih_ino);
-    (void)strcpy(name->n_inode, str);
+    strlcpy(name->n_inode, str, sizeof(name->n_inode));
     addtoname(name, name->n_inode);
 }
 
@@ -207,7 +268,8 @@ namei_HandleToName(namei_t * name, IHandle_t * ih)
  * name space.
  */
 #define VICE_README "These files and directories are a part of the AFS \
-namespace. Modifying them\nin any way will result in loss of AFS data.\n"
+namespace. Modifying them\nin any way will result in loss of AFS data,\n\
+ownership and permissions included.\n"
 int
 namei_ViceREADME(char *partition)
 {
@@ -221,7 +283,7 @@ namei_ViceREADME(char *partition)
 
     (void)afs_snprintf(filename, sizeof filename, "%s/%s/README", partition,
                       INODEDIR);
-    fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
+    fd = afs_open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0444);
     if (fd >= 0) {
        (void)write(fd, VICE_README, strlen(VICE_README));
        close(fd);
@@ -258,7 +320,7 @@ namei_CreateDataDirectories(namei_t * name, int *created)
 
     *created = 0;
 
-    (void)strcpy(tmp, name->n_base);
+    strlcpy(tmp, name->n_base, sizeof(tmp));
     create_dir();
 
     create_nextdir(name->n_voldir1);
@@ -295,7 +357,7 @@ delTree(char *root, char *tree, int *errp)
     char *cp;
     DIR *ds;
     struct dirent *dirp;
-    struct stat st;
+    struct afs_stat st;
 
     if (*tree) {
        /* delete the children first */
@@ -318,7 +380,7 @@ delTree(char *root, char *tree, int *errp)
                 */
                strcat(root, "/");
                strcat(root, dirp->d_name);
-               if (stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
+               if (afs_stat(root, &st) == 0 && S_ISDIR(st.st_mode)) {
                    /* delete this subtree */
                    delTree(root, cp + 1, errp);
                } else
@@ -367,7 +429,7 @@ namei_RemoveDataDirectories(namei_t * name)
     char pbuf[MAXPATHLEN], *path = pbuf;
     int prefixlen = strlen(name->n_base), err = 0;
 
-    strcpy(path, name->n_path);
+    strlcpy(path, name->n_path, sizeof(pbuf));
 
     /* move past the prefix */
     path = path + prefixlen + 1;       /* skip over the trailing / */
@@ -441,7 +503,7 @@ SetOGM(int fd, int parm, int tag)
 
 /* GetOGM - get parm and tag from owner, group and mode bits. */
 static void
-GetOGMFromStat(struct stat *status, int *parm, int *tag)
+GetOGMFromStat(struct afs_stat *status, int *parm, int *tag)
 {
     *parm = status->st_uid | (status->st_gid << 15);
     *parm |= (status->st_mode & 0x18) << 27;
@@ -451,8 +513,8 @@ GetOGMFromStat(struct stat *status, int *parm, int *tag)
 static int
 GetOGM(int fd, int *parm, int *tag)
 {
-    struct stat status;
-    if (fstat(fd, &status) < 0)
+    struct afs_stat status;
+    if (afs_fstat(fd, &status) < 0)
        return -1;
 
     GetOGMFromStat(&status, parm, tag);
@@ -528,12 +590,13 @@ namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
     }
 
     namei_HandleToName(&name, &tmp);
-    fd = open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+    fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
     if (fd < 0) {
        if (errno == ENOTDIR || errno == ENOENT) {
            if (namei_CreateDataDirectories(&name, &created_dir) < 0)
                goto bad;
-           fd = open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+           fd = afs_open(name.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR,
+                         0);
            if (fd < 0)
                goto bad;
        } else {
@@ -548,6 +611,8 @@ namei_icreate(IHandle_t * lh, char *part, int p1, int p2, int p3, int p4)
 
     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);
     }
@@ -579,7 +644,7 @@ namei_iopen(IHandle_t * h)
 
     /* Convert handle to file name. */
     namei_HandleToName(&name, h);
-    fd = open(name.n_path, O_RDWR, 0666);
+    fd = afs_open(name.n_path, O_RDWR, 0666);
     return fd;
 }
 
@@ -669,11 +734,26 @@ namei_dec(IHandle_t * ih, Inode ino, int p1)
                FDH_REALLYCLOSE(fdP);
                return -1;
            }
+       } 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);
+           IH_RELEASE(th);
+         
+           /* If we're less than 0, someone presumably unlinked;
+              don't bother setting count to 0, but we need to drop a lock */
+           if (namei_SetLinkCount(fdP, ino, 0, 1) < 0) {
+               FDH_REALLYCLOSE(fdP);
+               return -1;
+           }
        }
        if (count == 0) {
-           IHandle_t th = *ih;
-           th.ih_ino = ino;
-           namei_HandleToName(&name, &th);
+           IHandle_t *th;
+           IH_INIT(th, ih->ih_dev, ih->ih_vid, ino);
+
+           namei_HandleToName(&name, th);
+           IH_RELEASE(th);
            code = unlink(name.n_path);
        }
        FDH_CLOSE(fdP);
@@ -722,6 +802,78 @@ namei_inc(IHandle_t * h, Inode ino, int p1)
     return code;
 }
 
+int
+namei_replace_file_by_hardlink(IHandle_t *hLink, IHandle_t *hTarget)
+{
+    afs_int32 code;
+    namei_t nameLink;
+    namei_t nameTarget;
+    
+    /* Convert handle to file name. */
+    namei_HandleToName(&nameLink, hLink);
+    namei_HandleToName(&nameTarget, hTarget);
+    
+    unlink(nameLink.n_path);
+    code = link(nameTarget.n_path, nameLink.n_path);
+    return code;
+}
+
+int
+namei_copy_on_write(IHandle_t *h)
+{
+    afs_int32 fd, code = 0;
+    namei_t name;
+    FdHandle_t *fdP;
+    struct afs_stat tstat;
+    
+    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 *buf;
+       afs_size_t size;
+       ssize_t tlen;
+       
+       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) {
+           FDH_CLOSE(fdP);
+           return EIO;
+       }
+       buf = malloc(8192);
+       if (!buf) {
+           close(fd);
+           unlink(path);
+           FDH_CLOSE(fdP);
+           return ENOMEM;
+       }
+       size = tstat.st_size;
+       FDH_SEEK(fdP, 0, 0);
+       while (size) {
+           tlen = size > 8192 ? 8192 : size;
+           if (FDH_READ(fdP, buf, tlen) != tlen) 
+               break;
+           if (write(fd, buf, tlen) != tlen) 
+               break;
+           size -= tlen;
+       }
+       close(fd);
+       FDH_REALLYCLOSE(fdP);
+       free(buf);
+       if (size)
+           code = EIO;
+       else {
+           unlink(name.n_path);
+           code = rename(path, name.n_path);
+       }
+    }
+    return code;
+}
+
 /************************************************************************
  * File Name Structure
  ************************************************************************
@@ -805,12 +957,12 @@ namei_inc(IHandle_t * h, Inode ino, int p1)
 #define LINKTABLE_SHIFT 1      /* log 2 = 1 */
 
 static void
-namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
+namei_GetLCOffsetAndIndexFromIno(Inode ino, afs_foff_t * offset, int *index)
 {
     int toff = (int)(ino & NAMEI_VNODEMASK);
     int tindex = (int)((ino >> NAMEI_TAGSHIFT) & NAMEI_TAGMASK);
 
-    *offset = (toff << LINKTABLE_SHIFT) + 8;   /* * 2 + sizeof stamp */
+    *offset = (afs_foff_t) ((toff << LINKTABLE_SHIFT) + 8);    /* * 2 + sizeof stamp */
     *index = (tindex << 1) + tindex;
 }
 
@@ -819,52 +971,75 @@ namei_GetLCOffsetAndIndexFromIno(Inode ino, int *offset, int *index)
  * If lockit is set, lock the file and leave it locked upon a successful
  * return.
  */
-int
-namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit)
+static int
+namei_GetLinkCount2(FdHandle_t * h, Inode ino, int lockit, int fixup, int nowrite)
 {
     unsigned short row = 0;
-    int offset, index;
+    afs_foff_t offset;
+    ssize_t rc;
+    int index;
 
+    /* there's no linktable yet. the salvager will create one later */
+    if (h->fd_fd == -1 && fixup)
+       return 1;
     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
 
     if (lockit) {
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-       if (lockf(h->fd_fd, F_LOCK, 0) < 0)
-#else
        if (flock(h->fd_fd, LOCK_EX) < 0)
-#endif
            return -1;
     }
 
-    if (lseek(h->fd_fd, offset, SEEK_SET) == -1)
+    if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
        goto bad_getLinkByte;
 
-    if (read(h->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
+    rc = read(h->fd_fd, (char *)&row, sizeof(row));
+    if ((rc == 0 || !((row >> index) & NAMEI_TAGMASK)) && fixup && nowrite)
+        return 1;
+    if (rc == 0 && fixup) {
+        struct stat st;
+        if (fstat(h->fd_fd, &st) || st.st_size >= offset+sizeof(row))
+          goto bad_getLinkByte;
+        FDH_TRUNC(h, offset+sizeof(row));
+        row = 1 << index;
+        rc = write(h->fd_fd, (char *)&row, sizeof(row));
+    }
+    if (rc != sizeof(row)) {
        goto bad_getLinkByte;
     }
 
+    if (fixup && !((row >> index) & NAMEI_TAGMASK)) {
+        row |= 1<<index;
+        if (afs_lseek(h->fd_fd, offset, SEEK_SET) == -1)
+           goto bad_getLinkByte;
+        rc = write(h->fd_fd, (char *)&row, sizeof(row));
+        if (rc != sizeof(row))
+           goto bad_getLinkByte;
+    }
     return (int)((row >> index) & NAMEI_TAGMASK);
 
   bad_getLinkByte:
     if (lockit)
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-       lockf(h->fd_fd, F_ULOCK, 0);
-#else
        flock(h->fd_fd, LOCK_UN);
-#endif
     return -1;
 }
 
+int
+namei_GetLinkCount(FdHandle_t * h, Inode ino, int lockit) 
+{
+    return namei_GetLinkCount2(h, ino, lockit, 0, 1);
+}
+
 /* Return a free column index for this vnode. */
 static int
 GetFreeTag(IHandle_t * ih, int vno)
 {
     FdHandle_t *fdP;
-    int offset;
+    afs_foff_t offset;
     int col;
     int coldata;
     short row;
-    int code;
+    ssize_t nBytes;
 
 
     fdP = IH_OPEN(ih);
@@ -872,23 +1047,19 @@ GetFreeTag(IHandle_t * ih, int vno)
        return -1;
 
     /* Only one manipulates at a time. */
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-    if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
-#else
     if (flock(fdP->fd_fd, LOCK_EX) < 0) {
-#endif
        FDH_REALLYCLOSE(fdP);
        return -1;
     }
 
     offset = (vno << LINKTABLE_SHIFT) + 8;     /* * 2 + sizeof stamp */
-    if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
+    if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
        goto badGetFreeTag;
     }
 
-    code = read(fdP->fd_fd, (char *)&row, sizeof(row));
-    if (code != sizeof(row)) {
-       if (code != 0)
+    nBytes = read(fdP->fd_fd, (char *)&row, sizeof(row));
+    if (nBytes != sizeof(row)) {
+       if (nBytes != 0)
            goto badGetFreeTag;
        row = 0;
     }
@@ -899,33 +1070,27 @@ GetFreeTag(IHandle_t * ih, int vno)
        if ((row & coldata) == 0)
            break;
     }
-    if (col >= NAMEI_MAXVOLS)
+    if (col >= NAMEI_MAXVOLS) {
+       errno = ENOSPC;
        goto badGetFreeTag;
+    }
 
     coldata = 1 << (col * 3);
     row |= coldata;
 
-    if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
+    if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
        goto badGetFreeTag;
     }
     if (write(fdP->fd_fd, (char *)&row, sizeof(row)) != sizeof(row)) {
        goto badGetFreeTag;
     }
     FDH_SYNC(fdP);
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
     FDH_REALLYCLOSE(fdP);
     return col;;
 
   badGetFreeTag:
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
     FDH_REALLYCLOSE(fdP);
     return -1;
 }
@@ -939,31 +1104,28 @@ GetFreeTag(IHandle_t * ih, int vno)
 int
 namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
 {
-    int offset, index;
+    afs_foff_t offset;
+    int index;
     unsigned short row;
     int junk;
-    int code = -1;
+    ssize_t nBytes = -1;
 
     namei_GetLCOffsetAndIndexFromIno(ino, &offset, &index);
 
     if (!locked) {
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-       if (lockf(fdP->fd_fd, F_LOCK, 0) < 0) {
-#else
        if (flock(fdP->fd_fd, LOCK_EX) < 0) {
-#endif
            return -1;
        }
     }
-    if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
+    if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
        errno = EBADF;
        goto bad_SetLinkCount;
     }
 
 
-    code = read(fdP->fd_fd, (char *)&row, sizeof(row));
-    if (code != sizeof(row)) {
-       if (code != 0) {
+    nBytes = read(fdP->fd_fd, (char *)&row, sizeof(row));
+    if (nBytes != sizeof(row)) {
+       if (nBytes != 0) {
            errno = EBADF;
            goto bad_SetLinkCount;
        }
@@ -975,7 +1137,7 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
     row &= (unsigned short)~junk;
     row |= (unsigned short)count;
 
-    if (lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
+    if (afs_lseek(fdP->fd_fd, offset, SEEK_SET) == -1) {
        errno = EBADF;
        goto bad_SetLinkCount;
     }
@@ -986,31 +1148,27 @@ namei_SetLinkCount(FdHandle_t * fdP, Inode ino, int count, int locked)
     }
     FDH_SYNC(fdP);
 
-    code = 0;
+    nBytes = 0;
 
 
   bad_SetLinkCount:
-#if defined(AFS_AIX_ENV) || defined(AFS_SUN5_ENV)
-    lockf(fdP->fd_fd, F_ULOCK, 0);
-#else
     flock(fdP->fd_fd, LOCK_UN);
-#endif
 
-    return code;
+    return nBytes;
 }
 
 
 /* ListViceInodes - write inode data to a results file. */
 static int DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
-                      int volid);
-static int DecodeVolumeName(char *name, int *vid);
+                      unsigned int volid);
+static int DecodeVolumeName(char *name, unsigned int *vid);
 static int namei_ListAFSSubDirs(IHandle_t * dirIH,
                                int (*write_fun) (FILE *,
                                                  struct ViceInodeInfo *,
                                                  char *, char *), FILE * fp,
                                int (*judgeFun) (struct ViceInodeInfo *,
-                                                int vid),
-                               int singleVolumeNumber);
+                                                afs_uint32 vid, void *),
+                               afs_uint32 singleVolumeNumber, void *rock);
 
 
 /* WriteInodeInfo
@@ -1035,9 +1193,9 @@ int mode_errors;          /* Number of errors found in mode bits on directories. */
 void
 VerifyDirPerms(char *path)
 {
-    struct stat status;
+    struct afs_stat status;
 
-    if (stat(path, &status) < 0) {
+    if (afs_stat(path, &status) < 0) {
        Log("Unable to stat %s. Please manually verify mode bits for this"
            " directory\n", path);
     } else {
@@ -1060,63 +1218,51 @@ VerifyDirPerms(char *path)
  * If the resultFile is NULL, then don't call the write routine.
  */
 int
-ListViceInodes(char *devname, char *mountedOn, char *resultFile,
-              int (*judgeInode) (struct ViceInodeInfo * info, int vid),
-              int singleVolumeNumber, int *forcep, int forceR, char *wpath)
+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, 
+              void *rock)
 {
-    FILE *fp = (FILE *) - 1;
     int ninodes;
-    struct stat status;
+    struct afs_stat status;
 
-    if (resultFile) {
-       fp = fopen(resultFile, "w");
-       if (!fp) {
-           Log("Unable to create inode description file %s\n", resultFile);
-           return -1;
-       }
-    }
+    *forcep = 0; /* no need to salvage until further notice */
 
     /* Verify protections on directories. */
     mode_errors = 0;
     VerifyDirPerms(mountedOn);
 
     ninodes =
-       namei_ListAFSFiles(mountedOn, WriteInodeInfo, fp, judgeInode,
-                          singleVolumeNumber);
+       namei_ListAFSFiles(mountedOn, WriteInodeInfo, inodeFile, judgeInode,
+                          singleVolumeNumber, rock);
 
-    if (!resultFile)
+    if (!inodeFile)
        return ninodes;
 
     if (ninodes < 0) {
-       fclose(fp);
        return ninodes;
     }
 
-    if (fflush(fp) == EOF) {
+    if (fflush(inodeFile) == EOF) {
        Log("Unable to successfully flush inode file for %s\n", mountedOn);
-       fclose(fp);
        return -2;
     }
-    if (fsync(fileno(fp)) == -1) {
+    if (fsync(fileno(inodeFile)) == -1) {
        Log("Unable to successfully fsync inode file for %s\n", mountedOn);
-       fclose(fp);
-       return -2;
-    }
-    if (fclose(fp) == EOF) {
-       Log("Unable to successfully close inode file for %s\n", mountedOn);
        return -2;
     }
 
     /*
      * Paranoia:  check that the file is really the right size
      */
-    if (stat(resultFile, &status) == -1) {
+    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)) {
-       Log("Wrong size (%d instead of %d) in inode file for %s\n",
-           status.st_size, ninodes * sizeof(struct ViceInodeInfo),
+       Log("Wrong size (%d instead of %lu) in inode file for %s\n",
+           (int) status.st_size,
+           (long unsigned int) ninodes * sizeof(struct ViceInodeInfo),
            mountedOn);
        return -2;
     }
@@ -1135,8 +1281,8 @@ int
 namei_ListAFSFiles(char *dev,
                   int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
                                    char *), FILE * fp,
-                  int (*judgeFun) (struct ViceInodeInfo *, int),
-                  int singleVolumeNumber)
+                  int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
+                  afs_uint32 singleVolumeNumber, void *rock)
 {
     IHandle_t ih;
     namei_t name;
@@ -1156,7 +1302,7 @@ namei_ListAFSFiles(char *dev,
        namei_HandleToVolDir(&name, &ih);
        ninodes =
            namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
-                                singleVolumeNumber);
+                                singleVolumeNumber, rock);
        if (ninodes < 0)
            return ninodes;
     } else {
@@ -1169,9 +1315,8 @@ namei_ListAFSFiles(char *dev,
        while ((dp1 = readdir(dirp1))) {
            if (*dp1->d_name == '.')
                continue;
-           (void)strcpy(path2, name.n_path);
-           (void)strcat(path2, "/");
-           (void)strcat(path2, dp1->d_name);
+           afs_snprintf(path2, sizeof(path2), "%s/%s", name.n_path,
+                        dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -1180,7 +1325,7 @@ namei_ListAFSFiles(char *dev,
                    if (!DecodeVolumeName(dp2->d_name, &ih.ih_vid)) {
                        ninodes +=
                            namei_ListAFSSubDirs(&ih, writeFun, fp, judgeFun,
-                                                0);
+                                                0, rock);
                    }
                }
                closedir(dirp2);
@@ -1207,8 +1352,8 @@ static int
 namei_ListAFSSubDirs(IHandle_t * dirIH,
                     int (*writeFun) (FILE *, struct ViceInodeInfo *, char *,
                                      char *), FILE * fp,
-                    int (*judgeFun) (struct ViceInodeInfo *, int),
-                    int singleVolumeNumber)
+                    int (*judgeFun) (struct ViceInodeInfo *, afs_uint32, void *),
+                    afs_uint32 singleVolumeNumber, void *rock)
 {
     IHandle_t myIH = *dirIH;
     namei_t name;
@@ -1225,7 +1370,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 #endif
 
     namei_HandleToVolDir(&name, &myIH);
-    (void)strcpy(path1, name.n_path);
+    strlcpy(path1, name.n_path, sizeof(path1));
 
     /* Do the directory containing the special files first to pick up link
      * counts.
@@ -1247,11 +1392,11 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                /* Open this handle */
                (void)afs_snprintf(path2, sizeof path2, "%s/%s", path1,
                                   dp1->d_name);
-               linkHandle.fd_fd = open(path2, O_RDONLY, 0666);
+               linkHandle.fd_fd = afs_open(path2, Testing ? O_RDONLY : O_RDWR, 0666);
                info.linkCount =
-                   namei_GetLinkCount(&linkHandle, (Inode) 0, 0);
+                   namei_GetLinkCount2(&linkHandle, (Inode) 0, 1, 1, Testing);
            }
-           if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber))
+           if (judgeFun && !(*judgeFun) (&info, singleVolumeNumber, rock))
                continue;
 
            if ((*writeFun) (fp, &info, path1, dp1->d_name) < 0) {
@@ -1267,7 +1412,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 
     /* Now run through all the other subdirs */
     namei_HandleToVolDir(&name, &myIH);
-    (void)strcpy(path1, name.n_path);
+    strlcpy(path1, name.n_path, sizeof(path1));
 
     dirp1 = opendir(path1);
     if (dirp1) {
@@ -1278,9 +1423,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                continue;
 
            /* Now we've got a next level subdir. */
-           (void)strcpy(path2, path1);
-           (void)strcat(path2, "/");
-           (void)strcat(path2, dp1->d_name);
+           afs_snprintf(path2, sizeof(path2), "%s/%s", path1, dp1->d_name);
            dirp2 = opendir(path2);
            if (dirp2) {
                while ((dp2 = readdir(dirp2))) {
@@ -1288,9 +1431,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                        continue;
 
                    /* Now we've got to the actual data */
-                   (void)strcpy(path3, path2);
-                   (void)strcat(path3, "/");
-                   (void)strcat(path3, dp2->d_name);
+                   afs_snprintf(path3, sizeof(path3), "%s/%s", path2,
+                                dp2->d_name);
                    dirp3 = opendir(path3);
                    if (dirp3) {
                        while ((dp3 = readdir(dirp3))) {
@@ -1300,8 +1442,8 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                                (path3, dp3->d_name, &info, myIH.ih_vid) < 0)
                                continue;
                            info.linkCount =
-                               namei_GetLinkCount(&linkHandle,
-                                                  info.inodeNumber, 0);
+                               namei_GetLinkCount2(&linkHandle,
+                                                  info.inodeNumber, 1, 1, Testing);
                            if (info.linkCount == 0) {
 #ifdef DELETE_ZLC
                                Log("Found 0 link count file %s/%s, deleting it.\n", path3, dp3->d_name);
@@ -1313,7 +1455,7 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
                                continue;
                            }
                            if (judgeFun
-                               && !(*judgeFun) (&info, singleVolumeNumber))
+                               && !(*judgeFun) (&info, singleVolumeNumber, rock))
                                continue;
 
                            if ((*writeFun) (fp, &info, path3, dp3->d_name) <
@@ -1347,11 +1489,11 @@ namei_ListAFSSubDirs(IHandle_t * dirIH,
 }
 
 static int
-DecodeVolumeName(char *name, int *vid)
+DecodeVolumeName(char *name, unsigned int *vid)
 {
-    if (strlen(name) <= 2)
+    if (strlen(name) < 1)
        return -1;
-    *vid = (int)flipbase64_to_int64(name);
+    *vid = (unsigned int)flipbase64_to_int64(name);
     return 0;
 }
 
@@ -1362,23 +1504,27 @@ DecodeVolumeName(char *name, int *vid)
  * Get
  */
 static int
-DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
+DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info,
+           unsigned int volid)
 {
     char fpath[512];
-    struct stat status;
+    struct afs_stat status;
     int parm, tag;
+    lb64_string_t check;
 
-    (void)strcpy(fpath, dpath);
-    (void)strcat(fpath, "/");
-    (void)strcat(fpath, name);
+    afs_snprintf(fpath, sizeof(fpath), "%s/%s", dpath, name);
 
-    if (stat(fpath, &status) < 0) {
+    if (afs_stat(fpath, &status) < 0) {
        return -1;
     }
 
     info->byteCount = status.st_size;
-    info->inodeNumber = flipbase64_to_int64(name);
+    info->inodeNumber = (Inode) flipbase64_to_int64(name);
 
+    int64_to_flipbase64(check, info->inodeNumber);
+    if (strcmp(name, check))
+       return -1;
+    
     GetOGMFromStat(&status, &parm, &tag);
     if ((info->inodeNumber & NAMEI_INODESPECIAL) == NAMEI_INODESPECIAL) {
        /* p1 - vid, p2 - -1, p3 - type, p4 - rwvid */
@@ -1402,18 +1548,17 @@ DecodeInode(char *dpath, char *name, struct ViceInodeInfo *info, int volid)
  * this routine is called by namei_convertROtoRWvolume()
  */
 
+#ifdef FSSYNC_BUILD_CLIENT
 static afs_int32
-convertVolumeInfo(fdr, fdw, vid)
-     int fdr;
-     int fdw;
-     afs_uint32 vid;
+convertVolumeInfo(int fdr, int fdw, afs_uint32 vid)
 {
     struct VolumeDiskData vd;
     char *p;
 
     if (read(fdr, &vd, sizeof(struct VolumeDiskData)) !=
        sizeof(struct VolumeDiskData)) {
-       Log("1 convertVolumeInfo: read failed for %lu with code %d\n", vid,
+       Log("1 convertVolumeInfo: read failed for %lu with code %d\n",
+           afs_printable_uint32_lu(vid),
            errno);
        return -1;
     }
@@ -1422,20 +1567,23 @@ convertVolumeInfo(fdr, fdw, vid)
     vd.id = vd.parentId;
     vd.type = RWVOL;
     vd.dontSalvage = 0;
+    vd.inUse = 0;
     vd.uniquifier += 5000;     /* just in case there are still file copies from
                                 * the old RW volume around */
     p = strrchr(vd.name, '.');
     if (p && !strcmp(p, ".readonly")) {
-       bzero(p, 8);
+       memset(p, 0, 9);
     }
     if (write(fdw, &vd, sizeof(struct VolumeDiskData)) !=
        sizeof(struct VolumeDiskData)) {
-       Log("1 convertVolumeInfo: write failed for %lu with code %d\n", vid,
+       Log("1 convertVolumeInfo: write failed for %lu with code %d\n",
+           afs_printable_uint32_lu(vid),
            errno);
        return -1;
     }
     return 0;
 }
+#endif
 
 /*
  * Convert a RO-volume into a RW-volume
@@ -1458,32 +1606,72 @@ convertVolumeInfo(fdr, fdw, vid)
  */
 
 int
-namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
+namei_ConvertROtoRWvolume(char *pname, afs_uint32 volumeId)
 {
+    int code = 0;
+#ifdef FSSYNC_BUILD_CLIENT
     namei_t n;
     char dir_name[512], oldpath[512], newpath[512];
     char smallName[64];
     char largeName[64];
     char infoName[64];
     IHandle_t t_ih;
+    IHandle_t *ih;
     char infoSeen = 0;
     char smallSeen = 0;
     char largeSeen = 0;
     char linkSeen = 0;
-    int code, fd, fd2;
+    int fd, fd2;
     char *p;
     DIR *dirp;
+    Inode ino;
     struct dirent *dp;
+    struct DiskPartition64 *partP;
     struct ViceInodeInfo info;
+    struct VolumeDiskHeader h;
+# ifdef AFS_DEMAND_ATTACH_FS
+    int locktype = 0;
+# endif /* AFS_DEMAND_ATTACH_FS */
+
+    for (partP = DiskPartitionList; partP && strcmp(partP->name, pname);
+         partP = partP->next);
+    if (!partP) {
+        Log("1 namei_ConvertROtoRWvolume: Couldn't find DiskPartition for %s\n", pname);
+       code = EIO;
+       goto done;
+    }
+
+# ifdef AFS_DEMAND_ATTACH_FS
+    locktype = VVolLockType(V_VOLUPD, 1);
+    code = VLockVolumeByIdNB(volumeId, partP, locktype);
+    if (code) {
+       locktype = 0;
+       code = EIO;
+       goto done;
+    }
+# endif /* AFS_DEMAND_ATTACH_FS */
+
+    if (VReadVolumeDiskHeader(volumeId, partP, &h)) {
+       Log("1 namei_ConvertROtoRWvolume: Couldn't read header for RO-volume %lu.\n",
+           afs_printable_uint32_lu(volumeId));
+       code = EIO;
+       goto done;
+    }
+
+    FSYNC_VolOp(volumeId, pname, FSYNC_VOL_BREAKCBKS, 0, NULL);
+
+    ino = namei_MakeSpecIno(h.parent, VI_LINKTABLE);
+    IH_INIT(ih, partP->device, h.parent, ino);
 
-    namei_HandleToName(&n, h);
-    strcpy(dir_name, n.n_path);
+    namei_HandleToName(&n, ih);
+    strlcpy(dir_name, n.n_path, sizeof(dir_name));
     p = strrchr(dir_name, '/');
     *p = 0;
     dirp = opendir(dir_name);
     if (!dirp) {
        Log("1 namei_ConvertROtoRWvolume: Could not opendir(%s)\n", dir_name);
-       return EIO;
+       code = EIO;
+       goto done;
     }
 
     while ((dp = readdir(dirp))) {
@@ -1491,48 +1679,55 @@ namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
 
        if (*dp->d_name == '.')
            continue;
-       if (DecodeInode(dir_name, dp->d_name, &info, h->ih_vid) < 0) {
+       if (DecodeInode(dir_name, dp->d_name, &info, ih->ih_vid) < 0) {
            Log("1 namei_ConvertROtoRWvolume: DecodeInode failed for %s/%s\n",
                dir_name, dp->d_name);
            closedir(dirp);
-           return -1;
+           code = -1;
+           goto done;
        }
        if (info.u.param[1] != -1) {
            Log("1 namei_ConvertROtoRWvolume: found other than volume special file %s/%s\n", dir_name, dp->d_name);
            closedir(dirp);
-           return -1;
+           code = -1;
+           goto done;
        }
-       if (info.u.param[0] != vid) {
-           if (info.u.param[0] == h->ih_vid) {
+       if (info.u.param[0] != volumeId) {
+           if (info.u.param[0] == ih->ih_vid) {
                if (info.u.param[2] == VI_LINKTABLE) {  /* link table */
                    linkSeen = 1;
                    continue;
                }
            }
-           Log("1 namei_ConvertROtoRWvolume: found special file %s/%s for volume %lu\n", dir_name, dp->d_name, info.u.param[0]);
+           Log("1 namei_ConvertROtoRWvolume: found special file %s/%s"
+               " for volume %lu\n", dir_name, dp->d_name,
+               afs_printable_uint32_lu(info.u.param[0]));
            closedir(dirp);
-           return VVOLEXISTS;
+           code = VVOLEXISTS;
+           goto done;
        }
        if (info.u.param[2] == VI_VOLINFO) {    /* volume info file */
-           strcpy(infoName, dp->d_name);
+           strlcpy(infoName, dp->d_name, sizeof(infoName));
            infoSeen = 1;
        } else if (info.u.param[2] == VI_SMALLINDEX) {  /* small vnodes file */
-           strcpy(smallName, dp->d_name);
+           strlcpy(smallName, dp->d_name, sizeof(smallName));
            smallSeen = 1;
        } else if (info.u.param[2] == VI_LARGEINDEX) {  /* large vnodes file */
-           strcpy(largeName, dp->d_name);
+           strlcpy(largeName, dp->d_name, sizeof(largeName));
            largeSeen = 1;
        } else {
            closedir(dirp);
            Log("1 namei_ConvertROtoRWvolume: unknown type %d of special file found : %s/%s\n", info.u.param[2], dir_name, dp->d_name);
-           return -1;
+           code = -1;
+           goto done;
        }
     }
     closedir(dirp);
 
     if (!infoSeen || !smallSeen || !largeSeen || !linkSeen) {
        Log("1 namei_ConvertROtoRWvolume: not all special files found in %s\n", dir_name);
-       return -1;
+       code = -1;
+       goto done;
     }
 
     /*
@@ -1540,63 +1735,98 @@ namei_ConvertROtoRWvolume(IHandle_t * h, afs_uint32 vid)
      * proceed.
      */
 
-    bzero(&t_ih, sizeof(t_ih));
-    t_ih.ih_dev = h->ih_dev;
-    t_ih.ih_vid = h->ih_vid;
+    memset(&t_ih, 0, sizeof(t_ih));
+    t_ih.ih_dev = ih->ih_dev;
+    t_ih.ih_vid = ih->ih_vid;
 
     (void)afs_snprintf(oldpath, sizeof oldpath, "%s/%s", dir_name, infoName);
-    fd = open(oldpath, O_RDWR, 0);
+    fd = afs_open(oldpath, O_RDWR, 0);
     if (fd < 0) {
        Log("1 namei_ConvertROtoRWvolume: could not open RO info file: %s\n",
            oldpath);
-       return -1;
+       code = -1;
+       goto done;
     }
-    t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_VOLINFO);
+    t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_VOLINFO);
     namei_HandleToName(&n, &t_ih);
-    fd2 = open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
+    fd2 = afs_open(n.n_path, O_CREAT | O_EXCL | O_TRUNC | O_RDWR, 0);
     if (fd2 < 0) {
        Log("1 namei_ConvertROtoRWvolume: could not create RW info file: %s\n", n.n_path);
        close(fd);
-       return -1;
+       code = -1;
+       goto done;
     }
-    code = convertVolumeInfo(fd, fd2, h->ih_vid);
+    code = convertVolumeInfo(fd, fd2, ih->ih_vid);
     close(fd);
     if (code) {
        close(fd2);
        unlink(n.n_path);
-       return -1;
+       code = -1;
+       goto done;
     }
-    SetOGM(fd2, h->ih_vid, 1);
+    SetOGM(fd2, ih->ih_vid, 1);
     close(fd2);
 
-    t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_SMALLINDEX);
+    t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_SMALLINDEX);
     namei_HandleToName(&n, &t_ih);
     (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, smallName);
-    fd = open(newpath, O_RDWR, 0);
+    fd = afs_open(newpath, O_RDWR, 0);
     if (fd < 0) {
        Log("1 namei_ConvertROtoRWvolume: could not open SmallIndex file: %s\n", newpath);
-       return -1;
+       code = -1;
+       goto done;
     }
-    SetOGM(fd, h->ih_vid, 2);
+    SetOGM(fd, ih->ih_vid, 2);
     close(fd);
     link(newpath, n.n_path);
     unlink(newpath);
 
-    t_ih.ih_ino = namei_MakeSpecIno(h->ih_vid, VI_LARGEINDEX);
+    t_ih.ih_ino = namei_MakeSpecIno(ih->ih_vid, VI_LARGEINDEX);
     namei_HandleToName(&n, &t_ih);
     (void)afs_snprintf(newpath, sizeof newpath, "%s/%s", dir_name, largeName);
-    fd = open(newpath, O_RDWR, 0);
+    fd = afs_open(newpath, O_RDWR, 0);
     if (fd < 0) {
        Log("1 namei_ConvertROtoRWvolume: could not open LargeIndex file: %s\n", newpath);
-       return -1;
+       code = -1;
+       goto done;
     }
-    SetOGM(fd, h->ih_vid, 3);
+    SetOGM(fd, ih->ih_vid, 3);
     close(fd);
     link(newpath, n.n_path);
     unlink(newpath);
 
     unlink(oldpath);
-    return 0;
+
+    h.id = h.parent;
+    h.volumeInfo_hi = h.id;
+    h.smallVnodeIndex_hi = h.id;
+    h.largeVnodeIndex_hi = h.id;
+    h.linkTable_hi = h.id;
+
+    if (VCreateVolumeDiskHeader(&h, partP)) {
+        Log("1 namei_ConvertROtoRWvolume: Couldn't write header for RW-volume %lu\n",
+           afs_printable_uint32_lu(h.id));
+       code = EIO;
+       goto done;
+    }
+
+    if (VDestroyVolumeDiskHeader(partP, volumeId, h.parent)) {
+        Log("1 namei_ConvertROtoRWvolume: Couldn't unlink header for RO-volume %lu\n",
+           afs_printable_uint32_lu(volumeId));
+    }
+
+    FSYNC_VolOp(volumeId, pname, FSYNC_VOL_DONE, 0, NULL);
+    FSYNC_VolOp(h.id, pname, FSYNC_VOL_ON, 0, NULL);
+
+ done:
+# ifdef AFS_DEMAND_ATTACH_FS
+    if (locktype) {
+       VUnlockVolumeById(volumeId, partP);
+    }
+# endif /* AFS_DEMAND_ATTACH_FS */
+#endif
+
+    return code;
 }
 
 /* PrintInode