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>
17 #include "lockprocs.h"
18 #include <afs/afs_AdminErrors.h>
22 * VLDB entry conversion routines.
23 * convert from one type of VLDB record to another. There are only
24 * two types "old" and "new".
28 OldVLDB_to_NewVLDB(struct vldbentry *source, struct nvldbentry *dest,
35 memset(dest, 0, sizeof(struct nvldbentry));
36 strncpy(dest->name, source->name, sizeof(dest->name));
37 for (i = 0; i < source->nServers; i++) {
38 dest->serverNumber[i] = source->serverNumber[i];
39 dest->serverPartition[i] = source->serverPartition[i];
40 dest->serverFlags[i] = source->serverFlags[i];
42 dest->nServers = source->nServers;
43 for (i = 0; i < MAXTYPES; i++)
44 dest->volumeId[i] = source->volumeId[i];
45 dest->cloneId = source->cloneId;
46 dest->flags = source->flags;
57 * We can fail to store the new to old VLDB record if there are more
58 * servers in the cell than the old format can handle. If we fail,
63 NewVLDB_to_OldVLDB(struct nvldbentry *source, struct vldbentry *dest,
70 memset(dest, 0, sizeof(struct vldbentry));
71 strncpy(dest->name, source->name, sizeof(dest->name));
72 if (source->nServers <= OMAXNSERVERS) {
73 for (i = 0; i < source->nServers; i++) {
74 dest->serverNumber[i] = source->serverNumber[i];
75 dest->serverPartition[i] = source->serverPartition[i];
76 dest->serverFlags[i] = source->serverFlags[i];
79 for (i = 0; i < MAXTYPES; i++)
80 dest->volumeId[i] = source->volumeId[i];
81 dest->cloneId = source->cloneId;
82 dest->flags = source->flags;
95 VLDB_CreateEntry(afs_cell_handle_p cellHandle, struct nvldbentry *entryp,
98 struct vldbentry oentry;
103 if (cellHandle->vos_new) {
104 tst = ubik_VL_CreateEntryN(cellHandle->vos, 0, entryp);
106 if (tst == RXGEN_OPCODE) {
107 cellHandle->vos_new = 0;
113 if (NewVLDB_to_OldVLDB(entryp, &oentry, &tst)) {
114 tst = ubik_VL_CreateEntry(cellHandle->vos, 0, &oentry);
120 } while (tst == RXGEN_OPCODE);
129 aVLDB_GetEntryByID(afs_cell_handle_p cellHandle, afs_uint32 volid,
130 afs_int32 voltype, struct nvldbentry *entryp,
133 struct vldbentry oentry;
134 afs_status_t tst = 0;
138 if (cellHandle->vos_new) {
140 ubik_VL_GetEntryByIDN(cellHandle->vos, 0, volid,
143 if (tst == RXGEN_OPCODE) {
144 cellHandle->vos_new = 0;
151 ubik_VL_GetEntryByID(cellHandle->vos, 0, volid, voltype,
154 rc = OldVLDB_to_NewVLDB(&oentry, entryp, &tst);
158 } while (tst == RXGEN_OPCODE);
167 aVLDB_GetEntryByName(afs_cell_handle_p cellHandle, char *namep,
168 struct nvldbentry *entryp, afs_status_p st)
170 struct vldbentry oentry;
171 afs_status_t tst = 0;
175 if (cellHandle->vos_new) {
177 ubik_VL_GetEntryByNameN(cellHandle->vos, 0, namep,
180 if (tst == RXGEN_OPCODE) {
181 cellHandle->vos_new = 0;
188 ubik_VL_GetEntryByNameO(cellHandle->vos, 0, namep,
191 rc = OldVLDB_to_NewVLDB(&oentry, entryp, &tst);
195 } while (tst == RXGEN_OPCODE);
204 VLDB_ReplaceEntry(afs_cell_handle_p cellHandle, afs_uint32 volid,
205 afs_int32 voltype, struct nvldbentry *entryp,
206 afs_int32 releasetype, afs_status_p st)
208 struct vldbentry oentry;
209 afs_status_t tst = 0;
213 if (cellHandle->vos_new) {
215 ubik_VL_ReplaceEntryN(cellHandle->vos, 0, volid,
216 voltype, entryp, releasetype);
218 if (tst == RXGEN_OPCODE) {
219 cellHandle->vos_new = 0;
225 if (NewVLDB_to_OldVLDB(entryp, &oentry, &tst)) {
227 ubik_VL_ReplaceEntry(cellHandle->vos, 0, volid,
228 voltype, &oentry, releasetype);
234 } while (tst == RXGEN_OPCODE);
243 VLDB_ListAttributes(afs_cell_handle_p cellHandle,
244 VldbListByAttributes * attrp, afs_int32 * entriesp,
245 nbulkentries * blkentriesp, afs_status_p st)
247 bulkentries arrayEntries;
249 afs_status_t tst = 0;
253 if (cellHandle->vos_new) {
255 ubik_VL_ListAttributesN(cellHandle->vos, 0, attrp,
256 entriesp, blkentriesp);
258 if (tst == RXGEN_OPCODE) {
259 cellHandle->vos_new = 0;
264 if (*entriesp > blkentriesp->nbulkentries_len)
265 *entriesp = blkentriesp->nbulkentries_len;
269 memset((void *)&arrayEntries, 0, sizeof(arrayEntries));
271 ubik_VL_ListAttributes(cellHandle->vos, 0, attrp,
272 entriesp, &arrayEntries);
277 if (*entriesp > arrayEntries.bulkentries_len)
278 *entriesp = arrayEntries.bulkentries_len;
280 blkentriesp->nbulkentries_val =
281 (nvldbentry *) malloc(*entriesp * sizeof(*blkentriesp));
282 if (blkentriesp->nbulkentries_val != NULL) {
283 for (i = 0; i < *entriesp; i++) {
284 OldVLDB_to_NewVLDB((struct vldbentry *)&arrayEntries.
286 (struct nvldbentry *)&blkentriesp->
287 nbulkentries_val[i], &tst);
292 if (arrayEntries.bulkentries_val) {
293 free(arrayEntries.bulkentries_val);
300 } while (tst == RXGEN_OPCODE);
309 VLDB_ListAttributesN2(afs_cell_handle_p cellHandle,
310 VldbListByAttributes * attrp, char *name,
311 afs_int32 thisindex, afs_int32 * nentriesp,
312 nbulkentries * blkentriesp, afs_int32 * nextindexp,
316 afs_status_t tst = 0;
319 ubik_VL_ListAttributesN2(cellHandle->vos, 0, attrp,
320 (name ? name : ""), thisindex, nentriesp, blkentriesp,
333 VLDB_IsSameAddrs(afs_cell_handle_p cellHandle, afs_int32 serv1,
334 afs_int32 serv2, int *equal, afs_status_p st)
337 afs_status_t tst = 0;
339 ListAddrByAttributes attrs;
342 afs_int32 nentries, unique, i;
347 if (serv1 == serv2) {
350 goto fail_VLDB_IsSameAddrs;
353 memset(&attrs, 0, sizeof(attrs));
354 attrs.Mask = VLADDR_IPADDR;
355 attrs.ipaddr = serv1;
356 memset(&addrs, 0, sizeof(addrs));
357 memset(&uuid, 0, sizeof(uuid));
359 ubik_VL_GetAddrsU(cellHandle->vos, 0, &attrs, &uuid, &unique,
363 goto fail_VLDB_IsSameAddrs;
366 addrp = addrs.bulkaddrs_val;
367 for (i = 0; i < nentries; i++, addrp++) {
368 if (serv2 == *addrp) {
375 fail_VLDB_IsSameAddrs:
384 * GetVolumeInfo - retrieve information about a particular volume.
388 * IN cellHandle - a handle that corresponds to the cell where the volume
391 * IN volid - the volume to be retrieved.
393 * OUT rentry - the vldb entry of the volume.
395 * OUT server - the address of the server where the volume resides in
398 * OUT partition - the volume to be retrieved.
400 * OUT voltype - the type of volume retrieved.
404 * No locks are obtained or released by this function
408 * Returns != 0 upon successful completion.
412 GetVolumeInfo(afs_cell_handle_p cellHandle, afs_uint32 volid,
413 struct nvldbentry *rentry, afs_int32 * server,
414 afs_int32 * partition, afs_int32 * voltype, afs_status_p st)
420 if (!aVLDB_GetEntryByID(cellHandle, volid, -1, rentry, &tst)) {
422 goto fail_GetVolumeInfo;
425 if (volid == rentry->volumeId[ROVOL]) {
427 for (i = 0; i < rentry->nServers; i++) {
428 if ((index == -1) && (rentry->serverFlags[i] & ITSROVOL)
429 && !(rentry->serverFlags[i] & RO_DONTUSE))
434 goto fail_GetVolumeInfo;
436 *server = rentry->serverNumber[index];
437 *partition = rentry->serverPartition[index];
439 goto fail_GetVolumeInfo;
442 if ((index = Lp_GetRwIndex(cellHandle, rentry, &tst)) < 0) {
443 goto fail_GetVolumeInfo;
445 if (volid == rentry->volumeId[RWVOL]) {
447 *server = rentry->serverNumber[index];
448 *partition = rentry->serverPartition[index];
449 } else if (volid == rentry->volumeId[BACKVOL]) {
451 *server = rentry->serverNumber[index];
452 *partition = rentry->serverPartition[index];
465 * ValidateVolumeName - validate a potential volume name
469 * IN volumeName - the volume name to be validated.
473 * No locks are obtained or released by this function
477 * Returns != 0 upon successful completion.
481 ValidateVolumeName(const char *volumeName, afs_status_p st)
484 afs_status_t tst = 0;
487 if ((volumeName == NULL) || (*volumeName == 0)) {
488 tst = ADMVOSVOLUMENAMENULL;
489 goto fail_ValidateVolumeName;
492 if (!ISNAMEVALID(volumeName)) {
493 tst = ADMVOSVOLUMENAMETOOLONG;
494 goto fail_ValidateVolumeName;
497 len = strlen(volumeName);
499 if (((len > 8) && (!strcmp(&volumeName[len - 9], ".readonly")))
500 || ((len > 6) && (!strcmp(&volumeName[len - 7], ".backup")))) {
501 tst = ADMVOSVOLUMENAMEINVALID;
502 goto fail_ValidateVolumeName;
507 fail_ValidateVolumeName:
516 /*extract the name of volume <name> without readonly or backup suffixes
517 * and return the result as <rname>.
520 vsu_ExtractName(char *rname, char *name)
525 strncpy(sname, name, 32);
527 total = strlen(sname);
528 if ((total > 9) && (!strcmp(&sname[total - 9], ".readonly"))) {
529 /*discard the last 8 chars */
530 sname[total - 9] = '\0';
531 strcpy(rname, sname);
533 } else if ((total > 7) && (!strcmp(&sname[total - 7], ".backup"))) {
534 /*discard last 6 chars */
535 sname[total - 7] = '\0';
536 strcpy(rname, sname);
539 strncpy(rname, name, VOLSER_OLDMAXVOLNAME);
547 * AddressMatch - determines if an IP address matches a pattern
551 * IN addrTest - the IP address to test, in either byte-order
552 * IN addrPattern - the IP address pattern, in the same byte-order
556 * No locks are obtained or released by this function
560 * Returns != 0 if the address matches the pattern specified
561 * (where 255 in any byte in the pattern indicates a wildcard).
565 AddressMatch(int addrTest, int addrPattern)
570 /* Test the high byte */
571 bTest = addrTest >> 24;
572 bPattern = addrPattern >> 24;
573 if ((bTest != bPattern) && (bPattern != 255)) {
577 /* Test the next-highest byte */
578 bTest = (addrTest >> 16) & 255;
579 bPattern = (addrPattern >> 16) & 255;
580 if ((bTest != bPattern) && (bPattern != 255)) {
584 /* Test the next-to-lowest byte */
585 bTest = (addrTest >> 8) & 255;
586 bPattern = (addrPattern >> 8) & 255;
587 if ((bTest != bPattern) && (bPattern != 255)) {
591 /* Test the low byte */
592 bTest = addrTest & 255;
593 bPattern = addrPattern & 255;
594 if ((bTest != bPattern) && (bPattern != 255)) {
603 * RemoveBadAddresses - (optionally) removes addresses that are better ignored
607 * IN OUT totalp - the number of addresses in the addrsp structure
608 * IN OUT addrsp - a bulk array of addresses
612 * No locks are obtained or released by this function
616 * Returns != 0 upon successful completion.
619 static pthread_once_t badaddr_init_once = PTHREAD_ONCE_INIT;
620 static int addr_to_skip;
628 #define cszREG_IGNORE_KEY "Software\\OpenAFS\\AFS Control Center"
629 #define cszREG_IGNORE_VALUE "IgnoreBadAddrs"
632 * In order for this routine to do anything, it must first validate
633 * that the user of this machine wants this filtering to take place.
634 * There is an undocumented registry value which signifies that it's
635 * okay to filter bogus IP addresses--and, moreover, indicates
636 * the range of values which may be ignored. Check that value.
640 addr_to_skip = 0; /* don't ignore any addrs unless we find otherwise */
641 if (RegOpenKey(HKEY_LOCAL_MACHINE, cszREG_IGNORE_KEY, &hk) == 0) {
642 DWORD dwType = REG_DWORD;
643 DWORD dwSize = sizeof(addr_to_skip);
644 RegQueryValueEx(hk, cszREG_IGNORE_VALUE, 0, &dwType,
645 (PBYTE) & addr_to_skip, &dwSize);
651 * We only support this functionality (so far) on NT; on other
652 * platforms, we'll never ignore IP addresses. If the feature
653 * is needed in the future, here's the place to add it.
657 addr_to_skip = 0; /* don't skip any addresses */
664 RemoveBadAddresses(afs_int32 * totalp, bulkaddrs * addrsp)
666 pthread_once(&badaddr_init_once, badaddr_once);
669 * If we've been requested to skip anything, addr_to_skip will be
670 * non-zero. It's actually an IP address of the form:
672 * A "255" means any value is acceptable, and any other value means
673 * the to-be-skipped address must match that value.
676 if (addr_to_skip && addrsp && addrsp->bulkaddrs_val) {
679 for (; iiRead < addrsp->bulkaddrs_len; ++iiRead) {
682 * Check this IP address to see if it should be skipped.
685 if (!AddressMatch(addrsp->bulkaddrs_val[iiRead], addr_to_skip)) {
688 * The address is okay; make sure it stays in the list.
691 if (iiWrite != iiRead) {
692 addrsp->bulkaddrs_val[iiWrite] =
693 addrsp->bulkaddrs_val[iiRead];
699 *totalp = (afs_int32) iiWrite;
700 addrsp->bulkaddrs_len = iiWrite;