cmservice.c 16 KB

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