cmservice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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/sched.h>
  14. #include <linux/ip.h>
  15. #include "internal.h"
  16. #include "afs_cm.h"
  17. #if 0
  18. struct workqueue_struct *afs_cm_workqueue;
  19. #endif /* 0 */
  20. static int afs_deliver_cb_init_call_back_state(struct afs_call *,
  21. struct sk_buff *, bool);
  22. static int afs_deliver_cb_init_call_back_state3(struct afs_call *,
  23. struct sk_buff *, bool);
  24. static int afs_deliver_cb_probe(struct afs_call *, struct sk_buff *, bool);
  25. static int afs_deliver_cb_callback(struct afs_call *, struct sk_buff *, bool);
  26. static int afs_deliver_cb_get_capabilities(struct afs_call *, struct sk_buff *,
  27. bool);
  28. static void afs_cm_destructor(struct afs_call *);
  29. /*
  30. * CB.CallBack operation type
  31. */
  32. static const struct afs_call_type afs_SRXCBCallBack = {
  33. .name = "CB.CallBack",
  34. .deliver = afs_deliver_cb_callback,
  35. .abort_to_error = afs_abort_to_error,
  36. .destructor = afs_cm_destructor,
  37. };
  38. /*
  39. * CB.InitCallBackState operation type
  40. */
  41. static const struct afs_call_type afs_SRXCBInitCallBackState = {
  42. .name = "CB.InitCallBackState",
  43. .deliver = afs_deliver_cb_init_call_back_state,
  44. .abort_to_error = afs_abort_to_error,
  45. .destructor = afs_cm_destructor,
  46. };
  47. /*
  48. * CB.InitCallBackState3 operation type
  49. */
  50. static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
  51. .name = "CB.InitCallBackState3",
  52. .deliver = afs_deliver_cb_init_call_back_state3,
  53. .abort_to_error = afs_abort_to_error,
  54. .destructor = afs_cm_destructor,
  55. };
  56. /*
  57. * CB.Probe operation type
  58. */
  59. static const struct afs_call_type afs_SRXCBProbe = {
  60. .name = "CB.Probe",
  61. .deliver = afs_deliver_cb_probe,
  62. .abort_to_error = afs_abort_to_error,
  63. .destructor = afs_cm_destructor,
  64. };
  65. /*
  66. * CB.GetCapabilities operation type
  67. */
  68. static const struct afs_call_type afs_SRXCBGetCapabilites = {
  69. .name = "CB.GetCapabilities",
  70. .deliver = afs_deliver_cb_get_capabilities,
  71. .abort_to_error = afs_abort_to_error,
  72. .destructor = afs_cm_destructor,
  73. };
  74. /*
  75. * route an incoming cache manager call
  76. * - return T if supported, F if not
  77. */
  78. bool afs_cm_incoming_call(struct afs_call *call)
  79. {
  80. u32 operation_id = ntohl(call->operation_ID);
  81. _enter("{CB.OP %u}", operation_id);
  82. switch (operation_id) {
  83. case CBCallBack:
  84. call->type = &afs_SRXCBCallBack;
  85. return true;
  86. case CBInitCallBackState:
  87. call->type = &afs_SRXCBInitCallBackState;
  88. return true;
  89. case CBInitCallBackState3:
  90. call->type = &afs_SRXCBInitCallBackState3;
  91. return true;
  92. case CBProbe:
  93. call->type = &afs_SRXCBProbe;
  94. return true;
  95. case CBGetCapabilities:
  96. call->type = &afs_SRXCBGetCapabilites;
  97. return true;
  98. default:
  99. return false;
  100. }
  101. }
  102. /*
  103. * clean up a cache manager call
  104. */
  105. static void afs_cm_destructor(struct afs_call *call)
  106. {
  107. _enter("");
  108. afs_put_server(call->server);
  109. call->server = NULL;
  110. kfree(call->buffer);
  111. call->buffer = NULL;
  112. }
  113. /*
  114. * allow the fileserver to see if the cache manager is still alive
  115. */
  116. static void SRXAFSCB_CallBack(struct work_struct *work)
  117. {
  118. struct afs_call *call = container_of(work, struct afs_call, work);
  119. _enter("");
  120. /* be sure to send the reply *before* attempting to spam the AFS server
  121. * with FSFetchStatus requests on the vnodes with broken callbacks lest
  122. * the AFS server get into a vicious cycle of trying to break further
  123. * callbacks because it hadn't received completion of the CBCallBack op
  124. * yet */
  125. afs_send_empty_reply(call);
  126. afs_break_callbacks(call->server, call->count, call->request);
  127. _leave("");
  128. }
  129. /*
  130. * deliver request data to a CB.CallBack call
  131. */
  132. static int afs_deliver_cb_callback(struct afs_call *call, struct sk_buff *skb,
  133. bool last)
  134. {
  135. struct afs_callback *cb;
  136. struct afs_server *server;
  137. struct in_addr addr;
  138. __be32 *bp;
  139. u32 tmp;
  140. int ret, loop;
  141. _enter("{%u},{%u},%d", call->unmarshall, skb->len, last);
  142. switch (call->unmarshall) {
  143. case 0:
  144. call->offset = 0;
  145. call->unmarshall++;
  146. /* extract the FID array and its count in two steps */
  147. case 1:
  148. _debug("extract FID count");
  149. ret = afs_extract_data(call, skb, last, &call->tmp, 4);
  150. switch (ret) {
  151. case 0: break;
  152. case -EAGAIN: return 0;
  153. default: return ret;
  154. }
  155. call->count = ntohl(call->tmp);
  156. _debug("FID count: %u", call->count);
  157. if (call->count > AFSCBMAX)
  158. return -EBADMSG;
  159. call->buffer = kmalloc(call->count * 3 * 4, GFP_KERNEL);
  160. if (!call->buffer)
  161. return -ENOMEM;
  162. call->offset = 0;
  163. call->unmarshall++;
  164. case 2:
  165. _debug("extract FID array");
  166. ret = afs_extract_data(call, skb, last, call->buffer,
  167. call->count * 3 * 4);
  168. switch (ret) {
  169. case 0: break;
  170. case -EAGAIN: return 0;
  171. default: return ret;
  172. }
  173. _debug("unmarshall FID array");
  174. call->request = kcalloc(call->count,
  175. sizeof(struct afs_callback),
  176. GFP_KERNEL);
  177. if (!call->request)
  178. return -ENOMEM;
  179. cb = call->request;
  180. bp = call->buffer;
  181. for (loop = call->count; loop > 0; loop--, cb++) {
  182. cb->fid.vid = ntohl(*bp++);
  183. cb->fid.vnode = ntohl(*bp++);
  184. cb->fid.unique = ntohl(*bp++);
  185. cb->type = AFSCM_CB_UNTYPED;
  186. }
  187. call->offset = 0;
  188. call->unmarshall++;
  189. /* extract the callback array and its count in two steps */
  190. case 3:
  191. _debug("extract CB count");
  192. ret = afs_extract_data(call, skb, last, &call->tmp, 4);
  193. switch (ret) {
  194. case 0: break;
  195. case -EAGAIN: return 0;
  196. default: return ret;
  197. }
  198. tmp = ntohl(call->tmp);
  199. _debug("CB count: %u", tmp);
  200. if (tmp != call->count && tmp != 0)
  201. return -EBADMSG;
  202. call->offset = 0;
  203. call->unmarshall++;
  204. if (tmp == 0)
  205. goto empty_cb_array;
  206. case 4:
  207. _debug("extract CB array");
  208. ret = afs_extract_data(call, skb, last, call->request,
  209. call->count * 3 * 4);
  210. switch (ret) {
  211. case 0: break;
  212. case -EAGAIN: return 0;
  213. default: return ret;
  214. }
  215. _debug("unmarshall CB array");
  216. cb = call->request;
  217. bp = call->buffer;
  218. for (loop = call->count; loop > 0; loop--, cb++) {
  219. cb->version = ntohl(*bp++);
  220. cb->expiry = ntohl(*bp++);
  221. cb->type = ntohl(*bp++);
  222. }
  223. empty_cb_array:
  224. call->offset = 0;
  225. call->unmarshall++;
  226. case 5:
  227. _debug("trailer");
  228. if (skb->len != 0)
  229. return -EBADMSG;
  230. break;
  231. }
  232. if (!last)
  233. return 0;
  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. memcpy(&addr, &ip_hdr(skb)->saddr, 4);
  238. server = afs_find_server(&addr);
  239. if (!server)
  240. return -ENOTCONN;
  241. call->server = server;
  242. INIT_WORK(&call->work, SRXAFSCB_CallBack);
  243. schedule_work(&call->work);
  244. return 0;
  245. }
  246. /*
  247. * allow the fileserver to request callback state (re-)initialisation
  248. */
  249. static void SRXAFSCB_InitCallBackState(struct work_struct *work)
  250. {
  251. struct afs_call *call = container_of(work, struct afs_call, work);
  252. _enter("{%p}", call->server);
  253. afs_init_callback_state(call->server);
  254. afs_send_empty_reply(call);
  255. _leave("");
  256. }
  257. /*
  258. * deliver request data to a CB.InitCallBackState call
  259. */
  260. static int afs_deliver_cb_init_call_back_state(struct afs_call *call,
  261. struct sk_buff *skb,
  262. bool last)
  263. {
  264. struct afs_server *server;
  265. struct in_addr addr;
  266. _enter(",{%u},%d", skb->len, last);
  267. if (skb->len > 0)
  268. return -EBADMSG;
  269. if (!last)
  270. return 0;
  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. memcpy(&addr, &ip_hdr(skb)->saddr, 4);
  276. server = afs_find_server(&addr);
  277. if (!server)
  278. return -ENOTCONN;
  279. call->server = server;
  280. INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
  281. schedule_work(&call->work);
  282. return 0;
  283. }
  284. /*
  285. * deliver request data to a CB.InitCallBackState3 call
  286. */
  287. static int afs_deliver_cb_init_call_back_state3(struct afs_call *call,
  288. struct sk_buff *skb,
  289. bool last)
  290. {
  291. struct afs_server *server;
  292. struct in_addr addr;
  293. _enter(",{%u},%d", skb->len, last);
  294. if (!last)
  295. return 0;
  296. /* no unmarshalling required */
  297. call->state = AFS_CALL_REPLYING;
  298. /* we'll need the file server record as that tells us which set of
  299. * vnodes to operate upon */
  300. memcpy(&addr, &ip_hdr(skb)->saddr, 4);
  301. server = afs_find_server(&addr);
  302. if (!server)
  303. return -ENOTCONN;
  304. call->server = server;
  305. INIT_WORK(&call->work, SRXAFSCB_InitCallBackState);
  306. schedule_work(&call->work);
  307. return 0;
  308. }
  309. /*
  310. * allow the fileserver to see if the cache manager is still alive
  311. */
  312. static void SRXAFSCB_Probe(struct work_struct *work)
  313. {
  314. struct afs_call *call = container_of(work, struct afs_call, work);
  315. _enter("");
  316. afs_send_empty_reply(call);
  317. _leave("");
  318. }
  319. /*
  320. * deliver request data to a CB.Probe call
  321. */
  322. static int afs_deliver_cb_probe(struct afs_call *call, struct sk_buff *skb,
  323. bool last)
  324. {
  325. _enter(",{%u},%d", skb->len, last);
  326. if (skb->len > 0)
  327. return -EBADMSG;
  328. if (!last)
  329. return 0;
  330. /* no unmarshalling required */
  331. call->state = AFS_CALL_REPLYING;
  332. INIT_WORK(&call->work, SRXAFSCB_Probe);
  333. schedule_work(&call->work);
  334. return 0;
  335. }
  336. /*
  337. * allow the fileserver to ask about the cache manager's capabilities
  338. */
  339. static void SRXAFSCB_GetCapabilities(struct work_struct *work)
  340. {
  341. struct afs_interface *ifs;
  342. struct afs_call *call = container_of(work, struct afs_call, work);
  343. int loop, nifs;
  344. struct {
  345. struct /* InterfaceAddr */ {
  346. __be32 nifs;
  347. __be32 uuid[11];
  348. __be32 ifaddr[32];
  349. __be32 netmask[32];
  350. __be32 mtu[32];
  351. } ia;
  352. struct /* Capabilities */ {
  353. __be32 capcount;
  354. __be32 caps[1];
  355. } cap;
  356. } reply;
  357. _enter("");
  358. nifs = 0;
  359. ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
  360. if (ifs) {
  361. nifs = afs_get_ipv4_interfaces(ifs, 32, false);
  362. if (nifs < 0) {
  363. kfree(ifs);
  364. ifs = NULL;
  365. nifs = 0;
  366. }
  367. }
  368. memset(&reply, 0, sizeof(reply));
  369. reply.ia.nifs = htonl(nifs);
  370. reply.ia.uuid[0] = htonl(afs_uuid.time_low);
  371. reply.ia.uuid[1] = htonl(afs_uuid.time_mid);
  372. reply.ia.uuid[2] = htonl(afs_uuid.time_hi_and_version);
  373. reply.ia.uuid[3] = htonl((s8) afs_uuid.clock_seq_hi_and_reserved);
  374. reply.ia.uuid[4] = htonl((s8) afs_uuid.clock_seq_low);
  375. for (loop = 0; loop < 6; loop++)
  376. reply.ia.uuid[loop + 5] = htonl((s8) afs_uuid.node[loop]);
  377. if (ifs) {
  378. for (loop = 0; loop < nifs; loop++) {
  379. reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
  380. reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
  381. reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
  382. }
  383. kfree(ifs);
  384. }
  385. reply.cap.capcount = htonl(1);
  386. reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
  387. afs_send_simple_reply(call, &reply, sizeof(reply));
  388. _leave("");
  389. }
  390. /*
  391. * deliver request data to a CB.GetCapabilities call
  392. */
  393. static int afs_deliver_cb_get_capabilities(struct afs_call *call,
  394. struct sk_buff *skb, bool last)
  395. {
  396. _enter(",{%u},%d", skb->len, last);
  397. if (skb->len > 0)
  398. return -EBADMSG;
  399. if (!last)
  400. return 0;
  401. /* no unmarshalling required */
  402. call->state = AFS_CALL_REPLYING;
  403. INIT_WORK(&call->work, SRXAFSCB_GetCapabilities);
  404. schedule_work(&call->work);
  405. return 0;
  406. }