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;
84 else if (intype == ROVOL)
86 else if (intype == BACKVOL)
93 memset(&key, 0, sizeof(key));
94 strlcpy(key.name, vname, sizeof(key.name));
96 s = tfind(&key, &cell->volsbyname, ncompare);
98 ret = *(struct afscp_volume **)s;
103 code = ubik_VL_GetEntryByNameU(cell->vlservers, 0, (char *)vname, &u.u);
104 if (code == RXGEN_OPCODE) {
107 ubik_VL_GetEntryByNameN(cell->vlservers, 0, (char *)vname, &u.n);
108 if (code == RXGEN_OPCODE) {
110 code = ubik_VL_GetEntryByNameO(cell->vlservers, 0, (char *)vname,
118 ret = calloc(1, sizeof(struct afscp_volume));
120 afscp_errno = ENOMEM;
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] & VLSF_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;
175 server = afscp_ServerByIndex(ret->servers[0]);
177 i.s_addr = server->addrs[0];
181 afs_dprintf(("New volume BYNAME %s (%lu) on %s (%d)\n", ret->name,
182 afs_printable_uint32_lu(ret->id),
183 inet_ntoa(i), ret->servers[0]));
184 s = tsearch(&key, &cell->volsbyname, ncompare);
186 *(struct afscp_volume **)s = ret;
188 s = tsearch(&key, &cell->volsbyid, icompare);
190 *(struct afscp_volume **)s = ret;
194 struct afscp_volume *
195 afscp_VolumeById(struct afscp_cell *cell, afs_uint32 id)
197 union allvldbentry u;
198 struct afscp_volume *ret, key;
199 struct afscp_server *server;
200 afs_int32 code, vtype, type, srv;
208 memset(&key, 0, sizeof(key));
210 s = tfind(&key, &cell->volsbyid, icompare);
212 ret = *(struct afscp_volume **)s;
216 snprintf(idbuffer, sizeof(idbuffer), "%lu", afs_printable_uint32_lu(id));
218 code = ubik_VL_GetEntryByNameU(cell->vlservers, 0, idbuffer, &u.u);
219 if (code == RXGEN_OPCODE) {
221 code = ubik_VL_GetEntryByIDN(cell->vlservers, 0, id, -1, &u.n);
222 if (code == RXGEN_OPCODE) {
224 code = ubik_VL_GetEntryByID(cell->vlservers, 0, id, -1, &u.o);
231 ret = calloc(1, sizeof(struct afscp_volume));
233 afscp_errno = ENOMEM;
236 strlcpy(ret->name, u.u.name, sizeof(ret->name));
242 if (id == u.u.volumeId[RWVOL]) {
245 } else if (id == u.u.volumeId[ROVOL]) {
248 } else if (id == u.u.volumeId[BACKVOL]) {
249 vtype = VLSF_BACKVOL;
255 for (srv = 0; srv < u.u.nServers; srv++) {
256 if ((u.u.serverFlags[srv] & vtype) == 0)
258 if ((u.u.serverFlags[srv] & VLSF_UUID) == 0)
260 afscp_ServerByAddr(cell, u.u.serverNumber[srv].time_low);
262 server = afscp_ServerById(cell, &u.u.serverNumber[srv]);
265 ret->servers[ret->nservers++] = server->index;
269 if (id == u.n.volumeId[RWVOL]) {
272 } else if (id == u.n.volumeId[ROVOL]) {
275 } else if (id == u.n.volumeId[BACKVOL]) {
276 vtype = VLSF_BACKVOL;
282 for (srv = 0; srv < u.n.nServers; srv++) {
283 if ((u.n.serverFlags[srv] & vtype) == 0)
285 server = afscp_ServerByAddr(cell, u.n.serverNumber[srv]);
288 ret->servers[ret->nservers++] = server->index;
292 if (id == u.o.volumeId[RWVOL]) {
295 } else if (id == u.o.volumeId[ROVOL]) {
298 } else if (id == u.o.volumeId[BACKVOL]) {
299 vtype = VLSF_BACKVOL;
305 for (srv = 0; srv < u.o.nServers; srv++) {
306 if ((u.o.serverFlags[srv] & vtype) == 0)
308 server = afscp_ServerByAddr(cell, u.o.serverNumber[srv]);
311 ret->servers[ret->nservers++] = server->index;
315 ret->voltype = voltype;
317 server = afscp_ServerByIndex(ret->servers[0]);
319 i.s_addr = server->addrs[0];
323 afs_dprintf(("New volume BYID %s (%lu) on %s (%d)\n", ret->name,
324 afs_printable_uint32_lu(ret->id), inet_ntoa(i),
326 s = tsearch(&key, &cell->volsbyid, icompare);
328 *(struct afscp_volume **)s = ret;
329 strlcpy(key.name, ret->name, sizeof(key.name));
330 s = tsearch(&key, &cell->volsbyname, ncompare);
332 *(struct afscp_volume **)s = ret;