1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* AFS Cache Manager Service 3 * 4 * Copyright (C) 2002 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 #include <linux/module.h> 9 #include <linux/init.h> 10 #include <linux/slab.h> 11 #include <linux/sched.h> 12 #include <linux/ip.h> 13 #include "internal.h" 14 #include "afs_cm.h" 15 #include "protocol_yfs.h" 16 17 static int afs_deliver_cb_init_call_back_state(struct afs_call *); 18 static int afs_deliver_cb_init_call_back_state3(struct afs_call *); 19 static int afs_deliver_cb_probe(struct afs_call *); 20 static int afs_deliver_cb_callback(struct afs_call *); 21 static int afs_deliver_cb_probe_uuid(struct afs_call *); 22 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *); 23 static void afs_cm_destructor(struct afs_call *); 24 static void SRXAFSCB_CallBack(struct work_struct *); 25 static void SRXAFSCB_InitCallBackState(struct work_struct *); 26 static void SRXAFSCB_Probe(struct work_struct *); 27 static void SRXAFSCB_ProbeUuid(struct work_struct *); 28 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *); 29 30 static int afs_deliver_yfs_cb_callback(struct afs_call *); 31 32 #define CM_NAME(name) \ 33 char afs_SRXCB##name##_name[] __tracepoint_string = \ 34 "CB." #name 35 36 /* 37 * CB.CallBack operation type 38 */ 39 static CM_NAME(CallBack); 40 static const struct afs_call_type afs_SRXCBCallBack = { 41 .name = afs_SRXCBCallBack_name, 42 .deliver = afs_deliver_cb_callback, 43 .destructor = afs_cm_destructor, 44 .work = SRXAFSCB_CallBack, 45 }; 46 47 /* 48 * CB.InitCallBackState operation type 49 */ 50 static CM_NAME(InitCallBackState); 51 static const struct afs_call_type afs_SRXCBInitCallBackState = { 52 .name = afs_SRXCBInitCallBackState_name, 53 .deliver = afs_deliver_cb_init_call_back_state, 54 .destructor = afs_cm_destructor, 55 .work = SRXAFSCB_InitCallBackState, 56 }; 57 58 /* 59 * CB.InitCallBackState3 operation type 60 */ 61 static CM_NAME(InitCallBackState3); 62 static const struct afs_call_type afs_SRXCBInitCallBackState3 = { 63 .name = afs_SRXCBInitCallBackState3_name, 64 .deliver = afs_deliver_cb_init_call_back_state3, 65 .destructor = afs_cm_destructor, 66 .work = SRXAFSCB_InitCallBackState, 67 }; 68 69 /* 70 * CB.Probe operation type 71 */ 72 static CM_NAME(Probe); 73 static const struct afs_call_type afs_SRXCBProbe = { 74 .name = afs_SRXCBProbe_name, 75 .deliver = afs_deliver_cb_probe, 76 .destructor = afs_cm_destructor, 77 .work = SRXAFSCB_Probe, 78 }; 79 80 /* 81 * CB.ProbeUuid operation type 82 */ 83 static CM_NAME(ProbeUuid); 84 static const struct afs_call_type afs_SRXCBProbeUuid = { 85 .name = afs_SRXCBProbeUuid_name, 86 .deliver = afs_deliver_cb_probe_uuid, 87 .destructor = afs_cm_destructor, 88 .work = SRXAFSCB_ProbeUuid, 89 }; 90 91 /* 92 * CB.TellMeAboutYourself operation type 93 */ 94 static CM_NAME(TellMeAboutYourself); 95 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = { 96 .name = afs_SRXCBTellMeAboutYourself_name, 97 .deliver = afs_deliver_cb_tell_me_about_yourself, 98 .destructor = afs_cm_destructor, 99 .work = SRXAFSCB_TellMeAboutYourself, 100 }; 101 102 /* 103 * YFS CB.CallBack operation type 104 */ 105 static CM_NAME(YFS_CallBack); 106 static const struct afs_call_type afs_SRXYFSCB_CallBack = { 107 .name = afs_SRXCBYFS_CallBack_name, 108 .deliver = afs_deliver_yfs_cb_callback, 109 .destructor = afs_cm_destructor, 110 .work = SRXAFSCB_CallBack, 111 }; 112 113 /* 114 * route an incoming cache manager call 115 * - return T if supported, F if not 116 */ 117 bool afs_cm_incoming_call(struct afs_call *call) 118 { 119 _enter("{%u, CB.OP %u}", call->service_id, call->operation_ID); 120 121 switch (call->operation_ID) { 122 case CBCallBack: 123 call->type = &afs_SRXCBCallBack; 124 return true; 125 case CBInitCallBackState: 126 call->type = &afs_SRXCBInitCallBackState; 127 return true; 128 case CBInitCallBackState3: 129 call->type = &afs_SRXCBInitCallBackState3; 130 return true; 131 case CBProbe: 132 call->type = &afs_SRXCBProbe; 133 return true; 134 case CBProbeUuid: 135 call->type = &afs_SRXCBProbeUuid; 136 return true; 137 case CBTellMeAboutYourself: 138 call->type = &afs_SRXCBTellMeAboutYourself; 139 return true; 140 case YFSCBCallBack: 141 if (call->service_id != YFS_CM_SERVICE) 142 return false; 143 call->type = &afs_SRXYFSCB_CallBack; 144 return true; 145 default: 146 return false; 147 } 148 } 149 150 /* 151 * Find the server record by peer address and record a probe to the cache 152 * manager from a server. 153 */ 154 static int afs_find_cm_server_by_peer(struct afs_call *call) 155 { 156 struct sockaddr_rxrpc srx; 157 struct afs_server *server; 158 159 rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx); 160 161 server = afs_find_server(call->net, &srx); 162 if (!server) { 163 trace_afs_cm_no_server(call, &srx); 164 return 0; 165 } 166 167 call->server = server; 168 return 0; 169 } 170 171 /* 172 * Find the server record by server UUID and record a probe to the cache 173 * manager from a server. 174 */ 175 static int afs_find_cm_server_by_uuid(struct afs_call *call, 176 struct afs_uuid *uuid) 177 { 178 struct afs_server *server; 179 180 rcu_read_lock(); 181 server = afs_find_server_by_uuid(call->net, call->request); 182 rcu_read_unlock(); 183 if (!server) { 184 trace_afs_cm_no_server_u(call, call->request); 185 return 0; 186 } 187 188 call->server = server; 189 return 0; 190 } 191 192 /* 193 * Clean up a cache manager call. 194 */ 195 static void afs_cm_destructor(struct afs_call *call) 196 { 197 kfree(call->buffer); 198 call->buffer = NULL; 199 } 200 201 /* 202 * Abort a service call from within an action function. 203 */ 204 static void afs_abort_service_call(struct afs_call *call, u32 abort_code, int error, 205 const char *why) 206 { 207 rxrpc_kernel_abort_call(call->net->socket, call->rxcall, 208 abort_code, error, why); 209 afs_set_call_complete(call, error, 0); 210 } 211 212 /* 213 * The server supplied a list of callbacks that it wanted to break. 214 */ 215 static void SRXAFSCB_CallBack(struct work_struct *work) 216 { 217 struct afs_call *call = container_of(work, struct afs_call, work); 218 219 _enter(""); 220 221 /* We need to break the callbacks before sending the reply as the 222 * server holds up change visibility till it receives our reply so as 223 * to maintain cache coherency. 224 */ 225 if (call->server) { 226 trace_afs_server(call->server, 227 atomic_read(&call->server->ref), 228 atomic_read(&call->server->active), 229 afs_server_trace_callback); 230 afs_break_callbacks(call->server, call->count, call->request); 231 } 232 233 afs_send_empty_reply(call); 234 afs_put_call(call); 235 _leave(""); 236 } 237 238 /* 239 * deliver request data to a CB.CallBack call 240 */ 241 static int afs_deliver_cb_callback(struct afs_call *call) 242 { 243 struct afs_callback_break *cb; 244 __be32 *bp; 245 int ret, loop; 246 247 _enter("{%u}", call->unmarshall); 248 249 switch (call->unmarshall) { 250 case 0: 251 afs_extract_to_tmp(call); 252 call->unmarshall++; 253 254 /* extract the FID array and its count in two steps */ 255 fallthrough; 256 case 1: 257 _debug("extract FID count"); 258 ret = afs_extract_data(call, true); 259 if (ret < 0) 260 return ret; 261 262 call->count = ntohl(call->tmp); 263 _debug("FID count: %u", call->count); 264 if (call->count > AFSCBMAX) 265 return afs_protocol_error(call, afs_eproto_cb_fid_count); 266 267 call->buffer = kmalloc(array3_size(call->count, 3, 4), 268 GFP_KERNEL); 269 if (!call->buffer) 270 return -ENOMEM; 271 afs_extract_to_buf(call, call->count * 3 * 4); 272 call->unmarshall++; 273 274 fallthrough; 275 case 2: 276 _debug("extract FID array"); 277 ret = afs_extract_data(call, true); 278 if (ret < 0) 279 return ret; 280 281 _debug("unmarshall FID array"); 282 call->request = kcalloc(call->count, 283 sizeof(struct afs_callback_break), 284 GFP_KERNEL); 285 if (!call->request) 286 return -ENOMEM; 287 288 cb = call->request; 289 bp = call->buffer; 290 for (loop = call->count; loop > 0; loop--, cb++) { 291 cb->fid.vid = ntohl(*bp++); 292 cb->fid.vnode = ntohl(*bp++); 293 cb->fid.unique = ntohl(*bp++); 294 } 295 296 afs_extract_to_tmp(call); 297 call->unmarshall++; 298 299 /* extract the callback array and its count in two steps */ 300 fallthrough; 301 case 3: 302 _debug("extract CB count"); 303 ret = afs_extract_data(call, true); 304 if (ret < 0) 305 return ret; 306 307 call->count2 = ntohl(call->tmp); 308 _debug("CB count: %u", call->count2); 309 if (call->count2 != call->count && call->count2 != 0) 310 return afs_protocol_error(call, afs_eproto_cb_count); 311 call->iter = &call->def_iter; 312 iov_iter_discard(&call->def_iter, READ, call->count2 * 3 * 4); 313 call->unmarshall++; 314 315 fallthrough; 316 case 4: 317 _debug("extract discard %zu/%u", 318 iov_iter_count(call->iter), call->count2 * 3 * 4); 319 320 ret = afs_extract_data(call, false); 321 if (ret < 0) 322 return ret; 323 324 call->unmarshall++; 325 fallthrough; 326 327 case 5: 328 break; 329 } 330 331 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 332 return afs_io_error(call, afs_io_error_cm_reply); 333 334 /* we'll need the file server record as that tells us which set of 335 * vnodes to operate upon */ 336 return afs_find_cm_server_by_peer(call); 337 } 338 339 /* 340 * allow the fileserver to request callback state (re-)initialisation 341 */ 342 static void SRXAFSCB_InitCallBackState(struct work_struct *work) 343 { 344 struct afs_call *call = container_of(work, struct afs_call, work); 345 346 _enter("{%p}", call->server); 347 348 if (call->server) 349 afs_init_callback_state(call->server); 350 afs_send_empty_reply(call); 351 afs_put_call(call); 352 _leave(""); 353 } 354 355 /* 356 * deliver request data to a CB.InitCallBackState call 357 */ 358 static int afs_deliver_cb_init_call_back_state(struct afs_call *call) 359 { 360 int ret; 361 362 _enter(""); 363 364 afs_extract_discard(call, 0); 365 ret = afs_extract_data(call, false); 366 if (ret < 0) 367 return ret; 368 369 /* we'll need the file server record as that tells us which set of 370 * vnodes to operate upon */ 371 return afs_find_cm_server_by_peer(call); 372 } 373 374 /* 375 * deliver request data to a CB.InitCallBackState3 call 376 */ 377 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call) 378 { 379 struct afs_uuid *r; 380 unsigned loop; 381 __be32 *b; 382 int ret; 383 384 _enter(""); 385 386 _enter("{%u}", call->unmarshall); 387 388 switch (call->unmarshall) { 389 case 0: 390 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL); 391 if (!call->buffer) 392 return -ENOMEM; 393 afs_extract_to_buf(call, 11 * sizeof(__be32)); 394 call->unmarshall++; 395 396 fallthrough; 397 case 1: 398 _debug("extract UUID"); 399 ret = afs_extract_data(call, false); 400 switch (ret) { 401 case 0: break; 402 case -EAGAIN: return 0; 403 default: return ret; 404 } 405 406 _debug("unmarshall UUID"); 407 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL); 408 if (!call->request) 409 return -ENOMEM; 410 411 b = call->buffer; 412 r = call->request; 413 r->time_low = b[0]; 414 r->time_mid = htons(ntohl(b[1])); 415 r->time_hi_and_version = htons(ntohl(b[2])); 416 r->clock_seq_hi_and_reserved = ntohl(b[3]); 417 r->clock_seq_low = ntohl(b[4]); 418 419 for (loop = 0; loop < 6; loop++) 420 r->node[loop] = ntohl(b[loop + 5]); 421 422 call->unmarshall++; 423 fallthrough; 424 425 case 2: 426 break; 427 } 428 429 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 430 return afs_io_error(call, afs_io_error_cm_reply); 431 432 /* we'll need the file server record as that tells us which set of 433 * vnodes to operate upon */ 434 return afs_find_cm_server_by_uuid(call, call->request); 435 } 436 437 /* 438 * allow the fileserver to see if the cache manager is still alive 439 */ 440 static void SRXAFSCB_Probe(struct work_struct *work) 441 { 442 struct afs_call *call = container_of(work, struct afs_call, work); 443 444 _enter(""); 445 afs_send_empty_reply(call); 446 afs_put_call(call); 447 _leave(""); 448 } 449 450 /* 451 * deliver request data to a CB.Probe call 452 */ 453 static int afs_deliver_cb_probe(struct afs_call *call) 454 { 455 int ret; 456 457 _enter(""); 458 459 afs_extract_discard(call, 0); 460 ret = afs_extract_data(call, false); 461 if (ret < 0) 462 return ret; 463 464 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 465 return afs_io_error(call, afs_io_error_cm_reply); 466 return afs_find_cm_server_by_peer(call); 467 } 468 469 /* 470 * Allow the fileserver to quickly find out if the cache manager has been 471 * rebooted. 472 */ 473 static void SRXAFSCB_ProbeUuid(struct work_struct *work) 474 { 475 struct afs_call *call = container_of(work, struct afs_call, work); 476 struct afs_uuid *r = call->request; 477 478 _enter(""); 479 480 if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0) 481 afs_send_empty_reply(call); 482 else 483 afs_abort_service_call(call, 1, 1, "K-1"); 484 485 afs_put_call(call); 486 _leave(""); 487 } 488 489 /* 490 * deliver request data to a CB.ProbeUuid call 491 */ 492 static int afs_deliver_cb_probe_uuid(struct afs_call *call) 493 { 494 struct afs_uuid *r; 495 unsigned loop; 496 __be32 *b; 497 int ret; 498 499 _enter("{%u}", call->unmarshall); 500 501 switch (call->unmarshall) { 502 case 0: 503 call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL); 504 if (!call->buffer) 505 return -ENOMEM; 506 afs_extract_to_buf(call, 11 * sizeof(__be32)); 507 call->unmarshall++; 508 509 fallthrough; 510 case 1: 511 _debug("extract UUID"); 512 ret = afs_extract_data(call, false); 513 switch (ret) { 514 case 0: break; 515 case -EAGAIN: return 0; 516 default: return ret; 517 } 518 519 _debug("unmarshall UUID"); 520 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL); 521 if (!call->request) 522 return -ENOMEM; 523 524 b = call->buffer; 525 r = call->request; 526 r->time_low = b[0]; 527 r->time_mid = htons(ntohl(b[1])); 528 r->time_hi_and_version = htons(ntohl(b[2])); 529 r->clock_seq_hi_and_reserved = ntohl(b[3]); 530 r->clock_seq_low = ntohl(b[4]); 531 532 for (loop = 0; loop < 6; loop++) 533 r->node[loop] = ntohl(b[loop + 5]); 534 535 call->unmarshall++; 536 fallthrough; 537 538 case 2: 539 break; 540 } 541 542 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 543 return afs_io_error(call, afs_io_error_cm_reply); 544 return afs_find_cm_server_by_peer(call); 545 } 546 547 /* 548 * allow the fileserver to ask about the cache manager's capabilities 549 */ 550 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work) 551 { 552 struct afs_call *call = container_of(work, struct afs_call, work); 553 int loop; 554 555 struct { 556 struct /* InterfaceAddr */ { 557 __be32 nifs; 558 __be32 uuid[11]; 559 __be32 ifaddr[32]; 560 __be32 netmask[32]; 561 __be32 mtu[32]; 562 } ia; 563 struct /* Capabilities */ { 564 __be32 capcount; 565 __be32 caps[1]; 566 } cap; 567 } reply; 568 569 _enter(""); 570 571 memset(&reply, 0, sizeof(reply)); 572 573 reply.ia.uuid[0] = call->net->uuid.time_low; 574 reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid)); 575 reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version)); 576 reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved); 577 reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low); 578 for (loop = 0; loop < 6; loop++) 579 reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]); 580 581 reply.cap.capcount = htonl(1); 582 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION); 583 afs_send_simple_reply(call, &reply, sizeof(reply)); 584 afs_put_call(call); 585 _leave(""); 586 } 587 588 /* 589 * deliver request data to a CB.TellMeAboutYourself call 590 */ 591 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call) 592 { 593 int ret; 594 595 _enter(""); 596 597 afs_extract_discard(call, 0); 598 ret = afs_extract_data(call, false); 599 if (ret < 0) 600 return ret; 601 602 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 603 return afs_io_error(call, afs_io_error_cm_reply); 604 return afs_find_cm_server_by_peer(call); 605 } 606 607 /* 608 * deliver request data to a YFS CB.CallBack call 609 */ 610 static int afs_deliver_yfs_cb_callback(struct afs_call *call) 611 { 612 struct afs_callback_break *cb; 613 struct yfs_xdr_YFSFid *bp; 614 size_t size; 615 int ret, loop; 616 617 _enter("{%u}", call->unmarshall); 618 619 switch (call->unmarshall) { 620 case 0: 621 afs_extract_to_tmp(call); 622 call->unmarshall++; 623 624 /* extract the FID array and its count in two steps */ 625 fallthrough; 626 case 1: 627 _debug("extract FID count"); 628 ret = afs_extract_data(call, true); 629 if (ret < 0) 630 return ret; 631 632 call->count = ntohl(call->tmp); 633 _debug("FID count: %u", call->count); 634 if (call->count > YFSCBMAX) 635 return afs_protocol_error(call, afs_eproto_cb_fid_count); 636 637 size = array_size(call->count, sizeof(struct yfs_xdr_YFSFid)); 638 call->buffer = kmalloc(size, GFP_KERNEL); 639 if (!call->buffer) 640 return -ENOMEM; 641 afs_extract_to_buf(call, size); 642 call->unmarshall++; 643 644 fallthrough; 645 case 2: 646 _debug("extract FID array"); 647 ret = afs_extract_data(call, false); 648 if (ret < 0) 649 return ret; 650 651 _debug("unmarshall FID array"); 652 call->request = kcalloc(call->count, 653 sizeof(struct afs_callback_break), 654 GFP_KERNEL); 655 if (!call->request) 656 return -ENOMEM; 657 658 cb = call->request; 659 bp = call->buffer; 660 for (loop = call->count; loop > 0; loop--, cb++) { 661 cb->fid.vid = xdr_to_u64(bp->volume); 662 cb->fid.vnode = xdr_to_u64(bp->vnode.lo); 663 cb->fid.vnode_hi = ntohl(bp->vnode.hi); 664 cb->fid.unique = ntohl(bp->vnode.unique); 665 bp++; 666 } 667 668 afs_extract_to_tmp(call); 669 call->unmarshall++; 670 fallthrough; 671 672 case 3: 673 break; 674 } 675 676 if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING)) 677 return afs_io_error(call, afs_io_error_cm_reply); 678 679 /* We'll need the file server record as that tells us which set of 680 * vnodes to operate upon. 681 */ 682 return afs_find_cm_server_by_peer(call); 683 } 684