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
10 #include <afsconfig.h>
11 #include <afs/param.h>
18 #define UBIK_INTERNALS
23 static struct buffer {
24 struct ubik_dbase *dbase; /*!< dbase within which the buffer resides */
25 afs_int32 file; /*!< Unique cache key */
26 afs_int32 page; /*!< page number */
27 struct buffer *lru_next;
28 struct buffer *lru_prev;
29 struct buffer *hashNext; /*!< next dude in hash table */
30 char *data; /*!< ptr to the data */
31 char lockers; /*!< usage ref count */
32 char dirty; /*!< is buffer modified */
33 char hashIndex; /*!< back ptr to hash table */
36 #define pHash(page) ((page) & (PHSIZE-1))
38 afs_int32 ubik_nBuffers = NBUFFERS;
39 static struct buffer *phTable[PHSIZE]; /*!< page hash table */
40 static struct buffer *LruBuffer;
42 static int calls = 0, ios = 0, lastb = 0;
43 static char *BufferData;
44 static struct buffer *newslot(struct ubik_dbase *adbase, afs_int32 afid,
46 #define BADFID 0xffffffff
48 static int DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length);
50 static struct ubik_trunc *freeTruncList = 0;
53 * \brief Remove a transaction from the database's active transaction list. Don't free it.
56 unthread(struct ubik_trans *atrans)
58 struct ubik_trans **lt, *tt;
59 lt = &atrans->dbase->activeTrans;
60 for (tt = *lt; tt; lt = &tt->next, tt = *lt) {
67 return 2; /* no entry */
71 * \brief some debugging assistance
74 udisk_Debug(struct ubik_debug *aparm)
79 memcpy(&aparm->localVersion, &ubik_dbase->version,
80 sizeof(struct ubik_version));
81 aparm->lockedPages = 0;
82 aparm->writeLockedPages = 0;
84 for (i = 0; i < nbuffers; i++, tb++) {
88 aparm->writeLockedPages++;
94 * \brief Write an opcode to the log.
96 * log format is defined here, and implicitly in recovery.c
98 * 4 byte opcode, followed by parameters, each 4 bytes long. All integers
99 * are in logged in network standard byte order, in case we want to move logs
100 * from machine-to-machine someday.
102 * Begin transaction: opcode \n
103 * Commit transaction: opcode, version (8 bytes) \n
104 * Truncate file: opcode, file number, length \n
105 * Abort transaction: opcode \n
106 * Write data: opcode, file, position, length, <length> data bytes \n
109 udisk_LogOpcode(struct ubik_dbase *adbase, afs_int32 aopcode, int async)
111 struct ubik_stat ustat;
114 /* figure out where to write */
115 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
119 /* setup data and do write */
120 aopcode = htonl(aopcode);
122 (*adbase->write) (adbase, LOGFILE, (char *)&aopcode, ustat.size,
124 if (code != sizeof(afs_int32))
127 /* optionally sync data */
129 code = (*adbase->sync) (adbase, LOGFILE);
136 * \brief Log a commit, never syncing.
139 udisk_LogEnd(struct ubik_dbase *adbase, struct ubik_version *aversion)
143 struct ubik_stat ustat;
145 /* figure out where to write */
146 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
151 data[0] = htonl(LOGEND);
152 data[1] = htonl(aversion->epoch);
153 data[2] = htonl(aversion->counter);
157 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
158 3 * sizeof(afs_int32));
159 if (code != 3 * sizeof(afs_int32))
162 /* finally sync the log */
163 code = (*adbase->sync) (adbase, LOGFILE);
168 * \brief Log a truncate operation, never syncing.
171 udisk_LogTruncate(struct ubik_dbase *adbase, afs_int32 afile,
176 struct ubik_stat ustat;
178 /* figure out where to write */
179 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
184 data[0] = htonl(LOGTRUNCATE);
185 data[1] = htonl(afile);
186 data[2] = htonl(alength);
190 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
191 3 * sizeof(afs_int32));
192 if (code != 3 * sizeof(afs_int32))
198 * \brief Write some data to the log, never syncing.
201 udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, void *abuffer,
202 afs_int32 apos, afs_int32 alen)
204 struct ubik_stat ustat;
209 /* find end of log */
210 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
216 data[0] = htonl(LOGDATA);
217 data[1] = htonl(afile);
218 data[2] = htonl(apos);
219 data[3] = htonl(alen);
223 (*adbase->write) (adbase, LOGFILE, (char *)data, lpos, 4 * sizeof(afs_int32));
224 if (code != 4 * sizeof(afs_int32))
226 lpos += 4 * sizeof(afs_int32);
229 code = (*adbase->write) (adbase, LOGFILE, abuffer, lpos, alen);
236 udisk_Init(int abuffers)
238 /* Initialize the venus buffer system. */
241 Buffers = (struct buffer *)malloc(abuffers * sizeof(struct buffer));
242 memset(Buffers, 0, abuffers * sizeof(struct buffer));
243 BufferData = (char *)malloc(abuffers * UBIK_PAGESIZE);
245 for (i = 0; i < PHSIZE; i++)
247 for (i = 0; i < abuffers; i++) {
248 /* Fill in each buffer with an empty indication. */
250 tb->lru_next = &(Buffers[i + 1]);
251 tb->lru_prev = &(Buffers[i - 1]);
252 tb->data = &BufferData[UBIK_PAGESIZE * i];
255 Buffers[0].lru_prev = &(Buffers[abuffers - 1]);
256 Buffers[abuffers - 1].lru_next = &(Buffers[0]);
257 LruBuffer = &(Buffers[0]);
262 * \brief Take a buffer and mark it as the least recently used buffer.
265 Dlru(struct buffer *abuf)
267 if (LruBuffer == abuf)
270 /* Unthread from where it is in the list */
271 abuf->lru_next->lru_prev = abuf->lru_prev;
272 abuf->lru_prev->lru_next = abuf->lru_next;
274 /* Thread onto beginning of LRU list */
275 abuf->lru_next = LruBuffer;
276 abuf->lru_prev = LruBuffer->lru_prev;
278 LruBuffer->lru_prev->lru_next = abuf;
279 LruBuffer->lru_prev = abuf;
284 * \brief Take a buffer and mark it as the most recently used buffer.
287 Dmru(struct buffer *abuf)
289 if (LruBuffer == abuf) {
290 LruBuffer = LruBuffer->lru_next;
294 /* Unthread from where it is in the list */
295 abuf->lru_next->lru_prev = abuf->lru_prev;
296 abuf->lru_prev->lru_next = abuf->lru_next;
298 /* Thread onto end of LRU list - making it the MRU buffer */
299 abuf->lru_next = LruBuffer;
300 abuf->lru_prev = LruBuffer->lru_prev;
301 LruBuffer->lru_prev->lru_next = abuf;
302 LruBuffer->lru_prev = abuf;
306 MatchBuffer(struct buffer *buf, int page, afs_int32 fid,
307 struct ubik_trans *atrans)
309 if (buf->page != page) {
312 if (buf->file != fid) {
315 if (atrans->type == UBIK_READTRANS && buf->dirty) {
316 /* if 'buf' is dirty, it has uncommitted changes; we do not want to
317 * see uncommitted changes if we are a read transaction, so skip over
321 if (buf->dbase != atrans->dbase) {
328 * \brief Get a pointer to a particular buffer.
331 DRead(struct ubik_trans *atrans, afs_int32 fid, int page)
333 /* Read a page from the disk. */
334 struct buffer *tb, *lastbuffer, *found_tb = NULL;
336 struct ubik_dbase *dbase = atrans->dbase;
339 lastbuffer = LruBuffer->lru_prev;
341 /* Skip for write transactions for a clean page - this may not be the right page to use */
342 if (MatchBuffer(lastbuffer, page, fid, atrans)
343 && (atrans->type == UBIK_READTRANS || lastbuffer->dirty)) {
349 for (tb = phTable[pHash(page)]; tb; tb = tb->hashNext) {
350 if (MatchBuffer(tb, page, fid, atrans)) {
351 if (tb->dirty || atrans->type == UBIK_READTRANS) {
355 /* Remember this clean page - we might use it */
359 /* For a write transaction, use a matching clean page if no dirty one was found */
363 return found_tb->data;
367 tb = newslot(dbase, fid, page);
370 memset(tb->data, 0, UBIK_PAGESIZE);
374 (*dbase->read) (dbase, fid, tb->data, page * UBIK_PAGESIZE,
380 ubik_print("Ubik: Error reading database file: errno=%d\n", errno);
385 /* Note that findslot sets the page field in the buffer equal to
386 * what it is searching for.
392 * \brief Zap truncated pages.
395 DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length)
400 struct ubik_dbase *dbase = atrans->dbase;
402 maxPage = (length + UBIK_PAGESIZE - 1) >> UBIK_LOGPAGESIZE; /* first invalid page now in file */
403 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
404 if (tb->page >= maxPage && tb->file == fid && tb->dbase == dbase) {
413 * \brief Allocate a truncation entry.
415 * We allocate special entries representing truncations, rather than
416 * performing them immediately, so that we can abort a transaction easily by simply purging
417 * the in-core memory buffers and discarding these truncation entries.
419 static struct ubik_trunc *
422 struct ubik_trunc *tt;
423 if (!freeTruncList) {
425 (struct ubik_trunc *)malloc(sizeof(struct ubik_trunc));
426 freeTruncList->next = (struct ubik_trunc *)0;
429 freeTruncList = tt->next;
434 * \brief Free a truncation entry.
437 PutTrunc(struct ubik_trunc *at)
439 at->next = freeTruncList;
445 * \brief Find a truncation entry for a file, if any.
447 static struct ubik_trunc *
448 FindTrunc(struct ubik_trans *atrans, afs_int32 afile)
450 struct ubik_trunc *tt;
451 for (tt = atrans->activeTruncs; tt; tt = tt->next) {
452 if (tt->file == afile)
455 return (struct ubik_trunc *)0;
459 * \brief Do truncates associated with \p atrans, and free them.
462 DoTruncs(struct ubik_trans *atrans)
464 struct ubik_trunc *tt, *nt;
465 int (*tproc) (struct ubik_dbase *, afs_int32, afs_int32);
466 afs_int32 rcode = 0, code;
468 tproc = atrans->dbase->truncate;
469 for (tt = atrans->activeTruncs; tt; tt = nt) {
471 DTrunc(atrans, tt->file, tt->length); /* zap pages from buffer cache */
472 code = (*tproc) (atrans->dbase, tt->file, tt->length);
477 /* don't unthread, because we do the entire list's worth here */
478 atrans->activeTruncs = (struct ubik_trunc *)0;
483 * \brief Mark an \p fid as invalid.
486 udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid)
491 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
492 if (tb->file == afid) {
501 * \brief Move this page into the correct hash bucket.
504 FixupBucket(struct buffer *ap)
506 struct buffer **lp, *tp;
508 /* first try to get it out of its current hash bucket, in which it might not be */
511 for (tp = *lp; tp; tp = tp->hashNext) {
518 /* now figure the new hash bucket */
520 ap->hashIndex = i; /* remember where we are for deletion */
521 ap->hashNext = phTable[i]; /* add us to the list */
527 * \brief Create a new slot for a particular dbase page.
529 static struct buffer *
530 newslot(struct ubik_dbase *adbase, afs_int32 afid, afs_int32 apage)
532 /* Find a usable buffer slot */
534 struct buffer *pp, *tp;
536 pp = 0; /* last pure */
537 for (i = 0, tp = LruBuffer; i < nbuffers; i++, tp = tp->lru_next) {
538 if (!tp->lockers && !tp->dirty) {
545 /* There are no unlocked buffers that don't need to be written to the disk. */
547 ("Ubik: Internal Error: Unable to find free buffer in ubik cache\n");
551 /* Now fill in the header. */
556 FixupBucket(pp); /* move to the right hash bucket */
562 * \brief Release a buffer, specifying whether or not the buffer has been modified by the locker.
565 DRelease(char *ap, int flag)
572 index = (int)(ap - (char *)BufferData) >> UBIK_LOGPAGESIZE;
573 bp = &(Buffers[index]);
581 * \brief Flush all modified buffers, leaves dirty bits set (they're cleared
584 * \note Note interaction with DSync(): you call this thing first,
585 * writing the buffers to the disk. Then you call DSync() to sync all the
586 * files that were written, and to clear the dirty bits. You should
587 * always call DFlush/DSync as a pair.
590 DFlush(struct ubik_trans *atrans)
595 struct ubik_dbase *adbase = atrans->dbase;
598 for (i = 0; i < nbuffers; i++, tb++) {
600 code = tb->page * UBIK_PAGESIZE; /* offset within file */
602 (*adbase->write) (adbase, tb->file, tb->data, code,
604 if (code != UBIK_PAGESIZE)
612 * \brief Flush all modified buffers.
615 DAbort(struct ubik_trans *atrans)
621 for (i = 0; i < nbuffers; i++, tb++) {
632 * Invalidate any buffers that are duplicates of abuf. Duplicate buffers
633 * can appear if a read transaction reads a page that is dirty, then that
634 * dirty page is synced. The read transaction will skip over the dirty page,
635 * and create a new buffer, and when the dirty page is synced, it will be
636 * identical (except for contents) to the read-transaction buffer.
639 DedupBuffer(struct buffer *abuf)
642 for (tb = phTable[pHash(abuf->page)]; tb; tb = tb->hashNext) {
643 if (tb->page == abuf->page && tb != abuf && tb->file == abuf->file
644 && tb->dbase == abuf->dbase) {
653 * \attention DSync() must only be called after DFlush(), due to its interpretation of dirty flag.
656 DSync(struct ubik_trans *atrans)
663 struct ubik_dbase *adbase = atrans->dbase;
668 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
669 if (tb->dirty == 1) {
672 if (file != BADFID && tb->file == file) {
680 /* otherwise we have a file to sync */
681 code = (*adbase->sync) (adbase, file);
689 * \brief Same as DRead(), only do not even try to read the page.
692 DNew(struct ubik_trans *atrans, afs_int32 fid, int page)
695 struct ubik_dbase *dbase = atrans->dbase;
697 if ((tb = newslot(dbase, fid, page)) == 0)
700 memset(tb->data, 0, UBIK_PAGESIZE);
705 * \brief Read data from database.
708 udisk_read(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
709 afs_int32 apos, afs_int32 alen)
712 afs_int32 offset, len, totalLen;
714 if (atrans->flags & TRDONE)
718 bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
721 /* otherwise, min of remaining bytes and end of buffer to user mode */
722 offset = apos & (UBIK_PAGESIZE - 1);
723 len = UBIK_PAGESIZE - offset;
726 memcpy(abuffer, bp + offset, len);
727 abuffer = (char *)abuffer + len;
737 * \brief Truncate file.
740 udisk_truncate(struct ubik_trans *atrans, afs_int32 afile, afs_int32 alength)
743 struct ubik_trunc *tt;
745 if (atrans->flags & TRDONE)
747 if (atrans->type != UBIK_WRITETRANS)
750 /* write a truncate log record */
751 code = udisk_LogTruncate(atrans->dbase, afile, alength);
753 /* don't truncate until commit time */
754 tt = FindTrunc(atrans, afile);
756 /* this file not truncated yet */
758 tt->next = atrans->activeTruncs;
759 atrans->activeTruncs = tt;
761 tt->length = alength;
763 /* already truncated to a certain length */
764 if (tt->length > alength)
765 tt->length = alength;
771 * \brief Write data to database, using logs.
774 udisk_write(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
775 afs_int32 apos, afs_int32 alen)
778 afs_int32 offset, len, totalLen;
779 struct ubik_trunc *tt;
782 if (atrans->flags & TRDONE)
784 if (atrans->type != UBIK_WRITETRANS)
787 /* first write the data to the log */
788 code = udisk_LogWriteData(atrans->dbase, afile, abuffer, apos, alen);
792 /* expand any truncations of this file */
793 tt = FindTrunc(atrans, afile);
795 if (tt->length < apos + alen) {
796 tt->length = apos + alen;
803 bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
805 bp = DNew(atrans, afile, apos >> UBIK_LOGPAGESIZE);
808 memset(bp, 0, UBIK_PAGESIZE);
810 /* otherwise, min of remaining bytes and end of buffer to user mode */
811 offset = apos & (UBIK_PAGESIZE - 1);
812 len = UBIK_PAGESIZE - offset;
815 memcpy(bp + offset, abuffer, len);
816 abuffer = (char *)abuffer + len;
820 DRelease(bp, 1); /* buffer modified */
826 * \brief Begin a new local transaction.
829 udisk_begin(struct ubik_dbase *adbase, int atype, struct ubik_trans **atrans)
832 struct ubik_trans *tt;
834 *atrans = (struct ubik_trans *)NULL;
835 if (atype == UBIK_WRITETRANS) {
836 if (adbase->flags & DBWRITING)
838 code = udisk_LogOpcode(adbase, LOGNEW, 0);
842 tt = (struct ubik_trans *)malloc(sizeof(struct ubik_trans));
843 memset(tt, 0, sizeof(struct ubik_trans));
845 tt->next = adbase->activeTrans;
846 adbase->activeTrans = tt;
848 if (atype == UBIK_READTRANS)
850 else if (atype == UBIK_WRITETRANS) {
852 adbase->flags |= DBWRITING;
860 * \brief Commit transaction.
863 udisk_commit(struct ubik_trans *atrans)
865 struct ubik_dbase *dbase;
867 struct ubik_version oldversion, newversion;
869 if (atrans->flags & TRDONE)
872 if (atrans->type == UBIK_WRITETRANS) {
873 dbase = atrans->dbase;
875 /* On the first write to the database. We update the versions */
876 if (ubeacon_AmSyncSite() && !(urecovery_state & UBIK_RECLABELDB)) {
878 oldversion = dbase->version;
879 newversion.epoch = FT_ApproxTime();;
880 newversion.counter = 1;
882 code = (*dbase->setlabel) (dbase, 0, &newversion);
885 version_globals.ubik_epochTime = newversion.epoch;
886 dbase->version = newversion;
889 /* Ignore the error here. If the call fails, the site is
890 * marked down and when we detect it is up again, we will
891 * send the entire database to it.
893 ContactQuorum_DISK_SetVersion( atrans, 1 /*CStampVersion */ ,
894 &oldversion, &newversion);
895 urecovery_state |= UBIK_RECLABELDB;
899 dbase->version.counter++; /* bump commit count */
900 #ifdef AFS_PTHREAD_ENV
901 CV_BROADCAST(&dbase->version_cond);
903 LWP_NoYieldSignal(&dbase->version);
905 code = udisk_LogEnd(dbase, &dbase->version);
907 dbase->version.counter--;
912 /* If we fail anytime after this, then panic and let the
913 * recovery replay the log.
915 code = DFlush(atrans); /* write dirty pages to respective files */
917 panic("Writing Ubik DB modifications\n");
918 code = DSync(atrans); /* sync the files and mark pages not dirty */
920 panic("Synchronizing Ubik DB modifications\n");
922 code = DoTruncs(atrans); /* Perform requested truncations */
924 panic("Truncating Ubik DB\n");
926 /* label the committed dbase */
927 code = (*dbase->setlabel) (dbase, 0, &dbase->version);
929 panic("Truncating Ubik DB\n");
931 code = (*dbase->truncate) (dbase, LOGFILE, 0); /* discard log (optional) */
933 panic("Truncating Ubik logfile\n");
937 /* When the transaction is marked done, it also means the logfile
938 * has been truncated.
940 atrans->flags |= TRDONE;
945 * \brief Abort transaction.
948 udisk_abort(struct ubik_trans *atrans)
950 struct ubik_dbase *dbase;
953 if (atrans->flags & TRDONE)
956 /* Check if we are the write trans before logging abort, lest we
957 * abort a good write trans in progress.
958 * We don't really care if the LOGABORT gets to the log because we
959 * truncate the log next. If the truncate fails, we panic; for
960 * otherwise, the log entries remain. On restart, replay of the log
961 * will do nothing because the abort is there or no LogEnd opcode.
963 dbase = atrans->dbase;
964 if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
965 udisk_LogOpcode(dbase, LOGABORT, 1);
966 code = (*dbase->truncate) (dbase, LOGFILE, 0);
968 panic("Truncating Ubik logfile during an abort\n");
969 DAbort(atrans); /* remove all dirty pages */
972 /* When the transaction is marked done, it also means the logfile
973 * has been truncated.
975 atrans->flags |= (TRABORT | TRDONE);
980 * \brief Destroy a transaction after it has been committed or aborted.
982 * If it hasn't committed before you call this routine, we'll abort the
983 * transaction for you.
986 udisk_end(struct ubik_trans *atrans)
988 struct ubik_dbase *dbase;
990 if (!(atrans->flags & TRDONE))
992 dbase = atrans->dbase;
994 ulock_relLock(atrans);
997 /* check if we are the write trans before unsetting the DBWRITING bit, else
998 * we could be unsetting someone else's bit.
1000 if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
1002 dbase->flags &= ~DBWRITING;
1003 UBIK_VERSION_UNLOCK;
1007 if (atrans->iovec_info.iovec_wrt_val)
1008 free(atrans->iovec_info.iovec_wrt_val);
1009 if (atrans->iovec_data.iovec_buf_val)
1010 free(atrans->iovec_data.iovec_buf_val);
1013 /* Wakeup any writers waiting in BeginTrans() */
1014 #ifdef AFS_PTHREAD_ENV
1015 CV_BROADCAST(&dbase->flags_cond);
1017 LWP_NoYieldSignal(&dbase->flags);