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 extern afs_uint32 rd_HostAddress[MAXSERVERID + 1];
25 extern afs_uint32 wr_HostAddress[MAXSERVERID + 1];
26 struct extentaddr *rd_ex_addr[VL_MAX_ADDREXTBLKS] = { 0, 0, 0, 0 };
27 struct extentaddr *wr_ex_addr[VL_MAX_ADDREXTBLKS] = { 0, 0, 0, 0 };
28 struct vlheader rd_cheader; /* kept in network byte order */
29 struct vlheader wr_cheader;
32 static int index_OK(struct vl_ctx *ctx, afs_int32 blockindex);
34 #define ERROR_EXIT(code) do { \
39 /* Hashing algorithm based on the volume id; HASHSIZE must be prime */
41 IDHash(afs_int32 volumeid)
43 return ((abs(volumeid)) % HASHSIZE);
47 /* Hashing algorithm based on the volume name; name's size is implicit (64 chars) and if changed it should be reflected here. */
49 NameHash(char *volumename)
55 for (i = strlen(volumename), volumename += i - 1; i--; volumename--)
56 hash = (hash * 63) + (*((unsigned char *)volumename) - 63);
57 return (hash % HASHSIZE);
61 /* package up seek and write into one procedure for ease of use */
63 vlwrite(struct ubik_trans *trans, afs_int32 offset, void *buffer,
68 if ((errorcode = ubik_Seek(trans, 0, offset)))
70 return (ubik_Write(trans, buffer, length));
74 /* Package up seek and read into one procedure for ease of use */
76 vlread(struct ubik_trans *trans, afs_int32 offset, char *buffer,
81 if ((errorcode = ubik_Seek(trans, 0, offset)))
83 return (ubik_Read(trans, buffer, length));
87 /* take entry and convert to network order and write to disk */
89 vlentrywrite(struct ubik_trans *trans, afs_int32 offset, void *buffer,
92 struct vlentry oentry;
93 struct nvlentry nentry, *nep;
97 if (length != sizeof(oentry))
99 if (maxnservers == 13) {
100 nep = (struct nvlentry *)buffer;
101 for (i = 0; i < MAXTYPES; i++)
102 nentry.volumeId[i] = htonl(nep->volumeId[i]);
103 nentry.flags = htonl(nep->flags);
104 nentry.LockAfsId = htonl(nep->LockAfsId);
105 nentry.LockTimestamp = htonl(nep->LockTimestamp);
106 nentry.cloneId = htonl(nep->cloneId);
107 for (i = 0; i < MAXTYPES; i++)
108 nentry.nextIdHash[i] = htonl(nep->nextIdHash[i]);
109 nentry.nextNameHash = htonl(nep->nextNameHash);
110 memcpy(nentry.name, nep->name, VL_MAXNAMELEN);
111 memcpy(nentry.serverNumber, nep->serverNumber, NMAXNSERVERS);
112 memcpy(nentry.serverPartition, nep->serverPartition, NMAXNSERVERS);
113 memcpy(nentry.serverFlags, nep->serverFlags, NMAXNSERVERS);
114 bufp = (char *)&nentry;
116 memset(&oentry, 0, sizeof(struct vlentry));
117 nep = (struct nvlentry *)buffer;
118 for (i = 0; i < MAXTYPES; i++)
119 oentry.volumeId[i] = htonl(nep->volumeId[i]);
120 oentry.flags = htonl(nep->flags);
121 oentry.LockAfsId = htonl(nep->LockAfsId);
122 oentry.LockTimestamp = htonl(nep->LockTimestamp);
123 oentry.cloneId = htonl(nep->cloneId);
124 for (i = 0; i < MAXTYPES; i++)
125 oentry.nextIdHash[i] = htonl(nep->nextIdHash[i]);
126 oentry.nextNameHash = htonl(nep->nextNameHash);
127 memcpy(oentry.name, nep->name, VL_MAXNAMELEN);
128 memcpy(oentry.serverNumber, nep->serverNumber, OMAXNSERVERS);
129 memcpy(oentry.serverPartition, nep->serverPartition, OMAXNSERVERS);
130 memcpy(oentry.serverFlags, nep->serverFlags, OMAXNSERVERS);
131 bufp = (char *)&oentry;
133 return vlwrite(trans, offset, bufp, length);
136 /* read entry and convert to host order and write to disk */
138 vlentryread(struct ubik_trans *trans, afs_int32 offset, char *buffer,
141 struct vlentry *oep, tentry;
142 struct nvlentry *nep, *nbufp;
143 char *bufp = (char *)&tentry;
146 if (length != sizeof(vlentry))
148 i = vlread(trans, offset, bufp, length);
151 if (maxnservers == 13) {
152 nep = (struct nvlentry *)bufp;
153 nbufp = (struct nvlentry *)buffer;
154 for (i = 0; i < MAXTYPES; i++)
155 nbufp->volumeId[i] = ntohl(nep->volumeId[i]);
156 nbufp->flags = ntohl(nep->flags);
157 nbufp->LockAfsId = ntohl(nep->LockAfsId);
158 nbufp->LockTimestamp = ntohl(nep->LockTimestamp);
159 nbufp->cloneId = ntohl(nep->cloneId);
160 for (i = 0; i < MAXTYPES; i++)
161 nbufp->nextIdHash[i] = ntohl(nep->nextIdHash[i]);
162 nbufp->nextNameHash = ntohl(nep->nextNameHash);
163 memcpy(nbufp->name, nep->name, VL_MAXNAMELEN);
164 memcpy(nbufp->serverNumber, nep->serverNumber, NMAXNSERVERS);
165 memcpy(nbufp->serverPartition, nep->serverPartition, NMAXNSERVERS);
166 memcpy(nbufp->serverFlags, nep->serverFlags, NMAXNSERVERS);
168 oep = (struct vlentry *)bufp;
169 nbufp = (struct nvlentry *)buffer;
170 memset(nbufp, 0, sizeof(struct nvlentry));
171 for (i = 0; i < MAXTYPES; i++)
172 nbufp->volumeId[i] = ntohl(oep->volumeId[i]);
173 nbufp->flags = ntohl(oep->flags);
174 nbufp->LockAfsId = ntohl(oep->LockAfsId);
175 nbufp->LockTimestamp = ntohl(oep->LockTimestamp);
176 nbufp->cloneId = ntohl(oep->cloneId);
177 for (i = 0; i < MAXTYPES; i++)
178 nbufp->nextIdHash[i] = ntohl(oep->nextIdHash[i]);
179 nbufp->nextNameHash = ntohl(oep->nextNameHash);
180 memcpy(nbufp->name, oep->name, VL_MAXNAMELEN);
181 memcpy(nbufp->serverNumber, oep->serverNumber, NMAXNSERVERS);
182 memcpy(nbufp->serverPartition, oep->serverPartition, NMAXNSERVERS);
183 memcpy(nbufp->serverFlags, oep->serverFlags, NMAXNSERVERS);
188 /* Convenient write of small critical vldb header info to the database. */
190 write_vital_vlheader(struct vl_ctx *ctx)
193 (ctx->trans, 0, (char *)&ctx->cheader->vital_header, sizeof(vital_vlheader)))
201 /* This routine reads in the extent blocks for multi-homed servers.
202 * There used to be an initialization bug that would cause the contaddrs
203 * pointers in the first extent block to be bad. Here we will check the
204 * pointers and zero them in the in-memory copy if we find them bad. We
205 * also try to write the extent blocks back out. If we can't, then we
206 * will wait until the next write transaction to write them out
207 * (extent_mod tells us the on-disk copy is bad).
210 readExtents(struct ubik_trans *trans)
212 afs_uint32 extentAddr;
213 afs_int32 error = 0, code;
217 extentAddr = ntohl(rd_cheader.SIT);
221 /* Read the first extension block */
222 if (!rd_ex_addr[0]) {
223 rd_ex_addr[0] = malloc(VL_ADDREXTBLK_SIZE);
225 ERROR_EXIT(VL_NOMEM);
227 code = vlread(trans, extentAddr, (char *)rd_ex_addr[0], VL_ADDREXTBLK_SIZE);
229 free(rd_ex_addr[0]); /* Not the place to create it */
234 /* In case more that 64 mh servers are in use they're kept in these
235 * continuation blocks
237 for (i = 1; i < VL_MAX_ADDREXTBLKS; i++) {
238 if (!rd_ex_addr[0]->ex_contaddrs[i])
241 /* Before reading it in, check to see if the address is good */
242 if ((ntohl(rd_ex_addr[0]->ex_contaddrs[i]) <
243 ntohl(rd_ex_addr[0]->ex_contaddrs[i - 1]) + VL_ADDREXTBLK_SIZE)
244 || (ntohl(rd_ex_addr[0]->ex_contaddrs[i]) >
245 ntohl(rd_cheader.vital_header.eofPtr) - VL_ADDREXTBLK_SIZE)) {
247 rd_ex_addr[0]->ex_contaddrs[i] = 0;
252 /* Read the continuation block */
253 if (!rd_ex_addr[i]) {
254 rd_ex_addr[i] = malloc(VL_ADDREXTBLK_SIZE);
256 ERROR_EXIT(VL_NOMEM);
259 vlread(trans, ntohl(rd_ex_addr[0]->ex_contaddrs[i]),
260 (char *)rd_ex_addr[i], VL_ADDREXTBLK_SIZE);
262 free(rd_ex_addr[i]); /* Not the place to create it */
267 /* After reading it in, check to see if its a real continuation block */
268 if (ntohl(rd_ex_addr[i]->ex_hdrflags) != VLCONTBLOCK) {
270 rd_ex_addr[0]->ex_contaddrs[i] = 0;
271 free(rd_ex_addr[i]); /* Not the place to create it */
278 code = vlwrite(trans, extentAddr, rd_ex_addr[0], VL_ADDREXTBLK_SIZE);
280 VLog(0, ("Multihome server support modification\n"));
282 /* Keep extent_mod true in-case the transaction aborts */
283 /* Don't return error so we don't abort transaction */
290 /* Check that the database has been initialized. Be careful to fail in a safe
291 manner, to avoid bogusly reinitializing the db. */
293 * reads in db cache from ubik.
295 * @param[in] ut ubik transaction
296 * @param[in] rock opaque pointer to an int*; if 1, we should rebuild the db
297 * if it appears empty, if 0 we should return an error if the
300 * @return operation status
304 UpdateCache(struct ubik_trans *trans, void *rock)
306 int *builddb_rock = rock;
307 int builddb = *builddb_rock;
308 afs_int32 error = 0, i, code, ubcode;
310 /* if version changed (or first call), read the header */
311 ubcode = vlread(trans, 0, (char *)&rd_cheader, sizeof(rd_cheader));
312 vldbversion = ntohl(rd_cheader.vital_header.vldbversion);
314 if (!ubcode && (vldbversion != 0)) {
315 memcpy(rd_HostAddress, rd_cheader.IpMappedAddr, sizeof(rd_cheader.IpMappedAddr));
316 for (i = 0; i < MAXSERVERID + 1; i++) { /* cvt HostAddress to host order */
317 rd_HostAddress[i] = ntohl(rd_HostAddress[i]);
320 code = readExtents(trans);
325 /* now, if can't read, or header is wrong, write a new header */
326 if (ubcode || vldbversion == 0) {
328 printf("Can't read VLDB header, re-initialising...\n");
330 /* try to write a good header */
331 memset(&rd_cheader, 0, sizeof(rd_cheader));
332 rd_cheader.vital_header.vldbversion = htonl(VLDBVERSION);
333 rd_cheader.vital_header.headersize = htonl(sizeof(rd_cheader));
334 /* DANGER: Must get this from a master place!! */
335 rd_cheader.vital_header.MaxVolumeId = htonl(0x20000000);
336 rd_cheader.vital_header.eofPtr = htonl(sizeof(rd_cheader));
337 for (i = 0; i < MAXSERVERID + 1; i++) {
338 rd_cheader.IpMappedAddr[i] = 0;
339 rd_HostAddress[i] = 0;
341 code = vlwrite(trans, 0, (char *)&rd_cheader, sizeof(rd_cheader));
343 printf("Can't write VLDB header (error = %d)\n", code);
346 vldbversion = ntohl(rd_cheader.vital_header.vldbversion);
348 ERROR_EXIT(VL_EMPTY);
352 if ((vldbversion != VLDBVERSION) && (vldbversion != OVLDBVERSION)
353 && (vldbversion != VLDBVERSION_4)) {
355 ("VLDB version %d doesn't match this software version(%d, %d or %d), quitting!\n",
356 vldbversion, VLDBVERSION_4, VLDBVERSION, OVLDBVERSION);
357 return VL_BADVERSION;
360 maxnservers = ((vldbversion == 3 || vldbversion == 4) ? 13 : 8);
368 CheckInit(struct ubik_trans *trans, int builddb)
372 code = ubik_CheckCache(trans, UpdateCache, &builddb);
377 /* these next two cases shouldn't happen (UpdateCache should either
378 * rebuild the db or return an error if these cases occur), but just to
379 * be on the safe side... */
380 if (vldbversion == 0) {
383 if ((vldbversion != VLDBVERSION) && (vldbversion != OVLDBVERSION)
384 && (vldbversion != VLDBVERSION_4)) {
385 return VL_BADVERSION;
393 GetExtentBlock(struct vl_ctx *ctx, register afs_int32 base)
395 afs_int32 blockindex, code, error = 0;
397 /* Base 0 must exist before any other can be created */
398 if ((base != 0) && !ctx->ex_addr[0])
399 ERROR_EXIT(VL_CREATEFAIL); /* internal error */
401 if (!ctx->ex_addr[0] || !ctx->ex_addr[0]->ex_contaddrs[base]) {
402 /* Create a new extension block */
403 if (!ctx->ex_addr[base]) {
404 ctx->ex_addr[base] = malloc(VL_ADDREXTBLK_SIZE);
405 if (!ctx->ex_addr[base])
406 ERROR_EXIT(VL_NOMEM);
408 memset(ctx->ex_addr[base], 0, VL_ADDREXTBLK_SIZE);
410 /* Write the full extension block at end of vldb */
411 ctx->ex_addr[base]->ex_hdrflags = htonl(VLCONTBLOCK);
412 blockindex = ntohl(ctx->cheader->vital_header.eofPtr);
414 vlwrite(ctx->trans, blockindex, (char *)ctx->ex_addr[base],
419 /* Update the cheader.vitalheader structure on disk */
420 ctx->cheader->vital_header.eofPtr = blockindex + VL_ADDREXTBLK_SIZE;
421 ctx->cheader->vital_header.eofPtr = htonl(ctx->cheader->vital_header.eofPtr);
422 code = write_vital_vlheader(ctx);
426 /* Write the address of the base extension block in the vldb header */
428 ctx->cheader->SIT = htonl(blockindex);
430 vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->SIT),
431 (char *)&ctx->cheader->SIT, sizeof(ctx->cheader->SIT));
436 /* Write the address of this extension block into the base extension block */
437 ctx->ex_addr[0]->ex_contaddrs[base] = htonl(blockindex);
439 vlwrite(ctx->trans, ntohl(ctx->cheader->SIT), ctx->ex_addr[0],
440 sizeof(struct extentaddr));
451 FindExtentBlock(struct vl_ctx *ctx, afsUUID *uuidp,
452 afs_int32 createit, afs_int32 hostslot,
453 struct extentaddr **expp, afs_int32 *basep)
456 struct extentaddr *exp;
457 afs_int32 i, j, code, base, index, error = 0;
462 /* Create the first extension block if it does not exist */
463 if (!ctx->cheader->SIT) {
464 code = GetExtentBlock(ctx, 0);
469 for (i = 0; i < MAXSERVERID + 1; i++) {
470 if ((ctx->hostaddress[i] & 0xff000000) == 0xff000000) {
471 if ((base = (ctx->hostaddress[i] >> 16) & 0xff) > VL_MAX_ADDREXTBLKS) {
472 ERROR_EXIT(VL_INDEXERANGE);
474 if ((index = ctx->hostaddress[i] & 0x0000ffff) > VL_MHSRV_PERBLK) {
475 ERROR_EXIT(VL_INDEXERANGE);
477 exp = &ctx->ex_addr[base][index];
478 tuuid = exp->ex_hostuuid;
479 afs_ntohuuid(&tuuid);
480 if (afs_uuid_equal(uuidp, &tuuid)) {
489 if (hostslot == -1) {
490 for (i = 0; i < MAXSERVERID + 1; i++) {
491 if (!ctx->hostaddress[i])
495 ERROR_EXIT(VL_REPSFULL);
500 for (base = 0; base < VL_MAX_ADDREXTBLKS; base++) {
501 if (!ctx->ex_addr[0]->ex_contaddrs[base]) {
502 code = GetExtentBlock(ctx, base);
506 for (j = 1; j < VL_MHSRV_PERBLK; j++) {
507 exp = &ctx->ex_addr[base][j];
508 tuuid = exp->ex_hostuuid;
509 afs_ntohuuid(&tuuid);
510 if (afs_uuid_is_nil(&tuuid)) {
512 afs_htonuuid(&tuuid);
513 exp->ex_hostuuid = tuuid;
516 DOFFSET(ntohl(ctx->ex_addr[0]->ex_contaddrs[base]),
517 (char *)ctx->ex_addr[base], (char *)exp),
518 (char *)&tuuid, sizeof(tuuid));
521 ctx->hostaddress[i] =
522 0xff000000 | ((base << 16) & 0xff0000) | (j & 0xffff);
525 if (vldbversion != VLDBVERSION_4) {
526 ctx->cheader->vital_header.vldbversion =
527 htonl(VLDBVERSION_4);
528 code = write_vital_vlheader(ctx);
532 ctx->cheader->IpMappedAddr[i] = htonl(ctx->hostaddress[i]);
535 DOFFSET(0, ctx->cheader,
536 &ctx->cheader->IpMappedAddr[i]),
537 (char *)&ctx->cheader->IpMappedAddr[i],
545 ERROR_EXIT(VL_REPSFULL); /* No reason to utilize a new error code */
552 /* Allocate a free block of storage for entry, returning address of a new
553 zeroed entry (or zero if something is wrong). */
555 AllocBlock(struct vl_ctx *ctx, struct nvlentry *tentry)
557 afs_int32 blockindex;
559 if (ctx->cheader->vital_header.freePtr) {
560 /* allocate this dude */
561 blockindex = ntohl(ctx->cheader->vital_header.freePtr);
562 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(vlentry)))
564 ctx->cheader->vital_header.freePtr = htonl(tentry->nextIdHash[0]);
566 /* hosed, nothing on free list, grow file */
567 blockindex = ntohl(ctx->cheader->vital_header.eofPtr); /* remember this guy */
568 ctx->cheader->vital_header.eofPtr = htonl(blockindex + sizeof(vlentry));
570 ctx->cheader->vital_header.allocs++;
571 if (write_vital_vlheader(ctx))
573 memset(tentry, 0, sizeof(nvlentry)); /* zero new entry */
578 /* Free a block given its index. It must already have been unthreaded. Returns zero for success or an error code on failure. */
580 FreeBlock(struct vl_ctx *ctx, afs_int32 blockindex)
582 struct nvlentry tentry;
584 /* check validity of blockindex just to be on the safe side */
585 if (!index_OK(ctx, blockindex))
587 memset(&tentry, 0, sizeof(nvlentry));
588 tentry.nextIdHash[0] = ctx->cheader->vital_header.freePtr; /* already in network order */
589 tentry.flags = htonl(VLFREE);
590 ctx->cheader->vital_header.freePtr = htonl(blockindex);
591 if (vlwrite(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
593 ctx->cheader->vital_header.frees++;
594 if (write_vital_vlheader(ctx))
600 /* Look for a block by volid and voltype (if not known use -1 which searches
601 * all 3 volid hash lists. Note that the linked lists are read in first from
602 * the database header. If found read the block's contents into the area
603 * pointed to by tentry and return the block's index. If not found return 0.
606 FindByID(struct vl_ctx *ctx, afs_uint32 volid, afs_int32 voltype,
607 struct nvlentry *tentry, afs_int32 *error)
609 afs_int32 typeindex, hashindex, blockindex;
612 hashindex = IDHash(volid);
614 /* Should we have one big hash table for volids as opposed to the three ones? */
615 for (typeindex = 0; typeindex < MAXTYPES; typeindex++) {
616 for (blockindex = ntohl(ctx->cheader->VolidHash[typeindex][hashindex]);
618 blockindex = tentry->nextIdHash[typeindex]) {
620 (ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
624 if (volid == tentry->volumeId[typeindex])
629 for (blockindex = ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
630 blockindex != NULLO; blockindex = tentry->nextIdHash[voltype]) {
632 (ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
636 if (volid == tentry->volumeId[voltype])
640 return 0; /* no such entry */
644 /* Look for a block by volume name. If found read the block's contents into
645 * the area pointed to by tentry and return the block's index. If not
649 FindByName(struct vl_ctx *ctx, char *volname, struct nvlentry *tentry,
653 afs_int32 blockindex;
654 char tname[VL_MAXNAMELEN];
656 /* remove .backup or .readonly extensions for stupid backwards
659 hashindex = strlen(volname); /* really string length */
660 if (hashindex >= 8 && strcmp(volname + hashindex - 7, ".backup") == 0) {
661 /* this is a backup volume */
662 strcpy(tname, volname);
663 tname[hashindex - 7] = 0; /* zap extension */
664 } else if (hashindex >= 10
665 && strcmp(volname + hashindex - 9, ".readonly") == 0) {
666 /* this is a readonly volume */
667 strcpy(tname, volname);
668 tname[hashindex - 9] = 0; /* zap extension */
670 strcpy(tname, volname);
673 hashindex = NameHash(tname);
674 for (blockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
675 blockindex != NULLO; blockindex = tentry->nextNameHash) {
676 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
680 if (!strcmp(tname, tentry->name))
683 return 0; /* no such entry */
687 * Returns whether or not any of the supplied volume IDs already exist
690 * @param ctx transaction context
691 * @param ids an array of volume IDs
692 * @param ids_len the number of elements in the 'ids' array
693 * @param error filled in with an error code in case of error
695 * @return whether any of the volume IDs are already used
696 * @retval 1 at least one of the volume IDs is already used
697 * @retval 0 none of the volume IDs are used, or an error occurred
700 EntryIDExists(struct vl_ctx *ctx, const afs_uint32 *ids,
701 afs_int32 ids_len, afs_int32 *error)
704 struct nvlentry tentry;
708 for (typeindex = 0; typeindex < ids_len; typeindex++) {
710 && FindByID(ctx, ids[typeindex], -1, &tentry, error)) {
722 * Finds the next range of unused volume IDs in the vldb.
724 * @param ctx transaction context
725 * @param maxvolid the current max vol ID, and where to start looking
726 * for an unused volume ID range
727 * @param bump how many volume IDs we need to be unused
728 * @param error filled in with an error code in case of error
730 * @return the next volume ID 'volid' such that the range
731 * [volid, volid+bump) of volume IDs is unused, or 0 if there's
735 NextUnusedID(struct vl_ctx *ctx, afs_uint32 maxvolid, afs_uint32 bump,
738 struct nvlentry tentry;
744 /* we simply start at the given maxvolid, keep a running tally of
745 * how many free volume IDs we've seen in a row, and return when
746 * we've seen 'bump' unused IDs in a row */
747 for (id = maxvolid, nfree = 0; nfree < bump; ++id) {
748 if (FindByID(ctx, id, -1, &tentry, error)) {
757 /* 'id' is now at the end of the [maxvolid,maxvolid+bump) range,
758 * but we need to return the first unused id, so subtract the
759 * number of current running free IDs to get the beginning */
764 HashNDump(struct vl_ctx *ctx, int hashindex)
768 struct nvlentry tentry;
770 for (blockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
771 blockindex != NULLO; blockindex = tentry.nextNameHash) {
772 if (vlentryread(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
776 ("[%d]#%d: %10d %d %d (%s)\n", hashindex, i, tentry.volumeId[0],
777 tentry.nextIdHash[0], tentry.nextNameHash, tentry.name));
784 HashIdDump(struct vl_ctx *ctx, int hashindex)
788 struct nvlentry tentry;
790 for (blockindex = ntohl(ctx->cheader->VolidHash[0][hashindex]);
791 blockindex != NULLO; blockindex = tentry.nextIdHash[0]) {
792 if (vlentryread(ctx->trans, blockindex, (char *)&tentry, sizeof(nvlentry)))
796 ("[%d]#%d: %10d %d %d (%s)\n", hashindex, i, tentry.volumeId[0],
797 tentry.nextIdHash[0], tentry.nextNameHash, tentry.name));
803 /* Add a block to the hash table given a pointer to the block and its index.
804 * The block is threaded onto both hash tables and written to disk. The
805 * routine returns zero if there were no errors.
808 ThreadVLentry(struct vl_ctx *ctx, afs_int32 blockindex,
809 struct nvlentry *tentry)
813 if (!index_OK(ctx, blockindex))
815 /* Insert into volid's hash linked list */
816 if ((errorcode = HashVolid(ctx, RWVOL, blockindex, tentry)))
819 /* For rw entries we also enter the RO and BACK volume ids (if they
820 * exist) in the hash tables; note all there volids (RW, RO, BACK)
821 * should not be hashed yet! */
822 if (tentry->volumeId[ROVOL]) {
823 if ((errorcode = HashVolid(ctx, ROVOL, blockindex, tentry)))
826 if (tentry->volumeId[BACKVOL]) {
827 if ((errorcode = HashVolid(ctx, BACKVOL, blockindex, tentry)))
831 /* Insert into volname's hash linked list */
832 HashVolname(ctx, blockindex, tentry);
834 /* Update cheader entry */
835 if (write_vital_vlheader(ctx))
838 /* Update hash list pointers in the entry itself */
839 if (vlentrywrite(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry)))
845 /* Remove a block from both the hash tables. If success return 0, else
846 * return an error code. */
848 UnthreadVLentry(struct vl_ctx *ctx, afs_int32 blockindex,
849 struct nvlentry *aentry)
851 afs_int32 errorcode, typeindex;
853 if (!index_OK(ctx, blockindex))
855 if ((errorcode = UnhashVolid(ctx, RWVOL, blockindex, aentry)))
858 /* Take the RO/RW entries of their respective hash linked lists. */
859 for (typeindex = ROVOL; typeindex <= BACKVOL; typeindex++) {
860 if ((errorcode = UnhashVolid(ctx, typeindex, blockindex, aentry)))
864 /* Take it out of the Volname hash list */
865 if ((errorcode = UnhashVolname(ctx, blockindex, aentry)))
868 /* Update cheader entry */
869 write_vital_vlheader(ctx);
874 /* cheader must have be read before this routine is called. */
876 HashVolid(struct vl_ctx *ctx, afs_int32 voltype, afs_int32 blockindex,
877 struct nvlentry *tentry)
879 afs_int32 hashindex, errorcode;
880 struct nvlentry ventry;
883 (ctx, tentry->volumeId[voltype], voltype, &ventry, &errorcode))
884 return VL_IDALREADYHASHED;
887 hashindex = IDHash(tentry->volumeId[voltype]);
888 tentry->nextIdHash[voltype] =
889 ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
890 ctx->cheader->VolidHash[voltype][hashindex] = htonl(blockindex);
892 (ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolidHash[voltype][hashindex]),
893 (char *)&ctx->cheader->VolidHash[voltype][hashindex], sizeof(afs_int32)))
899 /* cheader must have be read before this routine is called. */
901 UnhashVolid(struct vl_ctx *ctx, afs_int32 voltype, afs_int32 blockindex,
902 struct nvlentry *aentry)
904 int hashindex, nextblockindex, prevblockindex;
905 struct nvlentry tentry;
909 if (aentry->volumeId[voltype] == NULLO) /* Assume no volume id */
911 /* Take it out of the VolId[voltype] hash list */
912 hashindex = IDHash(aentry->volumeId[voltype]);
913 nextblockindex = ntohl(ctx->cheader->VolidHash[voltype][hashindex]);
914 if (nextblockindex == blockindex) {
915 /* First on the hash list; just adjust pointers */
916 ctx->cheader->VolidHash[voltype][hashindex] =
917 htonl(aentry->nextIdHash[voltype]);
920 DOFFSET(0, ctx->cheader,
921 &ctx->cheader->VolidHash[voltype][hashindex]),
922 (char *)&ctx->cheader->VolidHash[voltype][hashindex],
927 while (nextblockindex != blockindex) {
928 prevblockindex = nextblockindex; /* always done once */
930 (ctx->trans, nextblockindex, (char *)&tentry, sizeof(nvlentry)))
932 if ((nextblockindex = tentry.nextIdHash[voltype]) == NULLO)
935 temp = tentry.nextIdHash[voltype] = aentry->nextIdHash[voltype];
936 temp = htonl(temp); /* convert to network byte order before writing */
939 DOFFSET(prevblockindex, &tentry, &tentry.nextIdHash[voltype]),
940 (char *)&temp, sizeof(afs_int32)))
943 aentry->nextIdHash[voltype] = 0;
949 HashVolname(struct vl_ctx *ctx, afs_int32 blockindex,
950 struct nvlentry *aentry)
955 /* Insert into volname's hash linked list */
956 hashindex = NameHash(aentry->name);
957 aentry->nextNameHash = ntohl(ctx->cheader->VolnameHash[hashindex]);
958 ctx->cheader->VolnameHash[hashindex] = htonl(blockindex);
960 vlwrite(ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolnameHash[hashindex]),
961 (char *)&ctx->cheader->VolnameHash[hashindex], sizeof(afs_int32));
969 UnhashVolname(struct vl_ctx *ctx, afs_int32 blockindex,
970 struct nvlentry *aentry)
972 afs_int32 hashindex, nextblockindex, prevblockindex;
973 struct nvlentry tentry;
976 /* Take it out of the Volname hash list */
977 hashindex = NameHash(aentry->name);
978 nextblockindex = ntohl(ctx->cheader->VolnameHash[hashindex]);
979 if (nextblockindex == blockindex) {
980 /* First on the hash list; just adjust pointers */
981 ctx->cheader->VolnameHash[hashindex] = htonl(aentry->nextNameHash);
983 (ctx->trans, DOFFSET(0, ctx->cheader, &ctx->cheader->VolnameHash[hashindex]),
984 (char *)&ctx->cheader->VolnameHash[hashindex], sizeof(afs_int32)))
987 while (nextblockindex != blockindex) {
988 prevblockindex = nextblockindex; /* always done at least once */
990 (ctx->trans, nextblockindex, (char *)&tentry, sizeof(nvlentry)))
992 if ((nextblockindex = tentry.nextNameHash) == NULLO)
995 tentry.nextNameHash = aentry->nextNameHash;
996 temp = htonl(tentry.nextNameHash);
998 (ctx->trans, DOFFSET(prevblockindex, &tentry, &tentry.nextNameHash),
999 (char *)&temp, sizeof(afs_int32)))
1002 aentry->nextNameHash = 0;
1007 /* Returns the vldb entry tentry at offset index; remaining is the number of
1008 * entries left; the routine also returns the index of the next sequential
1013 NextEntry(struct vl_ctx *ctx, afs_int32 blockindex,
1014 struct nvlentry *tentry, afs_int32 *remaining)
1016 afs_int32 lastblockindex;
1018 if (blockindex == 0) /* get first one */
1019 blockindex = sizeof(*ctx->cheader);
1021 if (!index_OK(ctx, blockindex)) {
1022 *remaining = -1; /* error */
1025 blockindex += sizeof(nvlentry);
1027 /* now search for the first entry that isn't free */
1028 for (lastblockindex = ntohl(ctx->cheader->vital_header.eofPtr);
1029 blockindex < lastblockindex;) {
1030 if (vlentryread(ctx->trans, blockindex, (char *)tentry, sizeof(nvlentry))) {
1034 if (tentry->flags == VLCONTBLOCK) {
1036 * This is a special mh extension block just simply skip over it
1038 blockindex += VL_ADDREXTBLK_SIZE;
1040 if (tentry->flags != VLFREE) {
1041 /* estimate remaining number of entries, not including this one */
1043 (lastblockindex - blockindex) / sizeof(nvlentry) - 1;
1046 blockindex += sizeof(nvlentry);
1049 *remaining = 0; /* no more entries */
1054 /* Routine to verify that index is a legal offset to a vldb entry in the
1058 index_OK(struct vl_ctx *ctx, afs_int32 blockindex)
1060 if ((blockindex < sizeof(*ctx->cheader))
1061 || (blockindex >= ntohl(ctx->cheader->vital_header.eofPtr)))
1066 /* makes a deep copy of src_ex into dst_ex */
1068 vlexcpy(struct extentaddr **dst_ex, struct extentaddr **src_ex)
1071 for (i = 0; i < VL_MAX_ADDREXTBLKS; i++) {
1074 dst_ex[i] = malloc(VL_ADDREXTBLK_SIZE);
1079 memcpy(dst_ex[i], src_ex[i], VL_ADDREXTBLK_SIZE);
1081 } else if (dst_ex[i]) {
1082 /* we have no src, but we have a dst... meaning, this block
1092 vlsetcache(struct vl_ctx *ctx, int locktype)
1094 if (locktype == LOCKREAD) {
1095 ctx->hostaddress = rd_HostAddress;
1096 ctx->ex_addr = rd_ex_addr;
1097 ctx->cheader = &rd_cheader;
1100 memcpy(wr_HostAddress, rd_HostAddress, sizeof(wr_HostAddress));
1101 memcpy(&wr_cheader, &rd_cheader, sizeof(wr_cheader));
1103 ctx->hostaddress = wr_HostAddress;
1104 ctx->ex_addr = wr_ex_addr;
1105 ctx->cheader = &wr_cheader;
1107 return vlexcpy(wr_ex_addr, rd_ex_addr);
1114 memcpy(rd_HostAddress, wr_HostAddress, sizeof(rd_HostAddress));
1115 memcpy(&rd_cheader, &wr_cheader, sizeof(rd_cheader));
1116 return vlexcpy(rd_ex_addr, wr_ex_addr);