Merge 1.8.0~pre4 packaging into master
[pkg-k5-afs_openafs.git] / src / volser / lockprocs.c
blob691a523138072fd1358ad52baba025fcd8e0a3f2
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
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
8 */
11 * Module: lockprocs.c
12 * System: Volser
13 * Instituition: ITC, CMU
14 * Date: December, 88
17 #include <afsconfig.h>
18 #include <afs/param.h>
20 #include <roken.h>
22 #include <afs/voldefs.h>
23 #include <rx/xdr.h>
24 #include <rx/rx.h>
25 #include <afs/vlserver.h>
26 #include <afs/nfs.h>
27 #include <afs/afsint.h>
29 #include "volint.h"
30 #include "volser.h"
31 #include "lockdata.h"
32 #include "vsutils_prototypes.h"
33 #include "lockprocs_prototypes.h"
35 /* Finds an index in VLDB entry that matches the volume type, server, and partition.
36 * If type is zero, will match first index of ANY type (RW, BK, or RO).
37 * If server is zero, will match first index of ANY server and partition
38 * Zero is a valid partition field.
40 static int
41 FindIndex(struct nvldbentry *entry, afs_uint32 server, afs_int32 part, afs_int32 type)
43 int e;
44 afs_int32 error = 0;
46 for (e = 0; (e < entry->nServers) && !error; e++) {
47 if (!type || (entry->serverFlags[e] & type)) {
48 if ((!server || (entry->serverPartition[e] == part))
49 && (!server
50 || VLDB_IsSameAddrs(entry->serverNumber[e], server,
51 &error)))
52 break;
53 if (type == VLSF_RWVOL)
54 return -1; /* quit when we are looking for RW entry (there's only 1) */
58 if (error) {
59 fprintf(STDERR,
60 "Failed to get info about server's %d address(es) from vlserver (err=%d)\n",
61 entry->serverNumber[e], error);
62 return -1;
65 if (e >= entry->nServers)
66 return -1; /* Didn't find it */
68 return e; /* return the index */
71 /* Changes the rw site only */
72 static void
73 SetAValue(struct nvldbentry *entry, afs_uint32 oserver, afs_int32 opart,
74 afs_uint32 nserver, afs_int32 npart, afs_int32 type)
76 int e;
78 e = FindIndex(entry, oserver, opart, type);
79 if (e == -1)
80 return; /* If didn't find it, just return */
82 entry->serverNumber[e] = nserver;
83 entry->serverPartition[e] = npart;
85 /* Now move rest of entries up */
86 if ((nserver == 0L) && (npart == 0L)) {
87 for (e++; e < entry->nServers; e++) {
88 entry->serverNumber[e - 1] = entry->serverNumber[e];
89 entry->serverPartition[e - 1] = entry->serverPartition[e];
90 entry->serverFlags[e - 1] = entry->serverFlags[e];
95 /* Changes the RW site only */
96 void
97 Lp_SetRWValue(struct nvldbentry *entry, afs_uint32 oserver, afs_int32 opart,
98 afs_uint32 nserver, afs_int32 npart)
100 SetAValue(entry, oserver, opart, nserver, npart, VLSF_RWVOL);
103 /* Changes the RO site only */
104 void
105 Lp_SetROValue(struct nvldbentry *entry, afs_uint32 oserver,
106 afs_int32 opart, afs_uint32 nserver, afs_int32 npart)
108 SetAValue(entry, oserver, opart, nserver, npart, VLSF_ROVOL);
111 /* Returns success if this server and partition matches the RW entry */
113 Lp_Match(afs_uint32 server, afs_int32 part,
114 struct nvldbentry *entry)
116 if (FindIndex(entry, server, part, VLSF_RWVOL) == -1)
117 return 0;
118 return 1;
121 /* Return the index of the RO entry (plus 1) if it exists, else return 0 */
123 Lp_ROMatch(afs_uint32 server, afs_int32 part, struct nvldbentry *entry)
125 return (FindIndex(entry, server, part, VLSF_ROVOL) + 1);
128 /* Return the index of the RW entry if it exists, else return -1 */
130 Lp_GetRwIndex(struct nvldbentry *entry)
132 return (FindIndex(entry, 0, 0, VLSF_RWVOL));
135 /*initialize queue pointed by <ahead>*/
136 void
137 Lp_QInit(struct qHead *ahead)
139 ahead->count = 0;
140 ahead->next = NULL;
143 /*add <elem> in front of queue <ahead> */
144 void
145 Lp_QAdd(struct qHead *ahead, struct aqueue *elem)
147 struct aqueue *temp;
149 if (ahead->count == 0) {
150 ahead->count += 1;
151 ahead->next = elem;
152 elem->next = NULL;
153 } else {
154 temp = ahead->next;
155 ahead->count += 1;
156 ahead->next = elem;
157 elem->next = temp;
162 Lp_QScan(struct qHead *ahead, afs_int32 id, int *success, struct aqueue **elem)
164 struct aqueue *cptr;
166 cptr = ahead->next;
167 while (cptr != NULL) {
168 if (cptr->ids[RWVOL] == id) {
169 *success = 1;
170 *elem = cptr;
171 return 0;
173 cptr = cptr->next;
175 *success = 0;
176 return 0;
179 /*return the element in the beginning of the queue <ahead>, free
180 *the space used by that element . <success> indicates if enumeration was ok*/
181 void
182 Lp_QEnumerate(struct qHead *ahead, int *success, struct aqueue *elem)
184 int i;
185 struct aqueue *temp;
187 if (ahead->count > 0) { /*more elements left */
188 ahead->count -= 1;
189 temp = ahead->next;
190 ahead->next = ahead->next->next;
191 strncpy(elem->name, temp->name, VOLSER_OLDMAXVOLNAME);
192 for (i = 0; i < 3; i++) {
193 elem->ids[i] = temp->ids[i];
194 elem->copyDate[i] = temp->copyDate[i];
195 elem->isValid[i] = temp->isValid[i];
197 elem->next = NULL;
198 *success = 1;
199 free(temp);
200 } else /*queue is empty */
201 *success = 0;
204 void
205 Lp_QTraverse(struct qHead *ahead)
207 int count;
208 struct aqueue *old, *new;
210 old = ahead->next;
211 new = old->next;
212 count = ahead->count;
213 printf
214 ("traversing the internal queue, which groups all the related volumes on a per partition basis\n");
215 while (count > 0) {
216 printf("---------------------------\n");
217 printf("%s RW-Id %lu", old->name, (unsigned long)old->ids[RWVOL]);
218 if (old->isValid[RWVOL])
219 printf(" valid ");
220 else
221 printf(" invalid ");
222 printf("RO-Id %lu", (unsigned long)old->ids[ROVOL]);
223 if (old->isValid[ROVOL])
224 printf(" valid ");
225 else
226 printf(" invalid ");
227 printf("BACKUP-Id %lu", (unsigned long)old->ids[BACKVOL]);
228 if (old->isValid[BACKVOL])
229 printf(" valid ");
230 else
231 printf(" invalid ");
232 printf("\n");
233 printf("---------------------------\n");
234 old = new;
235 if (count != 1)
236 new = new->next;
237 count--;