1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* AFS Volume Location Service client 3 * 4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/gfp.h> 9 #include <linux/init.h> 10 #include <linux/sched.h> 11 #include "afs_fs.h" 12 #include "internal.h" 13 14 /* 15 * Deliver reply data to a VL.GetEntryByNameU call. 16 */ 17 static int afs_deliver_vl_get_entry_by_name_u(struct afs_call *call) 18 { 19 struct afs_uvldbentry__xdr *uvldb; 20 struct afs_vldb_entry *entry; 21 bool new_only = false; 22 u32 tmp, nr_servers, vlflags; 23 int i, ret; 24 25 _enter(""); 26 27 ret = afs_transfer_reply(call); 28 if (ret < 0) 29 return ret; 30 31 /* unmarshall the reply once we've received all of it */ 32 uvldb = call->buffer; 33 entry = call->ret_vldb; 34 35 nr_servers = ntohl(uvldb->nServers); 36 if (nr_servers > AFS_NMAXNSERVERS) 37 nr_servers = AFS_NMAXNSERVERS; 38 39 for (i = 0; i < ARRAY_SIZE(uvldb->name) - 1; i++) 40 entry->name[i] = (u8)ntohl(uvldb->name[i]); 41 entry->name[i] = 0; 42 entry->name_len = strlen(entry->name); 43 44 /* If there is a new replication site that we can use, ignore all the 45 * sites that aren't marked as new. 46 */ 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)) 51 new_only = true; 52 } 53 54 vlflags = ntohl(uvldb->flags); 55 for (i = 0; i < nr_servers; i++) { 56 struct afs_uuid__xdr *xdr; 57 struct afs_uuid *uuid; 58 int j; 59 60 tmp = ntohl(uvldb->serverFlags[i]); 61 if (tmp & AFS_VLSF_DONTUSE || 62 (new_only && !(tmp & AFS_VLSF_NEWREPSITE))) 63 continue; 64 if (tmp & AFS_VLSF_RWVOL) { 65 entry->fs_mask[i] |= AFS_VOL_VTM_RW; 66 if (vlflags & AFS_VLF_BACKEXISTS) 67 entry->fs_mask[i] |= AFS_VOL_VTM_BAK; 68 } 69 if (tmp & AFS_VLSF_ROVOL) 70 entry->fs_mask[i] |= AFS_VOL_VTM_RO; 71 if (!entry->fs_mask[i]) 72 continue; 73 74 xdr = &uvldb->serverNumber[i]; 75 uuid = (struct afs_uuid *)&entry->fs_server[i]; 76 uuid->time_low = xdr->time_low; 77 uuid->time_mid = htons(ntohl(xdr->time_mid)); 78 uuid->time_hi_and_version = htons(ntohl(xdr->time_hi_and_version)); 79 uuid->clock_seq_hi_and_reserved = (u8)ntohl(xdr->clock_seq_hi_and_reserved); 80 uuid->clock_seq_low = (u8)ntohl(xdr->clock_seq_low); 81 for (j = 0; j < 6; j++) 82 uuid->node[j] = (u8)ntohl(xdr->node[j]); 83 84 entry->nr_servers++; 85 } 86 87 for (i = 0; i < AFS_MAXTYPES; i++) 88 entry->vid[i] = ntohl(uvldb->volumeId[i]); 89 90 if (vlflags & AFS_VLF_RWEXISTS) 91 __set_bit(AFS_VLDB_HAS_RW, &entry->flags); 92 if (vlflags & AFS_VLF_ROEXISTS) 93 __set_bit(AFS_VLDB_HAS_RO, &entry->flags); 94 if (vlflags & AFS_VLF_BACKEXISTS) 95 __set_bit(AFS_VLDB_HAS_BAK, &entry->flags); 96 97 if (!(vlflags & (AFS_VLF_RWEXISTS | AFS_VLF_ROEXISTS | AFS_VLF_BACKEXISTS))) { 98 entry->error = -ENOMEDIUM; 99 __set_bit(AFS_VLDB_QUERY_ERROR, &entry->flags); 100 } 101 102 __set_bit(AFS_VLDB_QUERY_VALID, &entry->flags); 103 _leave(" = 0 [done]"); 104 return 0; 105 } 106 107 static void afs_destroy_vl_get_entry_by_name_u(struct afs_call *call) 108 { 109 kfree(call->ret_vldb); 110 afs_flat_call_destructor(call); 111 } 112 113 /* 114 * VL.GetEntryByNameU operation type. 115 */ 116 static const struct afs_call_type afs_RXVLGetEntryByNameU = { 117 .name = "VL.GetEntryByNameU", 118 .op = afs_VL_GetEntryByNameU, 119 .deliver = afs_deliver_vl_get_entry_by_name_u, 120 .destructor = afs_destroy_vl_get_entry_by_name_u, 121 }; 122 123 /* 124 * Dispatch a get volume entry by name or ID operation (uuid variant). If the 125 * volname is a decimal number then it's a volume ID not a volume name. 126 */ 127 struct afs_vldb_entry *afs_vl_get_entry_by_name_u(struct afs_vl_cursor *vc, 128 const char *volname, 129 int volnamesz) 130 { 131 struct afs_vldb_entry *entry; 132 struct afs_call *call; 133 struct afs_net *net = vc->cell->net; 134 size_t reqsz, padsz; 135 __be32 *bp; 136 137 _enter(""); 138 139 padsz = (4 - (volnamesz & 3)) & 3; 140 reqsz = 8 + volnamesz + padsz; 141 142 entry = kzalloc(sizeof(struct afs_vldb_entry), GFP_KERNEL); 143 if (!entry) 144 return ERR_PTR(-ENOMEM); 145 146 call = afs_alloc_flat_call(net, &afs_RXVLGetEntryByNameU, reqsz, 147 sizeof(struct afs_uvldbentry__xdr)); 148 if (!call) { 149 kfree(entry); 150 return ERR_PTR(-ENOMEM); 151 } 152 153 call->key = vc->key; 154 call->ret_vldb = entry; 155 call->max_lifespan = AFS_VL_MAX_LIFESPAN; 156 157 /* Marshall the parameters */ 158 bp = call->request; 159 *bp++ = htonl(VLGETENTRYBYNAMEU); 160 *bp++ = htonl(volnamesz); 161 memcpy(bp, volname, volnamesz); 162 if (padsz > 0) 163 memset((void *)bp + volnamesz, 0, padsz); 164 165 trace_afs_make_vl_call(call); 166 afs_make_call(&vc->ac, call, GFP_KERNEL); 167 return (struct afs_vldb_entry *)afs_wait_for_call_to_complete(call, &vc->ac); 168 } 169 170 /* 171 * Deliver reply data to a VL.GetAddrsU call. 172 * 173 * GetAddrsU(IN ListAddrByAttributes *inaddr, 174 * OUT afsUUID *uuidp1, 175 * OUT uint32_t *uniquifier, 176 * OUT uint32_t *nentries, 177 * OUT bulkaddrs *blkaddrs); 178 */ 179 static int afs_deliver_vl_get_addrs_u(struct afs_call *call) 180 { 181 struct afs_addr_list *alist; 182 __be32 *bp; 183 u32 uniquifier, nentries, count; 184 int i, ret; 185 186 _enter("{%u,%zu/%u}", 187 call->unmarshall, iov_iter_count(call->_iter), call->count); 188 189 switch (call->unmarshall) { 190 case 0: 191 afs_extract_to_buf(call, 192 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32)); 193 call->unmarshall++; 194 195 /* Extract the returned uuid, uniquifier, nentries and 196 * blkaddrs size */ 197 /* Fall through */ 198 case 1: 199 ret = afs_extract_data(call, true); 200 if (ret < 0) 201 return ret; 202 203 bp = call->buffer + sizeof(struct afs_uuid__xdr); 204 uniquifier = ntohl(*bp++); 205 nentries = ntohl(*bp++); 206 count = ntohl(*bp); 207 208 nentries = min(nentries, count); 209 alist = afs_alloc_addrlist(nentries, FS_SERVICE, AFS_FS_PORT); 210 if (!alist) 211 return -ENOMEM; 212 alist->version = uniquifier; 213 call->ret_alist = alist; 214 call->count = count; 215 call->count2 = nentries; 216 call->unmarshall++; 217 218 more_entries: 219 count = min(call->count, 4U); 220 afs_extract_to_buf(call, count * sizeof(__be32)); 221 222 /* Fall through - and extract entries */ 223 case 2: 224 ret = afs_extract_data(call, call->count > 4); 225 if (ret < 0) 226 return ret; 227 228 alist = call->ret_alist; 229 bp = call->buffer; 230 count = min(call->count, 4U); 231 for (i = 0; i < count; i++) 232 if (alist->nr_addrs < call->count2) 233 afs_merge_fs_addr4(alist, *bp++, AFS_FS_PORT); 234 235 call->count -= count; 236 if (call->count > 0) 237 goto more_entries; 238 call->unmarshall++; 239 break; 240 } 241 242 _leave(" = 0 [done]"); 243 return 0; 244 } 245 246 static void afs_vl_get_addrs_u_destructor(struct afs_call *call) 247 { 248 afs_put_addrlist(call->ret_alist); 249 return afs_flat_call_destructor(call); 250 } 251 252 /* 253 * VL.GetAddrsU operation type. 254 */ 255 static const struct afs_call_type afs_RXVLGetAddrsU = { 256 .name = "VL.GetAddrsU", 257 .op = afs_VL_GetAddrsU, 258 .deliver = afs_deliver_vl_get_addrs_u, 259 .destructor = afs_vl_get_addrs_u_destructor, 260 }; 261 262 /* 263 * Dispatch an operation to get the addresses for a server, where the server is 264 * nominated by UUID. 265 */ 266 struct afs_addr_list *afs_vl_get_addrs_u(struct afs_vl_cursor *vc, 267 const uuid_t *uuid) 268 { 269 struct afs_ListAddrByAttributes__xdr *r; 270 const struct afs_uuid *u = (const struct afs_uuid *)uuid; 271 struct afs_call *call; 272 struct afs_net *net = vc->cell->net; 273 __be32 *bp; 274 int i; 275 276 _enter(""); 277 278 call = afs_alloc_flat_call(net, &afs_RXVLGetAddrsU, 279 sizeof(__be32) + sizeof(struct afs_ListAddrByAttributes__xdr), 280 sizeof(struct afs_uuid__xdr) + 3 * sizeof(__be32)); 281 if (!call) 282 return ERR_PTR(-ENOMEM); 283 284 call->key = vc->key; 285 call->ret_alist = NULL; 286 call->max_lifespan = AFS_VL_MAX_LIFESPAN; 287 288 /* Marshall the parameters */ 289 bp = call->request; 290 *bp++ = htonl(VLGETADDRSU); 291 r = (struct afs_ListAddrByAttributes__xdr *)bp; 292 r->Mask = htonl(AFS_VLADDR_UUID); 293 r->ipaddr = 0; 294 r->index = 0; 295 r->spare = 0; 296 r->uuid.time_low = u->time_low; 297 r->uuid.time_mid = htonl(ntohs(u->time_mid)); 298 r->uuid.time_hi_and_version = htonl(ntohs(u->time_hi_and_version)); 299 r->uuid.clock_seq_hi_and_reserved = htonl(u->clock_seq_hi_and_reserved); 300 r->uuid.clock_seq_low = htonl(u->clock_seq_low); 301 for (i = 0; i < 6; i++) 302 r->uuid.node[i] = htonl(u->node[i]); 303 304 trace_afs_make_vl_call(call); 305 afs_make_call(&vc->ac, call, GFP_KERNEL); 306 return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac); 307 } 308 309 /* 310 * Deliver reply data to an VL.GetCapabilities operation. 311 */ 312 static int afs_deliver_vl_get_capabilities(struct afs_call *call) 313 { 314 u32 count; 315 int ret; 316 317 _enter("{%u,%zu/%u}", 318 call->unmarshall, iov_iter_count(call->_iter), call->count); 319 320 switch (call->unmarshall) { 321 case 0: 322 afs_extract_to_tmp(call); 323 call->unmarshall++; 324 325 /* Fall through - and extract the capabilities word count */ 326 case 1: 327 ret = afs_extract_data(call, true); 328 if (ret < 0) 329 return ret; 330 331 count = ntohl(call->tmp); 332 call->count = count; 333 call->count2 = count; 334 335 call->unmarshall++; 336 afs_extract_discard(call, count * sizeof(__be32)); 337 338 /* Fall through - and extract capabilities words */ 339 case 2: 340 ret = afs_extract_data(call, false); 341 if (ret < 0) 342 return ret; 343 344 /* TODO: Examine capabilities */ 345 346 call->unmarshall++; 347 break; 348 } 349 350 _leave(" = 0 [done]"); 351 return 0; 352 } 353 354 static void afs_destroy_vl_get_capabilities(struct afs_call *call) 355 { 356 afs_put_vlserver(call->net, call->vlserver); 357 afs_flat_call_destructor(call); 358 } 359 360 /* 361 * VL.GetCapabilities operation type 362 */ 363 static const struct afs_call_type afs_RXVLGetCapabilities = { 364 .name = "VL.GetCapabilities", 365 .op = afs_VL_GetCapabilities, 366 .deliver = afs_deliver_vl_get_capabilities, 367 .done = afs_vlserver_probe_result, 368 .destructor = afs_destroy_vl_get_capabilities, 369 }; 370 371 /* 372 * Probe a volume server for the capabilities that it supports. This can 373 * return up to 196 words. 374 * 375 * We use this to probe for service upgrade to determine what the server at the 376 * other end supports. 377 */ 378 struct afs_call *afs_vl_get_capabilities(struct afs_net *net, 379 struct afs_addr_cursor *ac, 380 struct key *key, 381 struct afs_vlserver *server, 382 unsigned int server_index) 383 { 384 struct afs_call *call; 385 __be32 *bp; 386 387 _enter(""); 388 389 call = afs_alloc_flat_call(net, &afs_RXVLGetCapabilities, 1 * 4, 16 * 4); 390 if (!call) 391 return ERR_PTR(-ENOMEM); 392 393 call->key = key; 394 call->vlserver = afs_get_vlserver(server); 395 call->server_index = server_index; 396 call->upgrade = true; 397 call->async = true; 398 call->max_lifespan = AFS_PROBE_MAX_LIFESPAN; 399 400 /* marshall the parameters */ 401 bp = call->request; 402 *bp++ = htonl(VLGETCAPABILITIES); 403 404 /* Can't take a ref on server */ 405 trace_afs_make_vl_call(call); 406 afs_make_call(ac, call, GFP_KERNEL); 407 return call; 408 } 409 410 /* 411 * Deliver reply data to a YFSVL.GetEndpoints call. 412 * 413 * GetEndpoints(IN yfsServerAttributes *attr, 414 * OUT opr_uuid *uuid, 415 * OUT afs_int32 *uniquifier, 416 * OUT endpoints *fsEndpoints, 417 * OUT endpoints *volEndpoints) 418 */ 419 static int afs_deliver_yfsvl_get_endpoints(struct afs_call *call) 420 { 421 struct afs_addr_list *alist; 422 __be32 *bp; 423 u32 uniquifier, size; 424 int ret; 425 426 _enter("{%u,%zu,%u}", 427 call->unmarshall, iov_iter_count(call->_iter), call->count2); 428 429 switch (call->unmarshall) { 430 case 0: 431 afs_extract_to_buf(call, sizeof(uuid_t) + 3 * sizeof(__be32)); 432 call->unmarshall = 1; 433 434 /* Extract the returned uuid, uniquifier, fsEndpoints count and 435 * either the first fsEndpoint type or the volEndpoints 436 * count if there are no fsEndpoints. */ 437 /* Fall through */ 438 case 1: 439 ret = afs_extract_data(call, true); 440 if (ret < 0) 441 return ret; 442 443 bp = call->buffer + sizeof(uuid_t); 444 uniquifier = ntohl(*bp++); 445 call->count = ntohl(*bp++); 446 call->count2 = ntohl(*bp); /* Type or next count */ 447 448 if (call->count > YFS_MAXENDPOINTS) 449 return afs_protocol_error(call, -EBADMSG, 450 afs_eproto_yvl_fsendpt_num); 451 452 alist = afs_alloc_addrlist(call->count, FS_SERVICE, AFS_FS_PORT); 453 if (!alist) 454 return -ENOMEM; 455 alist->version = uniquifier; 456 call->ret_alist = alist; 457 458 if (call->count == 0) 459 goto extract_volendpoints; 460 461 next_fsendpoint: 462 switch (call->count2) { 463 case YFS_ENDPOINT_IPV4: 464 size = sizeof(__be32) * (1 + 1 + 1); 465 break; 466 case YFS_ENDPOINT_IPV6: 467 size = sizeof(__be32) * (1 + 4 + 1); 468 break; 469 default: 470 return afs_protocol_error(call, -EBADMSG, 471 afs_eproto_yvl_fsendpt_type); 472 } 473 474 size += sizeof(__be32); 475 afs_extract_to_buf(call, size); 476 call->unmarshall = 2; 477 478 /* Fall through - and extract fsEndpoints[] entries */ 479 case 2: 480 ret = afs_extract_data(call, true); 481 if (ret < 0) 482 return ret; 483 484 alist = call->ret_alist; 485 bp = call->buffer; 486 switch (call->count2) { 487 case YFS_ENDPOINT_IPV4: 488 if (ntohl(bp[0]) != sizeof(__be32) * 2) 489 return afs_protocol_error(call, -EBADMSG, 490 afs_eproto_yvl_fsendpt4_len); 491 afs_merge_fs_addr4(alist, bp[1], ntohl(bp[2])); 492 bp += 3; 493 break; 494 case YFS_ENDPOINT_IPV6: 495 if (ntohl(bp[0]) != sizeof(__be32) * 5) 496 return afs_protocol_error(call, -EBADMSG, 497 afs_eproto_yvl_fsendpt6_len); 498 afs_merge_fs_addr6(alist, bp + 1, ntohl(bp[5])); 499 bp += 6; 500 break; 501 default: 502 return afs_protocol_error(call, -EBADMSG, 503 afs_eproto_yvl_fsendpt_type); 504 } 505 506 /* Got either the type of the next entry or the count of 507 * volEndpoints if no more fsEndpoints. 508 */ 509 call->count2 = ntohl(*bp++); 510 511 call->count--; 512 if (call->count > 0) 513 goto next_fsendpoint; 514 515 extract_volendpoints: 516 /* Extract the list of volEndpoints. */ 517 call->count = call->count2; 518 if (!call->count) 519 goto end; 520 if (call->count > YFS_MAXENDPOINTS) 521 return afs_protocol_error(call, -EBADMSG, 522 afs_eproto_yvl_vlendpt_type); 523 524 afs_extract_to_buf(call, 1 * sizeof(__be32)); 525 call->unmarshall = 3; 526 527 /* Extract the type of volEndpoints[0]. Normally we would 528 * extract the type of the next endpoint when we extract the 529 * data of the current one, but this is the first... 530 */ 531 /* Fall through */ 532 case 3: 533 ret = afs_extract_data(call, true); 534 if (ret < 0) 535 return ret; 536 537 bp = call->buffer; 538 539 next_volendpoint: 540 call->count2 = ntohl(*bp++); 541 switch (call->count2) { 542 case YFS_ENDPOINT_IPV4: 543 size = sizeof(__be32) * (1 + 1 + 1); 544 break; 545 case YFS_ENDPOINT_IPV6: 546 size = sizeof(__be32) * (1 + 4 + 1); 547 break; 548 default: 549 return afs_protocol_error(call, -EBADMSG, 550 afs_eproto_yvl_vlendpt_type); 551 } 552 553 if (call->count > 1) 554 size += sizeof(__be32); /* Get next type too */ 555 afs_extract_to_buf(call, size); 556 call->unmarshall = 4; 557 558 /* Fall through - and extract volEndpoints[] entries */ 559 case 4: 560 ret = afs_extract_data(call, true); 561 if (ret < 0) 562 return ret; 563 564 bp = call->buffer; 565 switch (call->count2) { 566 case YFS_ENDPOINT_IPV4: 567 if (ntohl(bp[0]) != sizeof(__be32) * 2) 568 return afs_protocol_error(call, -EBADMSG, 569 afs_eproto_yvl_vlendpt4_len); 570 bp += 3; 571 break; 572 case YFS_ENDPOINT_IPV6: 573 if (ntohl(bp[0]) != sizeof(__be32) * 5) 574 return afs_protocol_error(call, -EBADMSG, 575 afs_eproto_yvl_vlendpt6_len); 576 bp += 6; 577 break; 578 default: 579 return afs_protocol_error(call, -EBADMSG, 580 afs_eproto_yvl_vlendpt_type); 581 } 582 583 /* Got either the type of the next entry or the count of 584 * volEndpoints if no more fsEndpoints. 585 */ 586 call->count--; 587 if (call->count > 0) 588 goto next_volendpoint; 589 590 end: 591 afs_extract_discard(call, 0); 592 call->unmarshall = 5; 593 594 /* Fall through - Done */ 595 case 5: 596 ret = afs_extract_data(call, false); 597 if (ret < 0) 598 return ret; 599 call->unmarshall = 6; 600 601 case 6: 602 break; 603 } 604 605 _leave(" = 0 [done]"); 606 return 0; 607 } 608 609 /* 610 * YFSVL.GetEndpoints operation type. 611 */ 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, 617 }; 618 619 /* 620 * Dispatch an operation to get the addresses for a server, where the server is 621 * nominated by UUID. 622 */ 623 struct afs_addr_list *afs_yfsvl_get_endpoints(struct afs_vl_cursor *vc, 624 const uuid_t *uuid) 625 { 626 struct afs_call *call; 627 struct afs_net *net = vc->cell->net; 628 __be32 *bp; 629 630 _enter(""); 631 632 call = afs_alloc_flat_call(net, &afs_YFSVLGetEndpoints, 633 sizeof(__be32) * 2 + sizeof(*uuid), 634 sizeof(struct in6_addr) + sizeof(__be32) * 3); 635 if (!call) 636 return ERR_PTR(-ENOMEM); 637 638 call->key = vc->key; 639 call->ret_alist = NULL; 640 call->max_lifespan = AFS_VL_MAX_LIFESPAN; 641 642 /* Marshall the parameters */ 643 bp = call->request; 644 *bp++ = htonl(YVLGETENDPOINTS); 645 *bp++ = htonl(YFS_SERVER_UUID); 646 memcpy(bp, uuid, sizeof(*uuid)); /* Type opr_uuid */ 647 648 trace_afs_make_vl_call(call); 649 afs_make_call(&vc->ac, call, GFP_KERNEL); 650 return (struct afs_addr_list *)afs_wait_for_call_to_complete(call, &vc->ac); 651 } 652