2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Dump is made to a local file. Structures are dumped in network byte order
12 * for transportability between hosts
15 #include <afsconfig.h>
16 #include <afs/param.h>
24 #include <netinet/in.h>
25 #include <sys/param.h>
28 #include <sys/types.h>
36 #include "error_macros.h"
37 #include "budb_errs.h"
38 #include "afs/audit.h"
41 /* dump ubik database - routines to scan the database and dump all
45 /* -----------------------
46 * synchronization on pipe
47 * -----------------------
50 /* interlocking for database dump */
53 dumpSyncP dumpSyncPtr = &dumpSync;
57 * check if we should dump more of the database. Waits for the reader
58 * to drain the information before allowing the writer to proceed.
68 extern dumpSyncP dumpSyncPtr;
70 ObtainWriteLock(&dumpSyncPtr->ds_lock);
72 /* let the pipe drain */
73 while (dumpSyncPtr->ds_bytes > 0) {
74 if (dumpSyncPtr->ds_readerStatus == DS_WAITING) {
75 dumpSyncPtr->ds_readerStatus = 0;
76 code = LWP_SignalProcess(&dumpSyncPtr->ds_readerStatus);
78 LogError(code, "canWrite: Signal delivery failed\n");
80 dumpSyncPtr->ds_writerStatus = DS_WAITING;
81 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
82 LWP_WaitProcess(&dumpSyncPtr->ds_writerStatus);
83 ObtainWriteLock(&dumpSyncPtr->ds_lock);
90 * record the fact that nbytes have been written. Signal the reader
91 * to proceed, and unlock.
101 extern dumpSyncP dumpSyncPtr;
103 dumpSyncPtr->ds_bytes += nbytes;
104 if (dumpSyncPtr->ds_readerStatus == DS_WAITING) {
105 dumpSyncPtr->ds_readerStatus = 0;
106 code = LWP_SignalProcess(&dumpSyncPtr->ds_readerStatus);
108 LogError(code, "haveWritten: Signal delivery failed\n");
110 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
114 * wait for the reader to drain all the information, and then set the
124 /* wait for the reader */
125 ObtainWriteLock(&dumpSyncPtr->ds_lock);
126 while (dumpSyncPtr->ds_readerStatus != DS_WAITING) {
127 LogDebug(4, "doneWriting: waiting for Reader\n");
128 dumpSyncPtr->ds_writerStatus = DS_WAITING;
129 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
130 LWP_WaitProcess(&dumpSyncPtr->ds_writerStatus);
131 ObtainWriteLock(&dumpSyncPtr->ds_lock);
134 LogDebug(4, "doneWriting: setting done\n");
136 /* signal that we are done */
138 dumpSyncPtr->ds_writerStatus = DS_DONE_ERROR;
140 dumpSyncPtr->ds_writerStatus = DS_DONE;
141 dumpSyncPtr->ds_readerStatus = 0;
142 code = LWP_NoYieldSignal(&dumpSyncPtr->ds_readerStatus);
144 LogError(code, "doneWriting: Signal delivery failed\n");
145 ReleaseWriteLock(&dumpSyncPtr->ds_lock);
149 * ut - setup and pass down
153 * write header appropriate for requested structure type
157 writeStructHeader(fid, type)
161 struct structDumpHeader hostDumpHeader, netDumpHeader;
163 hostDumpHeader.type = type;
164 hostDumpHeader.structversion = 1;
169 hostDumpHeader.size = sizeof(struct DbHeader);
173 hostDumpHeader.size = sizeof(struct budb_dumpEntry);
177 hostDumpHeader.size = sizeof(struct budb_tapeEntry);
181 hostDumpHeader.size = sizeof(struct budb_volumeEntry);
185 hostDumpHeader.size = 0;
189 LogError(0, "writeStructHeader: invalid type %d\n", type);
193 structDumpHeader_hton(&hostDumpHeader, &netDumpHeader);
195 if (canWrite(fid) <= 0)
196 return (BUDB_DUMPFAILED);
197 if (write(fid, &netDumpHeader, sizeof(netDumpHeader)) !=
198 sizeof(netDumpHeader))
199 return (BUDB_DUMPFAILED);
200 haveWritten(sizeof(netDumpHeader));
206 * write header appropriate for requested structure type
210 writeTextHeader(fid, type)
214 struct structDumpHeader hostDumpHeader, netDumpHeader;
216 hostDumpHeader.structversion = 1;
219 case TB_DUMPSCHEDULE:
220 hostDumpHeader.type = SD_TEXT_DUMPSCHEDULE;
224 hostDumpHeader.type = SD_TEXT_VOLUMESET;
228 hostDumpHeader.type = SD_TEXT_TAPEHOSTS;
232 LogError(0, "writeTextHeader: invalid type %d\n", type);
236 hostDumpHeader.size = ntohl(db.h.textBlock[type].size);
237 structDumpHeader_hton(&hostDumpHeader, &netDumpHeader);
239 if (canWrite(fid) <= 0)
240 return (BUDB_DUMPFAILED);
242 if (write(fid, &netDumpHeader, sizeof(netDumpHeader)) !=
243 sizeof(netDumpHeader))
244 return (BUDB_DUMPFAILED);
246 haveWritten(sizeof(netDumpHeader));
255 struct DbHeader header;
257 afs_int32 code = 0, tcode;
259 extern struct memoryDB db;
261 /* check the memory database header for integrity */
262 if (db.h.version != db.h.checkVersion)
263 ERROR(BUDB_DATABASEINCONSISTENT);
267 /* copy selected fields. Source is in xdr format. */
268 header.dbversion = db.h.version;
269 header.created = htonl(curtime);
270 strcpy(header.cell, "");
271 header.lastDumpId = db.h.lastDumpId;
272 header.lastInstanceId = db.h.lastInstanceId;
273 header.lastTapeId = db.h.lastTapeId;
275 tcode = writeStructHeader(fid, SD_DBHEADER);
279 if (canWrite(fid) <= 0)
280 ERROR(BUDB_DUMPFAILED);
282 if (write(fid, &header, sizeof(header)) != sizeof(header))
283 ERROR(BUDB_DUMPFAILED);
285 haveWritten(sizeof(header));
292 * write out a dump entry structure
296 writeDump(fid, dumpPtr)
300 struct budb_dumpEntry dumpEntry;
301 afs_int32 code = 0, tcode;
303 tcode = dumpToBudbDump(dumpPtr, &dumpEntry);
307 writeStructHeader(fid, SD_DUMP);
309 if (canWrite(fid) <= 0)
310 ERROR(BUDB_DUMPFAILED);
312 if (write(fid, &dumpEntry, sizeof(dumpEntry)) != sizeof(dumpEntry))
313 ERROR(BUDB_DUMPFAILED);
314 haveWritten(sizeof(dumpEntry));
321 writeTape(fid, tapePtr, dumpid)
323 struct tape *tapePtr;
326 struct budb_tapeEntry tapeEntry;
327 afs_int32 code = 0, tcode;
329 tcode = writeStructHeader(fid, SD_TAPE);
333 tapeToBudbTape(tapePtr, &tapeEntry);
335 tapeEntry.dump = htonl(dumpid);
337 if (canWrite(fid) <= 0)
338 ERROR(BUDB_DUMPFAILED);
340 if (write(fid, &tapeEntry, sizeof(tapeEntry)) != sizeof(tapeEntry))
341 ERROR(BUDB_DUMPFAILED);
343 haveWritten(sizeof(tapeEntry));
349 /* combines volFragment and volInfo */
352 writeVolume(ut, fid, volFragmentPtr, volInfoPtr, dumpid, tapeName)
353 struct ubik_trans *ut;
355 struct volFragment *volFragmentPtr;
356 struct volInfo *volInfoPtr;
360 struct budb_volumeEntry budbVolume;
363 volsToBudbVol(volFragmentPtr, volInfoPtr, &budbVolume);
365 budbVolume.dump = htonl(dumpid);
366 strcpy(budbVolume.tape, tapeName);
368 writeStructHeader(fid, SD_VOLUME);
370 if (canWrite(fid) <= 0)
371 ERROR(BUDB_DUMPFAILED);
373 if (write(fid, &budbVolume, sizeof(budbVolume)) != sizeof(budbVolume))
374 ERROR(BUDB_DUMPFAILED);
376 haveWritten(sizeof(budbVolume));
382 /* -------------------
383 * handlers for the text blocks
384 * -------------------
388 * make sure a text lock is NOT held
400 if ((textType < 0) || (textType > TB_NUM - 1))
401 return (BUDB_BADARGUMENT);
403 lockPtr = &db.h.textLocks[textType];
405 if (lockPtr->lockState != 0)
406 return (BUDB_LOCKED);
411 * check the integrity of the specified text type
414 checkText(ut, textType)
415 struct ubik_trans *ut;
418 struct textBlock *tbPtr;
419 afs_int32 nBytes = 0; /* accumulated actual size */
426 tbPtr = &db.h.textBlock[textType];
427 blockAddr = ntohl(tbPtr->textAddr);
428 size = ntohl(tbPtr->size);
430 while (blockAddr != 0) {
433 cdbread(ut, text_BLOCK, blockAddr, (char *)&block, sizeof(block));
438 if (block.h.type != text_BLOCK)
439 ERROR(BUDB_DATABASEINCONSISTENT);
441 /* add up the size */
442 nBytes += BLOCK_DATA_SIZE;
444 blockAddr = ntohl(block.h.next);
447 /* ensure that we have at least the expected amount of text */
449 ERROR(BUDB_DATABASEINCONSISTENT);
457 * textType - type of text block, e.g. TB_DUMPSCHEDULE
461 writeText(ut, fid, textType)
462 struct ubik_trans *ut;
466 struct textBlock *tbPtr;
467 afs_int32 textSize, writeSize;
472 /* check lock is free */
473 code = checkLock(textType);
477 /* ensure that this block has the correct type */
478 code = checkText(ut, textType);
480 LogError(0, "writeText: text type %d damaged\n", textType);
484 tbPtr = &db.h.textBlock[textType];
485 textSize = ntohl(tbPtr->size);
486 dbAddr = ntohl(tbPtr->textAddr);
489 goto error_exit; /* Don't save anything if no blocks */
491 writeTextHeader(fid, textType);
494 code = cdbread(ut, text_BLOCK, dbAddr, (char *)&block, sizeof(block));
498 writeSize = MIN(textSize, BLOCK_DATA_SIZE);
502 if (canWrite(fid) <= 0)
503 ERROR(BUDB_DUMPFAILED);
505 if (write(fid, &block.a[0], writeSize) != writeSize)
508 haveWritten(writeSize);
509 textSize -= writeSize;
511 dbAddr = ntohl(block.h.next);
518 #define MAXAPPENDS 200
521 writeDatabase(ut, fid)
522 struct ubik_trans *ut;
525 dbadr dbAddr, dbAppAddr;
526 struct dump diskDump, apDiskDump;
528 struct tape diskTape;
530 struct volFragment diskVolFragment;
531 struct volInfo diskVolInfo;
535 afs_int32 code = 0, tcode;
536 afs_int32 appDumpAddrs[MAXAPPENDS], numaddrs, appcount, j;
538 struct memoryHashTable *mht;
540 LogDebug(4, "writeDatabase:\n");
542 /* write out a header identifying this database etc */
543 tcode = writeDbHeader(fid);
545 LogError(tcode, "writeDatabase: Can't write Header\n");
549 /* write out the tree of dump structures */
551 mht = ht_GetType(HT_dumpIden_FUNCTION, &entrySize);
553 LogError(tcode, "writeDatabase: Can't get dump type\n");
554 ERROR(BUDB_BADARGUMENT);
557 for (old = 0; old <= 1; old++) {
559 /* only two states, old or not old */
560 length = (old ? mht->oldLength : mht->length);
564 for (hash = 0; hash < length; hash++) {
566 /* dump all the dumps in this hash bucket
568 for (dbAddr = ht_LookupBucket(ut, mht, hash, old); dbAddr; dbAddr = ntohl(diskDump.idHashChain)) { /*initialDumps */
569 /* now check if this dump had any errors/inconsistencies.
570 * If so, don't dump it
572 if (badEntry(dbAddr)) {
574 "writeDatabase: Damaged dump entry at addr 0x%x\n",
576 Log(" Skipping remainder of dumps on hash chain %d\n",
582 cdbread(ut, dump_BLOCK, dbAddr, &diskDump,
586 "writeDatabase: Can't read dump entry (addr 0x%x)\n",
588 Log(" Skipping remainder of dumps on hash chain %d\n",
593 /* Skip appended dumps, only start with initial dumps */
594 if (diskDump.initialDumpID != 0)
597 /* Skip appended dumps, only start with initial dumps. Then
598 * follow the appended dump chain so they are in order for restore.
600 appcount = numaddrs = 0;
601 for (dbAppAddr = dbAddr; dbAppAddr;
602 dbAppAddr = ntohl(apDiskDump.appendedDumpChain)) {
604 /* Check to see if we have a circular loop of appended dumps */
605 for (j = 0; j < numaddrs; j++) {
606 if (appDumpAddrs[j] == dbAppAddr)
607 break; /* circular loop */
609 if (j < numaddrs) { /* circular loop */
610 Log("writeDatabase: Circular loop found in appended dumps\n");
611 Log("Skipping rest of appended dumps of dumpID %u\n",
615 if (numaddrs >= MAXAPPENDS)
616 numaddrs = MAXAPPENDS - 1; /* don't overflow */
617 appDumpAddrs[numaddrs] = dbAppAddr;
620 /* If we dump a 1000 appended dumps, assume a loop */
621 if (appcount >= 5 * MAXAPPENDS) {
622 Log("writeDatabase: Potential circular loop of appended dumps\n");
623 Log("Skipping rest of appended dumps of dumpID %u. Dumped %d\n", ntohl(diskDump.id), appcount);
628 /* Read the dump entry */
629 if (dbAddr == dbAppAddr) {
630 /* First time through, don't need to read the dump entry again */
631 memcpy(&apDiskDump, &diskDump, sizeof(diskDump));
633 if (badEntry(dbAppAddr)) {
635 "writeDatabase: Damaged appended dump entry at addr 0x%x\n",
637 Log(" Skipping this and remainder of appended dumps of initial DumpID %u\n", ntohl(diskDump.id));
642 cdbread(ut, dump_BLOCK, dbAppAddr, &apDiskDump,
646 "writeDatabase: Can't read appended dump entry (addr 0x%x)\n",
648 Log(" Skipping this and remainder of appended dumps of initial DumpID %u\n", ntohl(diskDump.id));
652 /* Verify that this appended dump points to the initial dump */
653 if (ntohl(apDiskDump.initialDumpID) !=
654 ntohl(diskDump.id)) {
656 "writeDatabase: Appended dumpID %u does not reference initial dumpID %u\n",
657 ntohl(apDiskDump.id),
659 Log(" Skipping this appended dump\n");
664 /* Save the dump entry */
665 tcode = writeDump(fid, &apDiskDump);
668 "writeDatabase: Can't write dump entry\n");
672 /* For each tape on this dump
674 for (tapeAddr = ntohl(apDiskDump.firstTape); tapeAddr; tapeAddr = ntohl(diskTape.nextTape)) { /*tapes */
675 /* read the tape entry */
677 cdbread(ut, tape_BLOCK, tapeAddr, &diskTape,
681 "writeDatabase: Can't read tape entry (addr 0x%x) of dumpID %u\n",
682 tapeAddr, ntohl(apDiskDump.id));
683 Log(" Skipping this and remaining tapes in the dump (and all their volumes)\n");
687 /* Save the tape entry */
689 writeTape(fid, &diskTape, ntohl(apDiskDump.id));
692 "writeDatabase: Can't write tape entry\n");
696 /* For each volume on this tape.
698 for (volFragAddr = ntohl(diskTape.firstVol); volFragAddr; volFragAddr = ntohl(diskVolFragment.sameTapeChain)) { /*volumes */
699 /* Read the volume Fragment entry */
701 cdbread(ut, volFragment_BLOCK, volFragAddr,
703 sizeof(diskVolFragment));
706 "writeDatabase: Can't read volfrag entry (addr 0x%x) of dumpID %u\n",
707 volFragAddr, ntohl(apDiskDump.id));
708 Log(" Skipping this and remaining volumes on tape '%s'\n", diskTape.name);
712 /* Read the volume Info entry */
714 cdbread(ut, volInfo_BLOCK,
715 ntohl(diskVolFragment.vol),
716 &diskVolInfo, sizeof(diskVolInfo));
719 "writeDatabase: Can't read volinfo entry (addr 0x%x) of dumpID %u\n",
720 ntohl(diskVolFragment.vol),
721 ntohl(apDiskDump.id));
722 Log(" Skipping volume on tape '%s'\n",
727 /* Save the volume entry */
729 writeVolume(ut, fid, &diskVolFragment,
731 ntohl(apDiskDump.id),
735 "writeDatabase: Can't write volume entry\n");
745 /* write out the textual configuration information */
746 tcode = writeText(ut, fid, TB_DUMPSCHEDULE);
748 LogError(tcode, "writeDatabase: Can't write dump schedule\n");
751 tcode = writeText(ut, fid, TB_VOLUMESET);
753 LogError(tcode, "writeDatabase: Can't write volume set\n");
756 tcode = writeText(ut, fid, TB_TAPEHOSTS);
758 LogError(tcode, "writeDatabase: Can't write tape hosts\n");
762 tcode = writeStructHeader(fid, SD_END);
764 LogError(tcode, "writeDatabase: Can't write end savedb\n");
780 afs_int32 in, out, except;
791 code = IOMGR_Select(32, &in, &out, &except, &tp);