volser: Use unsigned addresses and volume IDs
[openafs.git] / src / volser / lockprocs.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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  */
9
10 /*
11  *  Module:         lockprocs.c
12  *  System:         Volser
13  *  Instituition:   ITC, CMU
14  *  Date:           December, 88
15  */
16
17 #include <afsconfig.h>
18 #include <afs/param.h>
19
20
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"
37
38 #include "vsutils_prototypes.h"
39 #include "lockprocs_prototypes.h"
40
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.
45  */
46 int
47 FindIndex(struct nvldbentry *entry, afs_uint32 server, afs_int32 part, afs_int32 type)
48 {
49     int e;
50     afs_int32 error = 0;
51
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) */
61         }
62     }
63
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;
69     }
70
71     if (e >= entry->nServers)
72         return -1;              /* Didn't find it */
73
74     return e;                   /* return the index */
75 }
76
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)
81 {
82     int e;
83
84     e = FindIndex(entry, oserver, opart, type);
85     if (e == -1)
86         return;                 /* If didn't find it, just return */
87
88     entry->serverNumber[e] = nserver;
89     entry->serverPartition[e] = npart;
90
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];
97         }
98     }
99 }
100
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)
105 {
106     SetAValue(entry, oserver, opart, nserver, npart, ITSRWVOL);
107 }
108
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)
113 {
114     SetAValue(entry, oserver, opart, nserver, npart, ITSROVOL);
115 }
116
117 /* Returns success if this server and partition matches the RW entry */
118 int 
119 Lp_Match(afs_uint32 server, afs_int32 part,
120          struct nvldbentry *entry)
121 {
122     if (FindIndex(entry, server, part, ITSRWVOL) == -1)
123         return 0;
124     return 1;
125 }
126
127 /* Return the index of the RO entry (plus 1) if it exists, else return 0 */
128 int 
129 Lp_ROMatch(afs_uint32 server, afs_int32 part, struct nvldbentry *entry)
130 {
131     return (FindIndex(entry, server, part, ITSROVOL) + 1);
132 }
133
134 /* Return the index of the RW entry if it exists, else return -1 */
135 int 
136 Lp_GetRwIndex(struct nvldbentry *entry)
137 {
138     return (FindIndex(entry, 0, 0, ITSRWVOL));
139 }
140
141 /*initialize queue pointed by <ahead>*/
142 void
143 Lp_QInit(struct qHead *ahead)
144 {
145     ahead->count = 0;
146     ahead->next = NULL;
147 }
148
149 /*add <elem> in front of queue <ahead> */
150 void
151 Lp_QAdd(struct qHead *ahead, struct aqueue *elem)
152 {
153     struct aqueue *temp;
154
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;
164     }
165 }
166
167 int 
168 Lp_QScan(struct qHead *ahead, afs_int32 id, int *success, struct aqueue **elem)
169 {
170     struct aqueue *cptr;
171
172     cptr = ahead->next;
173     while (cptr != NULL) {
174         if (cptr->ids[RWVOL] == id) {
175             *success = 1;
176             *elem = cptr;
177             return 0;
178         }
179         cptr = cptr->next;
180     }
181     *success = 0;
182     return 0;
183 }
184
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)
189 {
190     int i;
191     struct aqueue *temp;
192
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];
202         }
203         elem->next = NULL;
204         *success = 1;
205         free(temp);
206     } else                      /*queue is empty */
207         *success = 0;
208 }
209
210 void
211 Lp_QTraverse(struct qHead *ahead)
212 {
213     int count;
214     struct aqueue *old, *new;
215
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--;
244     }
245 }