cmservice.c 16 KB

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