Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / volser / lockprocs.c
bloba279ba47b9ffd8caa24adf7d77ff868de455d9e5
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>
21 #include <sys/types.h>
22 #ifdef AFS_NT40_ENV
23 #include <winsock2.h>
24 #else
25 #include <netinet/in.h>
26 #endif
27 #include <string.h>
28 #include <afs/voldefs.h>
29 #include <rx/xdr.h>
30 #include <rx/rx.h>
31 #include <afs/vlserver.h>
32 #include <afs/nfs.h>
33 #include <afs/afsint.h>
34 #include "volint.h"
35 #include "volser.h"
36 #include "lockdata.h"
38 #include "vsutils_prototypes.h"
39 #include "lockprocs_prototypes.h"
41 /* Finds an index in VLDB entry that matches the volume type, server, and partition.
42 * If type is zero, will match first index of ANY type (RW, BK, or RO).
43 * If server is zero, will match first index of ANY server and partition
44 * Zero is a valid partition field.
46 int
47 FindIndex(struct nvldbentry *entry, afs_uint32 server, afs_int32 part, afs_int32 type)
49 int e;
50 afs_int32 error = 0;
52 for (e = 0; (e < entry->nServers) && !error; e++) {
53 if (!type || (entry->serverFlags[e] & type)) {
54 if ((!server || (entry->serverPartition[e] == part))
55 && (!server
56 || VLDB_IsSameAddrs(entry->serverNumber[e], server,
57 &error)))
58 break;
59 if (type == ITSRWVOL)
60 return -1; /* quit when we are looking for RW entry (there's only 1) */
64 if (error) {
65 fprintf(STDERR,
66 "Failed to get info about server's %d address(es) from vlserver (err=%d)\n",
67 entry->serverNumber[e], error);
68 return -1;
71 if (e >= entry->nServers)
72 return -1; /* Didn't find it */
74 return e; /* return the index */
77 /* Changes the rw site only */
78 void
79 SetAValue(struct nvldbentry *entry, afs_uint32 oserver, afs_int32 opart,
80 afs_uint32 nserver, afs_int32 npart, afs_int32 type)
82 int e;
84 e = FindIndex(entry, oserver, opart, type);
85 if (e == -1)
86 return; /* If didn't find it, just return */
88 entry->serverNumber[e] = nserver;
89 entry->serverPartition[e] = npart;
91 /* Now move rest of entries up */
92 if ((nserver == 0L) && (npart == 0L)) {
93 for (e++; e < entry->nServers; e++) {
94 entry->serverNumber[e - 1] = entry->serverNumber[e];
95 entry->serverPartition[e - 1] = entry->serverPartition[e];
96 entry->serverFlags[e - 1] = entry->serverFlags[e];
101 /* Changes the RW site only */
102 void
103 Lp_SetRWValue(struct nvldbentry *entry, afs_uint32 oserver, afs_int32 opart,
104 afs_uint32 nserver, afs_int32 npart)
106 SetAValue(entry, oserver, opart, nserver, npart, ITSRWVOL);
109 /* Changes the RO site only */
110 void
111 Lp_SetROValue(struct nvldbentry *entry, afs_uint32 oserver,
112 afs_int32 opart, afs_uint32 nserver, afs_int32 npart)
114 SetAValue(entry, oserver, opart, nserver, npart, ITSROVOL);
117 /* Returns success if this server and partition matches the RW entry */
119 Lp_Match(afs_uint32 server, afs_int32 part,
120 struct nvldbentry *entry)
122 if (FindIndex(entry, server, part, ITSRWVOL) == -1)
123 return 0;
124 return 1;
127 /* Return the index of the RO entry (plus 1) if it exists, else return 0 */
129 Lp_ROMatch(afs_uint32 server, afs_int32 part, struct nvldbentry *entry)
131 return (FindIndex(entry, server, part, ITSROVOL) + 1);
134 /* Return the index of the RW entry if it exists, else return -1 */
136 Lp_GetRwIndex(struct nvldbentry *entry)
138 return (FindIndex(entry, 0, 0, ITSRWVOL));
141 /*initialize queue pointed by <ahead>*/
142 void
143 Lp_QInit(struct qHead *ahead)
145 ahead->count = 0;
146 ahead->next = NULL;
149 /*add <elem> in front of queue <ahead> */
150 void
151 Lp_QAdd(struct qHead *ahead, struct aqueue *elem)
153 struct aqueue *temp;
155 if (ahead->count == 0) {
156 ahead->count += 1;
157 ahead->next = elem;
158 elem->next = NULL;
159 } else {
160 temp = ahead->next;
161 ahead->count += 1;
162 ahead->next = elem;
163 elem->next = temp;
168 Lp_QScan(struct qHead *ahead, afs_int32 id, int *success, struct aqueue **elem)
170 struct aqueue *cptr;
172 cptr = ahead->next;
173 while (cptr != NULL) {
174 if (cptr->ids[RWVOL] == id) {
175 *success = 1;
176 *elem = cptr;
177 return 0;
179 cptr = cptr->next;
181 *success = 0;
182 return 0;
185 /*return the element in the beginning of the queue <ahead>, free
186 *the space used by that element . <success> indicates if enumeration was ok*/
187 void
188 Lp_QEnumerate(struct qHead *ahead, int *success, struct aqueue *elem)
190 int i;
191 struct aqueue *temp;
193 if (ahead->count > 0) { /*more elements left */
194 ahead->count -= 1;
195 temp = ahead->next;
196 ahead->next = ahead->next->next;
197 strncpy(elem->name, temp->name, VOLSER_OLDMAXVOLNAME);
198 for (i = 0; i < 3; i++) {
199 elem->ids[i] = temp->ids[i];
200 elem->copyDate[i] = temp->copyDate[i];
201 elem->isValid[i] = temp->isValid[i];
203 elem->next = NULL;
204 *success = 1;
205 free(temp);
206 } else /*queue is empty */
207 *success = 0;
210 void
211 Lp_QTraverse(struct qHead *ahead)
213 int count;
214 struct aqueue *old, *new;
216 old = ahead->next;
217 new = old->next;
218 count = ahead->count;
219 printf
220 ("traversing the internal queue, which groups all the related volumes on a per partition basis\n");
221 while (count > 0) {
222 printf("---------------------------\n");
223 printf("%s RW-Id %lu", old->name, (unsigned long)old->ids[RWVOL]);
224 if (old->isValid[RWVOL])
225 printf(" valid ");
226 else
227 printf(" invalid ");
228 printf("RO-Id %lu", (unsigned long)old->ids[ROVOL]);
229 if (old->isValid[ROVOL])
230 printf(" valid ");
231 else
232 printf(" invalid ");
233 printf("BACKUP-Id %lu", (unsigned long)old->ids[BACKVOL]);
234 if (old->isValid[BACKVOL])
235 printf(" valid ");
236 else
237 printf(" invalid ");
238 printf("\n");
239 printf("---------------------------\n");
240 old = new;
241 if (count != 1)
242 new = new->next;
243 count--;