1 /* AFS Volume Location Service client
3 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/gfp.h>
13 #include <linux/init.h>
14 #include <linux/sched.h>
19 * Deliver reply data to a VL.GetEntryByNameU call.
21 static int afs_deliver_vl_get_entry_by_name_u(struct afs_call *call)
23 struct afs_uvldbentry__xdr *uvldb;
24 struct afs_vldb_entry *entry;
25 bool new_only = false;
26 u32 tmp, nr_servers, vlflags;
31 ret = afs_transfer_reply(call);
35 /* unmarshall the reply once we've received all of it */
37 entry = call->reply[0];
39 nr_servers = ntohl(uvldb->nServers);
40 if (nr_servers > AFS_NMAXNSERVERS)
41 nr_servers = AFS_NMAXNSERVERS;
43 for (i = 0; i < ARRAY_SIZE(uvldb->name) - 1; i++)
44 entry->name[i] = (u8)ntohl(uvldb->name[i]);
46 entry->name_len = strlen(entry->name);
48 /* If there is a new replication site that we can use, ignore all the
49 * sites that aren't marked as new.
51 for (i = 0; i < nr_servers; i++) {
52 tmp = ntohl(uvldb->serverFlags[i]);
53 if (!(tmp & AFS_VLSF_DONTUSE) &&
54 (tmp & AFS_VLSF_NEWREPSITE))
58 vlflags = ntohl(uvldb->flags);
59 for (i = 0; i < nr_servers; i++) {
60 struct afs_uuid__xdr *xdr;
61 struct afs_uuid *uuid;
63 int n = entry->nr_servers;
65 tmp = ntohl(uvldb->serverFlags[i]);
66 if (tmp & AFS_VLSF_DONTUSE ||
67 (new_only && !(tmp & AFS_VLSF_NEWREPSITE)))
69 if (tmp & AFS_VLSF_RWVOL) {
70 entry->fs_mask[n] |= AFS_VOL_VTM_RW;
71 if (vlflags & AFS_VLF_BACKEXISTS)
72 entry->fs_mask[n] |= AFS_VOL_VTM_BAK;
74 if (tmp & AFS_VLSF_ROVOL)
75 entry->fs_mask[n] |= AFS_VOL_VTM_RO;
76 if (!entry->fs_mask[n])
79 xdr = &uvldb->serverNumber[i];
80 uuid = (struct afs_uuid *)&entry->fs_server[n];
81 uuid->time_low = xdr->time_low;
82 uuid->time_mid = htons(ntohl(xdr->time_mid));
83 uuid->time_hi_and_version = htons(ntohl(xdr->time_hi_and_version));
84 uuid->clock_seq_hi_and_reserved = (u8)ntohl(xdr->clock_seq_hi_and_reserved);
85 uuid->clock_seq_low = (u8)ntohl(xdr->clock_seq_low);
86 for (j = 0; j < 6; j++)
87 uuid->node[j] = (u8)ntohl(xdr->node[j]);
92 for (i = 0; i < AFS_MAXTYPES; i++)
93 entry->vid[i] = ntohl(uvldb->volumeId[i]);
95 if (vlflags & AFS_VLF_RWEXISTS)
96 __set_bit(AFS_VLDB_HAS_RW, &entry->flags);
97 if (vlflags & AFS_VLF_ROEXISTS)
98 __set_bit(AFS_VLDB_HAS_RO, &entry->flags);
99 if (vlflags & AFS_VLF_BACKEXISTS)
100 __set_bit(AFS_VLDB_HAS_BAK, &entry->flags);
102 if (!(vlflags & (AFS_VLF_RWEXISTS | AFS_VLF_ROEXISTS | AFS_VLF_BACKEXISTS))) {
103 entry->error = -ENOMEDIUM;
104 __set_bit(AFS_VLDB_QUERY_ERROR, &entry->flags);
107 __set_bit(AFS_VLDB_QUERY_VALID, &entry->flags);
108 _leave(" = 0 [done]");
112 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call *call)
114 kfree(call->reply[0]);
115 afs_flat_call_destructor(call);
119 * VL.GetEntryByNameU operation type.
121 static const struct afs_call_type afs_RXVLGetEntryByNameU = {
122 .name = "VL.GetEntryByNameU",
123 .op = afs_VL_GetEntryByNameU,
124 .deliver = afs_deliver_vl_get_entry_by_name_u,
125 .destructor = afs_destroy_vl_get_entry_by_name_u,
129 * Dispatch a get volume entry by name or ID operation (uuid variant). If the
130 * volname is a decimal number then it's a volume ID not a volume name.
132 struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_net *net,
133 struct afs_addr_cursor *ac,
138 struct afs_vldb_entry *entry;
139 struct afs_call *call;
145 padsz = (4 - (volnamesz & 3)) & 3;
146 reqsz = 8 + volnamesz + padsz;
148 entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL);
150 return ERR_PTR(-ENOMEM);
152 call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz,
153 sizeof(struct afs_uvldbentry__xdr));
156 return ERR_PTR(-ENOMEM);
160 call->reply[0] = entry;
161 call->ret_reply0 = true;
163 /* Marshall the parameters */
165 *bp++ = htonl(VLGETENTRYBYNAMEU);
166 *bp++ = htonl(volnamesz);
167 memcpy(bp, volname, volnamesz);
169 memset((void *)bp + volnamesz, 0, padsz);
171 trace_afs_make_vl_call(call);
172 return (struct afs_vldb_entry *)afs_make_call(ac, call, GFP_KERNEL, false);
176 * Deliver reply data to a VL.GetAddrsU call.
178 * GetAddrsU(IN ListAddrByAttributes *inaddr,
179 * OUT afsUUID *uuidp1,
180 * OUT uint32_t *uniquifier,
181 * OUT uint32_t *nentries,
182 * OUT bulkaddrs *blkaddrs);
184 static int afs_deliver_vl_get_addrs_u(struct afs_call *call)
186 struct afs_addr_list *alist;
188 u32 uniquifier, nentries, count;
191 _enter("{%u,%zu/%u}", call->unmarshall, call->offset, call->count);
194 switch (call->unmarshall) {
199 /* Extract the returned uuid, uniquifier, nentries and blkaddrs size */
201 ret = afs_extract_data(call, call->buffer,
202 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32),
207 bp = call->buffer + sizeof(struct afs_uuid__xdr);
208 uniquifier = ntohl(*bp++);
209 nentries = ntohl(*bp++);
212 nentries = min(nentries, count);
213 alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT);
216 alist->version = uniquifier;
217 call->reply[0] = alist;
219 call->count2 = nentries;
223 /* Extract entries */
225 count = min(call->count, 4U);
226 ret = afs_extract_data(call, call->buffer,
227 count * sizeof(__be32),
232 alist = call->reply[0];
234 for (i = 0; i < count; i++)
235 if (alist->nr_addrs < call->count2)
236 afs_merge_fs_addr4(alist, *bp++, AFS_FS_PORT);
238 call->count -= count;
246 _leave(" = 0 [done]");
250 static void afs_vl_get_addrs_u_destructor(struct afs_call *call)
252 afs_put_server(call->net, (struct afs_server *)call->reply[0]);
253 kfree(call->reply[1]);
254 return afs_flat_call_destructor(call);
258 * VL.GetAddrsU operation type.
260 static const struct afs_call_type afs_RXVLGetAddrsU = {
261 .name = "VL.GetAddrsU",
262 .op = afs_VL_GetAddrsU,
263 .deliver = afs_deliver_vl_get_addrs_u,
264 .destructor = afs_vl_get_addrs_u_destructor,
268 * Dispatch an operation to get the addresses for a server, where the server is
271 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_net *net,
272 struct afs_addr_cursor *ac,
276 struct afs_ListAddrByAttributes__xdr *r;
277 const struct afs_uuid *u = (const struct afs_uuid *)uuid;
278 struct afs_call *call;
284 call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU,
285 sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr),
286 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
288 return ERR_PTR(-ENOMEM);
291 call->reply[0] = NULL;
292 call->ret_reply0 = true;
294 /* Marshall the parameters */
296 *bp++ = htonl(VLGETADDRSU);
297 r = (struct afs_ListAddrByAttributes__xdr *)bp;
298 r->Mask = htonl(AFS_VLADDR_UUID);
302 r->uuid.time_low = u->time_low;
303 r->uuid.time_mid = htonl(ntohs(u->time_mid));
304 r->uuid.time_hi_and_version = htonl(ntohs(u->time_hi_and_version));
305 r->uuid.clock_seq_hi_and_reserved = htonl(u->clock_seq_hi_and_reserved);
306 r->uuid.clock_seq_low = htonl(u->clock_seq_low);
307 for (i = 0; i < 6; i++)
308 r->uuid.node[i] = htonl(u->node[i]);
310 trace_afs_make_vl_call(call);
311 return (struct afs_addr_list *)afs_make_call(ac, call, GFP_KERNEL, false);
315 * Deliver reply data to an VL.GetCapabilities operation.
317 static int afs_deliver_vl_get_capabilities(struct afs_call *call)
322 _enter("{%u,%zu/%u}", call->unmarshall, call->offset, call->count);
325 switch (call->unmarshall) {
330 /* Extract the capabilities word count */
332 ret = afs_extract_data(call, &call->tmp,
338 count = ntohl(call->tmp);
341 call->count2 = count;
345 /* Extract capabilities words */
347 count = min(call->count, 16U);
348 ret = afs_extract_data(call, call->buffer,
349 count * sizeof(__be32),
354 /* TODO: Examine capabilities */
356 call->count -= count;
364 call->reply[0] = (void *)(unsigned long)call->service_id;
366 _leave(" = 0 [done]");
371 * VL.GetCapabilities operation type
373 static const struct afs_call_type afs_RXVLGetCapabilities = {
374 .name = "VL.GetCapabilities",
375 .op = afs_VL_GetCapabilities,
376 .deliver = afs_deliver_vl_get_capabilities,
377 .destructor = afs_flat_call_destructor,
381 * Probe a fileserver for the capabilities that it supports. This can
382 * return up to 196 words.
384 * We use this to probe for service upgrade to determine what the server at the
385 * other end supports.
387 int afs_vl_get_capabilities(struct afs_net *net,
388 struct afs_addr_cursor *ac,
391 struct afs_call *call;
396 call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4);
401 call->upgrade = true; /* Let's see if this is a YFS server */
402 call->reply[0] = (void *)VLGETCAPABILITIES;
403 call->ret_reply0 = true;
405 /* marshall the parameters */
407 *bp++ = htonl(VLGETCAPABILITIES);
409 /* Can't take a ref on server */
410 trace_afs_make_vl_call(call);
411 return afs_make_call(ac, call, GFP_KERNEL, false);
415 * Deliver reply data to a YFSVL.GetEndpoints call.
417 * GetEndpoints(IN yfsServerAttributes *attr,
418 * OUT opr_uuid *uuid,
419 * OUT afs_int32 *uniquifier,
420 * OUT endpoints *fsEndpoints,
421 * OUT endpoints *volEndpoints)
423 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call)
425 struct afs_addr_list *alist;
427 u32 uniquifier, size;
430 _enter("{%u,%zu/%u,%u}", call->unmarshall, call->offset, call->count, call->count2);
433 switch (call->unmarshall) {
436 call->unmarshall = 1;
438 /* Extract the returned uuid, uniquifier, fsEndpoints count and
439 * either the first fsEndpoint type or the volEndpoints
440 * count if there are no fsEndpoints. */
442 ret = afs_extract_data(call, call->buffer,
449 bp = call->buffer + sizeof(uuid_t);
450 uniquifier = ntohl(*bp++);
451 call->count = ntohl(*bp++);
452 call->count2 = ntohl(*bp); /* Type or next count */
454 if (call->count > YFS_MAXENDPOINTS)
455 return afs_protocol_error(call, -EBADMSG);
457 alist = afs_alloc_addrlist(call->count, FS_SERVICE, AFS_FS_PORT);
460 alist->version = uniquifier;
461 call->reply[0] = alist;
464 if (call->count == 0)
465 goto extract_volendpoints;
467 call->unmarshall = 2;
469 /* Extract fsEndpoints[] entries */
471 switch (call->count2) {
472 case YFS_ENDPOINT_IPV4:
473 size = sizeof(__be32) * (1 + 1 + 1);
475 case YFS_ENDPOINT_IPV6:
476 size = sizeof(__be32) * (1 + 4 + 1);
479 return afs_protocol_error(call, -EBADMSG);
482 size += sizeof(__be32);
483 ret = afs_extract_data(call, call->buffer, size, true);
487 alist = call->reply[0];
489 switch (call->count2) {
490 case YFS_ENDPOINT_IPV4:
491 if (ntohl(bp[0]) != sizeof(__be32) * 2)
492 return afs_protocol_error(call, -EBADMSG);
493 afs_merge_fs_addr4(alist, bp[1], ntohl(bp[2]));
496 case YFS_ENDPOINT_IPV6:
497 if (ntohl(bp[0]) != sizeof(__be32) * 5)
498 return afs_protocol_error(call, -EBADMSG);
499 afs_merge_fs_addr6(alist, bp + 1, ntohl(bp[5]));
503 return afs_protocol_error(call, -EBADMSG);
506 /* Got either the type of the next entry or the count of
507 * volEndpoints if no more fsEndpoints.
509 call->count2 = ntohl(*bp++);
516 extract_volendpoints:
517 /* Extract the list of volEndpoints. */
518 call->count = call->count2;
521 if (call->count > YFS_MAXENDPOINTS)
522 return afs_protocol_error(call, -EBADMSG);
524 call->unmarshall = 3;
526 /* Extract the type of volEndpoints[0]. Normally we would
527 * extract the type of the next endpoint when we extract the
528 * data of the current one, but this is the first...
531 ret = afs_extract_data(call, call->buffer, sizeof(__be32), true);
536 call->count2 = ntohl(*bp++);
538 call->unmarshall = 4;
540 /* Extract volEndpoints[] entries */
542 switch (call->count2) {
543 case YFS_ENDPOINT_IPV4:
544 size = sizeof(__be32) * (1 + 1 + 1);
546 case YFS_ENDPOINT_IPV6:
547 size = sizeof(__be32) * (1 + 4 + 1);
550 return afs_protocol_error(call, -EBADMSG);
554 size += sizeof(__be32);
555 ret = afs_extract_data(call, call->buffer, size, true);
560 switch (call->count2) {
561 case YFS_ENDPOINT_IPV4:
562 if (ntohl(bp[0]) != sizeof(__be32) * 2)
563 return afs_protocol_error(call, -EBADMSG);
566 case YFS_ENDPOINT_IPV6:
567 if (ntohl(bp[0]) != sizeof(__be32) * 5)
568 return afs_protocol_error(call, -EBADMSG);
572 return afs_protocol_error(call, -EBADMSG);
575 /* Got either the type of the next entry or the count of
576 * volEndpoints if no more fsEndpoints.
580 if (call->count > 0) {
581 call->count2 = ntohl(*bp++);
586 call->unmarshall = 5;
590 ret = afs_extract_data(call, call->buffer, 0, false);
593 call->unmarshall = 6;
599 alist = call->reply[0];
601 /* Start with IPv6 if available. */
602 if (alist->nr_ipv4 < alist->nr_addrs)
603 alist->index = alist->nr_ipv4;
605 _leave(" = 0 [done]");
610 * YFSVL.GetEndpoints operation type.
612 static const struct afs_call_type afs_YFSVLGetEndpoints = {
613 .name = "YFSVL.GetEndpoints",
614 .op = afs_YFSVL_GetEndpoints,
615 .deliver = afs_deliver_yfsvl_get_endpoints,
616 .destructor = afs_vl_get_addrs_u_destructor,
620 * Dispatch an operation to get the addresses for a server, where the server is
623 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_net *net,
624 struct afs_addr_cursor *ac,
628 struct afs_call *call;
633 call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints,
634 sizeof(__be32) * 2 + sizeof(*uuid),
635 sizeof(struct in6_addr) + sizeof(__be32) * 3);
637 return ERR_PTR(-ENOMEM);
640 call->reply[0] = NULL;
641 call->ret_reply0 = true;
643 /* Marshall the parameters */
645 *bp++ = htonl(YVLGETENDPOINTS);
646 *bp++ = htonl(YFS_SERVER_UUID);
647 memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */
649 trace_afs_make_vl_call(call);
650 return (struct afs_addr_list *)afs_make_call(ac, call, GFP_KERNEL, false);