9b1d96cc34c4c04ed964f143cae10e0f15192b2d
[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 RCSID
21     ("$Header$");
22
23 #include <sys/types.h>
24 #ifdef AFS_NT40_ENV
25 #include <winsock2.h>
26 #else
27 #include <netinet/in.h>
28 #endif
29 #include <string.h>
30 #include <afs/voldefs.h>
31 #include <rx/xdr.h>
32 #include <rx/rx.h>
33 #include <afs/vlserver.h>
34 #include <afs/nfs.h>
35 #include <afs/afsint.h>
36 #include "volint.h"
37 #include "volser.h"
38 #include "lockdata.h"
39
40 /* Finds an index in VLDB entry that matches the volume type, server, and partition.
41  * If type is zero, will match first index of ANY type (RW, BK, or RO).
42  * If server is zero, will match first index of ANY server and partition 
43  * Zero is a valid partition field.
44  */
45 int
46 FindIndex(entry, server, part, type)
47      struct nvldbentry *entry;
48      afs_int32 server, part, type;
49 {
50     int e;
51     afs_int32 error = 0;
52
53     for (e = 0; (e < entry->nServers) && !error; e++) {
54         if (!type || (entry->serverFlags[e] & type)) {
55             if ((!server || (entry->serverPartition[e] == part))
56                 && (!server
57                     || VLDB_IsSameAddrs(entry->serverNumber[e], server,
58                                         &error)))
59                 break;
60             if (type == ITSRWVOL)
61                 return -1;      /* quit when we are looking for RW entry (there's only 1) */
62         }
63     }
64
65     if (error) {
66         fprintf(STDERR,
67                 "Failed to get info about server's %d address(es) from vlserver (err=%d)\n",
68                 entry->serverNumber[e], error);
69         return -1;
70     }
71
72     if (e >= entry->nServers)
73         return -1;              /* Didn't find it */
74
75     return e;                   /* return the index */
76 }
77
78 /* Changes the rw site only */
79 void
80 SetAValue(entry, oserver, opart, nserver, npart, type)
81      struct nvldbentry *entry;
82      afs_int32 oserver, opart, nserver, npart, type;
83 {
84     int e;
85     afs_int32 error = 0;
86
87     e = FindIndex(entry, oserver, opart, type);
88     if (e == -1)
89         return;                 /* If didn't find it, just return */
90
91     entry->serverNumber[e] = nserver;
92     entry->serverPartition[e] = npart;
93
94     /* Now move rest of entries up */
95     if ((nserver == 0L) && (npart == 0L)) {
96         for (e++; e < entry->nServers; e++) {
97             entry->serverNumber[e - 1] = entry->serverNumber[e];
98             entry->serverPartition[e - 1] = entry->serverPartition[e];
99             entry->serverFlags[e - 1] = entry->serverFlags[e];
100         }
101     }
102 }
103
104 /* Changes the RW site only */
105 void
106 Lp_SetRWValue(entry, oserver, opart, nserver, npart)
107      struct nvldbentry *entry;
108      afs_int32 oserver, opart, nserver, npart;
109 {
110     SetAValue(entry, oserver, opart, nserver, npart, ITSRWVOL);
111 }
112
113 /* Changes the RO site only */
114 void
115 Lp_SetROValue(entry, oserver, opart, nserver, npart)
116      struct nvldbentry *entry;
117      afs_int32 oserver, opart, nserver, npart;
118 {
119     SetAValue(entry, oserver, opart, nserver, npart, ITSROVOL);
120 }
121
122 /* Returns success if this server and partition matches the RW entry */
123 Lp_Match(server, part, entry)
124      afs_int32 server, part;
125      struct nvldbentry *entry;
126 {
127     if (FindIndex(entry, server, part, ITSRWVOL) == -1)
128         return 0;
129     return 1;
130 }
131
132 /* Return the index of the RO entry (plus 1) if it exists, else return 0 */
133 Lp_ROMatch(server, part, entry)
134      afs_int32 server, part;
135      struct nvldbentry *entry;
136 {
137     return (FindIndex(entry, server, part, ITSROVOL) + 1);
138 }
139
140 /* Return the index of the RW entry if it exists, else return -1 */
141 Lp_GetRwIndex(entry)
142      struct nvldbentry *entry;
143 {
144     return (FindIndex(entry, 0, 0, ITSRWVOL));
145 }
146
147 /*initialize queue pointed by <ahead>*/
148 void
149 Lp_QInit(ahead)
150      struct qHead *ahead;
151 {
152     ahead->count = 0;
153     ahead->next = NULL;
154 }
155
156 /*add <elem> in front of queue <ahead> */
157 void
158 Lp_QAdd(ahead, elem)
159      struct qHead *ahead;
160      struct aqueue *elem;
161 {
162     struct aqueue *temp;
163
164     if (ahead->count == 0) {
165         ahead->count += 1;
166         ahead->next = elem;
167         elem->next = NULL;
168     } else {
169         temp = ahead->next;
170         ahead->count += 1;
171         ahead->next = elem;
172         elem->next = temp;
173     }
174 }
175
176 Lp_QScan(ahead, id, success, elem)
177      struct qHead *ahead;
178      struct aqueue **elem;
179      afs_int32 id;
180      int *success;
181 {
182     struct aqueue *cptr;
183
184     cptr = ahead->next;
185     while (cptr != NULL) {
186         if (cptr->ids[RWVOL] == id) {
187             *success = 1;
188             *elem = cptr;
189             return 0;
190         }
191         cptr = cptr->next;
192     }
193     *success = 0;
194     return 0;
195 }
196
197 /*return the element in the beginning of the queue <ahead>, free
198 *the space used by that element . <success> indicates if enumeration was ok*/
199 void
200 Lp_QEnumerate(ahead, success, elem)
201      struct qHead *ahead;
202      struct aqueue *elem;
203      int *success;
204 {
205     int i;
206     struct aqueue *temp;
207
208     if (ahead->count > 0) {     /*more elements left */
209         ahead->count -= 1;
210         temp = ahead->next;
211         ahead->next = ahead->next->next;
212         strncpy(elem->name, temp->name, VOLSER_OLDMAXVOLNAME);
213         for (i = 0; i < 3; i++) {
214             elem->ids[i] = temp->ids[i];
215             elem->copyDate[i] = temp->copyDate[i];
216             elem->isValid[i] = temp->isValid[i];
217         }
218         elem->next = NULL;
219         *success = 1;
220         free(temp);
221     } else                      /*queue is empty */
222         *success = 0;
223 }
224
225 void
226 Lp_QTraverse(ahead)
227      struct qHead *ahead;
228 {
229     int count;
230     struct aqueue *old, *new;
231
232     old = ahead->next;
233     new = old->next;
234     count = ahead->count;
235     printf
236         ("traversing the internal queue, which groups all the related volumes on a per partition basis\n");
237     while (count > 0) {
238         printf("---------------------------\n");
239         printf("%s RW-Id %lu", old->name, (unsigned long)old->ids[RWVOL]);
240         if (old->isValid[RWVOL])
241             printf(" valid ");
242         else
243             printf(" invalid ");
244         printf("RO-Id %lu", (unsigned long)old->ids[ROVOL]);
245         if (old->isValid[ROVOL])
246             printf(" valid ");
247         else
248             printf(" invalid ");
249         printf("BACKUP-Id %lu", (unsigned long)old->ids[BACKVOL]);
250         if (old->isValid[BACKVOL])
251             printf(" valid ");
252         else
253             printf(" invalid ");
254         printf("\n");
255         printf("---------------------------\n");
256         old = new;
257         if (count != 1)
258             new = new->next;
259         count--;
260     }
261 }