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>
20 #include "vlserver_internal.h"
22 struct vlheader xheader;
23 extern int maxnservers;
24 struct extentaddr extentaddr;
25 extern afs_uint32 rd_HostAddress[MAXSERVERID + 1];
26 extern afs_uint32 wr_HostAddress[MAXSERVERID + 1];
27 struct extentaddr *rd_ex_addr[VL_MAX_ADDREXTBLKS] = { 0, 0, 0, 0 };
28 struct extentaddr *wr_ex_addr[VL_MAX_ADDREXTBLKS] = { 0, 0, 0, 0 };
29 struct vlheader rd_cheader; /* kept in network byte order */
30 struct vlheader wr_cheader;
33 static int index_OK(struct vl_ctx *ctx, afs_int32 blockindex);
35 #define ERROR_EXIT(code) do { \
40 /* Hashing algorithm based on the volume id; HASHSIZE must be prime */
42 IDHash(afs_int32 volumeid)
44 return ((abs(volumeid)) % HASHSIZE);
48 /* Hashing algorithm based on the volume name; name's size is implicit (64 chars) and if changed it should be reflected here. */
50 NameHash(char *volumename)
56 for (i = strlen(volumename), volumename += i - 1; i--; volumename--)
57 hash = (hash * 63) + (*((unsigned char *)volumename) - 63);
58 return (hash % HASHSIZE);
62 /* package up seek and write into one procedure for ease of use */
64 vlwrite(struct ubik_trans *trans, afs_int32 offset, void *buffer,
69 if ((errorcode = ubik_Seek(trans, 0, offset)))
71 return (ubik_Write(trans, buffer, length));
75 /* Package up seek and read into one procedure for ease of use */
77 vlread(struct ubik_trans *trans, afs_int32 offset, char *buffer,
82 if ((errorcode = ubik_Seek(trans, 0, offset)))
84 return (ubik_Read(trans, buffer, length));
88 /* take entry and convert to network order and write to disk */
90 vlentrywrite(struct ubik_trans *trans, afs_int32 offset, void *buffer,
93 struct vlentry oentry;
94 struct nvlentry nentry, *nep;
98 if (length != sizeof(oentry))
100 if (maxnservers == 13) {
101 nep = (struct nvlentry *)buffer;
102 for (i = 0; i < MAXTYPES; i++)
103 nentry.volumeId[i] = htonl(nep->volumeId[i]);
104 nentry.flags = htonl(nep->flags);
105 nentry.LockAfsId = htonl(nep->LockAfsId);
106 nentry.LockTimestamp = htonl(nep->LockTimestamp);
107 nentry.cloneId = htonl(nep->cloneId);
108 for (i = 0; i < MAXTYPES; i++)
109 nentry.nextIdHash[i] = htonl(nep->nextIdHash[i]);
110 nentry.nextNameHash = htonl(nep->nextNameHash);
111 memcpy(nentry.name, nep->name, VL_MAXNAMELEN);
112 memcpy(nentry.serverNumber, nep->serverNumber, NMAXNSERVERS);
113 memcpy(nentry.serverPartition, nep->serverPartition, NMAXNSERVERS);
114 memcpy(nentry.serverFlags, nep->serverFlags, NMAXNSERVERS);
115 bufp = (char *)&nentry;
117 memset(&oentry, 0, sizeof(struct vlentry));
118 nep = (struct nvlentry *)buffer;
119 for (i = 0; i < MAXTYPES; i++)
120 oentry.volumeId[i] = htonl(nep->volumeId[i]);
121 oentry.flags = htonl(nep->flags);
122 oentry.LockAfsId = htonl(nep->LockAfsId);
123 oentry.LockTimestamp = htonl(nep->LockTimestamp);
124 oentry.cloneId = htonl(nep->cloneId);
125 for (i = 0; i < MAXTYPES; i++)
126 oentry.nextIdHash[i] = htonl(nep->nextIdHash[i]);
127 oentry.nextNameHash = htonl(nep->nextNameHash);
128 memcpy(oentry.name, nep->name, VL_MAXNAMELEN);
129 memcpy(oentry.serverNumber, nep->serverNumber, OMAXNSERVERS);
130 memcpy(oentry.serverPartition, nep->serverPartition, OMAXNSERVERS);
131 memcpy(oentry.serverFlags, nep->serverFlags, OMAXNSERVERS);
132 bufp = (char *)&oentry;
134 return vlwrite(trans, offset, bufp, length);
137 /* read entry and convert to host order and write to disk */
139 vlentryread(struct ubik_trans *trans, afs_int32 offset, char *buffer,
142 struct vlentry *oep, tentry;
143 struct nvlentry *nep, *nbufp;
144 char *bufp = (char *)&tentry;
147 if (length != sizeof(vlentry))
149 i = vlread(trans, offset, bufp, length);
152 if (maxnservers == 13) {
153 nep = (struct nvlentry *)bufp;
154 nbufp = (struct nvlentry *)buffer;
155 for (i = 0; i < MAXTYPES; i++)
156 nbufp->volumeId[i] = ntohl(nep->volumeId[i]);
157 nbufp->flags = ntohl(nep->flags);
158 nbufp->LockAfsId = ntohl(nep->LockAfsId);
159 nbufp->LockTimestamp = ntohl(nep->LockTimestamp);
160 nbufp->cloneId = ntohl(nep->cloneId);
161 for (i = 0; i < MAXTYPES; i++)
162 nbufp->nextIdHash[i] = ntohl(nep->nextIdHash[i]);
163 nbufp->nextNameHash = ntohl(nep->nextNameHash);
164 memcpy(nbufp->name, nep->name, VL_MAXNAMELEN);
165 memcpy(nbufp->serverNumber, nep->serverNumber, NMAXNSERVERS);
166 memcpy(nbufp->serverPartition, nep->serverPartition, NMAXNSERVERS);
167 memcpy(nbufp->serverFlags, nep->serverFlags, NMAXNSERVERS);
169 oep = (struct vlentry *)bufp;
170 nbufp = (struct nvlentry *)buffer;
171 memset(nbufp, 0, sizeof(struct nvlentry));
172 for (i = 0; i < MAXTYPES; i++)
173 nbufp->volumeId[i] = ntohl(oep->volumeId[i]);
174 nbufp->flags = ntohl(oep->flags);
175 nbufp->LockAfsId = ntohl(oep->LockAfsId);
176 nbufp->LockTimestamp = ntohl(oep->LockTimestamp);
177 nbufp->cloneId = ntohl(oep->cloneId);
178 for (i = 0; i < MAXTYPES; i++)
179 nbufp->nextIdHash[i] = ntohl(oep->nextIdHash[i]);
180 nbufp->nextNameHash = ntohl(oep->nextNameHash);
181 memcpy(nbufp->name, oep->name, VL_MAXNAMELEN);
182 memcpy(nbufp->serverNumber, oep->serverNumber, NMAXNSERVERS);
183 memcpy(nbufp->serverPartition, oep->serverPartition, NMAXNSERVERS);
184 memcpy(nbufp->serverFlags, oep->serverFlags, NMAXNSERVERS);
189 /* Convenient write of small critical vldb header info to the database. */
191 write_vital_vlheader(struct vl_ctx *ctx)
194 (ctx->trans, 0, (char *)&ctx->cheader->vital_header, sizeof(vital_vlheader)))
202 /* This routine reads in the extent blocks for multi-homed servers.
203 * There used to be an initialization bug that would cause the contaddrs
204 * pointers in the first extent block to be bad. Here we will check the
205 * pointers and zero them in the in-memory copy if we find them bad. We
206 * also try to write the extent blocks back out. If we can't, then we
207 * will wait until the next write transaction to write them out
208 * (extent_mod tells us the on-disk copy is bad).
211 readExtents(struct ubik_trans *trans)
213 afs_uint32 extentAddr;
214 afs_int32 error = 0, code;
218 extentAddr = ntohl(rd_cheader.SIT);
222 /* Read the first extension block */
223 if (!rd_ex_addr[0]) {
224 rd_ex_addr[0] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
226 ERROR_EXIT(VL_NOMEM);
228 code = vlread(trans, extentAddr, (char *)rd_ex_addr[0], VL_ADDREXTBLK_SIZE);
230 free(rd_ex_addr[0]); /* Not the place to create it */
235 /* In case more that 64 mh servers are in use they're kept in these
236 * continuation blocks
238 for (i = 1; i < VL_MAX_ADDREXTBLKS; i++) {
239 if (!rd_ex_addr[0]->ex_contaddrs[i])
242 /* Before reading it in, check to see if the address is good */
243 if ((ntohl(rd_ex_addr[0]->ex_contaddrs[i]) <
244 ntohl(rd_ex_addr[0]->ex_contaddrs[i - 1]) + VL_ADDREXTBLK_SIZE)
245 || (ntohl(rd_ex_addr[0]->ex_contaddrs[i]) >
246 ntohl(rd_cheader.vital_header.eofPtr) - VL_ADDREXTBLK_SIZE)) {
248 rd_ex_addr[0]->ex_contaddrs[i] = 0;
253 /* Read the continuation block */
254 if (!rd_ex_addr[i]) {
255 rd_ex_addr[i] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
257 ERROR_EXIT(VL_NOMEM);
260 vlread(trans, ntohl(rd_ex_addr[0]->ex_contaddrs[i]),
261 (char *)rd_ex_addr[i], VL_ADDREXTBLK_SIZE);
263 free(rd_ex_addr[i]); /* Not the place to create it */
268 /* After reading it in, check to see if its a real continuation block */
269 if (ntohl(rd_ex_addr[i]->ex_hdrflags) != VLCONTBLOCK) {
271 rd_ex_addr[0]->ex_contaddrs[i] = 0;
272 free(rd_ex_addr[i]); /* Not the place to create it */
279 code = vlwrite(trans, extentAddr, rd_ex_addr[0], VL_ADDREXTBLK_SIZE);
281 VLog(0, ("Multihome server support modification\n"));
283 /* Keep extent_mod true in-case the transaction aborts */
284 /* Don't return error so we don't abort transaction */
291 /* Check that the database has been initialized. Be careful to fail in a safe
292 manner, to avoid bogusly reinitializing the db. */
294 * reads in db cache from ubik.
296 * @param[in] ut ubik transaction
297 * @param[in] rock opaque pointer to an int*; if 1, we should rebuild the db
298 * if it appears empty, if 0 we should return an error if the
301 * @return operation status
305 UpdateCache(struct ubik_trans *trans, void *rock)
307 int *builddb_rock = rock;
308 int builddb = *builddb_rock;
309 afs_int32 error = 0, i, code, ubcode;
311 /* if version changed (or first call), read the header */
312 ubcode = vlread(trans, 0, (char *)&rd_cheader, sizeof(rd_cheader));
313 vldbversion = ntohl(rd_cheader.vital_header.vldbversion);
315 if (!ubcode && (vldbversion != 0)) {
316 memcpy(rd_HostAddress, rd_cheader.IpMappedAddr, sizeof(rd_cheader.IpMappedAddr));
317 for (i = 0; i < MAXSERVERID + 1; i++) { /* cvt HostAddress to host order */
318 rd_HostAddress[i] = ntohl(rd_HostAddress[i]);
321 code = readExtents(trans);
326 /* now, if can't read, or header is wrong, write a new header */
327 if (ubcode || vldbversion == 0) {
329 printf("Can't read VLDB header, re-initialising...\n");
331 /* try to write a good header */
332 memset(&rd_cheader, 0, sizeof(rd_cheader));
333 rd_cheader.vital_header.vldbversion = htonl(VLDBVERSION);
334 rd_cheader.vital_header.headersize = htonl(sizeof(rd_cheader));
335 /* DANGER: Must get this from a master place!! */
336 rd_cheader.vital_header.MaxVolumeId = htonl(0x20000000);
337 rd_cheader.vital_header.eofPtr = htonl(sizeof(rd_cheader));
338 for (i = 0; i < MAXSERVERID + 1; i++) {
339 rd_cheader.IpMappedAddr[i] = 0;
340 rd_HostAddress[i] = 0;
342 code = vlwrite(trans, 0, (char *)&rd_cheader, sizeof(rd_cheader));
344 printf("Can't write VLDB header (error = %d)\n", code);
347 vldbversion = ntohl(rd_cheader.vital_header.vldbversion);
349 ERROR_EXIT(VL_EMPTY);
353 if ((vldbversion != VLDBVERSION) && (vldbversion != OVLDBVERSION)
354 && (vldbversion != VLDBVERSION_4)) {
356 ("VLDB version %d doesn't match this software version(%d, %d or %d), quitting!\n",
357 vldbversion, VLDBVERSION_4, VLDBVERSION, OVLDBVERSION);
358 return VL_BADVERSION;
361 maxnservers = ((vldbversion == 3 || vldbversion == 4) ? 13 : 8);
369 CheckInit(struct ubik_trans *trans, int builddb)
373 code = ubik_CheckCache(trans, UpdateCache, &builddb);
378 /* these next two cases shouldn't happen (UpdateCache should either
379 * rebuild the db or return an error if these cases occur), but just to
380 * be on the safe side... */
381 if (vldbversion == 0) {
384 if ((vldbversion != VLDBVERSION) && (vldbversion != OVLDBVERSION)
385 && (vldbversion != VLDBVERSION_4)) {
386 return VL_BADVERSION;
394 GetExtentBlock(struct vl_ctx *ctx, register afs_int32 base)
396 afs_int32 blockindex, code, error = 0;
398 /* Base 0 must exist before any other can be created */
399 if ((base != 0) && !ctx->ex_addr[0])
400 ERROR_EXIT(VL_CREATEFAIL); /* internal error */
402 if (!ctx->ex_addr[0] || !ctx->ex_addr[0]->ex_contaddrs[base]) {
403 /* Create a new extension block */
404 if (!ctx->ex_addr[base]) {
405 ctx->ex_addr[base] = (struct extentaddr *)malloc(VL_ADDREXTBLK_SIZE);
406 if (!ctx->ex_addr[base])
407 ERROR_EXIT(VL_NOMEM);
409 memset(ctx->ex_addr[base], 0, VL_ADDREXTBLK_SIZE);
411 /* Write the full extension block at end of vldb */
412 ctx->ex_addr[base]->ex_hdrflags = htonl(VLCONTBLOCK);
413 blockindex = ntohl(ctx->cheader->vital_header.eofPtr);
415 vlwrite(ctx->trans, blockindex, (char *)ctx->ex_addr[base],
420 /* Update the cheader.vitalheader structure on disk */
421 ctx->cheader->vital_header.eofPtr = blockindex + VL_ADDREXTBLK_SIZE;
422 ctx->cheader->vital_header.eofPtr = htonl(ctx->cheader->vital_header.eofPtr);
423 code = write_vital_vlheader(ctx);
427 /* Write the address of the base extension block in the vldb header */
429 ctx->cheader->SIT = htonl(blockindex);
431 vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->SIT),
432 (char *)&ctx->cheader->SIT, sizeof(ctx->cheader->SIT));
437 /* Write the address of this extension block into the base extension block */
438 ctx->ex_addr[0]->ex_contaddrs[base] = htonl(blockindex);
440 vlwrite(ctx->trans, ntohl(ctx->cheader->SIT), ctx->ex_addr[0],
441 sizeof(struct extentaddr));
452 FindExtentBlock(struct vl_ctx *ctx, afsUUID *uuidp,
453 afs_int32 createit, afs_int32 hostslot,
454 struct extentaddr **expp, afs_int32 *basep)
457 struct extentaddr *exp;
458 afs_int32 i, j, code, base, index, error = 0;
463 /* Create the first extension block if it does not exist */
464 if (!ctx->cheader->SIT) {
465 code = GetExtentBlock(ctx, 0);
470 for (i = 0; i < MAXSERVERID + 1; i++) {
471 if ((ctx->hostaddress[i] & 0xff000000) == 0xff000000) {
472 if ((base = (ctx->hostaddress[i] >> 16) & 0xff) > VL_MAX_ADDREXTBLKS) {
473 ERROR_EXIT(VL_INDEXERANGE);
475 if ((index = ctx->hostaddress[i] & 0x0000ffff) > VL_MHSRV_PERBLK) {
476 ERROR_EXIT(VL_INDEXERANGE);
478 exp = &ctx->ex_addr[base][index];
479 tuuid = exp->ex_hostuuid;
480 afs_ntohuuid(&tuuid);
481 if (afs_uuid_equal(uuidp, &tuuid)) {
490 if (hostslot == -1) {
491 for (i = 0; i < MAXSERVERID + 1; i++) {
492 if (!ctx->hostaddress[i])
496 ERROR_EXIT(VL_REPSFULL);
501 for (base = 0; base < VL_MAX_ADDREXTBLKS; base++) {
502 if (!ctx->ex_addr[0]->ex_contaddrs[base]) {
503 code = GetExtentBlock(ctx, base);
507 for (j = 1; j < VL_MHSRV_PERBLK; j++) {
508 exp = &ctx->ex_addr[base][j];
509 tuuid = exp->ex_hostuuid;
510 afs_ntohuuid(&tuuid);
511 if (afs_uuid_is_nil(&tuuid)) {
513 afs_htonuuid(&tuuid);
514 exp->ex_hostuuid = tuuid;
517 DOFFSET(ntohl(ctx->ex_addr[0]->ex_contaddrs[base]),
518 (char *)ctx->ex_addr[base], (char *)exp),
519 (char *)&tuuid, sizeof(tuuid));
522 ctx->hostaddress[i] =
523 0xff000000 | ((base << 16) & 0xff0000) | (j & 0xffff);
526 if (vldbversion != VLDBVERSION_4) {
527 ctx->cheader->vital_header.vldbversion =
528 htonl(VLDBVERSION_4);
529 code = write_vital_vlheader(ctx);
533 ctx->cheader->IpMappedAddr[i] = htonl(ctx->hostaddress[i]);
536 DOFFSET(0, ctx->cheader,
537 &ctx->cheader->IpMappedAddr[i]),
538 (char *)&ctx->cheader->IpMappedAddr[i],
546 ERROR_EXIT(VL_REPSFULL); /* No reason to utilize a new error code */
553 /* Allocate a free block of storage for entry, returning address of a new
554 zeroed entry (or zero if something is wrong). */
556 AllocBlock(struct vl_ctx *ctx, struct nvlentry *tentry)
558 afs_int32 blockindex;
560 if (ctx->cheader->vital_header.freePtr) {
561 /* allocate this dude */
562 blockindex = ntohl(ctx->cheader->vital_header.freePtr);
563 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(vlentry)))
565 ctx->cheader->vital_header.freePtr = htonl(tentry->nextIdHash[0]);
567 /* hosed, nothing on free list, grow file */
568 blockindex = ntohl(ctx->cheader->vital_header.eofPtr); /* remember this guy */
569 ctx->cheader->vital_header.eofPtr = htonl(blockindex + sizeof(vlentry));
571 ctx->cheader->vital_header.allocs++;
572 if (write_vital_vlheader(ctx))
574 memset(tentry, 0, sizeof(nvlentry)); /* zero new entry */
579 /* Free a block given its index. It must already have been unthreaded. Returns zero for success or an error code on failure. */
581 FreeBlock(struct vl_ctx *ctx, afs_int32 blockindex)
583 struct nvlentry tentry;
585 /* check validity of blockindex just to be on the safe side */
586 if (!index_OK(ctx, blockindex))
588 memset(&tentry, 0, sizeof(nvlentry));
589 tentry.nextIdHash[0] = ctx->cheader->vital_header.freePtr; /* already in network order */
590 tentry.flags = htonl(VLFREE);
591 ctx->cheader->vital_header.freePtr = htonl(blockindex);
592 if (vlwrite(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
594 ctx->cheader->vital_header.frees++;
595 if (write_vital_vlheader(ctx))
601 /* Look for a block by volid and voltype (if not known use -1 which searches
602 * all 3 volid hash lists. Note that the linked lists are read in first from
603 * the database header. If found read the block's contents into the area
604 * pointed to by tentry and return the block's index. If not found return 0.
607 FindByID(struct vl_ctx *ctx, afs_uint32 volid, afs_int32 voltype,
608 struct nvlentry *tentry, afs_int32 *error)
610 afs_int32 typeindex, hashindex, blockindex;
613 hashindex = IDHash(volid);
615 /* Should we have one big hash table for volids as opposed to the three ones? */
616 for (typeindex = 0; typeindex < MAXTYPES; typeindex++) {
617 for (blockindex = ntohl(ctx->cheader->VolidHash[typeindex][hashindex]);
619 blockindex = tentry->nextIdHash[typeindex]) {
621 (ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
625 if (volid == tentry->volumeId[typeindex])
630 for (blockindex = ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
631 blockindex != NULLO; blockindex = tentry->nextIdHash[voltype]) {
633 (ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
637 if (volid == tentry->volumeId[voltype])
641 return 0; /* no such entry */
645 /* Look for a block by volume name. If found read the block's contents into
646 * the area pointed to by tentry and return the block's index. If not
650 FindByName(struct vl_ctx *ctx, char *volname, struct nvlentry *tentry,
654 afs_int32 blockindex;
655 char tname[VL_MAXNAMELEN];
657 /* remove .backup or .readonly extensions for stupid backwards
660 hashindex = strlen(volname); /* really string length */
661 if (hashindex >= 8 && strcmp(volname + hashindex - 7, ".backup") == 0) {
662 /* this is a backup volume */
663 strcpy(tname, volname);
664 tname[hashindex - 7] = 0; /* zap extension */
665 } else if (hashindex >= 10
666 && strcmp(volname + hashindex - 9, ".readonly") == 0) {
667 /* this is a readonly volume */
668 strcpy(tname, volname);
669 tname[hashindex - 9] = 0; /* zap extension */
671 strcpy(tname, volname);
674 hashindex = NameHash(tname);
675 for (blockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
676 blockindex != NULLO; blockindex = tentry->nextNameHash) {
677 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
681 if (!strcmp(tname, tentry->name))
684 return 0; /* no such entry */
688 * Returns whether or not any of the supplied volume IDs already exist
691 * @param ctx transaction context
692 * @param ids an array of volume IDs
693 * @param ids_len the number of elements in the 'ids' array
694 * @param error filled in with an error code in case of error
696 * @return whether any of the volume IDs are already used
697 * @retval 1 at least one of the volume IDs is already used
698 * @retval 0 none of the volume IDs are used, or an error occurred
701 EntryIDExists(struct vl_ctx *ctx, const afs_uint32 *ids,
702 afs_int32 ids_len, afs_int32 *error)
705 struct nvlentry tentry;
709 for (typeindex = 0; typeindex < ids_len; typeindex++) {
711 && FindByID(ctx, ids[typeindex], -1, &tentry, error)) {
723 * Finds the next range of unused volume IDs in the vldb.
725 * @param ctx transaction context
726 * @param maxvolid the current max vol ID, and where to start looking
727 * for an unused volume ID range
728 * @param bump how many volume IDs we need to be unused
729 * @param error filled in with an error code in case of error
731 * @return the next volume ID 'volid' such that the range
732 * [volid, volid+bump) of volume IDs is unused, or 0 if there's
736 NextUnusedID(struct vl_ctx *ctx, afs_uint32 maxvolid, afs_uint32 bump,
739 struct nvlentry tentry;
745 /* we simply start at the given maxvolid, keep a running tally of
746 * how many free volume IDs we've seen in a row, and return when
747 * we've seen 'bump' unused IDs in a row */
748 for (id = maxvolid, nfree = 0; nfree < bump; ++id) {
749 if (FindByID(ctx, id, -1, &tentry, error)) {
758 /* 'id' is now at the end of the [maxvolid,maxvolid+bump) range,
759 * but we need to return the first unused id, so subtract the
760 * number of current running free IDs to get the beginning */
765 HashNDump(struct vl_ctx *ctx, int hashindex)
769 struct nvlentry tentry;
771 for (blockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
772 blockindex != NULLO; blockindex = tentry.nextNameHash) {
773 if (vlentryread(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
777 ("[%d]#%d: %10d %d %d (%s)\n", hashindex, i, tentry.volumeId[0],
778 tentry.nextIdHash[0], tentry.nextNameHash, tentry.name));
785 HashIdDump(struct vl_ctx *ctx, int hashindex)
789 struct nvlentry tentry;
791 for (blockindex = ntohl(ctx->cheader->VolidHash[0][hashindex]);
792 blockindex != NULLO; blockindex = tentry.nextIdHash[0]) {
793 if (vlentryread(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
797 ("[%d]#%d: %10d %d %d (%s)\n", hashindex, i, tentry.volumeId[0],
798 tentry.nextIdHash[0], tentry.nextNameHash, tentry.name));
804 /* Add a block to the hash table given a pointer to the block and its index.
805 * The block is threaded onto both hash tables and written to disk. The
806 * routine returns zero if there were no errors.
809 ThreadVLentry(struct vl_ctx *ctx, afs_int32 blockindex,
810 struct nvlentry *tentry)
814 if (!index_OK(ctx, blockindex))
816 /* Insert into volid's hash linked list */
817 if ((errorcode = HashVolid(ctx, RWVOL, blockindex, tentry)))
820 /* For rw entries we also enter the RO and BACK volume ids (if they
821 * exist) in the hash tables; note all there volids (RW, RO, BACK)
822 * should not be hashed yet! */
823 if (tentry->volumeId[ROVOL]) {
824 if ((errorcode = HashVolid(ctx, ROVOL, blockindex, tentry)))
827 if (tentry->volumeId[BACKVOL]) {
828 if ((errorcode = HashVolid(ctx, BACKVOL, blockindex, tentry)))
832 /* Insert into volname's hash linked list */
833 HashVolname(ctx, blockindex, tentry);
835 /* Update cheader entry */
836 if (write_vital_vlheader(ctx))
839 /* Update hash list pointers in the entry itself */
840 if (vlentrywrite(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry)))
846 /* Remove a block from both the hash tables. If success return 0, else
847 * return an error code. */
849 UnthreadVLentry(struct vl_ctx *ctx, afs_int32 blockindex,
850 struct nvlentry *aentry)
852 afs_int32 errorcode, typeindex;
854 if (!index_OK(ctx, blockindex))
856 if ((errorcode = UnhashVolid(ctx, RWVOL, blockindex, aentry)))
859 /* Take the RO/RW entries of their respective hash linked lists. */
860 for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
861 if ((errorcode = UnhashVolid(ctx, typeindex, blockindex, aentry)))
865 /* Take it out of the Volname hash list */
866 if ((errorcode = UnhashVolname(ctx, blockindex, aentry)))
869 /* Update cheader entry */
870 write_vital_vlheader(ctx);
875 /* cheader must have be read before this routine is called. */
877 HashVolid(struct vl_ctx *ctx, afs_int32 voltype, afs_int32 blockindex,
878 struct nvlentry *tentry)
880 afs_int32 hashindex, errorcode;
881 struct nvlentry ventry;
884 (ctx, tentry->volumeId[voltype], voltype, &ventry, &errorcode))
885 return VL_IDALREADYHASHED;
888 hashindex = IDHash(tentry->volumeId[voltype]);
889 tentry->nextIdHash[voltype] =
890 ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
891 ctx->cheader->VolidHash[voltype][hashindex] = htonl(blockindex);
893 (ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolidHash[voltype][hashindex]),
894 (char *)&ctx->cheader->VolidHash[voltype][hashindex], sizeof(afs_int32)))
900 /* cheader must have be read before this routine is called. */
902 UnhashVolid(struct vl_ctx *ctx, afs_int32 voltype, afs_int32 blockindex,
903 struct nvlentry *aentry)
905 int hashindex, nextblockindex, prevblockindex;
906 struct nvlentry tentry;
910 if (aentry->volumeId[voltype] == NULLO) /* Assume no volume id */
912 /* Take it out of the VolId[voltype] hash list */
913 hashindex = IDHash(aentry->volumeId[voltype]);
914 nextblockindex = ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
915 if (nextblockindex == blockindex) {
916 /* First on the hash list; just adjust pointers */
917 ctx->cheader->VolidHash[voltype][hashindex] =
918 htonl(aentry->nextIdHash[voltype]);
921 DOFFSET(0, ctx->cheader,
922 &ctx->cheader->VolidHash[voltype][hashindex]),
923 (char *)&ctx->cheader->VolidHash[voltype][hashindex],
928 while (nextblockindex != blockindex) {
929 prevblockindex = nextblockindex; /* always done once */
931 (ctx->trans, nextblockindex, (char *)&tentry, sizeof(nvlentry)))
933 if ((nextblockindex = tentry.nextIdHash[voltype]) == NULLO)
936 temp = tentry.nextIdHash[voltype] = aentry->nextIdHash[voltype];
937 temp = htonl(temp); /* convert to network byte order before writing */
940 DOFFSET(prevblockindex, &tentry, &tentry.nextIdHash[voltype]),
941 (char *)&temp, sizeof(afs_int32)))
944 aentry->nextIdHash[voltype] = 0;
950 HashVolname(struct vl_ctx *ctx, afs_int32 blockindex,
951 struct nvlentry *aentry)
956 /* Insert into volname's hash linked list */
957 hashindex = NameHash(aentry->name);
958 aentry->nextNameHash = ntohl(ctx->cheader->VolnameHash[hashindex]);
959 ctx->cheader->VolnameHash[hashindex] = htonl(blockindex);
961 vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolnameHash[hashindex]),
962 (char *)&ctx->cheader->VolnameHash[hashindex], sizeof(afs_int32));
970 UnhashVolname(struct vl_ctx *ctx, afs_int32 blockindex,
971 struct nvlentry *aentry)
973 afs_int32 hashindex, nextblockindex, prevblockindex;
974 struct nvlentry tentry;
977 /* Take it out of the Volname hash list */
978 hashindex = NameHash(aentry->name);
979 nextblockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
980 if (nextblockindex == blockindex) {
981 /* First on the hash list; just adjust pointers */
982 ctx->cheader->VolnameHash[hashindex] = htonl(aentry->nextNameHash);
984 (ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolnameHash[hashindex]),
985 (char *)&ctx->cheader->VolnameHash[hashindex], sizeof(afs_int32)))
988 while (nextblockindex != blockindex) {
989 prevblockindex = nextblockindex; /* always done at least once */
991 (ctx->trans, nextblockindex, (char *)&tentry, sizeof(nvlentry)))
993 if ((nextblockindex = tentry.nextNameHash) == NULLO)
996 tentry.nextNameHash = aentry->nextNameHash;
997 temp = htonl(tentry.nextNameHash);
999 (ctx->trans, DOFFSET(prevblockindex, &tentry, &tentry.nextNameHash),
1000 (char *)&temp, sizeof(afs_int32)))
1003 aentry->nextNameHash = 0;
1008 /* Returns the vldb entry tentry at offset index; remaining is the number of
1009 * entries left; the routine also returns the index of the next sequential
1014 NextEntry(struct vl_ctx *ctx, afs_int32 blockindex,
1015 struct nvlentry *tentry, afs_int32 *remaining)
1017 afs_int32 lastblockindex;
1019 if (blockindex == 0) /* get first one */
1020 blockindex = sizeof(*ctx->cheader);
1022 if (!index_OK(ctx, blockindex)) {
1023 *remaining = -1; /* error */
1026 blockindex += sizeof(nvlentry);
1028 /* now search for the first entry that isn't free */
1029 for (lastblockindex = ntohl(ctx->cheader->vital_header.eofPtr);
1030 blockindex < lastblockindex;) {
1031 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
1035 if (tentry->flags == VLCONTBLOCK) {
1037 * This is a special mh extension block just simply skip over it
1039 blockindex += VL_ADDREXTBLK_SIZE;
1041 if (tentry->flags != VLFREE) {
1042 /* estimate remaining number of entries, not including this one */
1044 (lastblockindex - blockindex) / sizeof(nvlentry) - 1;
1047 blockindex += sizeof(nvlentry);
1050 *remaining = 0; /* no more entries */
1055 /* Routine to verify that index is a legal offset to a vldb entry in the
1059 index_OK(struct vl_ctx *ctx, afs_int32 blockindex)
1061 if ((blockindex < sizeof(*ctx->cheader))
1062 || (blockindex >= ntohl(ctx->cheader->vital_header.eofPtr)))
1067 /* makes a deep copy of src_ex into dst_ex */
1069 vlexcpy(struct extentaddr **dst_ex, struct extentaddr **src_ex)
1072 for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
1075 dst_ex[i] = malloc(VL_ADDREXTBLK_SIZE);
1080 memcpy(dst_ex[i], src_ex[i], VL_ADDREXTBLK_SIZE);
1082 } else if (dst_ex[i]) {
1083 /* we have no src, but we have a dst... meaning, this block
1093 vlsetcache(struct vl_ctx *ctx, int locktype)
1095 if (locktype == LOCKREAD) {
1096 ctx->hostaddress = rd_HostAddress;
1097 ctx->ex_addr = rd_ex_addr;
1098 ctx->cheader = &rd_cheader;
1101 memcpy(wr_HostAddress, rd_HostAddress, sizeof(wr_HostAddress));
1102 memcpy(&wr_cheader, &rd_cheader, sizeof(wr_cheader));
1104 ctx->hostaddress = wr_HostAddress;
1105 ctx->ex_addr = wr_ex_addr;
1106 ctx->cheader = &wr_cheader;
1108 return vlexcpy(wr_ex_addr, rd_ex_addr);
1115 memcpy(rd_HostAddress, wr_HostAddress, sizeof(rd_HostAddress));
1116 memcpy(&rd_cheader, &wr_cheader, sizeof(rd_cheader));
1117 return vlexcpy(rd_ex_addr, wr_ex_addr);