cmservice.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /* cmservice.c: 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/completion.h>
  15. #include "server.h"
  16. #include "cell.h"
  17. #include "transport.h"
  18. #include <rxrpc/rxrpc.h>
  19. #include <rxrpc/transport.h>
  20. #include <rxrpc/connection.h>
  21. #include <rxrpc/call.h>
  22. #include "cmservice.h"
  23. #include "internal.h"
  24. static unsigned afscm_usage; /* AFS cache manager usage count */
  25. static struct rw_semaphore afscm_sem; /* AFS cache manager start/stop semaphore */
  26. static int afscm_new_call(struct rxrpc_call *call);
  27. static void afscm_attention(struct rxrpc_call *call);
  28. static void afscm_error(struct rxrpc_call *call);
  29. static void afscm_aemap(struct rxrpc_call *call);
  30. static void _SRXAFSCM_CallBack(struct rxrpc_call *call);
  31. static void _SRXAFSCM_InitCallBackState(struct rxrpc_call *call);
  32. static void _SRXAFSCM_Probe(struct rxrpc_call *call);
  33. typedef void (*_SRXAFSCM_xxxx_t)(struct rxrpc_call *call);
  34. static const struct rxrpc_operation AFSCM_ops[] = {
  35. {
  36. .id = 204,
  37. .asize = RXRPC_APP_MARK_EOF,
  38. .name = "CallBack",
  39. .user = _SRXAFSCM_CallBack,
  40. },
  41. {
  42. .id = 205,
  43. .asize = RXRPC_APP_MARK_EOF,
  44. .name = "InitCallBackState",
  45. .user = _SRXAFSCM_InitCallBackState,
  46. },
  47. {
  48. .id = 206,
  49. .asize = RXRPC_APP_MARK_EOF,
  50. .name = "Probe",
  51. .user = _SRXAFSCM_Probe,
  52. },
  53. #if 0
  54. {
  55. .id = 207,
  56. .asize = RXRPC_APP_MARK_EOF,
  57. .name = "GetLock",
  58. .user = _SRXAFSCM_GetLock,
  59. },
  60. {
  61. .id = 208,
  62. .asize = RXRPC_APP_MARK_EOF,
  63. .name = "GetCE",
  64. .user = _SRXAFSCM_GetCE,
  65. },
  66. {
  67. .id = 209,
  68. .asize = RXRPC_APP_MARK_EOF,
  69. .name = "GetXStatsVersion",
  70. .user = _SRXAFSCM_GetXStatsVersion,
  71. },
  72. {
  73. .id = 210,
  74. .asize = RXRPC_APP_MARK_EOF,
  75. .name = "GetXStats",
  76. .user = _SRXAFSCM_GetXStats,
  77. }
  78. #endif
  79. };
  80. static struct rxrpc_service AFSCM_service = {
  81. .name = "AFS/CM",
  82. .owner = THIS_MODULE,
  83. .link = LIST_HEAD_INIT(AFSCM_service.link),
  84. .new_call = afscm_new_call,
  85. .service_id = 1,
  86. .attn_func = afscm_attention,
  87. .error_func = afscm_error,
  88. .aemap_func = afscm_aemap,
  89. .ops_begin = &AFSCM_ops[0],
  90. .ops_end = &AFSCM_ops[ARRAY_SIZE(AFSCM_ops)],
  91. };
  92. static DECLARE_COMPLETION(kafscmd_alive);
  93. static DECLARE_COMPLETION(kafscmd_dead);
  94. static DECLARE_WAIT_QUEUE_HEAD(kafscmd_sleepq);
  95. static LIST_HEAD(kafscmd_attention_list);
  96. static LIST_HEAD(afscm_calls);
  97. static DEFINE_SPINLOCK(afscm_calls_lock);
  98. static DEFINE_SPINLOCK(kafscmd_attention_lock);
  99. static int kafscmd_die;
  100. /*****************************************************************************/
  101. /*
  102. * AFS Cache Manager kernel thread
  103. */
  104. static int kafscmd(void *arg)
  105. {
  106. DECLARE_WAITQUEUE(myself, current);
  107. struct rxrpc_call *call;
  108. _SRXAFSCM_xxxx_t func;
  109. int die;
  110. printk(KERN_INFO "kAFS: Started kafscmd %d\n", current->pid);
  111. daemonize("kafscmd");
  112. complete(&kafscmd_alive);
  113. /* loop around looking for things to attend to */
  114. do {
  115. if (list_empty(&kafscmd_attention_list)) {
  116. set_current_state(TASK_INTERRUPTIBLE);
  117. add_wait_queue(&kafscmd_sleepq, &myself);
  118. for (;;) {
  119. set_current_state(TASK_INTERRUPTIBLE);
  120. if (!list_empty(&kafscmd_attention_list) ||
  121. signal_pending(current) ||
  122. kafscmd_die)
  123. break;
  124. schedule();
  125. }
  126. remove_wait_queue(&kafscmd_sleepq, &myself);
  127. set_current_state(TASK_RUNNING);
  128. }
  129. die = kafscmd_die;
  130. /* dequeue the next call requiring attention */
  131. call = NULL;
  132. spin_lock(&kafscmd_attention_lock);
  133. if (!list_empty(&kafscmd_attention_list)) {
  134. call = list_entry(kafscmd_attention_list.next,
  135. struct rxrpc_call,
  136. app_attn_link);
  137. list_del_init(&call->app_attn_link);
  138. die = 0;
  139. }
  140. spin_unlock(&kafscmd_attention_lock);
  141. if (call) {
  142. /* act upon it */
  143. _debug("@@@ Begin Attend Call %p", call);
  144. func = call->app_user;
  145. if (func)
  146. func(call);
  147. rxrpc_put_call(call);
  148. _debug("@@@ End Attend Call %p", call);
  149. }
  150. } while(!die);
  151. /* and that's all */
  152. complete_and_exit(&kafscmd_dead, 0);
  153. } /* end kafscmd() */
  154. /*****************************************************************************/
  155. /*
  156. * handle a call coming in to the cache manager
  157. * - if I want to keep the call, I must increment its usage count
  158. * - the return value will be negated and passed back in an abort packet if
  159. * non-zero
  160. * - serialised by virtue of there only being one krxiod
  161. */
  162. static int afscm_new_call(struct rxrpc_call *call)
  163. {
  164. _enter("%p{cid=%u u=%d}",
  165. call, ntohl(call->call_id), atomic_read(&call->usage));
  166. rxrpc_get_call(call);
  167. /* add to my current call list */
  168. spin_lock(&afscm_calls_lock);
  169. list_add(&call->app_link,&afscm_calls);
  170. spin_unlock(&afscm_calls_lock);
  171. _leave(" = 0");
  172. return 0;
  173. } /* end afscm_new_call() */
  174. /*****************************************************************************/
  175. /*
  176. * queue on the kafscmd queue for attention
  177. */
  178. static void afscm_attention(struct rxrpc_call *call)
  179. {
  180. _enter("%p{cid=%u u=%d}",
  181. call, ntohl(call->call_id), atomic_read(&call->usage));
  182. spin_lock(&kafscmd_attention_lock);
  183. if (list_empty(&call->app_attn_link)) {
  184. list_add_tail(&call->app_attn_link, &kafscmd_attention_list);
  185. rxrpc_get_call(call);
  186. }
  187. spin_unlock(&kafscmd_attention_lock);
  188. wake_up(&kafscmd_sleepq);
  189. _leave(" {u=%d}", atomic_read(&call->usage));
  190. } /* end afscm_attention() */
  191. /*****************************************************************************/
  192. /*
  193. * handle my call being aborted
  194. * - clean up, dequeue and put my ref to the call
  195. */
  196. static void afscm_error(struct rxrpc_call *call)
  197. {
  198. int removed;
  199. _enter("%p{est=%s ac=%u er=%d}",
  200. call,
  201. rxrpc_call_error_states[call->app_err_state],
  202. call->app_abort_code,
  203. call->app_errno);
  204. spin_lock(&kafscmd_attention_lock);
  205. if (list_empty(&call->app_attn_link)) {
  206. list_add_tail(&call->app_attn_link, &kafscmd_attention_list);
  207. rxrpc_get_call(call);
  208. }
  209. spin_unlock(&kafscmd_attention_lock);
  210. removed = 0;
  211. spin_lock(&afscm_calls_lock);
  212. if (!list_empty(&call->app_link)) {
  213. list_del_init(&call->app_link);
  214. removed = 1;
  215. }
  216. spin_unlock(&afscm_calls_lock);
  217. if (removed)
  218. rxrpc_put_call(call);
  219. wake_up(&kafscmd_sleepq);
  220. _leave("");
  221. } /* end afscm_error() */
  222. /*****************************************************************************/
  223. /*
  224. * map afs abort codes to/from Linux error codes
  225. * - called with call->lock held
  226. */
  227. static void afscm_aemap(struct rxrpc_call *call)
  228. {
  229. switch (call->app_err_state) {
  230. case RXRPC_ESTATE_LOCAL_ABORT:
  231. call->app_abort_code = -call->app_errno;
  232. break;
  233. case RXRPC_ESTATE_PEER_ABORT:
  234. call->app_errno = -ECONNABORTED;
  235. break;
  236. default:
  237. break;
  238. }
  239. } /* end afscm_aemap() */
  240. /*****************************************************************************/
  241. /*
  242. * start the cache manager service if not already started
  243. */
  244. int afscm_start(void)
  245. {
  246. int ret;
  247. down_write(&afscm_sem);
  248. if (!afscm_usage) {
  249. ret = kernel_thread(kafscmd, NULL, 0);
  250. if (ret < 0)
  251. goto out;
  252. wait_for_completion(&kafscmd_alive);
  253. ret = rxrpc_add_service(afs_transport, &AFSCM_service);
  254. if (ret < 0)
  255. goto kill;
  256. afs_kafstimod_add_timer(&afs_mntpt_expiry_timer,
  257. afs_mntpt_expiry_timeout * HZ);
  258. }
  259. afscm_usage++;
  260. up_write(&afscm_sem);
  261. return 0;
  262. kill:
  263. kafscmd_die = 1;
  264. wake_up(&kafscmd_sleepq);
  265. wait_for_completion(&kafscmd_dead);
  266. out:
  267. up_write(&afscm_sem);
  268. return ret;
  269. } /* end afscm_start() */
  270. /*****************************************************************************/
  271. /*
  272. * stop the cache manager service
  273. */
  274. void afscm_stop(void)
  275. {
  276. struct rxrpc_call *call;
  277. down_write(&afscm_sem);
  278. BUG_ON(afscm_usage == 0);
  279. afscm_usage--;
  280. if (afscm_usage == 0) {
  281. /* don't want more incoming calls */
  282. rxrpc_del_service(afs_transport, &AFSCM_service);
  283. /* abort any calls I've still got open (the afscm_error() will
  284. * dequeue them) */
  285. spin_lock(&afscm_calls_lock);
  286. while (!list_empty(&afscm_calls)) {
  287. call = list_entry(afscm_calls.next,
  288. struct rxrpc_call,
  289. app_link);
  290. list_del_init(&call->app_link);
  291. rxrpc_get_call(call);
  292. spin_unlock(&afscm_calls_lock);
  293. rxrpc_call_abort(call, -ESRCH); /* abort, dequeue and
  294. * put */
  295. _debug("nuking active call %08x.%d",
  296. ntohl(call->conn->conn_id),
  297. ntohl(call->call_id));
  298. rxrpc_put_call(call);
  299. rxrpc_put_call(call);
  300. spin_lock(&afscm_calls_lock);
  301. }
  302. spin_unlock(&afscm_calls_lock);
  303. /* get rid of my daemon */
  304. kafscmd_die = 1;
  305. wake_up(&kafscmd_sleepq);
  306. wait_for_completion(&kafscmd_dead);
  307. /* dispose of any calls waiting for attention */
  308. spin_lock(&kafscmd_attention_lock);
  309. while (!list_empty(&kafscmd_attention_list)) {
  310. call = list_entry(kafscmd_attention_list.next,
  311. struct rxrpc_call,
  312. app_attn_link);
  313. list_del_init(&call->app_attn_link);
  314. spin_unlock(&kafscmd_attention_lock);
  315. rxrpc_put_call(call);
  316. spin_lock(&kafscmd_attention_lock);
  317. }
  318. spin_unlock(&kafscmd_attention_lock);
  319. afs_kafstimod_del_timer(&afs_mntpt_expiry_timer);
  320. }
  321. up_write(&afscm_sem);
  322. } /* end afscm_stop() */
  323. /*****************************************************************************/
  324. /*
  325. * handle the fileserver breaking a set of callbacks
  326. */
  327. static void _SRXAFSCM_CallBack(struct rxrpc_call *call)
  328. {
  329. struct afs_server *server;
  330. size_t count, qty, tmp;
  331. int ret = 0, removed;
  332. _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
  333. server = afs_server_get_from_peer(call->conn->peer);
  334. switch (call->app_call_state) {
  335. /* we've received the last packet
  336. * - drain all the data from the call and send the reply
  337. */
  338. case RXRPC_CSTATE_SRVR_GOT_ARGS:
  339. ret = -EBADMSG;
  340. qty = call->app_ready_qty;
  341. if (qty < 8 || qty > 50 * (6 * 4) + 8)
  342. break;
  343. {
  344. struct afs_callback *cb, *pcb;
  345. int loop;
  346. __be32 *fp, *bp;
  347. fp = rxrpc_call_alloc_scratch(call, qty);
  348. /* drag the entire argument block out to the scratch
  349. * space */
  350. ret = rxrpc_call_read_data(call, fp, qty, 0);
  351. if (ret < 0)
  352. break;
  353. /* and unmarshall the parameter block */
  354. ret = -EBADMSG;
  355. count = ntohl(*fp++);
  356. if (count>AFSCBMAX ||
  357. (count * (3 * 4) + 8 != qty &&
  358. count * (6 * 4) + 8 != qty))
  359. break;
  360. bp = fp + count*3;
  361. tmp = ntohl(*bp++);
  362. if (tmp > 0 && tmp != count)
  363. break;
  364. if (tmp == 0)
  365. bp = NULL;
  366. pcb = cb = rxrpc_call_alloc_scratch_s(
  367. call, struct afs_callback);
  368. for (loop = count - 1; loop >= 0; loop--) {
  369. pcb->fid.vid = ntohl(*fp++);
  370. pcb->fid.vnode = ntohl(*fp++);
  371. pcb->fid.unique = ntohl(*fp++);
  372. if (bp) {
  373. pcb->version = ntohl(*bp++);
  374. pcb->expiry = ntohl(*bp++);
  375. pcb->type = ntohl(*bp++);
  376. }
  377. else {
  378. pcb->version = 0;
  379. pcb->expiry = 0;
  380. pcb->type = AFSCM_CB_UNTYPED;
  381. }
  382. pcb++;
  383. }
  384. /* invoke the actual service routine */
  385. ret = SRXAFSCM_CallBack(server, count, cb);
  386. if (ret < 0)
  387. break;
  388. }
  389. /* send the reply */
  390. ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
  391. GFP_KERNEL, 0, &count);
  392. if (ret < 0)
  393. break;
  394. break;
  395. /* operation complete */
  396. case RXRPC_CSTATE_COMPLETE:
  397. call->app_user = NULL;
  398. removed = 0;
  399. spin_lock(&afscm_calls_lock);
  400. if (!list_empty(&call->app_link)) {
  401. list_del_init(&call->app_link);
  402. removed = 1;
  403. }
  404. spin_unlock(&afscm_calls_lock);
  405. if (removed)
  406. rxrpc_put_call(call);
  407. break;
  408. /* operation terminated on error */
  409. case RXRPC_CSTATE_ERROR:
  410. call->app_user = NULL;
  411. break;
  412. default:
  413. break;
  414. }
  415. if (ret < 0)
  416. rxrpc_call_abort(call, ret);
  417. afs_put_server(server);
  418. _leave(" = %d", ret);
  419. } /* end _SRXAFSCM_CallBack() */
  420. /*****************************************************************************/
  421. /*
  422. * handle the fileserver asking us to initialise our callback state
  423. */
  424. static void _SRXAFSCM_InitCallBackState(struct rxrpc_call *call)
  425. {
  426. struct afs_server *server;
  427. size_t count;
  428. int ret = 0, removed;
  429. _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
  430. server = afs_server_get_from_peer(call->conn->peer);
  431. switch (call->app_call_state) {
  432. /* we've received the last packet - drain all the data from the
  433. * call */
  434. case RXRPC_CSTATE_SRVR_GOT_ARGS:
  435. /* shouldn't be any args */
  436. ret = -EBADMSG;
  437. break;
  438. /* send the reply when asked for it */
  439. case RXRPC_CSTATE_SRVR_SND_REPLY:
  440. /* invoke the actual service routine */
  441. ret = SRXAFSCM_InitCallBackState(server);
  442. if (ret < 0)
  443. break;
  444. ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
  445. GFP_KERNEL, 0, &count);
  446. if (ret < 0)
  447. break;
  448. break;
  449. /* operation complete */
  450. case RXRPC_CSTATE_COMPLETE:
  451. call->app_user = NULL;
  452. removed = 0;
  453. spin_lock(&afscm_calls_lock);
  454. if (!list_empty(&call->app_link)) {
  455. list_del_init(&call->app_link);
  456. removed = 1;
  457. }
  458. spin_unlock(&afscm_calls_lock);
  459. if (removed)
  460. rxrpc_put_call(call);
  461. break;
  462. /* operation terminated on error */
  463. case RXRPC_CSTATE_ERROR:
  464. call->app_user = NULL;
  465. break;
  466. default:
  467. break;
  468. }
  469. if (ret < 0)
  470. rxrpc_call_abort(call, ret);
  471. afs_put_server(server);
  472. _leave(" = %d", ret);
  473. } /* end _SRXAFSCM_InitCallBackState() */
  474. /*****************************************************************************/
  475. /*
  476. * handle a probe from a fileserver
  477. */
  478. static void _SRXAFSCM_Probe(struct rxrpc_call *call)
  479. {
  480. struct afs_server *server;
  481. size_t count;
  482. int ret = 0, removed;
  483. _enter("%p{acs=%s}", call, rxrpc_call_states[call->app_call_state]);
  484. server = afs_server_get_from_peer(call->conn->peer);
  485. switch (call->app_call_state) {
  486. /* we've received the last packet - drain all the data from the
  487. * call */
  488. case RXRPC_CSTATE_SRVR_GOT_ARGS:
  489. /* shouldn't be any args */
  490. ret = -EBADMSG;
  491. break;
  492. /* send the reply when asked for it */
  493. case RXRPC_CSTATE_SRVR_SND_REPLY:
  494. /* invoke the actual service routine */
  495. ret = SRXAFSCM_Probe(server);
  496. if (ret < 0)
  497. break;
  498. ret = rxrpc_call_write_data(call, 0, NULL, RXRPC_LAST_PACKET,
  499. GFP_KERNEL, 0, &count);
  500. if (ret < 0)
  501. break;
  502. break;
  503. /* operation complete */
  504. case RXRPC_CSTATE_COMPLETE:
  505. call->app_user = NULL;
  506. removed = 0;
  507. spin_lock(&afscm_calls_lock);
  508. if (!list_empty(&call->app_link)) {
  509. list_del_init(&call->app_link);
  510. removed = 1;
  511. }
  512. spin_unlock(&afscm_calls_lock);
  513. if (removed)
  514. rxrpc_put_call(call);
  515. break;
  516. /* operation terminated on error */
  517. case RXRPC_CSTATE_ERROR:
  518. call->app_user = NULL;
  519. break;
  520. default:
  521. break;
  522. }
  523. if (ret < 0)
  524. rxrpc_call_abort(call, ret);
  525. afs_put_server(server);
  526. _leave(" = %d", ret);
  527. } /* end _SRXAFSCM_Probe() */