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>
14 #include <sys/types.h>
23 #include <netinet/in.h>
31 #define UBIK_INTERNALS
36 static struct buffer {
37 struct ubik_dbase *dbase; /*!< dbase within which the buffer resides */
38 afs_int32 file; /*!< Unique cache key */
39 afs_int32 page; /*!< page number */
40 struct buffer *lru_next;
41 struct buffer *lru_prev;
42 struct buffer *hashNext; /*!< next dude in hash table */
43 char *data; /*!< ptr to the data */
44 char lockers; /*!< usage ref count */
45 char dirty; /*!< is buffer modified */
46 char hashIndex; /*!< back ptr to hash table */
49 #define pHash(page) ((page) & (PHSIZE-1))
51 afs_int32 ubik_nBuffers = NBUFFERS;
52 static struct buffer *phTable[PHSIZE]; /*!< page hash table */
53 static struct buffer *LruBuffer;
55 static int calls = 0, ios = 0, lastb = 0;
56 static char *BufferData;
57 static struct buffer *newslot(struct ubik_dbase *adbase, afs_int32 afid,
60 #define BADFID 0xffffffff
62 static int DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length);
64 static struct ubik_trunc *freeTruncList = 0;
67 * \brief Remove a transaction from the database's active transaction list. Don't free it.
70 unthread(struct ubik_trans *atrans)
72 struct ubik_trans **lt, *tt;
73 lt = &atrans->dbase->activeTrans;
74 for (tt = *lt; tt; lt = &tt->next, tt = *lt) {
81 return 2; /* no entry */
85 * \brief some debugging assistance
88 udisk_Debug(struct ubik_debug *aparm)
93 memcpy(&aparm->localVersion, &ubik_dbase->version,
94 sizeof(struct ubik_version));
95 aparm->lockedPages = 0;
96 aparm->writeLockedPages = 0;
98 for (i = 0; i < nbuffers; i++, tb++) {
100 aparm->lockedPages++;
102 aparm->writeLockedPages++;
108 * \brief Write an opcode to the log.
110 * log format is defined here, and implicitly in recovery.c
112 * 4 byte opcode, followed by parameters, each 4 bytes long. All integers
113 * are in logged in network standard byte order, in case we want to move logs
114 * from machine-to-machine someday.
116 * Begin transaction: opcode \n
117 * Commit transaction: opcode, version (8 bytes) \n
118 * Truncate file: opcode, file number, length \n
119 * Abort transaction: opcode \n
120 * Write data: opcode, file, position, length, <length> data bytes \n
123 udisk_LogOpcode(struct ubik_dbase *adbase, afs_int32 aopcode, int async)
125 struct ubik_stat ustat;
128 /* figure out where to write */
129 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
133 /* setup data and do write */
134 aopcode = htonl(aopcode);
136 (*adbase->write) (adbase, LOGFILE, (char *)&aopcode, ustat.size,
138 if (code != sizeof(afs_int32))
141 /* optionally sync data */
143 code = (*adbase->sync) (adbase, LOGFILE);
150 * \brief Log a commit, never syncing.
153 udisk_LogEnd(struct ubik_dbase *adbase, struct ubik_version *aversion)
157 struct ubik_stat ustat;
159 /* figure out where to write */
160 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
165 data[0] = htonl(LOGEND);
166 data[1] = htonl(aversion->epoch);
167 data[2] = htonl(aversion->counter);
171 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
172 3 * sizeof(afs_int32));
173 if (code != 3 * sizeof(afs_int32))
176 /* finally sync the log */
177 code = (*adbase->sync) (adbase, LOGFILE);
182 * \brief Log a truncate operation, never syncing.
185 udisk_LogTruncate(struct ubik_dbase *adbase, afs_int32 afile,
190 struct ubik_stat ustat;
192 /* figure out where to write */
193 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
198 data[0] = htonl(LOGTRUNCATE);
199 data[1] = htonl(afile);
200 data[2] = htonl(alength);
204 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
205 3 * sizeof(afs_int32));
206 if (code != 3 * sizeof(afs_int32))
212 * \brief Write some data to the log, never syncing.
215 udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, void *abuffer,
216 afs_int32 apos, afs_int32 alen)
218 struct ubik_stat ustat;
223 /* find end of log */
224 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
230 data[0] = htonl(LOGDATA);
231 data[1] = htonl(afile);
232 data[2] = htonl(apos);
233 data[3] = htonl(alen);
237 (*adbase->write) (adbase, LOGFILE, (char *)data, lpos, 4 * sizeof(afs_int32));
238 if (code != 4 * sizeof(afs_int32))
240 lpos += 4 * sizeof(afs_int32);
243 code = (*adbase->write) (adbase, LOGFILE, abuffer, lpos, alen);
252 /* Initialize the venus buffer system. */
255 Buffers = (struct buffer *)malloc(abuffers * sizeof(struct buffer));
256 memset(Buffers, 0, abuffers * sizeof(struct buffer));
257 BufferData = (char *)malloc(abuffers * UBIK_PAGESIZE);
259 for (i = 0; i < PHSIZE; i++)
261 for (i = 0; i < abuffers; i++) {
262 /* Fill in each buffer with an empty indication. */
264 tb->lru_next = &(Buffers[i + 1]);
265 tb->lru_prev = &(Buffers[i - 1]);
266 tb->data = &BufferData[UBIK_PAGESIZE * i];
269 Buffers[0].lru_prev = &(Buffers[abuffers - 1]);
270 Buffers[abuffers - 1].lru_next = &(Buffers[0]);
271 LruBuffer = &(Buffers[0]);
276 * \brief Take a buffer and mark it as the least recently used buffer.
279 Dlru(struct buffer *abuf)
281 if (LruBuffer == abuf)
284 /* Unthread from where it is in the list */
285 abuf->lru_next->lru_prev = abuf->lru_prev;
286 abuf->lru_prev->lru_next = abuf->lru_next;
288 /* Thread onto beginning of LRU list */
289 abuf->lru_next = LruBuffer;
290 abuf->lru_prev = LruBuffer->lru_prev;
292 LruBuffer->lru_prev->lru_next = abuf;
293 LruBuffer->lru_prev = abuf;
298 * \brief Take a buffer and mark it as the most recently used buffer.
301 Dmru(struct buffer *abuf)
303 if (LruBuffer == abuf) {
304 LruBuffer = LruBuffer->lru_next;
308 /* Unthread from where it is in the list */
309 abuf->lru_next->lru_prev = abuf->lru_prev;
310 abuf->lru_prev->lru_next = abuf->lru_next;
312 /* Thread onto end of LRU list - making it the MRU buffer */
313 abuf->lru_next = LruBuffer;
314 abuf->lru_prev = LruBuffer->lru_prev;
315 LruBuffer->lru_prev->lru_next = abuf;
316 LruBuffer->lru_prev = abuf;
320 MatchBuffer(struct buffer *buf, int page, afs_int32 fid,
321 struct ubik_trans *atrans)
323 if (buf->page != page) {
326 if (buf->file != fid) {
329 if (atrans->type == UBIK_READTRANS && buf->dirty) {
330 /* if 'buf' is dirty, it has uncommitted changes; we do not want to
331 * see uncommitted changes if we are a read transaction, so skip over
335 if (buf->dbase != atrans->dbase) {
342 * \brief Get a pointer to a particular buffer.
345 DRead(struct ubik_trans *atrans, afs_int32 fid, int page)
347 /* Read a page from the disk. */
348 struct buffer *tb, *lastbuffer;
350 struct ubik_dbase *dbase = atrans->dbase;
353 lastbuffer = LruBuffer->lru_prev;
355 if (MatchBuffer(lastbuffer, page, fid, atrans)) {
361 for (tb = phTable[pHash(page)]; tb; tb = tb->hashNext) {
362 if (MatchBuffer(tb, page, fid, atrans)) {
369 tb = newslot(dbase, fid, page);
372 memset(tb->data, 0, UBIK_PAGESIZE);
376 (*dbase->read) (dbase, fid, tb->data, page * UBIK_PAGESIZE,
382 ubik_print("Ubik: Error reading database file: errno=%d\n", errno);
387 /* Note that findslot sets the page field in the buffer equal to
388 * what it is searching for.
394 * \brief Zap truncated pages.
397 DTrunc(struct ubik_trans *atrans, afs_int32 fid, afs_int32 length)
402 struct ubik_dbase *dbase = atrans->dbase;
404 maxPage = (length + UBIK_PAGESIZE - 1) >> UBIK_LOGPAGESIZE; /* first invalid page now in file */
405 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
406 if (tb->page >= maxPage && tb->file == fid && tb->dbase == dbase) {
415 * \brief Allocate a truncation entry.
417 * We allocate special entries representing truncations, rather than
418 * performing them immediately, so that we can abort a transaction easily by simply purging
419 * the in-core memory buffers and discarding these truncation entries.
421 static struct ubik_trunc *
424 struct ubik_trunc *tt;
425 if (!freeTruncList) {
427 (struct ubik_trunc *)malloc(sizeof(struct ubik_trunc));
428 freeTruncList->next = (struct ubik_trunc *)0;
431 freeTruncList = tt->next;
436 * \brief Free a truncation entry.
439 PutTrunc(struct ubik_trunc *at)
441 at->next = freeTruncList;
447 * \brief Find a truncation entry for a file, if any.
449 static struct ubik_trunc *
450 FindTrunc(struct ubik_trans *atrans, afs_int32 afile)
452 struct ubik_trunc *tt;
453 for (tt = atrans->activeTruncs; tt; tt = tt->next) {
454 if (tt->file == afile)
457 return (struct ubik_trunc *)0;
461 * \brief Do truncates associated with \p atrans, and free them.
464 DoTruncs(struct ubik_trans *atrans)
466 struct ubik_trunc *tt, *nt;
467 int (*tproc) (struct ubik_dbase *, afs_int32, afs_int32);
468 afs_int32 rcode = 0, code;
470 tproc = atrans->dbase->truncate;
471 for (tt = atrans->activeTruncs; tt; tt = nt) {
473 DTrunc(atrans, tt->file, tt->length); /* zap pages from buffer cache */
474 code = (*tproc) (atrans->dbase, tt->file, tt->length);
479 /* don't unthread, because we do the entire list's worth here */
480 atrans->activeTruncs = (struct ubik_trunc *)0;
485 * \brief Mark an \p fid as invalid.
488 udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid)
493 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
494 if (tb->file == afid) {
503 * \brief Move this page into the correct hash bucket.
506 FixupBucket(struct buffer *ap)
508 struct buffer **lp, *tp;
510 /* first try to get it out of its current hash bucket, in which it might not be */
513 for (tp = *lp; tp; tp = tp->hashNext) {
520 /* now figure the new hash bucket */
522 ap->hashIndex = i; /* remember where we are for deletion */
523 ap->hashNext = phTable[i]; /* add us to the list */
529 * \brief Create a new slot for a particular dbase page.
531 static struct buffer *
532 newslot(struct ubik_dbase *adbase, afs_int32 afid, afs_int32 apage)
534 /* Find a usable buffer slot */
536 struct buffer *pp, *tp;
538 pp = 0; /* last pure */
539 for (i = 0, tp = LruBuffer; i < nbuffers; i++, tp = tp->lru_next) {
540 if (!tp->lockers && !tp->dirty) {
547 /* There are no unlocked buffers that don't need to be written to the disk. */
549 ("Ubik: Internal Error: Unable to find free buffer in ubik cache\n");
553 /* Now fill in the header. */
558 FixupBucket(pp); /* move to the right hash bucket */
564 * \brief Release a buffer, specifying whether or not the buffer has been modified by the locker.
567 DRelease(char *ap, int flag)
574 index = (int)(ap - (char *)BufferData) >> UBIK_LOGPAGESIZE;
575 bp = &(Buffers[index]);
583 * \brief Flush all modified buffers, leaves dirty bits set (they're cleared
586 * \note Note interaction with DSync(): you call this thing first,
587 * writing the buffers to the disk. Then you call DSync() to sync all the
588 * files that were written, and to clear the dirty bits. You should
589 * always call DFlush/DSync as a pair.
592 DFlush(struct ubik_trans *atrans)
597 struct ubik_dbase *adbase = atrans->dbase;
600 for (i = 0; i < nbuffers; i++, tb++) {
602 code = tb->page * UBIK_PAGESIZE; /* offset within file */
604 (*adbase->write) (adbase, tb->file, tb->data, code,
606 if (code != UBIK_PAGESIZE)
614 * \brief Flush all modified buffers.
617 DAbort(struct ubik_trans *atrans)
623 for (i = 0; i < nbuffers; i++, tb++) {
634 * Invalidate any buffers that are duplicates of abuf. Duplicate buffers
635 * can appear if a read transaction reads a page that is dirty, then that
636 * dirty page is synced. The read transaction will skip over the dirty page,
637 * and create a new buffer, and when the dirty page is synced, it will be
638 * identical (except for contents) to the read-transaction buffer.
641 DedupBuffer(struct buffer *abuf)
644 for (tb = phTable[pHash(abuf->page)]; tb; tb = tb->hashNext) {
645 if (tb->page == abuf->page && tb != abuf && tb->file == abuf->file
646 && tb->dbase == abuf->dbase) {
655 * \attention DSync() must only be called after DFlush(), due to its interpretation of dirty flag.
658 DSync(struct ubik_trans *atrans)
665 struct ubik_dbase *adbase = atrans->dbase;
670 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
671 if (tb->dirty == 1) {
674 if (file != BADFID && tb->file == file) {
682 /* otherwise we have a file to sync */
683 code = (*adbase->sync) (adbase, file);
691 * \brief Same as DRead(), only do not even try to read the page.
694 DNew(struct ubik_trans *atrans, afs_int32 fid, int page)
697 struct ubik_dbase *dbase = atrans->dbase;
699 if ((tb = newslot(dbase, fid, page)) == 0)
702 memset(tb->data, 0, UBIK_PAGESIZE);
707 * \brief Read data from database.
710 udisk_read(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
711 afs_int32 apos, afs_int32 alen)
714 afs_int32 offset, len, totalLen;
716 if (atrans->flags & TRDONE)
720 bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
723 /* otherwise, min of remaining bytes and end of buffer to user mode */
724 offset = apos & (UBIK_PAGESIZE - 1);
725 len = UBIK_PAGESIZE - offset;
728 memcpy(abuffer, bp + offset, len);
729 abuffer = (char *)abuffer + len;
739 * \brief Truncate file.
742 udisk_truncate(struct ubik_trans *atrans, afs_int32 afile, afs_int32 alength)
745 struct ubik_trunc *tt;
747 if (atrans->flags & TRDONE)
749 if (atrans->type != UBIK_WRITETRANS)
752 /* write a truncate log record */
753 code = udisk_LogTruncate(atrans->dbase, afile, alength);
755 /* don't truncate until commit time */
756 tt = FindTrunc(atrans, afile);
758 /* this file not truncated yet */
760 tt->next = atrans->activeTruncs;
761 atrans->activeTruncs = tt;
763 tt->length = alength;
765 /* already truncated to a certain length */
766 if (tt->length > alength)
767 tt->length = alength;
773 * \brief Write data to database, using logs.
776 udisk_write(struct ubik_trans *atrans, afs_int32 afile, void *abuffer,
777 afs_int32 apos, afs_int32 alen)
780 afs_int32 offset, len, totalLen;
781 struct ubik_trunc *tt;
784 if (atrans->flags & TRDONE)
786 if (atrans->type != UBIK_WRITETRANS)
789 /* first write the data to the log */
790 code = udisk_LogWriteData(atrans->dbase, afile, abuffer, apos, alen);
794 /* expand any truncations of this file */
795 tt = FindTrunc(atrans, afile);
797 if (tt->length < apos + alen) {
798 tt->length = apos + alen;
805 bp = DRead(atrans, afile, apos >> UBIK_LOGPAGESIZE);
807 bp = DNew(atrans, afile, apos >> UBIK_LOGPAGESIZE);
810 memset(bp, 0, UBIK_PAGESIZE);
812 /* otherwise, min of remaining bytes and end of buffer to user mode */
813 offset = apos & (UBIK_PAGESIZE - 1);
814 len = UBIK_PAGESIZE - offset;
817 memcpy(bp + offset, abuffer, len);
818 abuffer = (char *)abuffer + len;
822 DRelease(bp, 1); /* buffer modified */
828 * \brief Begin a new local transaction.
831 udisk_begin(struct ubik_dbase *adbase, int atype, struct ubik_trans **atrans)
834 struct ubik_trans *tt;
836 *atrans = (struct ubik_trans *)NULL;
837 /* Make sure system is initialized before doing anything */
840 DInit(ubik_nBuffers);
842 if (atype == UBIK_WRITETRANS) {
843 if (adbase->flags & DBWRITING)
845 code = udisk_LogOpcode(adbase, LOGNEW, 0);
849 tt = (struct ubik_trans *)malloc(sizeof(struct ubik_trans));
850 memset(tt, 0, sizeof(struct ubik_trans));
852 tt->next = adbase->activeTrans;
853 adbase->activeTrans = tt;
855 if (atype == UBIK_READTRANS)
857 else if (atype == UBIK_WRITETRANS)
858 adbase->flags |= DBWRITING;
864 * \brief Commit transaction.
867 udisk_commit(struct ubik_trans *atrans)
869 struct ubik_dbase *dbase;
871 struct ubik_version oldversion, newversion;
873 if (atrans->flags & TRDONE)
876 if (atrans->type == UBIK_WRITETRANS) {
877 dbase = atrans->dbase;
879 /* On the first write to the database. We update the versions */
880 if (ubeacon_AmSyncSite() && !(urecovery_state & UBIK_RECLABELDB)) {
881 oldversion = dbase->version;
882 newversion.epoch = FT_ApproxTime();;
883 newversion.counter = 1;
885 code = (*dbase->setlabel) (dbase, 0, &newversion);
888 ubik_epochTime = newversion.epoch;
889 dbase->version = newversion;
891 /* Ignore the error here. If the call fails, the site is
892 * marked down and when we detect it is up again, we will
893 * send the entire database to it.
895 ContactQuorum_DISK_SetVersion( atrans, 1 /*CStampVersion */ ,
896 &oldversion, &newversion);
897 urecovery_state |= UBIK_RECLABELDB;
900 dbase->version.counter++; /* bump commit count */
901 #ifdef AFS_PTHREAD_ENV
902 assert(pthread_cond_broadcast(&dbase->version_cond) == 0);
904 LWP_NoYieldSignal(&dbase->version);
906 code = udisk_LogEnd(dbase, &dbase->version);
908 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 defined(UBIK_PAUSE)
991 /* Another thread is trying to lock this transaction.
992 * That can only be an RPC doing SDISK_Lock.
993 * Unlock the transaction, 'cause otherwise the other
994 * thread will never wake up. Don't free it because
995 * the caller will do that already.
997 if (atrans->flags & TRSETLOCK) {
998 atrans->flags |= TRSTALE;
999 ulock_relLock(atrans);
1002 #endif /* UBIK_PAUSE */
1003 if (!(atrans->flags & TRDONE))
1004 udisk_abort(atrans);
1005 dbase = atrans->dbase;
1007 ulock_relLock(atrans);
1010 /* check if we are the write trans before unsetting the DBWRITING bit, else
1011 * we could be unsetting someone else's bit.
1013 if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
1014 dbase->flags &= ~DBWRITING;
1018 if (atrans->iovec_info.iovec_wrt_val)
1019 free(atrans->iovec_info.iovec_wrt_val);
1020 if (atrans->iovec_data.iovec_buf_val)
1021 free(atrans->iovec_data.iovec_buf_val);
1024 /* Wakeup any writers waiting in BeginTrans() */
1025 #ifdef AFS_PTHREAD_ENV
1026 assert(pthread_cond_broadcast(&dbase->flags_cond) == 0);
1028 LWP_NoYieldSignal(&dbase->flags);