volser: dont double-stat vnodes when dumping
[openafs.git] / src / volser / dumpstuff.c
index 2b25dc8..16f5e79 100644 (file)
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
-#include <sys/types.h>
 #include <ctype.h>
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#ifdef AFS_NT40_ENV
-#include <fcntl.h>
-#else
-#include <sys/param.h>
-#include <sys/file.h>
-#include <sys/uio.h>
-#include <netinet/in.h>
-#include <unistd.h>
-#endif
-#include <sys/stat.h>
-#ifdef AFS_PTHREAD_ENV
-#include <assert.h>
-#else /* AFS_PTHREAD_ENV */
-#include <afs/assert.h>
-#endif /* AFS_PTHREAD_ENV */
+
+#include <afs/afs_assert.h>
 #include <rx/xdr.h>
 #include <rx/rx.h>
 #include <afs/afsint.h>
 #include <afs/vnode.h>
 #include <afs/volume.h>
 #include <afs/partition.h>
-#include "dump.h"
 #include <afs/daemon_com.h>
 #include <afs/fssync.h>
 #include <afs/acl.h>
 #include <afs/com_err.h>
 #include <afs/vol_prototypes.h>
+
+#include "dump.h"
 #include "volser.h"
 #include "volint.h"
 #include "dumpstuff.h"
@@ -79,7 +64,7 @@ static int DumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
                     int volid, int vnodeNumber, int dumpEverything);
 static int ReadDumpHeader(struct iod *iodp, struct DumpHeader *hp);
 static int ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
