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