cmservice.c 14 KB

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