cmservice.c 14 KB

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