cmservice.c 11 KB

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