Always check return code from iod_Write
[openafs.git] / src / volser / dumpstuff.c
index e2d79bc..845d642 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Copyright 2000, International Business Machines Corporation and others.
  * All Rights Reserved.
- * 
+ *
  * This software has been released under the terms of the IBM Public
  * License.  For details, see the LICENSE file in the top-level source
  * directory or online at http://www.openafs.org/dl/license10.html
@@ -168,8 +168,8 @@ iod_InitMulti(struct iod *iodp, struct rx_call **calls, int ncalls,
 /* For the single dump case, it's ok to just return the "bytes written"
  * that rx_Write returns, since all the callers of iod_Write abort when
  * the returned value is less than they expect.  For the multi dump case,
- * I don't think we want half the replicas to go bad just because one 
- * connection timed out, but if they all time out, then we should give up. 
+ * I don't think we want half the replicas to go bad just because one
+ * connection timed out, but if they all time out, then we should give up.
  */
 static int
 iod_Write(struct iod *iodp, char *buf, int nbytes)
@@ -664,6 +664,8 @@ DumpStandardTag(struct iod *iodp, char tag, afs_uint32 section)
         return VOLSERDUMPERROR;
     }
     code = iod_Write(iodp, &tag, 1);
+    if (code != 1)
+       return VOLSERDUMPERROR;
     return 0;
 }
 
@@ -711,9 +713,10 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
     afs_sfsize_t nbytes, howBig;
     ssize_t n;
     size_t howMany;
-    afs_foff_t lcode = 0;
+    afs_foff_t howFar = 0;
     byte *p;
     afs_uint32 hi, lo;
+    afs_ino_str_t stmp;
 #ifndef AFS_NT40_ENV
     struct afs_stat status;
 #endif
@@ -737,7 +740,7 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
     howBig = status.st_size;
 
 #ifdef AFS_AIX_ENV
-    /* Unfortunately in AIX valuable fields such as st_blksize are 
+    /* Unfortunately in AIX valuable fields such as st_blksize are
      * gone from the stat structure.
      */
 #if defined(AFS_AIX52_ENV)
@@ -777,8 +780,9 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
        if (nbytes < howMany)
            howMany = nbytes;
 
-       /* Read the data - unless we know we can't */
-       n = (lcode ? 0 : FDH_READ(handleP, p, howMany));
+       /* Read the data */
+       n = FDH_PREAD(handleP, p, howMany, howFar);
+       howFar += n;
 
        /* If read any good data and we null padded previously, log the
         * amount that we had null padded.
@@ -796,12 +800,12 @@ DumpFile(struct iod *iodp, int vnode, FdHandle_t * handleP)
            /* Record the read error */
            if (n < 0) {
                n = 0;
-               Log("1 Volser: DumpFile: Error reading inode %s for vnode %d: %s\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode, afs_error_message(errno));
+               Log("1 Volser: DumpFile: Error reading inode %s for vnode %d: %s\n", PrintInode(stmp, handleP->fd_ih->ih_ino), vnode, afs_error_message(errno));
            } else if (!pad) {
-               Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
+               Log("1 Volser: DumpFile: Error reading inode %s for vnode %d\n", PrintInode(stmp, handleP->fd_ih->ih_ino), vnode);
            }
 
-           /* Pad the rest of the buffer with zeros. Remember offset we started 
+           /* Pad the rest of the buffer with zeros. Remember offset we started
             * padding. Keep total tally of padding.
             */
            memset(p + n, 0, howMany - n);
@@ -812,17 +816,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.
             */
-           lcode = FDH_SEEK(handleP, (size_t)((size - nbytes) + howMany), SEEK_SET);
-           if (lcode != ((size - nbytes) + howMany)) {
-               if (lcode < 0) {
-                   Log("1 Volser: DumpFile: Error seeking in inode %s for vnode %d: %s\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode, afs_error_message(errno));
-               } else {
-                   Log("1 Volser: DumpFile: Error seeking in inode %s for vnode %d\n", PrintInode(NULL, handleP->fd_ih->ih_ino), vnode);
-                   lcode = -1;
-               }
-           } else {
-               lcode = 0;
-           }
+           howFar = (size_t)((size - nbytes) + howMany);
        }
 
        /* Now write the data out */
@@ -1005,7 +999,7 @@ DumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
        assert((nVnodes + 1) * vcp->diskSize == size);
-       assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
+       assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;
@@ -1136,21 +1130,21 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
        for (i = 0; i < *sizep; i++) {
            if (Buf[i]) {
                cnt++;
-               STREAM_SEEK(afile, Buf[i], 0);
+               STREAM_ASEEK(afile, Buf[i]);
                code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
                if (code == 1) {
                    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), 
+                           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));
                        }
                        IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(vnode),
                               V_parentId(vp));
                        DOPOLL;
                    }
