cmservice.c 14 KB

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