ubik: cleanup DB lock usage in SDISK_SendFile
[openafs.git] / src / ubik / remote.c
index 9e1caea..ba41a5e 100644 (file)
@@ -10,6 +10,7 @@
 #include <afsconfig.h>
 #include <afs/param.h>
 
+#include <roken.h>
 
 #include <sys/types.h>
 #include <string.h>
@@ -152,13 +153,9 @@ SDISK_ReleaseLocks(struct rx_call *rxcall, struct ubik_tid *atid)
     }
 
     /* If the thread is not waiting for lock - ok to end it */
-#if !defined(UBIK_PAUSE)
     if (ubik_currentTrans->locktype != LOCKWAIT) {
-#endif /* UBIK_PAUSE */
        udisk_end(ubik_currentTrans);
-#if !defined(UBIK_PAUSE)
     }
-#endif /* UBIK_PAUSE */
     ubik_currentTrans = (struct ubik_trans *)0;
     DBRELE(dbase);
     return 0;
@@ -192,13 +189,9 @@ SDISK_Abort(struct rx_call *rxcall, struct ubik_tid *atid)
 
     code = udisk_abort(ubik_currentTrans);
     /* If the thread is not waiting for lock - ok to end it */
-#if !defined(UBIK_PAUSE)
     if (ubik_currentTrans->locktype != LOCKWAIT) {
-#endif /* UBIK_PAUSE */
        udisk_end(ubik_currentTrans);
-#if !defined(UBIK_PAUSE)
     }
-#endif /* UBIK_PAUSE */
     ubik_currentTrans = (struct ubik_trans *)0;
     DBRELE(dbase);
     return code;
@@ -474,16 +467,17 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
     struct rx_connection *tconn;
     afs_uint32 otherHost = 0;
     char hoststr[16];
-#ifndef OLD_URECOVERY
     char pbuffer[1028];
     int fd = -1;
     afs_int32 epoch = 0;
     afs_int32 pass;
-#endif
 
     /* send the file back to the requester */
 
+    dbase = ubik_dbase;
+
     if ((code = ubik_CheckAuth(rxcall))) {
+       DBHOLD(dbase);
        goto failed;
     }
 
@@ -504,10 +498,10 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
     if (offset && offset != otherHost) {
        /* we *know* this is the wrong guy */
        code = USYNC;
+       DBHOLD(dbase);
        goto failed;
     }
 
-    dbase = ubik_dbase;
     DBHOLD(dbase);
 
     /* abort any active trans that may scribble over the database */
@@ -517,15 +511,8 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
               afs_inet_ntoa_r(otherHost, hoststr));
 
     offset = 0;
-#ifdef OLD_URECOVERY
-    (*dbase->truncate) (dbase, file, 0);       /* truncate first */
-    tversion.counter = 0;
-#else
-    epoch =
-#endif
-    tversion.epoch = 0;                /* start off by labelling in-transit db as invalid */
+    epoch = tversion.epoch = 0;                /* start off by labelling in-transit db as invalid */
     (*dbase->setlabel) (dbase, file, &tversion);       /* setlabel does sync */
-#ifndef OLD_URECOVERY
     afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.TMP", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
     fd = open(pbuffer, O_CREAT | O_RDWR | O_TRUNC, 0600);
     if (fd < 0) {
@@ -538,30 +525,23 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
        goto failed;
     }
     pass = 0;
-#endif
     memcpy(&ubik_dbase->version, &tversion, sizeof(struct ubik_version));
     while (length > 0) {
        tlen = (length > sizeof(tbuffer) ? sizeof(tbuffer) : length);
-#if !defined(OLD_URECOVERY) && !defined(AFS_PTHREAD_ENV)
+#if !defined(AFS_PTHREAD_ENV)
        if (pass % 4 == 0)
            IOMGR_Poll();
 #endif
        code = rx_Read(rxcall, tbuffer, tlen);
        if (code != tlen) {
-           DBRELE(dbase);
            ubik_dprint("Rx-read length error=%d\n", code);
            code = BULK_ERROR;
            close(fd);
            goto failed;
        }
-#ifdef OLD_URECOVERY
-       code = (*dbase->write) (dbase, file, tbuffer, offset, tlen);
-#else
        code = write(fd, tbuffer, tlen);
        pass++;
-#endif
        if (code != tlen) {
-           DBRELE(dbase);
            ubik_dprint("write failed error=%d\n", code);
            code = UIOERROR;
            close(fd);
@@ -570,17 +550,12 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
        offset += tlen;
        length -= tlen;
     }
-#ifndef OLD_URECOVERY
     code = close(fd);
     if (code)
        goto failed;
-#endif
 
     /* sync data first, then write label and resync (resync done by setlabel call).
      * This way, good label is only on good database. */
-#ifdef OLD_URECOVERY
-    (*ubik_dbase->sync) (dbase, file);
-#else
     afs_snprintf(tbuffer, sizeof(tbuffer), "%s.DB%s%d", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
 #ifdef AFS_NT40_ENV
     afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
@@ -593,15 +568,12 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
        code = rename(pbuffer, tbuffer);
     if (!code) {
        (*ubik_dbase->open) (ubik_dbase, file);
-#endif
        code = (*ubik_dbase->setlabel) (dbase, file, avers);
-#ifndef OLD_URECOVERY
     }
 #ifdef AFS_NT40_ENV
     afs_snprintf(pbuffer, sizeof(pbuffer), "%s.DB%s%d.OLD", ubik_dbase->pathName, (file<0)?"SYS":"", (file<0)?-file:file);
     unlink(pbuffer);
 #endif
-#endif
     memcpy(&ubik_dbase->version, avers, sizeof(struct ubik_version));
     udisk_Invalidate(dbase, file);     /* new dbase, flush disk buffers */
 #ifdef AFS_PTHREAD_ENV
@@ -609,23 +581,22 @@ SDISK_SendFile(struct rx_call *rxcall, afs_int32 file,
 #else
     LWP_NoYieldSignal(&dbase->version);
 #endif
-    DBRELE(dbase);
-  failed:
+
+failed:
     if (code) {
-#ifndef OLD_URECOVERY
        unlink(pbuffer);
        /* Failed to sync. Allow reads again for now. */
        if (dbase != NULL) {
            tversion.epoch = epoch;
            (*dbase->setlabel) (dbase, file, &tversion);
        }
-#endif
        ubik_print
            ("Ubik: Synchronize database with server %s failed (error = %d)\n",
             afs_inet_ntoa_r(otherHost, hoststr), code);
     } else {
        ubik_print("Ubik: Synchronize database completed\n");
     }
+    DBRELE(dbase);
     return code;
 }