cmservice.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. static int afs_deliver_cb_init_call_back_state(struct afs_call *);
  19. static int afs_deliver_cb_init_call_back_state3(struct afs_call *);
  20. static int afs_deliver_cb_probe(struct afs_call *);
  21. static int afs_deliver_cb_callback(struct afs_call *);
  22. static int afs_deliver_cb_probe_uuid(struct afs_call *);
  23. static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *);
  24. static void afs_cm_destructor(struct afs_call *);
  25. static void SRXAFSCB_CallBack(struct work_struct *);
  26. static void SRXAFSCB_InitCallBackState(struct work_struct *);
  27. static void SRXAFSCB_Probe(struct work_struct *);
  28. static void SRXAFSCB_ProbeUuid(struct work_struct *);
  29. static void SRXAFSCB_TellMeAboutYourself(struct work_struct *);
  30. #define CM_NAME(name) \
  31. const char afs_SRXCB##name##_name[] __tracepoint_string = \
  32. "CB." #name
  33. /*
  34. * CB.CallBack operation type
  35. */
  36. static CM_NAME(CallBack);
  37. static const struct afs_call_type afs_SRXCBCallBack = {
  38. .name = afs_SRXCBCallBack_name,
  39. .deliver = afs_deliver_cb_callback,
  40. .destructor = afs_cm_destructor,
  41. .work = SRXAFSCB_CallBack,
  42. };
  43. /*
  44. * CB.InitCallBackState operation type
  45. */
  46. static CM_NAME(InitCallBackState);
  47. static const struct afs_call_type afs_SRXCBInitCallBackState = {
  48. .name = afs_SRXCBInitCallBackState_name,
  49. .deliver = afs_deliver_cb_init_call_back_state,
  50. .destructor = afs_cm_destructor,
  51. .work = SRXAFSCB_InitCallBackState,
  52. };
  53. /*
  54. * CB.InitCallBackState3 operation type
  55. */
  56. static CM_NAME(InitCallBackState3);
  57. static const struct afs_call_type afs_SRXCBInitCallBackState3 = {
  58. .name = afs_SRXCBInitCallBackState3_name,
  59. .deliver = afs_deliver_cb_init_call_back_state3,
  60. .destructor = afs_cm_destructor,
  61. .work = SRXAFSCB_InitCallBackState,
  62. };
  63. /*
  64. * CB.Probe operation type
  65. */
  66. static CM_NAME(Probe);
  67. static const struct afs_call_type afs_SRXCBProbe = {
  68. .name = afs_SRXCBProbe_name,
  69. .deliver = afs_deliver_cb_probe,
  70. .destructor = afs_cm_destructor,
  71. .work = SRXAFSCB_Probe,
  72. };
  73. /*
  74. * CB.ProbeUuid operation type
  75. */
  76. static CM_NAME(ProbeUuid);
  77. static const struct afs_call_type afs_SRXCBProbeUuid = {
  78. .name = afs_SRXCBProbeUuid_name,
  79. .deliver = afs_deliver_cb_probe_uuid,
  80. .destructor = afs_cm_destructor,
  81. .work = SRXAFSCB_ProbeUuid,
  82. };
  83. /*
  84. * CB.TellMeAboutYourself operation type
  85. */
  86. static CM_NAME(TellMeAboutYourself);
  87. static const struct afs_call_type afs_SRXCBTellMeAboutYourself = {
  88. .name = afs_SRXCBTellMeAboutYourself_name,
  89. .deliver = afs_deliver_cb_tell_me_about_yourself,
  90. .destructor = afs_cm_destructor,
  91. .work = SRXAFSCB_TellMeAboutYourself,
  92. };
  93. /*
  94. * route an incoming cache manager call
  95. * - return T if supported, F if not
  96. */
  97. bool afs_cm_incoming_call(struct afs_call *call)
  98. {
  99. _enter("{CB.OP %u}", call->operation_ID);
  100. switch (call->operation_ID) {
  101. case CBCallBack:
  102. call->type = &afs_SRXCBCallBack;
  103. return true;
  104. case CBInitCallBackState:
  105. call->type = &afs_SRXCBInitCallBackState;
  106. return true;
  107. case CBInitCallBackState3:
  108. call->type = &afs_SRXCBInitCallBackState3;
  109. return true;
  110. case CBProbe:
  111. call->type = &afs_SRXCBProbe;
  112. return true;
  113. case CBProbeUuid:
  114. call->type = &afs_SRXCBProbeUuid;
  115. return true;
  116. case CBTellMeAboutYourself:
  117. call->type = &afs_SRXCBTellMeAboutYourself;
  118. return true;
  119. default:
  120. return false;
  121. }
  122. }
  123. /*
  124. * Clean up a cache manager call.
  125. */
  126. static void afs_cm_destructor(struct afs_call *call)
  127. {
  128. kfree(call->buffer);
  129. call->buffer = NULL;
  130. }
  131. /*
  132. * The server supplied a list of callbacks that it wanted to break.
  133. */
  134. static void SRXAFSCB_CallBack(struct work_struct *work)
  135. {
  136. struct afs_call *call = container_of(work, struct afs_call, work);
  137. _enter("");
  138. /* We need to break the callbacks before sending the reply as the
  139. * server holds up change visibility till it receives our reply so as
  140. * to maintain cache coherency.
  141. */
  142. if (call->cm_server)
  143. afs_break_callbacks(call->cm_server, call->count, call->request);
  144. afs_send_empty_reply(call);
  145. afs_put_call(call);
  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)
  152. {
  153. struct afs_callback_break *cb;
  154. struct sockaddr_rxrpc srx;
  155. __be32 *bp;
  156. int ret, loop;
  157. _enter("{%u}", call->unmarshall);
  158. switch (call->unmarshall) {
  159. case 0:
  160. call->offset = 0;
  161. call->unmarshall++;
  162. /* extract the FID array and its count in two steps */
  163. case 1:
  164. _debug("extract FID count");
  165. ret = afs_extract_data(call, &call->tmp, 4, true);
  166. if (ret < 0)
  167. return ret;
  168. call->count = ntohl(call->tmp);
  169. _debug("FID count: %u", call->count);
  170. if (call->count > AFSCBMAX)
  171. return afs_protocol_error(call, -EBADMSG);
  172. call->buffer = kmalloc(array3_size(call->count, 3, 4),
  173. GFP_KERNEL);
  174. if (!call->buffer)
  175. return -ENOMEM;
  176. call->offset = 0;
  177. call->unmarshall++;
  178. case 2:
  179. _debug("extract FID array");
  180. ret = afs_extract_data(call, call->buffer,
  181. call->count * 3 * 4, true);
  182. if (ret < 0)
  183. return ret;
  184. _debug("unmarshall FID array");
  185. call->request = kcalloc(call->count,
  186. sizeof(struct afs_callback_break),
  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->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, &call->tmp, 4, true);
  204. if (ret < 0)
  205. return ret;
  206. call->count2 = ntohl(call->tmp);
  207. _debug("CB count: %u", call->count2);
  208. if (call->count2 != call->count && call->count2 != 0)
  209. return afs_protocol_error(call, -EBADMSG);
  210. call->offset = 0;
  211. call->unmarshall++;
  212. case 4:
  213. _debug("extract CB array");
  214. ret = afs_extract_data(call, call->buffer,
  215. call->count2 * 3 * 4, false);
  216. if (ret < 0)
  217. return ret;
  218. _debug("unmarshall CB array");
  219. cb = call->request;
  220. bp = call->buffer;
  221. for (loop = call->count2; loop > 0; loop--, cb++) {
  222. cb->cb.version = ntohl(*bp++);
  223. cb->cb.expiry = ntohl(*bp++);
  224. cb->cb.type = ntohl(*bp++);
  225. }
  226. call->offset = 0;
  227. call->unmarshall++;
  228. case 5:
  229. break;
  230. }
  231. if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
  232. return -EIO;
  233. /* we'll need the file server record as that tells us which set of
  234. * vnodes to operate upon */
  235. rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
  236. call->cm_server = afs_find_server(call->net, &srx);
  237. if (!call->cm_server)
  238. trace_afs_cm_no_server(call, &srx);
  239. return afs_queue_call_work(call);
  240. }
  241. /*
  242. * allow the fileserver to request callback state (re-)initialisation
  243. */
  244. static void SRXAFSCB_InitCallBackState(struct work_struct *work)
  245. {
  246. struct afs_call *call = container_of(work, struct afs_call, work);
  247. _enter("{%p}", call->cm_server);
  248. if (call->cm_server)
  249. afs_init_callback_state(call->cm_server);
  250. afs_send_empty_reply(call);
  251. afs_put_call(call);
  252. _leave("");
  253. }
  254. /*
  255. * deliver request data to a CB.InitCallBackState call
  256. */
  257. static int afs_deliver_cb_init_call_back_state(struct afs_call *call)
  258. {
  259. struct sockaddr_rxrpc srx;
  260. int ret;
  261. _enter("");
  262. rxrpc_kernel_get_peer(call->net->socket, call->rxcall, &srx);
  263. ret = afs_extract_data(call, NULL, 0, false);
  264. if (ret < 0)
  265. return ret;
  266. /* we'll need the file server record as that tells us which set of
  267. * vnodes to operate upon */
  268. call->cm_server = afs_find_server(call->net, &srx);
  269. if (!call->cm_server)
  270. trace_afs_cm_no_server(call, &srx);
  271. return afs_queue_call_work(call);
  272. }
  273. /*
  274. * deliver request data to a CB.InitCallBackState3 call
  275. */
  276. static int afs_deliver_cb_init_call_back_state3(struct afs_call *call)
  277. {
  278. struct afs_uuid *r;
  279. unsigned loop;
  280. __be32 *b;
  281. int ret;
  282. _enter("");
  283. _enter("{%u}", call->unmarshall);
  284. switch (call->unmarshall) {
  285. case 0:
  286. call->offset = 0;
  287. call->buffer = kmalloc_array(11, sizeof(__be32), GFP_KERNEL);
  288. if (!call->buffer)
  289. return -ENOMEM;
  290. call->unmarshall++;
  291. case 1:
  292. _debug("extract UUID");
  293. ret = afs_extract_data(call, call->buffer,
  294. 11 * sizeof(__be32), false);
  295. switch (ret) {
  296. case 0: break;
  297. case -EAGAIN: return 0;
  298. default: return ret;
  299. }
  300. _debug("unmarshall UUID");
  301. call->request = kmalloc(sizeof(struct afs_uuid), GFP_KERNEL);
  302. if (!call->request)
  303. return -ENOMEM;
  304. b = call->buffer;
  305. r = call->request;
  306. r->time_low = b[0];
  307. r->time_mid = htons(ntohl(b[1]));
  308. r->time_hi_and_version = htons(ntohl(b[2]));
  309. r->clock_seq_hi_and_reserved = ntohl(b[3]);
  310. r->clock_seq_low = ntohl(b[4]);
  311. for (loop = 0; loop < 6; loop++)
  312. r->node[loop] = ntohl(b[loop + 5]);
  313. call->offset = 0;
  314. call->unmarshall++;
  315. case 2:
  316. break;
  317. }
  318. if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
  319. return -EIO;
  320. /* we'll need the file server record as that tells us which set of
  321. * vnodes to operate upon */
  322. rcu_read_lock();
  323. call->cm_server = afs_find_server_by_uuid(call->net, call->request);
  324. rcu_read_unlock();
  325. if (!call->cm_server)
  326. trace_afs_cm_no_server_u(call, call->request);
  327. return afs_queue_call_work(call);
  328. }
  329. /*
  330. * allow the fileserver to see if the cache manager is still alive
  331. */
  332. static void SRXAFSCB_Probe(struct work_struct *work)
  333. {
  334. struct afs_call *call = container_of(work, struct afs_call, work);
  335. _enter("");
  336. afs_send_empty_reply(call);
  337. afs_put_call(call);
  338. _leave("");
  339. }
  340. /*
  341. * deliver request data to a CB.Probe call
  342. */
  343. static int afs_deliver_cb_probe(struct afs_call *call)
  344. {
  345. int ret;
  346. _enter("");
  347. ret = afs_extract_data(call, NULL, 0, false);
  348. if (ret < 0)
  349. return ret;
  350. if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
  351. return -EIO;
  352. return afs_queue_call_work(call);
  353. }
  354. /*
  355. * allow the fileserver to quickly find out if the fileserver has been rebooted
  356. */
  357. static void SRXAFSCB_ProbeUuid(struct work_struct *work)
  358. {
  359. struct afs_call *call = container_of(work, struct afs_call, work);
  360. struct afs_uuid *r = call->request;
  361. _enter("");
  362. if (memcmp(r, &call->net->uuid, sizeof(call->net->uuid)) == 0)
  363. afs_send_empty_reply(call);
  364. else
  365. rxrpc_kernel_abort_call(call->net->socket, call->rxcall,
  366. 1, 1, "K-1");
  367. afs_put_call(call);
  368. _leave("");
  369. }
  370. /*
  371. * deliver request data to a CB.ProbeUuid call
  372. */
  373. static int afs_deliver_cb_probe_uuid(struct afs_call *call)
  374. {
  375. struct afs_uuid *r;
  376. unsigned loop;
  377. __be32 *b;
  378. int ret;
  379. _enter("{%u}", call->unmarshall);
  380. switch (call->unmarshall) {
  381. case 0:
  382. call->offset = 0;
  383. call->buffer = kmalloc_array(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, call->buffer,
  390. 11 * sizeof(__be32), false);
  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 = b[0];
  403. r->time_mid = htons(ntohl(b[1]));
  404. r->time_hi_and_version = htons(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. break;
  413. }
  414. if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
  415. return -EIO;
  416. return afs_queue_call_work(call);
  417. }
  418. /*
  419. * allow the fileserver to ask about the cache manager's capabilities
  420. */
  421. static void SRXAFSCB_TellMeAboutYourself(struct work_struct *work)
  422. {
  423. struct afs_interface *ifs;
  424. struct afs_call *call = container_of(work, struct afs_call, work);
  425. int loop, nifs;
  426. struct {
  427. struct /* InterfaceAddr */ {
  428. __be32 nifs;
  429. __be32 uuid[11];
  430. __be32 ifaddr[32];
  431. __be32 netmask[32];
  432. __be32 mtu[32];
  433. } ia;
  434. struct /* Capabilities */ {
  435. __be32 capcount;
  436. __be32 caps[1];
  437. } cap;
  438. } reply;
  439. _enter("");
  440. nifs = 0;
  441. ifs = kcalloc(32, sizeof(*ifs), GFP_KERNEL);
  442. if (ifs) {
  443. nifs = afs_get_ipv4_interfaces(call->net, ifs, 32, false);
  444. if (nifs < 0) {
  445. kfree(ifs);
  446. ifs = NULL;
  447. nifs = 0;
  448. }
  449. }
  450. memset(&reply, 0, sizeof(reply));
  451. reply.ia.nifs = htonl(nifs);
  452. reply.ia.uuid[0] = call->net->uuid.time_low;
  453. reply.ia.uuid[1] = htonl(ntohs(call->net->uuid.time_mid));
  454. reply.ia.uuid[2] = htonl(ntohs(call->net->uuid.time_hi_and_version));
  455. reply.ia.uuid[3] = htonl((s8) call->net->uuid.clock_seq_hi_and_reserved);
  456. reply.ia.uuid[4] = htonl((s8) call->net->uuid.clock_seq_low);
  457. for (loop = 0; loop < 6; loop++)
  458. reply.ia.uuid[loop + 5] = htonl((s8) call->net->uuid.node[loop]);
  459. if (ifs) {
  460. for (loop = 0; loop < nifs; loop++) {
  461. reply.ia.ifaddr[loop] = ifs[loop].address.s_addr;
  462. reply.ia.netmask[loop] = ifs[loop].netmask.s_addr;
  463. reply.ia.mtu[loop] = htonl(ifs[loop].mtu);
  464. }
  465. kfree(ifs);
  466. }
  467. reply.cap.capcount = htonl(1);
  468. reply.cap.caps[0] = htonl(AFS_CAP_ERROR_TRANSLATION);
  469. afs_send_simple_reply(call, &reply, sizeof(reply));
  470. afs_put_call(call);
  471. _leave("");
  472. }
  473. /*
  474. * deliver request data to a CB.TellMeAboutYourself call
  475. */
  476. static int afs_deliver_cb_tell_me_about_yourself(struct afs_call *call)
  477. {
  478. int ret;
  479. _enter("");
  480. ret = afs_extract_data(call, NULL, 0, false);
  481. if (ret < 0)
  482. return ret;
  483. if (!afs_check_call_state(call, AFS_CALL_SV_REPLYING))
  484. return -EIO;
  485. return afs_queue_call_work(call);
  486. }