home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / log / openLogFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  6.1 KB  |  202 lines

  1. /*
  2.  *   $RCSfile: openLogFile.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:49 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "pool.h"
  45. #include "tid.h"
  46. #include "io.h"
  47. #include "lock.h"
  48. #include "object.h"
  49. #include "msgdefs.h"
  50. #include "thread.h"
  51. #include "semaphore.h"
  52. #include "latch.h"
  53. #include "link.h"
  54. #include "lsn.h"
  55. #include "bf.h"
  56. #include "volume.h"
  57. #include "openlog.h"
  58. #include "trans.h"
  59. #include "logrecs.h"
  60. #include "log_intfuncs.h"
  61. #include "log_extfuncs.h"
  62. #include "bf_extfuncs.h"
  63. #include "io_extfuncs.h"
  64. #include "log_globals.h"
  65. #include "thread_globals.h"
  66.  
  67.  
  68.  int
  69. openLogFile (
  70.  
  71.     VOLREC                *volRec,
  72.     int                    writePages,
  73.     int                    readPages,
  74.     BOOL                performRecovery
  75. )
  76. {
  77.  
  78.     register OPENLOG    *openLog;
  79.     CHECKPOINTMASTER    *checkMaster;
  80.     PID                    pid;
  81.  
  82.  
  83.     TRACE(TR_LOG, TR_LEVEL_1);
  84.  
  85.     /* get a register pointer to the openlog structure */
  86.     openLog = &OpenLog;
  87.  
  88.     /* initialize the open magic */
  89.     INIT_OPENLOG_MAGIC(openLog);
  90.  
  91.     SM_ASSERT(LEVEL_3, MAX_LOGREC_LEN <= RAISETWO(LOGREC_BUF_PAGE2SIZE));
  92.  
  93.     if (bf_OpenServerGroups(writePages, readPages) != esmNOERROR) {
  94.         return(esmFAILURE);
  95.     }
  96.  
  97.     /* mark that the write group is a force group */
  98.     openLog->writeGroup->flags |= (LOG_GROUP|FORCE_GROUP);
  99.  
  100.     /* mark the groups as log groups */
  101.     openLog->readGroup->flags |= LOG_GROUP;
  102.     openLog->writeGroup->flags |= LOG_GROUP;
  103.  
  104.     /*
  105.      *    initialize the rest of the openlog structure
  106.      */
  107.     openLog->volid           = volRec->header->volid;
  108.     openLog->volRec           = volRec;
  109.     openLog->logFileAddr   = volRec->header->logFileAddr;
  110.     openLog->readPages     = readPages;
  111.     openLog->writePages    = writePages;
  112.     openLog->page2size     = volRec->header->logFileBlock2size;
  113.     openLog->pageSize      = RAISETWO(volRec->header->logFileBlock2size);
  114.     openLog->pageMask      = openLog->pageSize - 1;
  115.     openLog->usableBytes   = openLog->pageSize - (2 * sizeof(LOGPAGEHDR));
  116.     openLog->filePages     = (int) volRec->header->numLogFileBlocks;
  117.     openLog->fileBytes     = openLog->filePages * openLog->pageSize;
  118.     openLog->blocksPerPage = RAISETWO(openLog->page2size - MIN_PAGE2SIZE);
  119.     openLog->tailPid       = NULLPID;
  120.     openLog->tailLSN       = 0;
  121.     openLog->tailBuffer    = NULL;
  122.     openLog->tailLink       = NULL;
  123.     openLog->activePid     = NULLPID;
  124.     openLog->activeLSN     = 0;
  125.     openLog->nextValidLSN.wrapCount  = 0;
  126.     openLog->nextValidLSN.offset  = 0;
  127.     openLog->flags           = NOFLAGS;
  128.     openLog->logRecordCount = 2;
  129.     openLog->checkPointCount = 0;
  130.     openLog->lastUsableByte = openLog->pageSize - sizeof(LOGPAGEHDR);
  131.     openLog->checkPointUnique = openLog->logRecordCount;
  132.  
  133.     /* This should already have been set in: processCommandLine() */
  134.     if ( openLog->checkPointInterval < 1) {
  135.         SM_ERROR(TYPE_FATAL, esmINTERNAL);
  136.     }
  137.  
  138.     /*
  139.      *    initialize the semaphores and the latches
  140.      */
  141.     initializeSemaphore( &(openLog->writeSemaphore), FALSE );
  142.     initializeSemaphore( &(openLog->readSemaphore), FALSE);
  143.     initializeLatch( &(openLog->logLatch) );
  144.     initializeList( &(openLog->checkPointList) );
  145.     initializeList( &(openLog->recoverList) );
  146.     initializeList( &(openLog->activeList) );
  147.     initializeList( &(openLog->closeList) );
  148.  
  149.     /* construct the pid of the control buffer */
  150.     pid.volid = openLog->volid;
  151.     pid.page  = volRec->header->logControlAddr;
  152.  
  153.     /*
  154.      *    check to see if we are going to recover the log
  155.      */
  156.     if (performRecovery)    {
  157.  
  158.         TRPRINT(TR_LOG, TR_LEVEL_2, ("recovering log"));
  159.             
  160.         /* read in the control buffer */
  161.         if ((openLog->controlBuffer = bf_ReadPage(openLog->readGroup, &pid,
  162.                                                   MIN_PAGE2SIZE, NOFLAGS)) == NULL)    {
  163.             SM_ERROR(TYPE_FATAL, Active->errno);
  164.         }
  165.  
  166.         /* get a pointer to the checkpoint pointer page */
  167.         checkMaster = (CHECKPOINTMASTER *) openLog->controlBuffer->bufFrame;
  168.  
  169.         /* check the magic number */
  170.         if (checkMaster->magic != CHECKPOINTMASTER_MAGIC)    {
  171.  
  172.             fprintf(sm_ErrorStream, "SERVER ERROR: log volume is incorrect.\n");
  173.             fprintf(sm_ErrorStream, "\tHave you recently reformatted it?  If so, please regenerate the log.\n");
  174.             fprintf(sm_ErrorStream, "\tRun with -h option for information on regenerating the log.\n");
  175.             SM_ERROR(TYPE_FATAL, esmBADLOGVOLUME);
  176.         }
  177.  
  178.         /*
  179.          *    record the location of the initial checkpoint
  180.          */
  181.         openLog->checkPointLSN = checkMaster->checkRecordLSN;
  182.         openLog->checkPointPid = checkMaster->checkRecordLSN.offset >> openLog->page2size;
  183.         openLog->wrapCount       = (int) checkMaster->wrapCount;
  184.         TRPRINT(TR_LOG, TR_LEVEL_2, ("checkPointLSN:%d checkPointPid:%d",
  185.                 openLog->checkPointLSN.offset, openLog->checkPointPid));
  186.             
  187.     } else {
  188.  
  189.         TRPRINT(TR_LOG, TR_LEVEL_2, ("regenerating log"));
  190.             
  191.         /*
  192.          *    read in the control buffer
  193.          */
  194.         if ((openLog->controlBuffer = bf_ReadPage(openLog->readGroup, &pid,
  195.                                                   MIN_PAGE2SIZE, BF_NOREAD)) == NULL)    {
  196.             SM_ERROR(TYPE_FATAL, Active->errno);
  197.         }
  198.     }
  199.  
  200.     return(esmNOERROR);
  201. }
  202.