volser: update log formatting in dump and restore
[openafs.git] / src / volser / dumpstuff.c
index 11e0745..8c01aa0 100644 (file)
@@ -14,9 +14,9 @@
 
 #include <ctype.h>
 
-#include <afs/afs_assert.h>
-#include <rx/xdr.h>
+#include <afs/opr.h>
 #include <rx/rx.h>
+#include <rx/rx_queue.h>
 #include <afs/afsint.h>
 #include <afs/nfs.h>
 #include <afs/errors.h>
@@ -50,6 +50,7 @@
 /*@printflike@*/ extern void Log(const char *format, ...);
 
 extern int DoLogging;
+extern int DoPreserveVolumeStats;
 
 
 /* Forward Declarations */
@@ -61,7 +62,7 @@ static int DumpVnodeIndex(struct iod *iodp, Volume * vp,
                          VnodeClass class, afs_int32 fromtime,
                          int forcedump);
 static int DumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
-                    int volid, int vnodeNumber, int dumpEverything);
+                    VolumeId 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_foff_t * Lbuf, afs_int32 s1, afs_foff_t * Sbuf,
@@ -81,11 +82,13 @@ static int SizeDumpVnodeIndex(struct iod *iodp, Volume * vp,
                              int forcedump,
                              struct volintSize *size);
 static int SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v,
-                        int volid, int vnodeNumber, int dumpEverything,
+                        VolumeId volid, int vnodeNumber, int dumpEverything,
                         struct volintSize *size);
 
 #define MAX_SECTIONS    3
-#define MIN_TLV_TAG     5
+
+/* The TLV range must start above D_MAX */
+#define MIN_TLV_TAG     21
 #define MAX_TLV_TAG     0x60
 #define MAX_STANDARD_TAG 0x7a
 static afs_uint32 oldtags[MAX_SECTIONS][16];
@@ -162,7 +165,7 @@ iod_Write(struct iod *iodp, char *buf, int nbytes)
     int code, i;
     int one_success = 0;
 
