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>
16 #include <sys/types.h>
21 #include <netinet/in.h>
36 #define UBIK_INTERNALS
41 static struct buffer {
42 struct ubik_dbase *dbase; /* dbase within which the buffer resides */
43 afs_int32 file; /* Unique cache key */
44 afs_int32 page; /* page number */
45 struct buffer *lru_next;
46 struct buffer *lru_prev;
47 struct buffer *hashNext; /* next dude in hash table */
48 char *data; /* ptr to the data */
49 char lockers; /* usage ref count */
50 char dirty; /* is buffer modified */
51 char hashIndex; /* back ptr to hash table */
54 #define pHash(page) ((page) & (PHSIZE-1))
56 afs_int32 ubik_nBuffers = NBUFFERS;
57 static struct buffer *phTable[PHSIZE]; /* page hash table */
58 static struct buffer *LruBuffer;
60 static int calls = 0, ios = 0, lastb = 0;
61 static char *BufferData;
62 static struct buffer *newslot(struct ubik_dbase *adbase, afs_int32 afid,
65 #define BADFID 0xffffffff
67 static DTrunc(struct ubik_dbase *dbase, afs_int32 fid, afs_int32 length);
69 static struct ubik_trunc *freeTruncList = 0;
71 /* remove a transaction from the database's active transaction list. Don't free it */
73 unthread(struct ubik_trans *atrans)
75 struct ubik_trans **lt, *tt;
76 lt = &atrans->dbase->activeTrans;
77 for (tt = *lt; tt; lt = &tt->next, tt = *lt) {
84 return 2; /* no entry */
87 /* some debugging assistance */
89 udisk_Debug(struct ubik_debug *aparm)
94 memcpy(&aparm->localVersion, &ubik_dbase->version,
95 sizeof(struct ubik_version));
96 aparm->lockedPages = 0;
97 aparm->writeLockedPages = 0;
99 for (i = 0; i < nbuffers; i++, tb++) {
101 aparm->lockedPages++;
103 aparm->writeLockedPages++;
109 /* log format is defined here, and implicitly in recovery.c
111 * 4 byte opcode, followed by parameters, each 4 bytes long. All integers
112 * are in logged in network standard byte order, in case we want to move logs
113 * from machine-to-machine someday.
115 * Begin transaction: opcode
116 * Commit transaction: opcode, version (8 bytes)
117 * Truncate file: opcode, file number, length
118 * Abort transaction: opcode
119 * Write data: opcode, file, position, length, <length> data bytes
122 /* write an opcode to the log */
124 udisk_LogOpcode(struct ubik_dbase *adbase, afs_int32 aopcode, int async)
126 struct ubik_stat ustat;
129 /* figure out where to write */
130 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
134 /* setup data and do write */
135 aopcode = htonl(aopcode);
137 (*adbase->write) (adbase, LOGFILE, (char *)&aopcode, ustat.size,
139 if (code != sizeof(afs_int32))
142 /* optionally sync data */
144 code = (*adbase->sync) (adbase, LOGFILE);
150 /* log a commit, never syncing */
152 udisk_LogEnd(struct ubik_dbase *adbase, struct ubik_version *aversion)
156 struct ubik_stat ustat;
158 /* figure out where to write */
159 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
164 data[0] = htonl(LOGEND);
165 data[1] = htonl(aversion->epoch);
166 data[2] = htonl(aversion->counter);
170 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
171 3 * sizeof(afs_int32));
172 if (code != 3 * sizeof(afs_int32))
175 /* finally sync the log */
176 code = (*adbase->sync) (adbase, LOGFILE);
180 /* log a truncate operation, never syncing */
182 udisk_LogTruncate(struct ubik_dbase *adbase, afs_int32 afile,
187 struct ubik_stat ustat;
189 /* figure out where to write */
190 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
195 data[0] = htonl(LOGTRUNCATE);
196 data[1] = htonl(afile);
197 data[2] = htonl(alength);
201 (*adbase->write) (adbase, LOGFILE, (char *)data, ustat.size,
202 3 * sizeof(afs_int32));
203 if (code != 3 * sizeof(afs_int32))
208 /* write some data to the log, never syncing */
210 udisk_LogWriteData(struct ubik_dbase *adbase, afs_int32 afile, char *abuffer,
211 afs_int32 apos, afs_int32 alen)
213 struct ubik_stat ustat;
218 /* find end of log */
219 code = (*adbase->stat) (adbase, LOGFILE, &ustat);
225 data[0] = htonl(LOGDATA);
226 data[1] = htonl(afile);
227 data[2] = htonl(apos);
228 data[3] = htonl(alen);
232 (*adbase->write) (adbase, LOGFILE, (char *)data, lpos, 4 * sizeof(afs_int32));
233 if (code != 4 * sizeof(afs_int32))
235 lpos += 4 * sizeof(afs_int32);
238 code = (*adbase->write) (adbase, LOGFILE, abuffer, lpos, alen);
247 /* Initialize the venus buffer system. */
250 Buffers = (struct buffer *)malloc(abuffers * sizeof(struct buffer));
251 memset(Buffers, 0, abuffers * sizeof(struct buffer));
252 BufferData = (char *)malloc(abuffers * UBIK_PAGESIZE);
254 for (i = 0; i < PHSIZE; i++)
256 for (i = 0; i < abuffers; i++) {
257 /* Fill in each buffer with an empty indication. */
259 tb->lru_next = &(Buffers[i + 1]);
260 tb->lru_prev = &(Buffers[i - 1]);
261 tb->data = &BufferData[UBIK_PAGESIZE * i];
264 Buffers[0].lru_prev = &(Buffers[abuffers - 1]);
265 Buffers[abuffers - 1].lru_next = &(Buffers[0]);
266 LruBuffer = &(Buffers[0]);
270 /* Take a buffer and mark it as the least recently used buffer */
272 Dlru(struct buffer *abuf)
274 if (LruBuffer == abuf)
277 /* Unthread from where it is in the list */
278 abuf->lru_next->lru_prev = abuf->lru_prev;
279 abuf->lru_prev->lru_next = abuf->lru_next;
281 /* Thread onto beginning of LRU list */
282 abuf->lru_next = LruBuffer;
283 abuf->lru_prev = LruBuffer->lru_prev;
285 LruBuffer->lru_prev->lru_next = abuf;
286 LruBuffer->lru_prev = abuf;
290 /* Take a buffer and mark it as the most recently used buffer */
292 Dmru(struct buffer *abuf)
294 if (LruBuffer == abuf) {
295 LruBuffer = LruBuffer->lru_next;
299 /* Unthread from where it is in the list */
300 abuf->lru_next->lru_prev = abuf->lru_prev;
301 abuf->lru_prev->lru_next = abuf->lru_next;
303 /* Thread onto end of LRU list - making it the MRU buffer */
304 abuf->lru_next = LruBuffer;
305 abuf->lru_prev = LruBuffer->lru_prev;
306 LruBuffer->lru_prev->lru_next = abuf;
307 LruBuffer->lru_prev = abuf;
310 /* get a pointer to a particular buffer */
312 DRead(struct ubik_dbase *dbase, afs_int32 fid, int page)
314 /* Read a page from the disk. */
315 struct buffer *tb, *lastbuffer;
319 lastbuffer = LruBuffer->lru_prev;
321 if ((lastbuffer->page == page) && (lastbuffer->file == fid)
322 && (lastbuffer->dbase == dbase)) {
328 for (tb = phTable[pHash(page)]; tb; tb = tb->hashNext) {
329 if (tb->page == page && tb->file == fid && tb->dbase == dbase) {
336 tb = newslot(dbase, fid, page);
339 memset(tb->data, 0, UBIK_PAGESIZE);
343 (*dbase->read) (dbase, fid, tb->data, page * UBIK_PAGESIZE,
349 ubik_print("Ubik: Error reading database file: errno=%d\n", errno);
354 /* Note that findslot sets the page field in the buffer equal to
355 * what it is searching for.
360 /* zap truncated pages */
362 DTrunc(struct ubik_dbase *dbase, afs_int32 fid, afs_int32 length)
368 maxPage = (length + UBIK_PAGESIZE - 1) >> UBIK_LOGPAGESIZE; /* first invalid page now in file */
369 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
370 if (tb->page >= maxPage && tb->file == fid && tb->dbase == dbase) {
378 /* allocate a truncation entry. We allocate special entries representing truncations, rather than
379 performing them immediately, so that we can abort a transaction easily by simply purging
380 the in-core memory buffers and discarding these truncation entries.
382 static struct ubik_trunc *
385 struct ubik_trunc *tt;
386 if (!freeTruncList) {
388 (struct ubik_trunc *)malloc(sizeof(struct ubik_trunc));
389 freeTruncList->next = (struct ubik_trunc *)0;
392 freeTruncList = tt->next;
396 /* free a truncation entry */
398 PutTrunc(struct ubik_trunc *at)
400 at->next = freeTruncList;
405 /* find a truncation entry for a file, if any */
406 static struct ubik_trunc *
407 FindTrunc(struct ubik_trans *atrans, afs_int32 afile)
409 struct ubik_trunc *tt;
410 for (tt = atrans->activeTruncs; tt; tt = tt->next) {
411 if (tt->file == afile)
414 return (struct ubik_trunc *)0;
417 /* do truncates associated with trans, and free them */
419 DoTruncs(struct ubik_trans *atrans)
421 struct ubik_trunc *tt, *nt;
423 afs_int32 rcode = 0, code;
425 tproc = atrans->dbase->truncate;
426 for (tt = atrans->activeTruncs; tt; tt = nt) {
428 DTrunc(atrans->dbase, tt->file, tt->length); /* zap pages from buffer cache */
429 code = (*tproc) (atrans->dbase, tt->file, tt->length);
434 /* don't unthread, because we do the entire list's worth here */
435 atrans->activeTruncs = (struct ubik_trunc *)0;
439 /* mark a fid as invalid */
441 udisk_Invalidate(struct ubik_dbase *adbase, afs_int32 afid)
446 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
447 if (tb->file == afid) {
455 /* move this page into the correct hash bucket */
457 FixupBucket(struct buffer *ap)
459 struct buffer **lp, *tp;
461 /* first try to get it out of its current hash bucket, in which it might not be */
464 for (tp = *lp; tp; tp = tp->hashNext) {
471 /* now figure the new hash bucket */
473 ap->hashIndex = i; /* remember where we are for deletion */
474 ap->hashNext = phTable[i]; /* add us to the list */
479 /* create a new slot for a particular dbase page */
480 static struct buffer *
481 newslot(struct ubik_dbase *adbase, afs_int32 afid, afs_int32 apage)
483 /* Find a usable buffer slot */
485 struct buffer *pp, *tp;
487 pp = 0; /* last pure */
488 for (i = 0, tp = LruBuffer; i < nbuffers; i++, tp = tp->lru_next) {
489 if (!tp->lockers && !tp->dirty) {
496 /* There are no unlocked buffers that don't need to be written to the disk. */
498 ("Ubik: Internal Error: Unable to find free buffer in ubik cache\n");
502 /* Now fill in the header. */
507 FixupBucket(pp); /* move to the right hash bucket */
512 /* Release a buffer, specifying whether or not the buffer has been modified by the locker. */
514 DRelease(char *ap, int flag)
521 index = (int)(ap - (char *)BufferData) >> UBIK_LOGPAGESIZE;
522 bp = &(Buffers[index]);
529 /* flush all modified buffers, leaves dirty bits set (they're cleared
530 * by DSync). Note interaction with DSync: you call this thing first,
531 * writing the buffers to the disk. Then you call DSync to sync all the
532 * files that were written, and to clear the dirty bits. You should
533 * always call DFlush/DSync as a pair.
536 DFlush(struct ubik_dbase *adbase)
543 for (i = 0; i < nbuffers; i++, tb++) {
545 code = tb->page * UBIK_PAGESIZE; /* offset within file */
547 (*adbase->write) (adbase, tb->file, tb->data, code,
549 if (code != UBIK_PAGESIZE)
556 /* flush all modified buffers */
558 DAbort(struct ubik_dbase *adbase)
564 for (i = 0; i < nbuffers; i++, tb++) {
574 /* must only be called after DFlush, due to its interpretation of dirty flag */
576 DSync(struct ubik_dbase *adbase)
587 for (i = 0, tb = Buffers; i < nbuffers; i++, tb++) {
588 if (tb->dirty == 1) {
591 if (file != BADFID && tb->file == file)
597 /* otherwise we have a file to sync */
598 code = (*adbase->sync) (adbase, file);
605 /* Same as read, only do not even try to read the page */
607 DNew(struct ubik_dbase *dbase, afs_int32 fid, int page)
611 if ((tb = newslot(dbase, fid, page)) == 0)
614 memset(tb->data, 0, UBIK_PAGESIZE);
618 /* read data from database */
620 udisk_read(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
621 afs_int32 apos, afs_int32 alen)
624 afs_int32 offset, len, totalLen;
625 struct ubik_dbase *dbase;
627 if (atrans->flags & TRDONE)
630 dbase = atrans->dbase;
632 bp = DRead(dbase, afile, apos >> UBIK_LOGPAGESIZE);
635 /* otherwise, min of remaining bytes and end of buffer to user mode */
636 offset = apos & (UBIK_PAGESIZE - 1);
637 len = UBIK_PAGESIZE - offset;
640 memcpy(abuffer, bp + offset, len);
652 udisk_truncate(struct ubik_trans *atrans, afs_int32 afile, afs_int32 alength)
655 struct ubik_trunc *tt;
657 if (atrans->flags & TRDONE)
659 if (atrans->type != UBIK_WRITETRANS)
662 /* write a truncate log record */
663 code = udisk_LogTruncate(atrans->dbase, afile, alength);
665 /* don't truncate until commit time */
666 tt = FindTrunc(atrans, afile);
668 /* this file not truncated yet */
670 tt->next = atrans->activeTruncs;
671 atrans->activeTruncs = tt;
673 tt->length = alength;
675 /* already truncated to a certain length */
676 if (tt->length > alength)
677 tt->length = alength;
682 /* write data to database, using logs */
684 udisk_write(struct ubik_trans *atrans, afs_int32 afile, char *abuffer,
685 afs_int32 apos, afs_int32 alen)
688 afs_int32 offset, len, totalLen;
689 struct ubik_dbase *dbase;
690 struct ubik_trunc *tt;
693 if (atrans->flags & TRDONE)
695 if (atrans->type != UBIK_WRITETRANS)
698 dbase = atrans->dbase;
699 /* first write the data to the log */
700 code = udisk_LogWriteData(dbase, afile, abuffer, apos, alen);
704 /* expand any truncations of this file */
705 tt = FindTrunc(atrans, afile);
707 if (tt->length < apos + alen) {
708 tt->length = apos + alen;
715 bp = DRead(dbase, afile, apos >> UBIK_LOGPAGESIZE);
717 bp = DNew(dbase, afile, apos >> UBIK_LOGPAGESIZE);
720 memset(bp, 0, UBIK_PAGESIZE);
722 /* otherwise, min of remaining bytes and end of buffer to user mode */
723 offset = apos & (UBIK_PAGESIZE - 1);
724 len = UBIK_PAGESIZE - offset;
727 memcpy(bp + offset, abuffer, len);
732 DRelease(bp, 1); /* buffer modified */
737 /* begin a new local transaction */
739 udisk_begin(struct ubik_dbase *adbase, int atype, struct ubik_trans **atrans)
742 struct ubik_trans *tt;
744 *atrans = (struct ubik_trans *)NULL;
745 /* Make sure system is initialized before doing anything */
748 DInit(ubik_nBuffers);
750 if (atype == UBIK_WRITETRANS) {
751 if (adbase->flags & DBWRITING)
753 code = udisk_LogOpcode(adbase, LOGNEW, 0);
757 tt = (struct ubik_trans *)malloc(sizeof(struct ubik_trans));
758 memset(tt, 0, sizeof(struct ubik_trans));
760 tt->next = adbase->activeTrans;
761 adbase->activeTrans = tt;
763 if (atype == UBIK_READTRANS)
765 else if (atype == UBIK_WRITETRANS)
766 adbase->flags |= DBWRITING;
771 /* commit transaction */
773 udisk_commit(struct ubik_trans *atrans)
775 struct ubik_dbase *dbase;
777 struct ubik_version oldversion, newversion;
779 if (atrans->flags & TRDONE)
782 if (atrans->type == UBIK_WRITETRANS) {
783 dbase = atrans->dbase;
785 /* On the first write to the database. We update the versions */
786 if (ubeacon_AmSyncSite() && !(urecovery_state & UBIK_RECLABELDB)) {
787 oldversion = dbase->version;
788 newversion.epoch = FT_ApproxTime();;
789 newversion.counter = 1;
791 code = (*dbase->setlabel) (dbase, 0, &newversion);
794 ubik_epochTime = newversion.epoch;
795 dbase->version = newversion;
797 /* Ignore the error here. If the call fails, the site is
798 * marked down and when we detect it is up again, we will
799 * send the entire database to it.
801 ContactQuorum(DISK_SetVersion, atrans, 1 /*CStampVersion */ ,
802 &oldversion, &newversion);
803 urecovery_state |= UBIK_RECLABELDB;
806 dbase->version.counter++; /* bump commit count */
807 LWP_NoYieldSignal(&dbase->version);
809 code = udisk_LogEnd(dbase, &dbase->version);
811 dbase->version.counter--;
815 /* If we fail anytime after this, then panic and let the
816 * recovery replay the log.
818 code = DFlush(dbase); /* write dirty pages to respective files */
820 panic("Writing Ubik DB modifications\n");
821 code = DSync(dbase); /* sync the files and mark pages not dirty */
823 panic("Synchronizing Ubik DB modifications\n");
825 code = DoTruncs(atrans); /* Perform requested truncations */
827 panic("Truncating Ubik DB\n");
829 /* label the committed dbase */
830 code = (*dbase->setlabel) (dbase, 0, &dbase->version);
832 panic("Truncating Ubik DB\n");
834 code = (*dbase->truncate) (dbase, LOGFILE, 0); /* discard log (optional) */
836 panic("Truncating Ubik logfile\n");
840 /* When the transaction is marked done, it also means the logfile
841 * has been truncated.
843 atrans->flags |= TRDONE;
847 /* abort transaction */
849 udisk_abort(struct ubik_trans *atrans)
851 struct ubik_dbase *dbase;
854 if (atrans->flags & TRDONE)
857 /* Check if we are the write trans before logging abort, lest we
858 * abort a good write trans in progress.
859 * We don't really care if the LOGABORT gets to the log because we
860 * truncate the log next. If the truncate fails, we panic; for
861 * otherwise, the log entries remain. On restart, replay of the log
862 * will do nothing because the abort is there or no LogEnd opcode.
864 dbase = atrans->dbase;
865 if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
866 udisk_LogOpcode(dbase, LOGABORT, 1);
867 code = (*dbase->truncate) (dbase, LOGFILE, 0);
869 panic("Truncating Ubik logfile during an abort\n");
870 DAbort(dbase); /* remove all dirty pages */
873 /* When the transaction is marked done, it also means the logfile
874 * has been truncated.
876 atrans->flags |= (TRABORT | TRDONE);
880 /* destroy a transaction after it has been committed or aborted. if
881 * it hasn't committed before you call this routine, we'll abort the
882 * transaction for you.
885 udisk_end(struct ubik_trans *atrans)
887 struct ubik_dbase *dbase;
889 #if defined(UBIK_PAUSE)
890 /* Another thread is trying to lock this transaction.
891 * That can only be an RPC doing SDISK_Lock.
892 * Unlock the transaction, 'cause otherwise the other
893 * thread will never wake up. Don't free it because
894 * the caller will do that already.
896 if (atrans->flags & TRSETLOCK) {
897 atrans->flags |= TRSTALE;
898 ulock_relLock(atrans);
901 #endif /* UBIK_PAUSE */
902 if (!(atrans->flags & TRDONE))
904 dbase = atrans->dbase;
906 ulock_relLock(atrans);
909 /* check if we are the write trans before unsetting the DBWRITING bit, else
910 * we could be unsetting someone else's bit.
912 if (atrans->type == UBIK_WRITETRANS && dbase->flags & DBWRITING) {
913 dbase->flags &= ~DBWRITING;
917 if (atrans->iovec_info.iovec_wrt_val)
918 free(atrans->iovec_info.iovec_wrt_val);
919 if (atrans->iovec_data.iovec_buf_val)
920 free(atrans->iovec_data.iovec_buf_val);
923 /* Wakeup any writers waiting in BeginTrans() */
924 LWP_NoYieldSignal(&dbase->flags);