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 20 static int afs_deliver_cb_init_call_back_state(struct afs_call *); 21 static int afs_deliver_cb_init_call_back_state3(struct afs_call *); 22 static int afs_deliver_cb_probe(struct afs_call *); 23 static int afs_deliver_cb_callback(struct afs_call *); 24 static int afs_deliver_cb_probe_uuid(struct afs_call *); 25 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *); 26 static void afs_cm_destructor(struct afs_call *); 27 28 #define CM_NAME(name) \ 29 const char afs_SRXCB##name##_name[] __tracepoint_string = \ 30 "CB." #name 31 32 /* 33 * CB.CallBack operation type 34 */ 35 static CM_NAME(CallBack); 36 static const struct afs_call_type afs_SRXCBCallBack = { 37 .name = afs_SRXCBCallBack_name, 38 .deliver = afs_deliver_cb_callback, 39 .abort_to_error = afs_abort_to_error, 40 .destructor = afs_cm_destructor, 41 }; 42 43 /* 44 * CB.InitCallBackState operation type 45 */ 46 static CM_NAME(InitCallBackState); 47 static const struct afs_call_type afs_SRXCBInitCallBackState = { 48 .name = afs_SRXCBInitCallBackState_name, 49 .deliver = afs_deliver_cb_init_call_back_state, 50 .abort_to_error = afs_abort_to_error, 51 .destructor = afs_cm_destructor, 52 }; 53 54 /* 55 * CB.InitCallBackState3 operation type 56 */ 57 static CM_NAME(InitCallBackState3); 58 static const struct afs_call_type afs_SRXCBInitCallBackState3 = { 59 .name = afs_SRXCBInitCallBackState3_name, 60 .deliver = afs_deliver_cb_init_call_back_state3, 61 .abort_to_error = afs_abort_to_error, 62 .destructor = afs_cm_destructor, 63 }; 64 65 /* 66 * CB.Probe operation type 67 */ 68 static CM_NAME(Probe); 69 static const struct afs_call_type afs_SRXCBProbe = { 70 .name = afs_SRXCBProbe_name, 71 .deliver = afs_deliver_cb_probe, 72 .abort_to_error = afs_abort_to_error, 73 .destructor = afs_cm_destructor, 74 }; 75 76 /* 77 * CB.ProbeUuid operation type 78 */ 79 static CM_NAME(ProbeUuid); 80 static const struct afs_call_type afs_SRXCBProbeUuid = { 81 .name = afs_SRXCBProbeUuid_name, 82 .deliver = afs_deliver_cb_probe_uuid, 83 .abort_to_error = afs_abort_to_error, 84 .destructor = afs_cm_destructor, 85 }; 86 87 /* 88 * CB.TellMeAboutYourself operation type 89 */ 90 static CM_NAME(TellMeAboutYourself); 91 static const struct afs_call_type afs_SRXCBTellMeAboutYourself = { 92 .name = afs_SRXCBTellMeAboutYourself_name, 93 .deliver = afs_deliver_cb_tell_me_about_yourself, 94 .abort_to_error = afs_abort_to_error, 95 .destructor = afs_cm_destructor, 96 }; 97 98 /* 99 * route an incoming cache manager call 100 * - return T if supported, F if not 101 */ 102 bool afs_cm_incoming_call(struct afs_call *call) 103 { 104 _enter("{CB.OP %u}", call->operation_ID); 105 106 switch (call->operation_ID) { 107 case CBCallBack: 108 call->type = &afs_SRXCBCallBack; 109 return true; 110 case CBInitCallBackState: 111 call->type = &afs_SRXCBInitCallBackState; 112 return true; 113 case CBInitCallBackState3: 114 call->type = &afs_SRXCBInitCallBackState3; 115 return true; 116 case CBProbe: 117 call->type = &afs_SRXCBProbe; 118 return true; 119 case CBTellMeAboutYourself: 120 call->type = &afs_SRXCBTellMeAboutYourself; 121 return true; 122 default: 123 return false; 124 } 125 } 126 127 /* 128 * clean up a cache manager call 129 */ 130 static void afs_cm_destructor(struct afs_call *call) 131 { 132 _enter(""); 133 134 /* Break the callbacks here so that we do it after the final ACK is 135 * received. The step number here must match the final number in 136 * afs_deliver_cb_callback(). 137 */ 138 if (call->unmarshall == 5) { 139 ASSERT(call->server && call->count && call->request); 140 afs_break_callbacks(call->server, call->count, call->request); 141 } 142 143 afs_put_server(call->server); 144 call->server = NULL; 145 kfree(call->buffer); 146 call->buffer = NULL; 147 } 148 149 /* 150 * allow the fileserver to see if the cache manager is still alive 151 */ 152 static void SRXAFSCB_CallBack(struct work_struct *work) 153 { 154 struct afs_call *call = container_of(work, struct afs_call, work); 155 156 _enter(""); 157 158 /* be sure to send the reply *before* attempting to spam the AFS server 159 * with FSFetchStatus requests on the vnodes with broken callbacks lest 160 * the AFS server get into a vicious cycle of trying to break further 161 * callbacks because it hadn't received completion of the CBCallBack op 162 * yet */ 163 afs_send_empty_reply(call); 164 165 afs_break_callbacks(call->server, call->count, call->request); 166 _leave(""); 167 } 168 169 /* 170 * deliver request data to a CB.CallBack call 171 */ 172 static int afs_deliver_cb_callback(struct afs_call *call) 173 { 174 struct sockaddr_rxrpc srx; 175 struct afs_callback *cb; 176 struct afs_server *server; 177 __be32 *bp; 178 u32 tmp; 179 int ret, loop; 180 181 _enter("{%u}", call->unmarshall); 182 183 switch (call->unmarshall) { 184 case 0: 185 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx); 186 call->offset = 0; 187 call->unmarshall++; 188 189 /* extract the FID array and its count in two steps */ 190 case 1: 191 _debug("extract FID count"); 192 ret = afs_extract_data(call, &call->tmp, 4, true); 193 if (ret < 0) 194 return ret; 195 196 call->count = ntohl(call->tmp); 197 _debug("FID count: %u", call->count); 198 if (call->count > AFSCBMAX) 199 return -EBADMSG; 200 201 call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL); 202 if (!call->buffer) 203 return -ENOMEM; 204 call->offset = 0; 205 call->unmarshall++; 206 207 case 2: 208 _debug("extract FID array"); 209 ret = afs_extract_data(call, call->buffer, 210 call->count * 3 * 4, true); 211 if (ret < 0) 212 return ret; 213 214 _debug("unmarshall FID array"); 215 call->request = kcalloc(call->count, 216 sizeof(struct afs_callback), 217 GFP_KERNEL); 218 if (!call->request) 219 return -ENOMEM; 220 221 cb = call->request; 222 bp = call->buffer; 223 for (loop = call->count; loop > 0; loop--, cb++) { 224 cb->fid.vid = ntohl(*bp++); 225 cb->fid.vnode = ntohl(*bp++); 226 cb->fid.unique = ntohl(*bp++); 227 cb->type = AFSCM_CB_UNTYPED; 228 } 229 230 call->offset = 0; 231 call->unmarshall++; 232 233 /* extract the callback array and its count in two steps */ 234 case 3: 235 _debug("extract CB count"); 236 ret = afs_extract_data(call, &call->tmp, 4, true); 237 if (ret < 0) 238 return ret; 239 240 tmp = ntohl(call->tmp); 241 _debug("CB count: %u", tmp); 242 if (tmp != call->count && tmp != 0) 243 return -EBADMSG; 244 call->offset = 0; 245 call->unmarshall++; 246 247 case 4: 248 _debug("extract CB array"); 249 ret = afs_extract_data(call, call->buffer, 250 call->count * 3 * 4, false); 251 if (ret < 0) 252 return ret; 253 254 _debug("unmarshall CB array"); 255 cb = call->request; 256 bp = call->buffer; 257 for (loop = call->count; loop > 0; loop--, cb++) { 258 cb->version = ntohl(*bp++); 259 cb->expiry = ntohl(*bp++); 260 cb->type = ntohl(*bp++); 261 } 262 263 call->offset = 0; 264 call->unmarshall++; 265 266 /* Record that the message was unmarshalled successfully so 267 * that the call destructor can know do the callback breaking 268 * work, even if the final ACK isn't received. 269 * 270 * If the step number changes, then afs_cm_destructor() must be 271 * updated also. 272 */ 273 call->unmarshall++; 274 case 5: 275 break; 276 } 277 278 call->state = AFS_CALL_REPLYING; 279 280 /* we'll need the file server record as that tells us which set of 281 * vnodes to operate upon */ 282 server = afs_find_server(&srx); 283 if (!server) 284 return -ENOTCONN; 285 call->server = server; 286 287 INIT_WORK(&call->work, SRXAFSCB_CallBack); 288 queue_work(afs_wq, &call->work); 289 return 0; 290 } 291 292 /* 293 * allow the fileserver to request callback state (re-)initialisation 294 */ 295 static void SRXAFSCB_InitCallBackState(struct work_struct *work) 296 { 297 struct afs_call *call = container_of(work, struct afs_call, work); 298 299 _enter("{%p}", call->server); 300 301 afs_init_callback_state(call->server); 302 afs_send_empty_reply(call); 303 _leave(""); 304 } 305 306 /* 307 * deliver request data to a CB.InitCallBackState call 308 */ 309 static int afs_deliver_cb_init_call_back_state(struct afs_call *call) 310 { 311 struct sockaddr_rxrpc srx; 312 struct afs_server *server; 313 int ret; 314 315 _enter(""); 316 317 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx); 318 319 ret = afs_extract_data(call, NULL, 0, false); 320 if (ret < 0) 321 return ret; 322 323 /* no unmarshalling required */ 324 call->state = AFS_CALL_REPLYING; 325 326 /* we'll need the file server record as that tells us which set of 327 * vnodes to operate upon */ 328 server = afs_find_server(&srx); 329 if (!server) 330 return -ENOTCONN; 331 call->server = server; 332 333 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState); 334 queue_work(afs_wq, &call->work); 335 return 0; 336 } 337 338 /* 339 * deliver request data to a CB.InitCallBackState3 call 340 */ 341 static int afs_deliver_cb_init_call_back_state3(struct afs_call *call) 342 { 343 struct sockaddr_rxrpc srx; 344 struct afs_server *server; 345 struct afs_uuid *r; 346 unsigned loop; 347 __be32 *b; 348 int ret; 349 350 _enter(""); 351 352 rxrpc_kernel_get_peer(afs_socket, call->rxcall, &srx); 353 354 _enter("{%u}", call->unmarshall); 355 356 switch (call->unmarshall) { 357 case 0: 358 call->offset = 0; 359 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL); 360 if (!call->buffer) 361 return -ENOMEM; 362 call->unmarshall++; 363 364 case 1: 365 _debug("extract UUID"); 366 ret = afs_extract_data(call, call->buffer, 367 11 * sizeof(__be32), false); 368 switch (ret) { 369 case 0: break; 370 case -EAGAIN: return 0; 371 default: return ret; 372 } 373 374 _debug("unmarshall UUID"); 375 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL); 376 if (!call->request) 377 return -ENOMEM; 378 379 b = call->buffer; 380 r = call->request; 381 r->time_low = ntohl(b[0]); 382 r->time_mid = ntohl(b[1]); 383 r->time_hi_and_version = ntohl(b[2]); 384 r->clock_seq_hi_and_reserved = ntohl(b[3]); 385 r->clock_seq_low = ntohl(b[4]); 386 387 for (loop = 0; loop < 6; loop++) 388 r->node[loop] = ntohl(b[loop + 5]); 389 390 call->offset = 0; 391 call->unmarshall++; 392 393 case 2: 394 break; 395 } 396 397 /* no unmarshalling required */ 398 call->state = AFS_CALL_REPLYING; 399 400 /* we'll need the file server record as that tells us which set of 401 * vnodes to operate upon */ 402 server = afs_find_server(&srx); 403 if (!server) 404 return -ENOTCONN; 405 call->server = server; 406 407 INIT_WORK(&call->work, SRXAFSCB_InitCallBackState); 408 queue_work(afs_wq, &call->work); 409 return 0; 410 } 411 412 /* 413 * allow the fileserver to see if the cache manager is still alive 414 */ 415 static void SRXAFSCB_Probe(struct work_struct *work) 416 { 417 struct afs_call *call = container_of(work, struct afs_call, work); 418 419 _enter(""); 420 afs_send_empty_reply(call); 421 _leave(""); 422 } 423 424 /* 425 * deliver request data to a CB.Probe call 426 */ 427 static int afs_deliver_cb_probe(struct afs_call *call) 428 { 429 int ret; 430 431 _enter(""); 432 433 ret = afs_extract_data(call, NULL, 0, false); 434 if (ret < 0) 435 return ret; 436 437 /* no unmarshalling required */ 438 call->state = AFS_CALL_REPLYING; 439 440 INIT_WORK(&call->work, SRXAFSCB_Probe); 441 queue_work(afs_wq, &call->work); 442 return 0; 443 } 444 445 /* 446 * allow the fileserver to quickly find out if the fileserver has been rebooted 447 */ 448 static void SRXAFSCB_ProbeUuid(struct work_struct *work) 449 { 450 struct afs_call *call = container_of(work, struct afs_call, work); 451 struct afs_uuid *r = call->request; 452 453 struct { 454 __be32 match; 455 } reply; 456 457 _enter(""); 458 459 if (memcmp(r, &afs_uuid, sizeof(afs_uuid)) == 0) 460 reply.match = htonl(0); 461 else 462 reply.match = htonl(1); 463 464 afs_send_simple_reply(call, &reply, sizeof(reply)); 465 _leave(""); 466 } 467 468 /* 469 * deliver request data to a CB.ProbeUuid call 470 */ 471 static int afs_deliver_cb_probe_uuid(struct afs_call *call) 472 { 473 struct afs_uuid *r; 474 unsigned loop; 475 __be32 *b; 476 int ret; 477 478 _enter("{%u}", call->unmarshall); 479 480 switch (call->unmarshall) { 481 case 0: 482 call->offset = 0; 483 call->buffer = kmalloc(11 * sizeof(__be32), GFP_KERNEL); 484 if (!call->buffer) 485 return -ENOMEM; 486 call->unmarshall++; 487 488 case 1: 489 _debug("extract UUID"); 490 ret = afs_extract_data(call, call->buffer, 491 11 * sizeof(__be32), false); 492 switch (ret) { 493 case 0: break; 494 case -EAGAIN: return 0; 495 default: return ret; 496 } 497 498 _debug("unmarshall UUID"); 499 call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL); 500 if (!call->request) 501 return -ENOMEM; 502 503 b = call->buffer; 504 r = call->request; 505 r->time_low = ntohl(b[0]); 506 r->time_mid = ntohl(b[1]); 507 r->time_hi_and_version = ntohl(b[2]); 508 r->clock_seq_hi_and_reserved = ntohl(b[3]); 509 r->clock_seq_low = ntohl(b[4]); 510 511 for (loop = 0; loop < 6; loop++) 512 r->node[loop] = ntohl(b[loop + 5]); 513 514 call->offset = 0; 515 call->unmarshall++; 516 517 case 2: 518 break; 519 } 520 521 call->state = AFS_CALL_REPLYING; 522 523 INIT_WORK(&call->work, SRXAFSCB_ProbeUuid); 524 queue_work(afs_wq, &call->work); 525 return 0; 526 } 527 528 /* 529 * allow the fileserver to ask about the cache manager's capabilities 530 */ 531 static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work) 532 { 533 struct afs_interface *ifs; 534 struct afs_call *call = container_of(work, struct afs_call, work); 535 int loop, nifs; 536 537 struct { 538 struct /* InterfaceAddr */ { 539 __be32 nifs; 540 __be32 uuid[11]; 541 __be32 ifaddr[32]; 542 __be32 netmask[32]; 543 __be32 mtu[32]; 544 } ia; 545 struct /* Capabilities */ { 546 __be32 capcount; 547 __be32 caps[1]; 548 } cap; 549 } reply; 550 551 _enter(""); 552 553 nifs = 0; 554 ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL); 555 if (ifs) { 556 nifs = afs_get_ipv4_interfaces(ifs, 32, false); 557 if (nifs < 0) { 558 kfree(ifs); 559 ifs = NULL; 560 nifs = 0; 561 } 562 } 563 564 memset(&reply, 0, sizeof(reply)); 565 reply.ia.nifs = htonl(nifs); 566 567 reply.ia.uuid[0] = htonl(afs_uuid.time_low); 568 reply.ia.uuid[1] = htonl(afs_uuid.time_mid); 569 reply.ia.uuid[2] = htonl(afs_uuid.time_hi_and_version); 570 reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved); 571 reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low); 572 for (loop = 0; loop < 6; loop++) 573 reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]); 574 575 if (ifs) { 576 for (loop = 0; loop < nifs; loop++) { 577 reply.ia.ifaddr[loop] = ifs[loop].address.s_addr; 578 reply.ia.netmask[loop] = ifs[loop].netmask.s_addr; 579 reply.ia.mtu[loop] = htonl(ifs[loop].mtu); 580 } 581 kfree(ifs); 582 } 583 584 reply.cap.capcount = htonl(1); 585 reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION); 586 afs_send_simple_reply(call, &reply, sizeof(reply)); 587 588 _leave(""); 589 } 590 591 /* 592 * deliver request data to a CB.TellMeAboutYourself call 593 */ 594 static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call) 595 { 596 int ret; 597 598 _enter(""); 599 600 ret = afs_extract_data(call, NULL, 0, false); 601 if (ret < 0) 602 return ret; 603 604 /* no unmarshalling required */ 605 call->state = AFS_CALL_REPLYING; 606 607 INIT_WORK(&call->work, SRXAFSCB_TellMeAboutYourself); 608 queue_work(afs_wq, &call->work); 609 return 0; 610 } 611