-    osi_Assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
+    opr_Assert((iodp->call && iodp->ncalls == 1 && !iodp->calls)
           || (!iodp->call && iodp->ncalls >= 1 && iodp->calls));
 
     if (iodp->call) {
@@ -280,7 +283,8 @@ ReadStandardTagLen(struct iod *iodp, unsigned char tag, afs_int32 section,
     afs_int32 code, i;
     afs_uint32 off = tag >> 5;
     afs_uint32 mask = 1 << (tag & 0x1f);
-    unsigned char len, buf[8], *p;
+    int len;
+    unsigned char buf[8], *p;
 
     if (!oldtagsInited)
         initNonStandardTags();
@@ -294,7 +298,9 @@ ReadStandardTagLen(struct iod *iodp, unsigned char tag, afs_int32 section,
     }
     if (tag <= MAX_TLV_TAG) {
         len = iod_getc(iodp);
-        if (len < 128)
+       if (len == EOF)
+           return VOLSERDUMPERROR;
+       else if (len < 128)
             *length = len;
         else {
             len &= 0x7f;
@@ -704,8 +710,9 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
     afs_ino_str_t stmp;
 #ifndef AFS_NT40_ENV
     struct afs_stat status;
+#else
+    LARGE_INTEGER fileSize;
 #endif
-    afs_sfsize_t size;
 #ifdef AFS_AIX_ENV
 #include <sys/statfs.h>
 #if defined(AFS_AIX52_ENV)
@@ -717,7 +724,11 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
 #endif
 
 #ifdef AFS_NT40_ENV
-    howBig = _filelength(handleP->fd_fd);
+    if (!GetFileSizeEx(handleP->fd_fd, &fileSize)) {
+        Log("DumpFile: GetFileSizeEx returned error code %d on descriptor %d\n", GetLastError(), handleP->fd_fd);
+           return VOLSERDUMPERROR;
+    }
+    howBig = fileSize.QuadPart;
     howMany = 4096;
 
 #else
@@ -744,8 +755,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 {
@@ -761,7 +771,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;
 
@@ -801,7 +811,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 */
@@ -976,15 +986,15 @@ DumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     int vnodeIndex;
 
     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
-    osi_Assert(fdP != NULL);
+    opr_Assert(fdP != NULL);
     file = FDH_FDOPEN(fdP, "r+");
-    osi_Assert(file != NULL);
+    opr_Assert(file != NULL);
     size = OS_SIZE(fdP->fd_fd);
-    osi_Assert(size != -1);
+    opr_Assert(size != -1);
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
-       osi_Assert((nVnodes + 1) * vcp->diskSize == size);
-       osi_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
+       opr_Assert((nVnodes + 1) * vcp->diskSize == size);
+       opr_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;
@@ -1027,19 +1037,34 @@ DumpDumpHeader(struct iod *iodp, Volume * vp,
     if (!code)
        code = DumpString(iodp, 'n', V_name(vp));
     dumpTimes[0] = fromtime;
-    dumpTimes[1] = V_backupDate(vp);   /* Until the time the clone was made */
+    switch (V_type(vp)) {
+    case readwriteVolume:
+       dumpTimes[1] = V_updateDate(vp);        /* until last update */
+       break;
+    case readonlyVolume:
+       dumpTimes[1] = V_creationDate(vp);      /* until clone was updated */
+       break;
+    case backupVolume:
+       /* until backup was made */
+       dumpTimes[1] = V_backupDate(vp) != 0 ? V_backupDate(vp) :
+                                              V_creationDate(vp);
+       break;
+    default:
+       code = EINVAL;
+    }
     if (!code)
        code = DumpArrayInt32(iodp, 't', (afs_uint32 *) dumpTimes, 2);
     return code;
 }
 
 static int
-DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, int volid,
+DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
          int vnodeNumber, int dumpEverything)
 {
     int code = 0;
     IHandle_t *ihP;
     FdHandle_t *fdP;
+    afs_ino_str_t stmp;
 
     if (!v || v->type == vNull)
        return code;
@@ -1075,11 +1100,28 @@ DumpVnode(struct iod *iodp, struct VnodeDiskObject *v, int volid,
                               VAclDiskSize(v));
     }
     if (VNDISK_GET_INO(v)) {
+       afs_sfsize_t indexlen, disklen;
        IH_INIT(ihP, iodp->device, iodp->parentId, VNDISK_GET_INO(v));
        fdP = IH_OPEN(ihP);
        if (fdP == NULL) {
-           Log("1 Volser: DumpVnode: dump: Unable to open inode %llu for vnode %u (volume %i); not dumped, error %d\n", (afs_uintmax_t) VNDISK_GET_INO(v), vnodeNumber, volid, errno);
+           Log("1 Volser: DumpVnode: dump: Unable to open inode %s "
+               "for vnode %u (volume %" AFS_VOLID_FMT "); "
+               "not dumped, error %d\n",
+               PrintInode(stmp, VNDISK_GET_INO(v)), vnodeNumber,
+               afs_printable_VolumeId_lu(volid), errno);
+           IH_RELEASE(ihP);
+           return VOLSERREAD_DUMPERROR;
+       }
+       VNDISK_GET_LEN(indexlen, v);
+       disklen = FDH_SIZE(fdP);
+       if (indexlen != disklen) {
+           FDH_REALLYCLOSE(fdP);
            IH_RELEASE(ihP);
+           Log("DumpVnode: volume %"AFS_VOLID_FMT" "
+               "vnode %lu has inconsistent length "
+               "(index %lu disk %lu); aborting dump\n",
+               afs_printable_VolumeId_lu(volid), (unsigned long)vnodeNumber,
+               (unsigned long)indexlen, (unsigned long)disklen);
            return VOLSERREAD_DUMPERROR;
        }
        code = DumpFile(iodp, vnodeNumber, fdP);
@@ -1104,6 +1146,7 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
     struct VnodeClassInfo *vcp = &VnodeClassInfo[class];
     char buf[SIZEOF_LARGEDISKVNODE], zero[SIZEOF_LARGEDISKVNODE];
     struct VnodeDiskObject *vnode = (struct VnodeDiskObject *)buf;
+    afs_ino_str_t stmp;
 
     memset(zero, 0, sizeof(zero));     /* zero out our proto-vnode */
     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
@@ -1122,9 +1165,12 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
                    if (vnode->type != vNull && VNDISK_GET_INO(vnode)) {
                        cnt1++;
                        if (DoLogging) {
-                           Log("RestoreVolume %u Cleanup: Removing old vnode=%u inode=%llu size=unknown\n",
-                     V_id(vp), bitNumberToVnodeNumber(i, class),
-                     (afs_uintmax_t) VNDISK_GET_INO(vnode));
+                           Log("RestoreVolume %"AFS_VOLID_FMT" "
+                               "Cleanup: Removing old vnode=%u inode=%s "
+                               "size=unknown\n",
+                               afs_printable_VolumeId_lu(V_id(vp)),
+                               bitNumberToVnodeNumber(i, class),
+                               PrintInode(stmp, VNDISK_GET_INO(vnode)));
                        }
                        IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
                               V_parentId(vp));
@@ -1144,22 +1190,17 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
        OS_SYNC(afile->str_fd);
     } else {
        size = OS_SIZE(fdP->fd_fd);
-       osi_Assert(size != -1);
+       opr_Assert(size != -1);
        nVnodes =
            (size <=
             vcp->diskSize ? 0 : size - vcp->diskSize) >> vcp->logSize;
        if (nVnodes > 0) {
-           if (DoLogging) {
-               Log("RestoreVolume ProcessIndex: Set up %d inodes for volume %d\n",
-                   nVnodes, V_id(vp));
-           }
-           Buf = malloc(nVnodes * sizeof(afs_foff_t));
+           Buf = calloc(nVnodes,  sizeof(afs_foff_t));
            if (Buf == NULL) {
                STREAM_CLOSE(afile);
                FDH_CLOSE(fdP);
                return -1;
            }
-           memset(Buf, 0, nVnodes * sizeof(afs_foff_t));
            STREAM_ASEEK(afile, offset = vcp->diskSize);
            while (1) {
                code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
@@ -1172,9 +1213,6 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_foff_t ** Bufp, int *sizep,
                }
                offset += vcp->diskSize;
            }
-           if (DoLogging) {
-               Log("RestoreVolume ProcessIndex: found %d inodes\n", cnt);
-           }
            *Bufp = Buf;
            *sizep = nVnodes;
        }
@@ -1199,11 +1237,16 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
     afs_foff_t *b1 = NULL, *b2 = NULL;
     int s1 = 0, s2 = 0, delo = 0, tdelo;
     int tag;
+    VolumeDiskData saved_header;
 
     iod_Init(iodp, call);
 
     vp = avp;
 
+    if (DoPreserveVolumeStats) {
+       CopyVolumeStats(&V_disk(vp), &saved_header);
+    }
+
     if (!ReadDumpHeader(iodp, &header)) {
        Log("1 Volser: RestoreVolume: Error reading header file for dump; aborted\n");
        return VOLSERREAD_DUMPERROR;
@@ -1230,6 +1273,8 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
     vol.cloneId = cookie->clone;
     vol.parentId = cookie->parent;
 
+    V_needsSalvaged(vp) = 0;
+
     tdelo = delo;
     while (1) {
        if (ReadVnodes(iodp, vp, 0, b1, s1, b2, s2, tdelo)) {
@@ -1270,7 +1315,16 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
     }
 
   clean:
-    ClearVolumeStats(&vol);
+    if (DoPreserveVolumeStats) {
+       CopyVolumeStats(&saved_header, &vol);
+    } else {
+       ClearVolumeStats(&vol);
+    }
+    if (V_needsSalvaged(vp)) {
+       /* needsSalvaged may have been set while we tried to write volume data.
+        * prevent it from getting overwritten. */
+       vol.needsSalvaged = V_needsSalvaged(vp);
+    }
     CopyVolumeHeader(&vol, &V_disk(vp));
     V_destroyMe(vp) = 0;
     VUpdateVolume(&vupdate, vp);
@@ -1282,9 +1336,9 @@ RestoreVolume(struct rx_call *call, Volume * avp, int incremental,
   out:
     /* Free the malloced space above */
     if (b1)
-       free((char *)b1);
+       free(b1);
     if (b2)
-       free((char *)b2);
+       free(b2);
     return error;
 }
 
@@ -1303,7 +1357,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);
@@ -1318,9 +1372,6 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
        if (!ReadInt32(iodp, &vnode->uniquifier))
            return VOLSERREAD_DUMPERROR;
 
-       if (DoLogging) {
-           Log("ReadVnodes: setup %d/%d\n", vnodeNumber, vnode->uniquifier);
-       }
        while ((tag = iod_getc(iodp)) > D_MAX && tag != EOF) {
            haveStuff = 1;
             if (critical)
@@ -1392,24 +1443,26 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                    }
                    saw_f = 1;
 
-                   ino =
-                       IH_CREATE(V_linkHandle(vp), V_device(vp),
+                   tmpH =
+                       IH_CREATE_INIT(V_linkHandle(vp), V_device(vp),
                                  VPartitionPath(V_partition(vp)), nearInode,
                                  V_parentId(vp), vnodeNumber,
                                  vnode->uniquifier, vnode->dataVersion);
-                   if (!VALID_INO(ino)) {
+                   if (!tmpH) {
                        Log("1 Volser: ReadVnodes: IH_CREATE: %s - restore aborted\n",
                             afs_error_message(errno));
+                       V_needsSalvaged(vp) = 1;
                        return VOLSERREAD_DUMPERROR;
                    }
+                   ino = tmpH->ih_ino;
                    nearInode = ino;
                    VNDISK_SET_INO(vnode, ino);
-                   IH_INIT(tmpH, vp->device, V_parentId(vp), ino);
                    fdP = IH_OPEN(tmpH);
                    if (fdP == NULL) {
                        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 =
@@ -1421,6 +1474,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;
@@ -1454,6 +1508,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)) ==
@@ -1468,6 +1523,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);
@@ -1514,7 +1570,7 @@ volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
        }
        FillInt64(filesize, filesize_high, filesize_low);
     }
-    p = (unsigned char *)malloc(size);
+    p = malloc(size);
     if (p == NULL) {
        *status = 2;
        return (0);
@@ -1750,7 +1806,7 @@ SizeDumpDumpHeader(struct iod *iodp, Volume * vp,
 }
 
 static int
-SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v, int volid,
+SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v, VolumeId volid,
              int vnodeNumber, int dumpEverything,
              struct volintSize *v_size)
 {
@@ -1804,7 +1860,11 @@ SizeDumpVnode(struct iod *iodp, struct VnodeDiskObject *v, int volid,
     }
 
     if (VNDISK_GET_INO(v)) {
-       FillInt64(addvar,0, (v->length + 5));
+       VNDISK_GET_LEN(addvar, v);
+       if (v->vn_length_hi)
+           addvar += 9;
+       else
+           addvar += 5;
        AddUInt64(v_size->dump_size, addvar, &v_size->dump_size);
     }
     return code;
@@ -1844,15 +1904,15 @@ SizeDumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     int vnodeIndex;
 
     fdP = IH_OPEN(vp->vnodeIndex[class].handle);
-    osi_Assert(fdP != NULL);
+    opr_Assert(fdP != NULL);
     file = FDH_FDOPEN(fdP, "r+");
-    osi_Assert(file != NULL);
+    opr_Assert(file != NULL);
     size = OS_SIZE(fdP->fd_fd);
-    osi_Assert(size != -1);
+    opr_Assert(size != -1);
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
-       osi_Assert((nVnodes + 1) * vcp->diskSize == size);
-       osi_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
+       opr_Assert((nVnodes + 1) * vcp->diskSize == size);
+       opr_Assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;