1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* AFS Volume Location Service client
4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
9 #include <linux/init.h>
10 #include <linux/sched.h>
15 * Deliver reply data to a VL.GetEntryByNameU call.
17 static int afs_deliver_vl_get_entry_by_name_u(struct afs_call *call)
19 struct afs_uvldbentry__xdr *uvldb;
20 struct afs_vldb_entry *entry;
21 bool new_only = false;
22 u32 tmp, nr_servers, vlflags;
27 ret = afs_transfer_reply(call);
31 /* unmarshall the reply once we've received all of it */
33 entry = call->ret_vldb;
35 nr_servers = ntohl(uvldb->nServers);
36 if (nr_servers > AFS_NMAXNSERVERS)
37 nr_servers = AFS_NMAXNSERVERS;
39 for (i = 0; i < ARRAY_SIZE(uvldb->name) - 1; i++)
40 entry->name[i] = (u8)ntohl(uvldb->name[i]);
42 entry->name_len = strlen(entry->name);
44 /* If there is a new replication site that we can use, ignore all the
45 * sites that aren't marked as new.
47 for (i = 0; i < nr_servers; i++) {
48 tmp = ntohl(uvldb->serverFlags[i]);
49 if (!(tmp & AFS_VLSF_DONTUSE) &&
50 (tmp & AFS_VLSF_NEWREPSITE))
54 vlflags = ntohl(uvldb->flags);
55 for (i = 0; i < nr_servers; i++) {
56 struct afs_uuid__xdr *xdr;
57 struct afs_uuid *uuid;
59 int n = entry->nr_servers;
61 tmp = ntohl(uvldb->serverFlags[i]);
62 if (tmp & AFS_VLSF_DONTUSE ||
63 (new_only && !(tmp & AFS_VLSF_NEWREPSITE)))
65 if (tmp & AFS_VLSF_RWVOL) {
66 entry->fs_mask[n] |= AFS_VOL_VTM_RW;
67 if (vlflags & AFS_VLF_BACKEXISTS)
68 entry->fs_mask[n] |= AFS_VOL_VTM_BAK;
70 if (tmp & AFS_VLSF_ROVOL)
71 entry->fs_mask[n] |= AFS_VOL_VTM_RO;
72 if (!entry->fs_mask[n])
75 xdr = &uvldb->serverNumber[i];
76 uuid = (struct afs_uuid *)&entry->fs_server[n];
77 uuid->time_low = xdr->time_low;
78 uuid->time_mid = htons(ntohl(xdr->time_mid));
79 uuid->time_hi_and_version = htons(ntohl(xdr->time_hi_and_version));
80 uuid->clock_seq_hi_and_reserved = (u8)ntohl(xdr->clock_seq_hi_and_reserved);
81 uuid->clock_seq_low = (u8)ntohl(xdr->clock_seq_low);
82 for (j = 0; j < 6; j++)
83 uuid->node[j] = (u8)ntohl(xdr->node[j]);
85 entry->addr_version[n] = ntohl(uvldb->serverUnique[i]);
89 for (i = 0; i < AFS_MAXTYPES; i++)
90 entry->vid[i] = ntohl(uvldb->volumeId[i]);
92 if (vlflags & AFS_VLF_RWEXISTS)
93 __set_bit(AFS_VLDB_HAS_RW, &entry->flags);
94 if (vlflags & AFS_VLF_ROEXISTS)
95 __set_bit(AFS_VLDB_HAS_RO, &entry->flags);
96 if (vlflags & AFS_VLF_BACKEXISTS)
97 __set_bit(AFS_VLDB_HAS_BAK, &entry->flags);
99 if (!(vlflags & (AFS_VLF_RWEXISTS | AFS_VLF_ROEXISTS | AFS_VLF_BACKEXISTS))) {
100 entry->error = -ENOMEDIUM;
101 __set_bit(AFS_VLDB_QUERY_ERROR, &entry->flags);
104 __set_bit(AFS_VLDB_QUERY_VALID, &entry->flags);
105 _leave(" = 0 [done]");
109 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call *call)
111 kfree(call->ret_vldb);
112 afs_flat_call_destructor(call);
116 * VL.GetEntryByNameU operation type.
118 static const struct afs_call_type afs_RXVLGetEntryByNameU = {
119 .name = "VL.GetEntryByNameU",
120 .op = afs_VL_GetEntryByNameU,
121 .deliver = afs_deliver_vl_get_entry_by_name_u,
122 .destructor = afs_destroy_vl_get_entry_by_name_u,
126 * Dispatch a get volume entry by name or ID operation (uuid variant). If the
127 * volname is a decimal number then it's a volume ID not a volume name.
129 struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_vl_cursor *vc,
133 struct afs_vldb_entry *entry;
134 struct afs_call *call;
135 struct afs_net *net = vc->cell->net;
141 padsz = (4 - (volnamesz & 3)) & 3;
142 reqsz = 8 + volnamesz + padsz;
144 entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL);
146 return ERR_PTR(-ENOMEM);
148 call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz,
149 sizeof(struct afs_uvldbentry__xdr));
152 return ERR_PTR(-ENOMEM);
156 call->ret_vldb = entry;
157 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
159 /* Marshall the parameters */
161 *bp++ = htonl(VLGETENTRYBYNAMEU);
162 *bp++ = htonl(volnamesz);
163 memcpy(bp, volname, volnamesz);
165 memset((void *)bp + volnamesz, 0, padsz);
167 trace_afs_make_vl_call(call);
168 afs_make_call(&vc->ac, call, GFP_KERNEL);
169 return (struct afs_vldb_entry *)afs_wait_for_call_to_complete(call, &vc->ac);
173 * Deliver reply data to a VL.GetAddrsU call.
175 * GetAddrsU(IN ListAddrByAttributes *inaddr,
176 * OUT afsUUID *uuidp1,
177 * OUT uint32_t *uniquifier,
178 * OUT uint32_t *nentries,
179 * OUT bulkaddrs *blkaddrs);
181 static int afs_deliver_vl_get_addrs_u(struct afs_call *call)
183 struct afs_addr_list *alist;
185 u32 uniquifier, nentries, count;
188 _enter("{%u,%zu/%u}",
189 call->unmarshall, iov_iter_count(call->iter), call->count);
191 switch (call->unmarshall) {
193 afs_extract_to_buf(call,
194 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
197 /* Extract the returned uuid, uniquifier, nentries and
201 ret = afs_extract_data(call, true);
205 bp = call->buffer + sizeof(struct afs_uuid__xdr);
206 uniquifier = ntohl(*bp++);
207 nentries = ntohl(*bp++);
210 nentries = min(nentries, count);
211 alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT);
214 alist->version = uniquifier;
215 call->ret_alist = alist;
217 call->count2 = nentries;
221 count = min(call->count, 4U);
222 afs_extract_to_buf(call, count * sizeof(__be32));
224 fallthrough; /* and extract entries */
226 ret = afs_extract_data(call, call->count > 4);
230 alist = call->ret_alist;
232 count = min(call->count, 4U);
233 for (i = 0; i < count; i++)
234 if (alist->nr_addrs < call->count2)
235 afs_merge_fs_addr4(alist, *bp++, AFS_FS_PORT);
237 call->count -= count;
244 _leave(" = 0 [done]");
248 static void afs_vl_get_addrs_u_destructor(struct afs_call *call)
250 afs_put_addrlist(call->ret_alist);
251 return afs_flat_call_destructor(call);
255 * VL.GetAddrsU operation type.
257 static const struct afs_call_type afs_RXVLGetAddrsU = {
258 .name = "VL.GetAddrsU",
259 .op = afs_VL_GetAddrsU,
260 .deliver = afs_deliver_vl_get_addrs_u,
261 .destructor = afs_vl_get_addrs_u_destructor,
265 * Dispatch an operation to get the addresses for a server, where the server is
268 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *vc,
271 struct afs_ListAddrByAttributes__xdr *r;
272 const struct afs_uuid *u = (const struct afs_uuid *)uuid;
273 struct afs_call *call;
274 struct afs_net *net = vc->cell->net;
280 call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU,
281 sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr),
282 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
284 return ERR_PTR(-ENOMEM);
287 call->ret_alist = NULL;
288 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
290 /* Marshall the parameters */
292 *bp++ = htonl(VLGETADDRSU);
293 r = (struct afs_ListAddrByAttributes__xdr *)bp;
294 r->Mask = htonl(AFS_VLADDR_UUID);
298 r->uuid.time_low = u->time_low;
299 r->uuid.time_mid = htonl(ntohs(u->time_mid));
300 r->uuid.time_hi_and_version = htonl(ntohs(u->time_hi_and_version));
301 r->uuid.clock_seq_hi_and_reserved = htonl(u->clock_seq_hi_and_reserved);
302 r->uuid.clock_seq_low = htonl(u->clock_seq_low);
303 for (i = 0; i < 6; i++)
304 r->uuid.node[i] = htonl(u->node[i]);
306 trace_afs_make_vl_call(call);
307 afs_make_call(&vc->ac, call, GFP_KERNEL);
308 return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac);
312 * Deliver reply data to an VL.GetCapabilities operation.
314 static int afs_deliver_vl_get_capabilities(struct afs_call *call)
319 _enter("{%u,%zu/%u}",
320 call->unmarshall, iov_iter_count(call->iter), call->count);
322 switch (call->unmarshall) {
324 afs_extract_to_tmp(call);
327 fallthrough; /* and extract the capabilities word count */
329 ret = afs_extract_data(call, true);
333 count = ntohl(call->tmp);
335 call->count2 = count;
338 afs_extract_discard(call, count * sizeof(__be32));
340 fallthrough; /* and extract capabilities words */
342 ret = afs_extract_data(call, false);
346 /* TODO: Examine capabilities */
352 _leave(" = 0 [done]");
356 static void afs_destroy_vl_get_capabilities(struct afs_call *call)
358 afs_put_vlserver(call->net, call->vlserver);
359 afs_flat_call_destructor(call);
363 * VL.GetCapabilities operation type
365 static const struct afs_call_type afs_RXVLGetCapabilities = {
366 .name = "VL.GetCapabilities",
367 .op = afs_VL_GetCapabilities,
368 .deliver = afs_deliver_vl_get_capabilities,
369 .done = afs_vlserver_probe_result,
370 .destructor = afs_destroy_vl_get_capabilities,
374 * Probe a volume server for the capabilities that it supports. This can
375 * return up to 196 words.
377 * We use this to probe for service upgrade to determine what the server at the
378 * other end supports.
380 struct afs_call *afs_vl_get_capabilities(struct afs_net *net,
381 struct afs_addr_cursor *ac,
383 struct afs_vlserver *server,
384 unsigned int server_index)
386 struct afs_call *call;
391 call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4);
393 return ERR_PTR(-ENOMEM);
396 call->vlserver = afs_get_vlserver(server);
397 call->server_index = server_index;
398 call->upgrade = true;
400 call->max_lifespan = AFS_PROBE_MAX_LIFESPAN;
402 /* marshall the parameters */
404 *bp++ = htonl(VLGETCAPABILITIES);
406 /* Can't take a ref on server */
407 trace_afs_make_vl_call(call);
408 afs_make_call(ac, call, GFP_KERNEL);
413 * Deliver reply data to a YFSVL.GetEndpoints call.
415 * GetEndpoints(IN yfsServerAttributes *attr,
416 * OUT opr_uuid *uuid,
417 * OUT afs_int32 *uniquifier,
418 * OUT endpoints *fsEndpoints,
419 * OUT endpoints *volEndpoints)
421 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call)
423 struct afs_addr_list *alist;
425 u32 uniquifier, size;
428 _enter("{%u,%zu,%u}",
429 call->unmarshall, iov_iter_count(call->iter), call->count2);
431 switch (call->unmarshall) {
433 afs_extract_to_buf(call, sizeof(uuid_t) + 3 * sizeof(__be32));
434 call->unmarshall = 1;
436 /* Extract the returned uuid, uniquifier, fsEndpoints count and
437 * either the first fsEndpoint type or the volEndpoints
438 * count if there are no fsEndpoints. */
441 ret = afs_extract_data(call, true);
445 bp = call->buffer + sizeof(uuid_t);
446 uniquifier = ntohl(*bp++);
447 call->count = ntohl(*bp++);
448 call->count2 = ntohl(*bp); /* Type or next count */
450 if (call->count > YFS_MAXENDPOINTS)
451 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_num);
453 alist = afs_alloc_addrlist(call->count, FS_SERVICE, AFS_FS_PORT);
456 alist->version = uniquifier;
457 call->ret_alist = alist;
459 if (call->count == 0)
460 goto extract_volendpoints;
463 switch (call->count2) {
464 case YFS_ENDPOINT_IPV4:
465 size = sizeof(__be32) * (1 + 1 + 1);
467 case YFS_ENDPOINT_IPV6:
468 size = sizeof(__be32) * (1 + 4 + 1);
471 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_type);
474 size += sizeof(__be32);
475 afs_extract_to_buf(call, size);
476 call->unmarshall = 2;
478 fallthrough; /* and extract fsEndpoints[] entries */
480 ret = afs_extract_data(call, true);
484 alist = call->ret_alist;
486 switch (call->count2) {
487 case YFS_ENDPOINT_IPV4:
488 if (ntohl(bp[0]) != sizeof(__be32) * 2)
489 return afs_protocol_error(
490 call, afs_eproto_yvl_fsendpt4_len);
491 afs_merge_fs_addr4(alist, bp[1], ntohl(bp[2]));
494 case YFS_ENDPOINT_IPV6:
495 if (ntohl(bp[0]) != sizeof(__be32) * 5)
496 return afs_protocol_error(
497 call, afs_eproto_yvl_fsendpt6_len);
498 afs_merge_fs_addr6(alist, bp + 1, ntohl(bp[5]));
502 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_type);
505 /* Got either the type of the next entry or the count of
506 * volEndpoints if no more fsEndpoints.
508 call->count2 = ntohl(*bp++);
512 goto next_fsendpoint;
514 extract_volendpoints:
515 /* Extract the list of volEndpoints. */
516 call->count = call->count2;
519 if (call->count > YFS_MAXENDPOINTS)
520 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
522 afs_extract_to_buf(call, 1 * sizeof(__be32));
523 call->unmarshall = 3;
525 /* Extract the type of volEndpoints[0]. Normally we would
526 * extract the type of the next endpoint when we extract the
527 * data of the current one, but this is the first...
531 ret = afs_extract_data(call, true);
538 call->count2 = ntohl(*bp++);
539 switch (call->count2) {
540 case YFS_ENDPOINT_IPV4:
541 size = sizeof(__be32) * (1 + 1 + 1);
543 case YFS_ENDPOINT_IPV6:
544 size = sizeof(__be32) * (1 + 4 + 1);
547 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
551 size += sizeof(__be32); /* Get next type too */
552 afs_extract_to_buf(call, size);
553 call->unmarshall = 4;
555 fallthrough; /* and extract volEndpoints[] entries */
557 ret = afs_extract_data(call, true);
562 switch (call->count2) {
563 case YFS_ENDPOINT_IPV4:
564 if (ntohl(bp[0]) != sizeof(__be32) * 2)
565 return afs_protocol_error(
566 call, afs_eproto_yvl_vlendpt4_len);
569 case YFS_ENDPOINT_IPV6:
570 if (ntohl(bp[0]) != sizeof(__be32) * 5)
571 return afs_protocol_error(
572 call, afs_eproto_yvl_vlendpt6_len);
576 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
579 /* Got either the type of the next entry or the count of
580 * volEndpoints if no more fsEndpoints.
584 goto next_volendpoint;
587 afs_extract_discard(call, 0);
588 call->unmarshall = 5;
590 fallthrough; /* Done */
592 ret = afs_extract_data(call, false);
595 call->unmarshall = 6;
602 _leave(" = 0 [done]");
607 * YFSVL.GetEndpoints operation type.
609 static const struct afs_call_type afs_YFSVLGetEndpoints = {
610 .name = "YFSVL.GetEndpoints",
611 .op = afs_YFSVL_GetEndpoints,
612 .deliver = afs_deliver_yfsvl_get_endpoints,
613 .destructor = afs_vl_get_addrs_u_destructor,
617 * Dispatch an operation to get the addresses for a server, where the server is
620 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *vc,
623 struct afs_call *call;
624 struct afs_net *net = vc->cell->net;
629 call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints,
630 sizeof(__be32) * 2 + sizeof(*uuid),
631 sizeof(struct in6_addr) + sizeof(__be32) * 3);
633 return ERR_PTR(-ENOMEM);
636 call->ret_alist = NULL;
637 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
639 /* Marshall the parameters */
641 *bp++ = htonl(YVLGETENDPOINTS);
642 *bp++ = htonl(YFS_SERVER_UUID);
643 memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */
645 trace_afs_make_vl_call(call);
646 afs_make_call(&vc->ac, call, GFP_KERNEL);
647 return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac);
651 * Deliver reply data to a YFSVL.GetCellName operation.
653 static int afs_deliver_yfsvl_get_cell_name(struct afs_call *call)
656 u32 namesz, paddedsz;
659 _enter("{%u,%zu/%u}",
660 call->unmarshall, iov_iter_count(call->iter), call->count);
662 switch (call->unmarshall) {
664 afs_extract_to_tmp(call);
667 fallthrough; /* and extract the cell name length */
669 ret = afs_extract_data(call, true);
673 namesz = ntohl(call->tmp);
674 if (namesz > AFS_MAXCELLNAME)
675 return afs_protocol_error(call, afs_eproto_cellname_len);
676 paddedsz = (namesz + 3) & ~3;
677 call->count = namesz;
678 call->count2 = paddedsz - namesz;
680 cell_name = kmalloc(namesz + 1, GFP_KERNEL);
683 cell_name[namesz] = 0;
684 call->ret_str = cell_name;
686 afs_extract_begin(call, cell_name, namesz);
689 fallthrough; /* and extract cell name */
691 ret = afs_extract_data(call, true);
695 afs_extract_discard(call, call->count2);
698 fallthrough; /* and extract padding */
700 ret = afs_extract_data(call, false);
708 _leave(" = 0 [done]");
712 static void afs_destroy_yfsvl_get_cell_name(struct afs_call *call)
714 kfree(call->ret_str);
715 afs_flat_call_destructor(call);
719 * VL.GetCapabilities operation type
721 static const struct afs_call_type afs_YFSVLGetCellName = {
722 .name = "YFSVL.GetCellName",
723 .op = afs_YFSVL_GetCellName,
724 .deliver = afs_deliver_yfsvl_get_cell_name,
725 .destructor = afs_destroy_yfsvl_get_cell_name,
729 * Probe a volume server for the capabilities that it supports. This can
730 * return up to 196 words.
732 * We use this to probe for service upgrade to determine what the server at the
733 * other end supports.
735 char *afs_yfsvl_get_cell_name(struct afs_vl_cursor *vc)
737 struct afs_call *call;
738 struct afs_net *net = vc->cell->net;
743 call = afs_alloc_flat_call(net, &afs_YFSVLGetCellName, 1 * 4, 0);
745 return ERR_PTR(-ENOMEM);
748 call->ret_str = NULL;
749 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
751 /* marshall the parameters */
753 *bp++ = htonl(YVLGETCELLNAME);
755 /* Can't take a ref on server */
756 trace_afs_make_vl_call(call);
757 afs_make_call(&vc->ac, call, GFP_KERNEL);
758 return (char *)afs_wait_for_call_to_complete(call, &vc->ac);