cmservice.c 13 KB

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