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]");
110 * VL.GetEntryByNameU operation type.
112 static const struct afs_call_type afs_RXVLGetEntryByNameU = {
113 .name = "VL.GetEntryByNameU",
114 .op = afs_VL_GetEntryByNameU,
115 .deliver = afs_deliver_vl_get_entry_by_name_u,
116 .destructor = afs_flat_call_destructor,
120 * Dispatch a get volume entry by name or ID operation (uuid variant). If the
121 * volname is a decimal number then it's a volume ID not a volume name.
123 struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_vl_cursor *vc,
127 struct afs_vldb_entry *entry;
128 struct afs_call *call;
129 struct afs_net *net = vc->cell->net;
135 padsz = (4 - (volnamesz & 3)) & 3;
136 reqsz = 8 + volnamesz + padsz;
138 entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL);
140 return ERR_PTR(-ENOMEM);
142 call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz,
143 sizeof(struct afs_uvldbentry__xdr));
146 return ERR_PTR(-ENOMEM);
150 call->ret_vldb = entry;
151 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
153 /* Marshall the parameters */
155 *bp++ = htonl(VLGETENTRYBYNAMEU);
156 *bp++ = htonl(volnamesz);
157 memcpy(bp, volname, volnamesz);
159 memset((void *)bp + volnamesz, 0, padsz);
161 trace_afs_make_vl_call(call);
162 afs_make_call(&vc->ac, call, GFP_KERNEL);
163 afs_wait_for_call_to_complete(call, &vc->ac);
164 vc->call_abort_code = call->abort_code;
165 vc->call_error = call->error;
166 vc->call_responded = call->responded;
168 if (vc->call_error) {
170 return ERR_PTR(vc->call_error);
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}",
192 call->unmarshall, iov_iter_count(call->iter), call->count);
194 switch (call->unmarshall) {
196 afs_extract_to_buf(call,
197 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
200 /* Extract the returned uuid, uniquifier, nentries and
204 ret = afs_extract_data(call, true);
208 bp = call->buffer + sizeof(struct afs_uuid__xdr);
209 uniquifier = ntohl(*bp++);
210 nentries = ntohl(*bp++);
213 nentries = min(nentries, count);
214 alist = afs_alloc_addrlist(nentries, FS_SERVICE);
217 alist->version = uniquifier;
218 call->ret_alist = alist;
220 call->count2 = nentries;
224 count = min(call->count, 4U);
225 afs_extract_to_buf(call, count * sizeof(__be32));
227 fallthrough; /* and extract entries */
229 ret = afs_extract_data(call, call->count > 4);
233 alist = call->ret_alist;
235 count = min(call->count, 4U);
236 for (i = 0; i < count; i++) {
237 if (alist->nr_addrs < call->count2) {
238 ret = afs_merge_fs_addr4(call->net, alist, *bp++, AFS_FS_PORT);
244 call->count -= count;
251 _leave(" = 0 [done]");
256 * VL.GetAddrsU operation type.
258 static const struct afs_call_type afs_RXVLGetAddrsU = {
259 .name = "VL.GetAddrsU",
260 .op = afs_VL_GetAddrsU,
261 .deliver = afs_deliver_vl_get_addrs_u,
262 .destructor = afs_flat_call_destructor,
266 * Dispatch an operation to get the addresses for a server, where the server is
269 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *vc,
272 struct afs_ListAddrByAttributes__xdr *r;
273 struct afs_addr_list *alist;
274 const struct afs_uuid *u = (const struct afs_uuid *)uuid;
275 struct afs_call *call;
276 struct afs_net *net = vc->cell->net;
282 call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU,
283 sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr),
284 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32));
286 return ERR_PTR(-ENOMEM);
289 call->ret_alist = NULL;
290 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
292 /* Marshall the parameters */
294 *bp++ = htonl(VLGETADDRSU);
295 r = (struct afs_ListAddrByAttributes__xdr *)bp;
296 r->Mask = htonl(AFS_VLADDR_UUID);
300 r->uuid.time_low = u->time_low;
301 r->uuid.time_mid = htonl(ntohs(u->time_mid));
302 r->uuid.time_hi_and_version = htonl(ntohs(u->time_hi_and_version));
303 r->uuid.clock_seq_hi_and_reserved = htonl(u->clock_seq_hi_and_reserved);
304 r->uuid.clock_seq_low = htonl(u->clock_seq_low);
305 for (i = 0; i < 6; i++)
306 r->uuid.node[i] = htonl(u->node[i]);
308 trace_afs_make_vl_call(call);
309 afs_make_call(&vc->ac, call, GFP_KERNEL);
310 afs_wait_for_call_to_complete(call, &vc->ac);
311 vc->call_abort_code = call->abort_code;
312 vc->call_error = call->error;
313 vc->call_responded = call->responded;
314 alist = call->ret_alist;
316 if (vc->call_error) {
317 afs_put_addrlist(alist);
318 return ERR_PTR(vc->call_error);
324 * Deliver reply data to an VL.GetCapabilities operation.
326 static int afs_deliver_vl_get_capabilities(struct afs_call *call)
331 _enter("{%u,%zu/%u}",
332 call->unmarshall, iov_iter_count(call->iter), call->count);
334 switch (call->unmarshall) {
336 afs_extract_to_tmp(call);
339 fallthrough; /* and extract the capabilities word count */
341 ret = afs_extract_data(call, true);
345 count = ntohl(call->tmp);
347 call->count2 = count;
350 afs_extract_discard(call, count * sizeof(__be32));
352 fallthrough; /* and extract capabilities words */
354 ret = afs_extract_data(call, false);
358 /* TODO: Examine capabilities */
364 _leave(" = 0 [done]");
368 static void afs_destroy_vl_get_capabilities(struct afs_call *call)
370 afs_put_vlserver(call->net, call->vlserver);
371 afs_flat_call_destructor(call);
375 * VL.GetCapabilities operation type
377 static const struct afs_call_type afs_RXVLGetCapabilities = {
378 .name = "VL.GetCapabilities",
379 .op = afs_VL_GetCapabilities,
380 .deliver = afs_deliver_vl_get_capabilities,
381 .done = afs_vlserver_probe_result,
382 .destructor = afs_destroy_vl_get_capabilities,
386 * Probe a volume server for the capabilities that it supports. This can
387 * return up to 196 words.
389 * We use this to probe for service upgrade to determine what the server at the
390 * other end supports.
392 struct afs_call *afs_vl_get_capabilities(struct afs_net *net,
393 struct afs_addr_cursor *ac,
395 struct afs_vlserver *server,
396 unsigned int server_index)
398 struct afs_call *call;
403 call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4);
405 return ERR_PTR(-ENOMEM);
408 call->vlserver = afs_get_vlserver(server);
409 call->server_index = server_index;
410 call->upgrade = true;
412 call->max_lifespan = AFS_PROBE_MAX_LIFESPAN;
414 /* marshall the parameters */
416 *bp++ = htonl(VLGETCAPABILITIES);
418 /* Can't take a ref on server */
419 trace_afs_make_vl_call(call);
420 afs_make_call(ac, call, GFP_KERNEL);
425 * Deliver reply data to a YFSVL.GetEndpoints call.
427 * GetEndpoints(IN yfsServerAttributes *attr,
428 * OUT opr_uuid *uuid,
429 * OUT afs_int32 *uniquifier,
430 * OUT endpoints *fsEndpoints,
431 * OUT endpoints *volEndpoints)
433 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call)
435 struct afs_addr_list *alist;
437 u32 uniquifier, size;
440 _enter("{%u,%zu,%u}",
441 call->unmarshall, iov_iter_count(call->iter), call->count2);
443 switch (call->unmarshall) {
445 afs_extract_to_buf(call, sizeof(uuid_t) + 3 * sizeof(__be32));
446 call->unmarshall = 1;
448 /* Extract the returned uuid, uniquifier, fsEndpoints count and
449 * either the first fsEndpoint type or the volEndpoints
450 * count if there are no fsEndpoints. */
453 ret = afs_extract_data(call, true);
457 bp = call->buffer + sizeof(uuid_t);
458 uniquifier = ntohl(*bp++);
459 call->count = ntohl(*bp++);
460 call->count2 = ntohl(*bp); /* Type or next count */
462 if (call->count > YFS_MAXENDPOINTS)
463 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_num);
465 alist = afs_alloc_addrlist(call->count, FS_SERVICE);
468 alist->version = uniquifier;
469 call->ret_alist = alist;
471 if (call->count == 0)
472 goto extract_volendpoints;
475 switch (call->count2) {
476 case YFS_ENDPOINT_IPV4:
477 size = sizeof(__be32) * (1 + 1 + 1);
479 case YFS_ENDPOINT_IPV6:
480 size = sizeof(__be32) * (1 + 4 + 1);
483 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_type);
486 size += sizeof(__be32);
487 afs_extract_to_buf(call, size);
488 call->unmarshall = 2;
490 fallthrough; /* and extract fsEndpoints[] entries */
492 ret = afs_extract_data(call, true);
496 alist = call->ret_alist;
498 switch (call->count2) {
499 case YFS_ENDPOINT_IPV4:
500 if (ntohl(bp[0]) != sizeof(__be32) * 2)
501 return afs_protocol_error(
502 call, afs_eproto_yvl_fsendpt4_len);
503 ret = afs_merge_fs_addr4(call->net, alist, bp[1], ntohl(bp[2]));
508 case YFS_ENDPOINT_IPV6:
509 if (ntohl(bp[0]) != sizeof(__be32) * 5)
510 return afs_protocol_error(
511 call, afs_eproto_yvl_fsendpt6_len);
512 ret = afs_merge_fs_addr6(call->net, alist, bp + 1, ntohl(bp[5]));
518 return afs_protocol_error(call, afs_eproto_yvl_fsendpt_type);
521 /* Got either the type of the next entry or the count of
522 * volEndpoints if no more fsEndpoints.
524 call->count2 = ntohl(*bp++);
528 goto next_fsendpoint;
530 extract_volendpoints:
531 /* Extract the list of volEndpoints. */
532 call->count = call->count2;
535 if (call->count > YFS_MAXENDPOINTS)
536 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
538 afs_extract_to_buf(call, 1 * sizeof(__be32));
539 call->unmarshall = 3;
541 /* Extract the type of volEndpoints[0]. Normally we would
542 * extract the type of the next endpoint when we extract the
543 * data of the current one, but this is the first...
547 ret = afs_extract_data(call, true);
554 call->count2 = ntohl(*bp++);
555 switch (call->count2) {
556 case YFS_ENDPOINT_IPV4:
557 size = sizeof(__be32) * (1 + 1 + 1);
559 case YFS_ENDPOINT_IPV6:
560 size = sizeof(__be32) * (1 + 4 + 1);
563 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
567 size += sizeof(__be32); /* Get next type too */
568 afs_extract_to_buf(call, size);
569 call->unmarshall = 4;
571 fallthrough; /* and extract volEndpoints[] entries */
573 ret = afs_extract_data(call, true);
578 switch (call->count2) {
579 case YFS_ENDPOINT_IPV4:
580 if (ntohl(bp[0]) != sizeof(__be32) * 2)
581 return afs_protocol_error(
582 call, afs_eproto_yvl_vlendpt4_len);
585 case YFS_ENDPOINT_IPV6:
586 if (ntohl(bp[0]) != sizeof(__be32) * 5)
587 return afs_protocol_error(
588 call, afs_eproto_yvl_vlendpt6_len);
592 return afs_protocol_error(call, afs_eproto_yvl_vlendpt_type);
595 /* Got either the type of the next entry or the count of
596 * volEndpoints if no more fsEndpoints.
600 goto next_volendpoint;
603 afs_extract_discard(call, 0);
604 call->unmarshall = 5;
606 fallthrough; /* Done */
608 ret = afs_extract_data(call, false);
611 call->unmarshall = 6;
618 _leave(" = 0 [done]");
623 * YFSVL.GetEndpoints operation type.
625 static const struct afs_call_type afs_YFSVLGetEndpoints = {
626 .name = "YFSVL.GetEndpoints",
627 .op = afs_YFSVL_GetEndpoints,
628 .deliver = afs_deliver_yfsvl_get_endpoints,
629 .destructor = afs_flat_call_destructor,
633 * Dispatch an operation to get the addresses for a server, where the server is
636 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *vc,
639 struct afs_addr_list *alist;
640 struct afs_call *call;
641 struct afs_net *net = vc->cell->net;
646 call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints,
647 sizeof(__be32) * 2 + sizeof(*uuid),
648 sizeof(struct in6_addr) + sizeof(__be32) * 3);
650 return ERR_PTR(-ENOMEM);
653 call->ret_alist = NULL;
654 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
656 /* Marshall the parameters */
658 *bp++ = htonl(YVLGETENDPOINTS);
659 *bp++ = htonl(YFS_SERVER_UUID);
660 memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */
662 trace_afs_make_vl_call(call);
663 afs_make_call(&vc->ac, call, GFP_KERNEL);
664 afs_wait_for_call_to_complete(call, &vc->ac);
665 vc->call_abort_code = call->abort_code;
666 vc->call_error = call->error;
667 vc->call_responded = call->responded;
668 alist = call->ret_alist;
670 if (vc->call_error) {
671 afs_put_addrlist(alist);
672 return ERR_PTR(vc->call_error);
678 * Deliver reply data to a YFSVL.GetCellName operation.
680 static int afs_deliver_yfsvl_get_cell_name(struct afs_call *call)
683 u32 namesz, paddedsz;
686 _enter("{%u,%zu/%u}",
687 call->unmarshall, iov_iter_count(call->iter), call->count);
689 switch (call->unmarshall) {
691 afs_extract_to_tmp(call);
694 fallthrough; /* and extract the cell name length */
696 ret = afs_extract_data(call, true);
700 namesz = ntohl(call->tmp);
701 if (namesz > AFS_MAXCELLNAME)
702 return afs_protocol_error(call, afs_eproto_cellname_len);
703 paddedsz = (namesz + 3) & ~3;
704 call->count = namesz;
705 call->count2 = paddedsz - namesz;
707 cell_name = kmalloc(namesz + 1, GFP_KERNEL);
710 cell_name[namesz] = 0;
711 call->ret_str = cell_name;
713 afs_extract_begin(call, cell_name, namesz);
716 fallthrough; /* and extract cell name */
718 ret = afs_extract_data(call, true);
722 afs_extract_discard(call, call->count2);
725 fallthrough; /* and extract padding */
727 ret = afs_extract_data(call, false);
735 _leave(" = 0 [done]");
740 * VL.GetCapabilities operation type
742 static const struct afs_call_type afs_YFSVLGetCellName = {
743 .name = "YFSVL.GetCellName",
744 .op = afs_YFSVL_GetCellName,
745 .deliver = afs_deliver_yfsvl_get_cell_name,
746 .destructor = afs_flat_call_destructor,
750 * Probe a volume server for the capabilities that it supports. This can
751 * return up to 196 words.
753 * We use this to probe for service upgrade to determine what the server at the
754 * other end supports.
756 char *afs_yfsvl_get_cell_name(struct afs_vl_cursor *vc)
758 struct afs_call *call;
759 struct afs_net *net = vc->cell->net;
765 call = afs_alloc_flat_call(net, &afs_YFSVLGetCellName, 1 * 4, 0);
767 return ERR_PTR(-ENOMEM);
770 call->ret_str = NULL;
771 call->max_lifespan = AFS_VL_MAX_LIFESPAN;
773 /* marshall the parameters */
775 *bp++ = htonl(YVLGETCELLNAME);
777 /* Can't take a ref on server */
778 trace_afs_make_vl_call(call);
779 afs_make_call(&vc->ac, call, GFP_KERNEL);
780 afs_wait_for_call_to_complete(call, &vc->ac);
781 vc->call_abort_code = call->abort_code;
782 vc->call_error = call->error;
783 vc->call_responded = call->responded;
784 cellname = call->ret_str;
786 if (vc->call_error) {
788 return ERR_PTR(vc->call_error);