-                   STREAM_SEEK(afile, Buf[i], 0);
+                   STREAM_ASEEK(afile, Buf[i]);
                    (void)STREAM_WRITE(zero, vcp->diskSize, 1, afile);  /* Zero it out */
                }
                Buf[i] = 0;
@@ -1180,7 +1174,7 @@ ProcessIndex(Volume * vp, VnodeClass class, afs_int32 ** Bufp, int *sizep,
                return -1;
            }
            memset(Buf, 0, nVnodes * sizeof(afs_int32));
-           STREAM_SEEK(afile, offset = vcp->diskSize, 0);
+           STREAM_ASEEK(afile, offset = vcp->diskSize);
            while (1) {
                code = STREAM_READ(vnode, vcp->diskSize, 1, afile);
                if (code != 1) {
@@ -1476,14 +1470,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                    afs_error_message(errno));
                return VOLSERREAD_DUMPERROR;
            }
-           if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
-               0) {
-               Log("1 Volser: ReadVnodes: Error seeking into vnode index: %s; restore aborted\n",
-                   afs_error_message(errno));
-               FDH_REALLYCLOSE(fdP);
-               return VOLSERREAD_DUMPERROR;
-           }
-           if (FDH_READ(fdP, &oldvnode, sizeof(oldvnode)) ==
+           if (FDH_PREAD(fdP, &oldvnode, sizeof(oldvnode), vnodeIndexOffset(vcp, vnodeNumber)) ==
                sizeof(oldvnode)) {
                if (oldvnode.type != vNull && VNDISK_GET_INO(&oldvnode)) {
                    IH_DEC(V_linkHandle(vp), VNDISK_GET_INO(&oldvnode),
@@ -1491,14 +1478,7 @@ ReadVnodes(struct iod *iodp, Volume * vp, int incremental,
                }
            }
            vnode->vnodeMagic = vcp->magic;
-           if (FDH_SEEK(fdP, vnodeIndexOffset(vcp, vnodeNumber), SEEK_SET) <
-               0) {
-               Log("1 Volser: ReadVnodes: Error seeking into vnode index: %s; restore aborted\n",
-                   afs_error_message(errno));
-               FDH_REALLYCLOSE(fdP);
-               return VOLSERREAD_DUMPERROR;
-           }
-           if (FDH_WRITE(fdP, vnode, vcp->diskSize) != vcp->diskSize) {
+           if (FDH_PWRITE(fdP, vnode, vcp->diskSize, vnodeIndexOffset(vcp, vnodeNumber)) != vcp->diskSize) {
                Log("1 Volser: ReadVnodes: Error writing vnode index: %s; restore aborted\n",
                    afs_error_message(errno));
                FDH_REALLYCLOSE(fdP);
@@ -1570,7 +1550,7 @@ volser_WriteFile(int vn, struct iod *iodp, FdHandle_t * handleP, int tag,
            break;
        }
        if (handleP) {
-           nBytes = FDH_WRITE(handleP, p, size);
+           nBytes = FDH_PWRITE(handleP, p, size, written);
            if (nBytes > 0)
                written += nBytes;
            if (nBytes != size) {
@@ -1893,7 +1873,7 @@ SizeDumpVnodeIndex(struct iod *iodp, Volume * vp, VnodeClass class,
     nVnodes = (size / vcp->diskSize) - 1;
     if (nVnodes > 0) {
        assert((nVnodes + 1) * vcp->diskSize == size);
-       assert(STREAM_SEEK(file, vcp->diskSize, 0) == 0);
+       assert(STREAM_ASEEK(file, vcp->diskSize) == 0);
     } else
        nVnodes = 0;
     for (vnodeIndex = 0;