cmservice.c 13 KB

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