-                     afs_int32 * Lbuf, afs_int32 s1, afs_int32 * Sbuf,
+                     afs_foff_t * Lbuf, afs_int32 s1, afs_foff_t * Sbuf,
                      afs_int32 s2, afs_int32 delo);
 static afs_fsize_t volser_WriteFile(int vn, struct iod *iodp,
                                    FdHandle_t * handleP, int tag,
@@ -177,7 +162,7 @@ iod_Write(struct iod *iodp, char *buf, int nbytes)
     int code, i;
     int one_success = 0;
 
-    assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
+    osi_Assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
           || (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
 
     if (iodp->call) {
@@ -545,8 +530,8 @@ DumpByte(struct iod *iodp, char tag, byte value)
     return ((iod_Write(iodp, tbuffer, 2) == 2) ? 0 : VOLSERDUMPERROR);
 }
 
-#define putint32(p, v)  *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
-#define putshort(p, v) *p++ = v>>8, *p++ = v
+#define afs_putint32(p, v)  *p++ = v>>24, *p++ = v>>16, *p++ = v>>8, *p++ = v
+#define afs_putshort(p, v) *p++ = v>>8, *p++ = v
 
 static int
 DumpDouble(struct iod *iodp, char tag, afs_uint32 value1,
@@ -555,8 +540,8 @@ DumpDouble(struct iod *iodp, char tag, afs_uint32 value1,
     char tbuffer[9];
     byte *p = (unsigned char *)tbuffer;
     *p++ = tag;
-    putint32(p, value1);
-    putint32(p, value2);
+    afs_putint32(p, value1);
+    afs_putint32(p, value2);
     return ((iod_Write(iodp, tbuffer, 9) == 9) ? 0 : VOLSERDUMPERROR);
 }
 
@@ -566,7 +551,7 @@ DumpInt32(struct iod *iodp, char tag, afs_uint32 value)
     char tbuffer[5];
     byte *p = (unsigned char *)tbuffer;
     *p++ = tag;
-    putint32(p, value);
+    afs_putint32(p, value);
     return ((iod_Write(iodp, tbuffer, 5) == 5) ? 0 : VOLSERDUMPERROR);
 }
 
@@ -579,7 +564,7 @@ DumpArrayInt32(struct iod *iodp, char tag,
     int code = 0;
     byte *p = (unsigned char *)tbuffer;
     *p++ = tag;
-    putshort(p, nelem);
+    afs_putshort(p, nelem);
     code = iod_Write(iodp, tbuffer, 3);
     if (code != 3)
        return VOLSERDUMPERROR;
@@ -587,7 +572,7 @@ DumpArrayInt32(struct iod *iodp, char tag,
        p = (unsigned char *)tbuffer;
        v = *array++;           /*this was register */
 
-       putint32(p, v);
+       afs_putint32(p, v);
        code = iod_Write(iodp, tbuffer, 4);
        if (code != 4)
            return VOLSERDUMPERROR;
@@ -709,7 +694,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
 {
     int code = 0, error = 0;
     afs_int32 pad = 0;
-    afs_int32 offset = 0;
+    afs_foff_t offset = 0;
     afs_sfsize_t nbytes, howBig;
     ssize_t n;
     size_t howMany;
@@ -720,7 +705,6 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
 #ifndef AFS_NT40_ENV
     struct afs_stat status;
 #endif
-    afs_sfsize_t size;
 #ifdef AFS_AIX_ENV
 #include <sys/statfs.h>
 #if defined(AFS_AIX52_ENV)
@@ -759,8 +743,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
 #endif /* AFS_NT40_ENV */
 
 
-    size = FDH_SIZE(handleP);
-    SplitInt64(size, hi, lo);
+    SplitInt64(howBig, hi, lo);
     if (hi == 0L) {
        code = DumpInt32(iodp, 'f', lo);
     } else {
@@ -776,7 +759,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
        return VOLSERDUMPERROR;
     }
 
-    for (nbytes = size; (nbytes && !error); nbytes -= howMany) {
+    for (nbytes = howBig; (nbytes && !error); nbytes -= howMany) {
        if (nbytes < howMany)
            howMany = nbytes;
 
@@ -788,7 +771,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
         * amount that we had null padded.
         */
        if ((n > 0) && pad) {
-           Log("1 Volser: DumpFile: Null padding file %d bytes at offset %u\n", pad, offset);
+           Log("1 Volser: DumpFile: Null padding file %d bytes at offset %lld\n", pad, (long long)offset);
            pad = 0;
        }
 
@@ -816,7 +799,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
            /* Now seek over the data we could not get. An error here means we
             * can't do the next read.
             */
-           howFar = (size_t)((size - nbytes) + howMany);
+           howFar = (size_t)((howBig - nbytes) + howMany);
        }
 
        /* Now write the data out */
@@ -828,8 +811,8 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
     }
 
     if (pad) {                 /* Any padding we hadn't reported yet */
-       Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %u\n",
-           pad, offset);
+       Log("1 Volser: DumpFile: Null padding file: %d bytes at offset %lld\n",
+           pad, (long long)offset);
     }
 
     free(p);
@@ -991,15 +974,15 @@ DumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     int vnodeIndex;
 
     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
-    assert(fdP != NULL);
+    osi_Assert(fdP != NULL);
     file = FDH_FDOPEN(fdP, "r+");
-    assert(file != NULL);
+    osi_Assert(file != NULL);
     size = OS_SIZE(fdP->fd_fd);
-    assert(size != -1);
+    osi_Assert(size != -1);
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
-       assert((nVnodes + 1) * vcp->diskSize == size);
-       assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
+       osi_Assert((nVnodes + 1) * vcp->diskSize == size);
+       osi_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;
@@ -1106,11 +1089,12 @@ DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, int volid,
 
 
 int
-ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
+ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
             int del)
 {
-    int i, nVnodes, offset, code;
-    afs_int32 *Buf;
+    int i, nVnodes, code;
+    afs_foff_t offset;
+    afs_foff_t *Buf;
     int cnt = 0;
     afs_sfsize_t size;
     StreamHandle_t *afile;
@@ -1158,7 +1142,7 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
        OS_SYNC(afile->str_fd);
     } else {
        size = OS_SIZE(fdP->fd_fd);
-       assert(size != -1);
+       osi_Assert(size != -1);
        nVnodes =
            (size <=
             vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
@@ -1167,13 +1151,13 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
                Log("RestoreVolume ProcessIndex: Set up %d inodes for volume %d\n",
                    nVnodes, V_id(vp));
            }
-           Buf = (afs_int32 *) malloc(nVnodes * sizeof(afs_int32));
+           Buf = malloc(nVnodes * sizeof(afs_foff_t));
            if (Buf == NULL) {
                STREAM_CLOSE(afile);
                FDH_CLOSE(fdP);
                return -1;
            }
-           memset(Buf, 0, nVnodes * sizeof(afs_int32));
+           memset(Buf, 0, nVnodes * sizeof(afs_foff_t));
            STREAM_ASEEK(afile, offset = vcp->diskSize);
            while (1) {
                code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
@@ -1210,7 +1194,7 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
     Volume *vp;
     struct iod iod;
     struct iod *iodp = &iod;
-    afs_int32 *b1 = NULL, *b2 = NULL;
+    afs_foff_t *b1 = NULL, *b2 = NULL;
     int s1 = 0, s2 = 0, delo = 0, tdelo;
     int tag;
 
@@ -1304,7 +1288,7 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
 
 static int
 ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
-          afs_int32 * Lbuf, afs_int32 s1, afs_int32 * Sbuf, afs_int32 s2,
+          afs_foff_t * Lbuf, afs_int32 s1, afs_foff_t * Sbuf, afs_int32 s2,
           afs_int32 delo)
 {
     afs_int32 vnodeNumber;
@@ -1317,7 +1301,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
     struct VnodeClassInfo *vcp;
     IHandle_t *tmpH;
     FdHandle_t *fdP;
-    Inode nearInode;
+    Inode nearInode AFS_UNUSED;
     afs_int32 critical = 0;
 
     tag = iod_getc(iodp);
@@ -1414,6 +1398,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                    if (!VALID_INO(ino)) {
                        Log("1 Volser: ReadVnodes: IH_CREATE: %s - restore aborted\n",
                             afs_error_message(errno));
+                       V_needsSalvaged(vp) = 1;
                        return VOLSERREAD_DUMPERROR;
                    }
                    nearInode = ino;
@@ -1424,6 +1409,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                        Log("1 Volser: ReadVnodes: IH_OPEN: %s - restore aborted\n",
                             afs_error_message(errno));
                        IH_RELEASE(tmpH);
+                       V_needsSalvaged(vp) = 1;
                        return VOLSERREAD_DUMPERROR;
                    }
                    vnodeLength =
@@ -1435,6 +1421,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                        Log("1 Volser: ReadVnodes: IDEC inode %llu\n",
                            (afs_uintmax_t) ino);
                        IH_DEC(V_linkHandle(vp), ino, V_parentId(vp));
+                       V_needsSalvaged(vp) = 1;
                        return VOLSERREAD_DUMPERROR;
                    }
                    break;
@@ -1468,6 +1455,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
            if (fdP == NULL) {
                Log("1 Volser: ReadVnodes: Error opening vnode index: %s; restore aborted\n",
                    afs_error_message(errno));
+               V_needsSalvaged(vp) = 1;
                return VOLSERREAD_DUMPERROR;
            }
            if (FDH_PREAD(fdP, &oldvnode, sizeof(oldvnode), vnodeIndexOffset(vcp, vnodeNumber)) ==
@@ -1482,6 +1470,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                Log("1 Volser: ReadVnodes: Error writing vnode index: %s; restore aborted\n",
                    afs_error_message(errno));
                FDH_REALLYCLOSE(fdP);
+               V_needsSalvaged(vp) = 1;
                return VOLSERREAD_DUMPERROR;
            }
            FDH_CLOSE(fdP);
@@ -1858,15 +1847,15 @@ SizeDumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     int vnodeIndex;
 
     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
-    assert(fdP != NULL);
+    osi_Assert(fdP != NULL);
     file = FDH_FDOPEN(fdP, "r+");
-    assert(file != NULL);
+    osi_Assert(file != NULL);
     size = OS_SIZE(fdP->fd_fd);
-    assert(size != -1);
+    osi_Assert(size != -1);
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
-       assert((nVnodes + 1) * vcp->diskSize == size);
-       assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
+       osi_Assert((nVnodes + 1) * vcp->diskSize == size);
+       osi_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;