2 Copyright (C) 2003 - 2010 Chaskiel Grundman
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 1. Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 2. Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <afsconfig.h>
28 #include <afs/param.h>
34 #include <afs/vlserver.h>
35 #include <afs/vldbint.h>
36 #include <afs/volint.h>
38 #include "afscp_internal.h"
41 icompare(const void *pa, const void *pb)
43 const struct afscp_volume *va = pa, *vb = pb;
53 ncompare(const void *pa, const void *pb)
55 const struct afscp_volume *va = pa, *vb = pb;
57 if (va->voltype > vb->voltype)
59 if (vb->voltype < va->voltype)
61 return strcmp(va->name, vb->name);
71 afscp_VolumeByName(struct afscp_cell *cell, const char *vname,
75 struct afscp_volume *ret, key;
76 struct afscp_server *server;
77 afs_int32 code, vtype, type, srv;
79 /* struct in_addr i; */
83 else if (intype == ROVOL)
85 else if (intype == BACKVOL)
92 memset(&key, 0, sizeof(key));
93 strlcpy(key.name, vname, sizeof(key.name));
95 s = tfind(&key, &cell->volsbyname, ncompare);
97 ret = *(struct afscp_volume **)s;
102 code = ubik_VL_GetEntryByNameU(cell->vlservers, 0, (char *)vname, &u.u);
103 if (code == RXGEN_OPCODE) {
106 ubik_VL_GetEntryByNameN(cell->vlservers, 0, (char *)vname, &u.n);
107 if (code == RXGEN_OPCODE) {
109 code = ubik_VL_GetEntryByNameO(cell->vlservers, 0, (char *)vname,
117 ret = malloc(sizeof(struct afscp_volume));
119 afscp_errno = ENOMEM;
122 memset(ret, 0, sizeof(struct afscp_volume));
123 strlcpy(ret->name, u.u.name, sizeof(ret->name));
128 ret->id = u.u.volumeId[intype];
129 for (srv = 0; srv < u.u.nServers; srv++) {
130 if ((u.u.serverFlags[srv] & vtype) == 0)
132 afs_dprintf(("uvldbentry server %d flags: %x\n", srv,
133 u.u.serverFlags[srv]));
135 if ((u.u.serverFlags[srv] & VLSERVER_FLAG_UUID) == 0)
137 afscp_ServerByAddr(cell, u.u.serverNumber[srv].time_low);
139 server = afscp_ServerById(cell, &u.u.serverNumber[srv]);
142 ret->servers[ret->nservers++] = server->index;
146 ret->id = u.n.volumeId[intype];
147 for (srv = 0; srv < u.n.nServers; srv++) {
148 if ((u.n.serverFlags[srv] & vtype) == 0)
150 server = afscp_ServerByAddr(cell, u.n.serverNumber[srv]);
153 ret->servers[ret->nservers++] = server->index;
157 ret->id = u.o.volumeId[intype];
158 for (srv = 0; srv < u.o.nServers; srv++) {
159 if ((u.o.serverFlags[srv] & vtype) == 0)
161 server = afscp_ServerByAddr(cell, u.o.serverNumber[srv]);
164 ret->servers[ret->nservers++] = server->index;
168 if (!ret->nservers || !ret->id) {
173 ret->voltype = intype;
174 server = afscp_ServerByIndex(ret->servers[0]);
175 /* if (server != NULL)
176 * i.s_addr = server->addrs[0];
179 /* i.s_addr is set but not used later */
180 afs_dprintf(("New volume BYNAME %s (%lu) on %s (%d)\n", ret->name,
181 afs_printable_uint32_lu(ret->id),
182 inet_ntoa(i), ret->servers[0]));
183 s = tsearch(&key, &cell->volsbyname, ncompare);
185 *(struct afscp_volume **)s = ret;
187 s = tsearch(&key, &cell->volsbyid, icompare);
189 *(struct afscp_volume **)s = ret;
193 struct afscp_volume *
194 afscp_VolumeById(struct afscp_cell *cell, afs_uint32 id)
196 union allvldbentry u;
197 struct afscp_volume *ret, key;
198 struct afscp_server *server;
199 afs_int32 code, vtype, type, srv;
203 /* struct in_addr i; */
205 memset(&key, 0, sizeof(key));
207 s = tfind(&key, &cell->volsbyid, icompare);
209 ret = *(struct afscp_volume **)s;
213 snprintf(idbuffer, sizeof(idbuffer), "%lu", afs_printable_uint32_lu(id));
215 code = ubik_VL_GetEntryByNameU(cell->vlservers, 0, idbuffer, &u.u);
216 if (code == RXGEN_OPCODE) {
218 code = ubik_VL_GetEntryByIDN(cell->vlservers, 0, id, -1, &u.n);
219 if (code == RXGEN_OPCODE) {
221 code = ubik_VL_GetEntryByID(cell->vlservers, 0, id, -1, &u.o);
228 ret = malloc(sizeof(struct afscp_volume));
230 afscp_errno = ENOMEM;
233 memset(ret, 0, sizeof(struct afscp_volume));
234 strlcpy(ret->name, u.u.name, sizeof(ret->name));
240 if (id == u.u.volumeId[RWVOL]) {
243 } else if (id == u.u.volumeId[ROVOL]) {
246 } else if (id == u.u.volumeId[BACKVOL]) {
247 vtype = VLSF_BACKVOL;
253 for (srv = 0; srv < u.u.nServers; srv++) {
254 if ((u.u.serverFlags[srv] & vtype) == 0)
256 if ((u.u.serverFlags[srv] & VLSERVER_FLAG_UUID) == 0)
258 afscp_ServerByAddr(cell, u.u.serverNumber[srv].time_low);
260 server = afscp_ServerById(cell, &u.u.serverNumber[srv]);
263 ret->servers[ret->nservers++] = server->index;
267 if (id == u.n.volumeId[RWVOL]) {
270 } else if (id == u.n.volumeId[ROVOL]) {
273 } else if (id == u.n.volumeId[BACKVOL]) {
274 vtype = VLSF_BACKVOL;
280 for (srv = 0; srv < u.n.nServers; srv++) {
281 if ((u.n.serverFlags[srv] & vtype) == 0)
283 server = afscp_ServerByAddr(cell, u.n.serverNumber[srv]);
286 ret->servers[ret->nservers++] = server->index;
290 if (id == u.o.volumeId[RWVOL]) {
293 } else if (id == u.o.volumeId[ROVOL]) {
296 } else if (id == u.o.volumeId[BACKVOL]) {
297 vtype = VLSF_BACKVOL;
303 for (srv = 0; srv < u.o.nServers; srv++) {
304 if ((u.o.serverFlags[srv] & vtype) == 0)
306 server = afscp_ServerByAddr(cell, u.o.serverNumber[srv]);
309 ret->servers[ret->nservers++] = server->index;
313 ret->voltype = voltype;
314 server = afscp_ServerByIndex(ret->servers[0]);
316 * i.s_addr = server->addrs[0];
319 /* i.s_addr is set but not referenced later */
320 afs_dprintf(("New volume BYID %s (%lu) on %s (%d)\n", ret->name,
321 afs_printable_uint32_lu(ret->id), inet_ntoa(i),
323 s = tsearch(&key, &cell->volsbyid, icompare);
325 *(struct afscp_volume **)s = ret;
326 strlcpy(key.name, ret->name, sizeof(key.name));
327 s = tsearch(&key, &cell->volsbyname, ncompare);
329 *(struct afscp_volume **)s = ret;