cmservice.c 14 KB

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