vlocation.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /* vlocation.c: volume location management
  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/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/slab.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include "volume.h"
  18. #include "cell.h"
  19. #include "cmservice.h"
  20. #include "fsclient.h"
  21. #include "vlclient.h"
  22. #include "kafstimod.h"
  23. #include <rxrpc/connection.h>
  24. #include "internal.h"
  25. #define AFS_VLDB_TIMEOUT HZ*1000
  26. static void afs_vlocation_update_timer(struct afs_timer *timer);
  27. static void afs_vlocation_update_attend(struct afs_async_op *op);
  28. static void afs_vlocation_update_discard(struct afs_async_op *op);
  29. static void __afs_put_vlocation(struct afs_vlocation *vlocation);
  30. static void __afs_vlocation_timeout(struct afs_timer *timer)
  31. {
  32. struct afs_vlocation *vlocation =
  33. list_entry(timer, struct afs_vlocation, timeout);
  34. _debug("VL TIMEOUT [%s{u=%d}]",
  35. vlocation->vldb.name, atomic_read(&vlocation->usage));
  36. afs_vlocation_do_timeout(vlocation);
  37. }
  38. static const struct afs_timer_ops afs_vlocation_timer_ops = {
  39. .timed_out = __afs_vlocation_timeout,
  40. };
  41. static const struct afs_timer_ops afs_vlocation_update_timer_ops = {
  42. .timed_out = afs_vlocation_update_timer,
  43. };
  44. static const struct afs_async_op_ops afs_vlocation_update_op_ops = {
  45. .attend = afs_vlocation_update_attend,
  46. .discard = afs_vlocation_update_discard,
  47. };
  48. static LIST_HEAD(afs_vlocation_update_pendq); /* queue of VLs awaiting update */
  49. static struct afs_vlocation *afs_vlocation_update; /* VL currently being updated */
  50. static DEFINE_SPINLOCK(afs_vlocation_update_lock); /* lock guarding update queue */
  51. #ifdef AFS_CACHING_SUPPORT
  52. static cachefs_match_val_t afs_vlocation_cache_match(void *target,
  53. const void *entry);
  54. static void afs_vlocation_cache_update(void *source, void *entry);
  55. struct cachefs_index_def afs_vlocation_cache_index_def = {
  56. .name = "vldb",
  57. .data_size = sizeof(struct afs_cache_vlocation),
  58. .keys[0] = { CACHEFS_INDEX_KEYS_ASCIIZ, 64 },
  59. .match = afs_vlocation_cache_match,
  60. .update = afs_vlocation_cache_update,
  61. };
  62. #endif
  63. /*****************************************************************************/
  64. /*
  65. * iterate through the VL servers in a cell until one of them admits knowing
  66. * about the volume in question
  67. * - caller must have cell->vl_sem write-locked
  68. */
  69. static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vlocation,
  70. const char *name,
  71. unsigned namesz,
  72. struct afs_cache_vlocation *vldb)
  73. {
  74. struct afs_server *server = NULL;
  75. struct afs_cell *cell = vlocation->cell;
  76. int count, ret;
  77. _enter("%s,%*.*s,%u", cell->name, namesz, namesz, name, namesz);
  78. ret = -ENOMEDIUM;
  79. for (count = cell->vl_naddrs; count > 0; count--) {
  80. _debug("CellServ[%hu]: %08x",
  81. cell->vl_curr_svix,
  82. cell->vl_addrs[cell->vl_curr_svix].s_addr);
  83. /* try and create a server */
  84. ret = afs_server_lookup(cell,
  85. &cell->vl_addrs[cell->vl_curr_svix],
  86. &server);
  87. switch (ret) {
  88. case 0:
  89. break;
  90. case -ENOMEM:
  91. case -ENONET:
  92. goto out;
  93. default:
  94. goto rotate;
  95. }
  96. /* attempt to access the VL server */
  97. ret = afs_rxvl_get_entry_by_name(server, name, namesz, vldb);
  98. switch (ret) {
  99. case 0:
  100. afs_put_server(server);
  101. goto out;
  102. case -ENOMEM:
  103. case -ENONET:
  104. case -ENETUNREACH:
  105. case -EHOSTUNREACH:
  106. case -ECONNREFUSED:
  107. down_write(&server->sem);
  108. if (server->vlserver) {
  109. rxrpc_put_connection(server->vlserver);
  110. server->vlserver = NULL;
  111. }
  112. up_write(&server->sem);
  113. afs_put_server(server);
  114. if (ret == -ENOMEM || ret == -ENONET)
  115. goto out;
  116. goto rotate;
  117. case -ENOMEDIUM:
  118. afs_put_server(server);
  119. goto out;
  120. default:
  121. afs_put_server(server);
  122. ret = -ENOMEDIUM;
  123. goto rotate;
  124. }
  125. /* rotate the server records upon lookup failure */
  126. rotate:
  127. cell->vl_curr_svix++;
  128. cell->vl_curr_svix %= cell->vl_naddrs;
  129. }
  130. out:
  131. _leave(" = %d", ret);
  132. return ret;
  133. } /* end afs_vlocation_access_vl_by_name() */
  134. /*****************************************************************************/
  135. /*
  136. * iterate through the VL servers in a cell until one of them admits knowing
  137. * about the volume in question
  138. * - caller must have cell->vl_sem write-locked
  139. */
  140. static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vlocation,
  141. afs_volid_t volid,
  142. afs_voltype_t voltype,
  143. struct afs_cache_vlocation *vldb)
  144. {
  145. struct afs_server *server = NULL;
  146. struct afs_cell *cell = vlocation->cell;
  147. int count, ret;
  148. _enter("%s,%x,%d,", cell->name, volid, voltype);
  149. ret = -ENOMEDIUM;
  150. for (count = cell->vl_naddrs; count > 0; count--) {
  151. _debug("CellServ[%hu]: %08x",
  152. cell->vl_curr_svix,
  153. cell->vl_addrs[cell->vl_curr_svix].s_addr);
  154. /* try and create a server */
  155. ret = afs_server_lookup(cell,
  156. &cell->vl_addrs[cell->vl_curr_svix],
  157. &server);
  158. switch (ret) {
  159. case 0:
  160. break;
  161. case -ENOMEM:
  162. case -ENONET:
  163. goto out;
  164. default:
  165. goto rotate;
  166. }
  167. /* attempt to access the VL server */
  168. ret = afs_rxvl_get_entry_by_id(server, volid, voltype, vldb);
  169. switch (ret) {
  170. case 0:
  171. afs_put_server(server);
  172. goto out;
  173. case -ENOMEM:
  174. case -ENONET:
  175. case -ENETUNREACH:
  176. case -EHOSTUNREACH:
  177. case -ECONNREFUSED:
  178. down_write(&server->sem);
  179. if (server->vlserver) {
  180. rxrpc_put_connection(server->vlserver);
  181. server->vlserver = NULL;
  182. }
  183. up_write(&server->sem);
  184. afs_put_server(server);
  185. if (ret == -ENOMEM || ret == -ENONET)
  186. goto out;
  187. goto rotate;
  188. case -ENOMEDIUM:
  189. afs_put_server(server);
  190. goto out;
  191. default:
  192. afs_put_server(server);
  193. ret = -ENOMEDIUM;
  194. goto rotate;
  195. }
  196. /* rotate the server records upon lookup failure */
  197. rotate:
  198. cell->vl_curr_svix++;
  199. cell->vl_curr_svix %= cell->vl_naddrs;
  200. }
  201. out:
  202. _leave(" = %d", ret);
  203. return ret;
  204. } /* end afs_vlocation_access_vl_by_id() */
  205. /*****************************************************************************/
  206. /*
  207. * lookup volume location
  208. * - caller must have cell->vol_sem write-locked
  209. * - iterate through the VL servers in a cell until one of them admits knowing
  210. * about the volume in question
  211. * - lookup in the local cache if not able to find on the VL server
  212. * - insert/update in the local cache if did get a VL response
  213. */
  214. int afs_vlocation_lookup(struct afs_cell *cell,
  215. const char *name,
  216. unsigned namesz,
  217. struct afs_vlocation **_vlocation)
  218. {
  219. struct afs_cache_vlocation vldb;
  220. struct afs_vlocation *vlocation;
  221. afs_voltype_t voltype;
  222. afs_volid_t vid;
  223. int active = 0, ret;
  224. _enter("{%s},%*.*s,%u,", cell->name, namesz, namesz, name, namesz);
  225. if (namesz > sizeof(vlocation->vldb.name)) {
  226. _leave(" = -ENAMETOOLONG");
  227. return -ENAMETOOLONG;
  228. }
  229. /* search the cell's active list first */
  230. list_for_each_entry(vlocation, &cell->vl_list, link) {
  231. if (namesz < sizeof(vlocation->vldb.name) &&
  232. vlocation->vldb.name[namesz] != '\0')
  233. continue;
  234. if (memcmp(vlocation->vldb.name, name, namesz) == 0)
  235. goto found_in_memory;
  236. }
  237. /* search the cell's graveyard list second */
  238. spin_lock(&cell->vl_gylock);
  239. list_for_each_entry(vlocation, &cell->vl_graveyard, link) {
  240. if (namesz < sizeof(vlocation->vldb.name) &&
  241. vlocation->vldb.name[namesz] != '\0')
  242. continue;
  243. if (memcmp(vlocation->vldb.name, name, namesz) == 0)
  244. goto found_in_graveyard;
  245. }
  246. spin_unlock(&cell->vl_gylock);
  247. /* not in the cell's in-memory lists - create a new record */
  248. vlocation = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
  249. if (!vlocation)
  250. return -ENOMEM;
  251. atomic_set(&vlocation->usage, 1);
  252. INIT_LIST_HEAD(&vlocation->link);
  253. rwlock_init(&vlocation->lock);
  254. memcpy(vlocation->vldb.name, name, namesz);
  255. afs_timer_init(&vlocation->timeout, &afs_vlocation_timer_ops);
  256. afs_timer_init(&vlocation->upd_timer, &afs_vlocation_update_timer_ops);
  257. afs_async_op_init(&vlocation->upd_op, &afs_vlocation_update_op_ops);
  258. afs_get_cell(cell);
  259. vlocation->cell = cell;
  260. list_add_tail(&vlocation->link, &cell->vl_list);
  261. #ifdef AFS_CACHING_SUPPORT
  262. /* we want to store it in the cache, plus it might already be
  263. * encached */
  264. cachefs_acquire_cookie(cell->cache,
  265. &afs_volume_cache_index_def,
  266. vlocation,
  267. &vlocation->cache);
  268. if (vlocation->valid)
  269. goto found_in_cache;
  270. #endif
  271. /* try to look up an unknown volume in the cell VL databases by name */
  272. ret = afs_vlocation_access_vl_by_name(vlocation, name, namesz, &vldb);
  273. if (ret < 0) {
  274. printk("kAFS: failed to locate '%*.*s' in cell '%s'\n",
  275. namesz, namesz, name, cell->name);
  276. goto error;
  277. }
  278. goto found_on_vlserver;
  279. found_in_graveyard:
  280. /* found in the graveyard - resurrect */
  281. _debug("found in graveyard");
  282. atomic_inc(&vlocation->usage);
  283. list_move_tail(&vlocation->link, &cell->vl_list);
  284. spin_unlock(&cell->vl_gylock);
  285. afs_kafstimod_del_timer(&vlocation->timeout);
  286. goto active;
  287. found_in_memory:
  288. /* found in memory - check to see if it's active */
  289. _debug("found in memory");
  290. atomic_inc(&vlocation->usage);
  291. active:
  292. active = 1;
  293. #ifdef AFS_CACHING_SUPPORT
  294. found_in_cache:
  295. #endif
  296. /* try to look up a cached volume in the cell VL databases by ID */
  297. _debug("found in cache");
  298. _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
  299. vlocation->vldb.name,
  300. vlocation->vldb.vidmask,
  301. ntohl(vlocation->vldb.servers[0].s_addr),
  302. vlocation->vldb.srvtmask[0],
  303. ntohl(vlocation->vldb.servers[1].s_addr),
  304. vlocation->vldb.srvtmask[1],
  305. ntohl(vlocation->vldb.servers[2].s_addr),
  306. vlocation->vldb.srvtmask[2]
  307. );
  308. _debug("Vids: %08x %08x %08x",
  309. vlocation->vldb.vid[0],
  310. vlocation->vldb.vid[1],
  311. vlocation->vldb.vid[2]);
  312. if (vlocation->vldb.vidmask & AFS_VOL_VTM_RW) {
  313. vid = vlocation->vldb.vid[0];
  314. voltype = AFSVL_RWVOL;
  315. }
  316. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_RO) {
  317. vid = vlocation->vldb.vid[1];
  318. voltype = AFSVL_ROVOL;
  319. }
  320. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_BAK) {
  321. vid = vlocation->vldb.vid[2];
  322. voltype = AFSVL_BACKVOL;
  323. }
  324. else {
  325. BUG();
  326. vid = 0;
  327. voltype = 0;
  328. }
  329. ret = afs_vlocation_access_vl_by_id(vlocation, vid, voltype, &vldb);
  330. switch (ret) {
  331. /* net error */
  332. default:
  333. printk("kAFS: failed to volume '%*.*s' (%x) up in '%s': %d\n",
  334. namesz, namesz, name, vid, cell->name, ret);
  335. goto error;
  336. /* pulled from local cache into memory */
  337. case 0:
  338. goto found_on_vlserver;
  339. /* uh oh... looks like the volume got deleted */
  340. case -ENOMEDIUM:
  341. printk("kAFS: volume '%*.*s' (%x) does not exist '%s'\n",
  342. namesz, namesz, name, vid, cell->name);
  343. /* TODO: make existing record unavailable */
  344. goto error;
  345. }
  346. found_on_vlserver:
  347. _debug("Done VL Lookup: %*.*s %02x { %08x(%x) %08x(%x) %08x(%x) }",
  348. namesz, namesz, name,
  349. vldb.vidmask,
  350. ntohl(vldb.servers[0].s_addr), vldb.srvtmask[0],
  351. ntohl(vldb.servers[1].s_addr), vldb.srvtmask[1],
  352. ntohl(vldb.servers[2].s_addr), vldb.srvtmask[2]
  353. );
  354. _debug("Vids: %08x %08x %08x", vldb.vid[0], vldb.vid[1], vldb.vid[2]);
  355. if ((namesz < sizeof(vlocation->vldb.name) &&
  356. vlocation->vldb.name[namesz] != '\0') ||
  357. memcmp(vldb.name, name, namesz) != 0)
  358. printk("kAFS: name of volume '%*.*s' changed to '%s' on server\n",
  359. namesz, namesz, name, vldb.name);
  360. memcpy(&vlocation->vldb, &vldb, sizeof(vlocation->vldb));
  361. afs_kafstimod_add_timer(&vlocation->upd_timer, 10 * HZ);
  362. #ifdef AFS_CACHING_SUPPORT
  363. /* update volume entry in local cache */
  364. cachefs_update_cookie(vlocation->cache);
  365. #endif
  366. *_vlocation = vlocation;
  367. _leave(" = 0 (%p)",vlocation);
  368. return 0;
  369. error:
  370. if (vlocation) {
  371. if (active) {
  372. __afs_put_vlocation(vlocation);
  373. }
  374. else {
  375. list_del(&vlocation->link);
  376. #ifdef AFS_CACHING_SUPPORT
  377. cachefs_relinquish_cookie(vlocation->cache, 0);
  378. #endif
  379. afs_put_cell(vlocation->cell);
  380. kfree(vlocation);
  381. }
  382. }
  383. _leave(" = %d", ret);
  384. return ret;
  385. } /* end afs_vlocation_lookup() */
  386. /*****************************************************************************/
  387. /*
  388. * finish using a volume location record
  389. * - caller must have cell->vol_sem write-locked
  390. */
  391. static void __afs_put_vlocation(struct afs_vlocation *vlocation)
  392. {
  393. struct afs_cell *cell;
  394. if (!vlocation)
  395. return;
  396. _enter("%s", vlocation->vldb.name);
  397. cell = vlocation->cell;
  398. /* sanity check */
  399. BUG_ON(atomic_read(&vlocation->usage) <= 0);
  400. spin_lock(&cell->vl_gylock);
  401. if (likely(!atomic_dec_and_test(&vlocation->usage))) {
  402. spin_unlock(&cell->vl_gylock);
  403. _leave("");
  404. return;
  405. }
  406. /* move to graveyard queue */
  407. list_move_tail(&vlocation->link,&cell->vl_graveyard);
  408. /* remove from pending timeout queue (refcounted if actually being
  409. * updated) */
  410. list_del_init(&vlocation->upd_op.link);
  411. /* time out in 10 secs */
  412. afs_kafstimod_del_timer(&vlocation->upd_timer);
  413. afs_kafstimod_add_timer(&vlocation->timeout, 10 * HZ);
  414. spin_unlock(&cell->vl_gylock);
  415. _leave(" [killed]");
  416. } /* end __afs_put_vlocation() */
  417. /*****************************************************************************/
  418. /*
  419. * finish using a volume location record
  420. */
  421. void afs_put_vlocation(struct afs_vlocation *vlocation)
  422. {
  423. if (vlocation) {
  424. struct afs_cell *cell = vlocation->cell;
  425. down_write(&cell->vl_sem);
  426. __afs_put_vlocation(vlocation);
  427. up_write(&cell->vl_sem);
  428. }
  429. } /* end afs_put_vlocation() */
  430. /*****************************************************************************/
  431. /*
  432. * timeout vlocation record
  433. * - removes from the cell's graveyard if the usage count is zero
  434. */
  435. void afs_vlocation_do_timeout(struct afs_vlocation *vlocation)
  436. {
  437. struct afs_cell *cell;
  438. _enter("%s", vlocation->vldb.name);
  439. cell = vlocation->cell;
  440. BUG_ON(atomic_read(&vlocation->usage) < 0);
  441. /* remove from graveyard if still dead */
  442. spin_lock(&cell->vl_gylock);
  443. if (atomic_read(&vlocation->usage) == 0)
  444. list_del_init(&vlocation->link);
  445. else
  446. vlocation = NULL;
  447. spin_unlock(&cell->vl_gylock);
  448. if (!vlocation) {
  449. _leave("");
  450. return; /* resurrected */
  451. }
  452. /* we can now destroy it properly */
  453. #ifdef AFS_CACHING_SUPPORT
  454. cachefs_relinquish_cookie(vlocation->cache, 0);
  455. #endif
  456. afs_put_cell(cell);
  457. kfree(vlocation);
  458. _leave(" [destroyed]");
  459. } /* end afs_vlocation_do_timeout() */
  460. /*****************************************************************************/
  461. /*
  462. * send an update operation to the currently selected server
  463. */
  464. static int afs_vlocation_update_begin(struct afs_vlocation *vlocation)
  465. {
  466. afs_voltype_t voltype;
  467. afs_volid_t vid;
  468. int ret;
  469. _enter("%s{ufs=%u ucs=%u}",
  470. vlocation->vldb.name,
  471. vlocation->upd_first_svix,
  472. vlocation->upd_curr_svix);
  473. /* try to look up a cached volume in the cell VL databases by ID */
  474. if (vlocation->vldb.vidmask & AFS_VOL_VTM_RW) {
  475. vid = vlocation->vldb.vid[0];
  476. voltype = AFSVL_RWVOL;
  477. }
  478. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_RO) {
  479. vid = vlocation->vldb.vid[1];
  480. voltype = AFSVL_ROVOL;
  481. }
  482. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_BAK) {
  483. vid = vlocation->vldb.vid[2];
  484. voltype = AFSVL_BACKVOL;
  485. }
  486. else {
  487. BUG();
  488. vid = 0;
  489. voltype = 0;
  490. }
  491. /* contact the chosen server */
  492. ret = afs_server_lookup(
  493. vlocation->cell,
  494. &vlocation->cell->vl_addrs[vlocation->upd_curr_svix],
  495. &vlocation->upd_op.server);
  496. switch (ret) {
  497. case 0:
  498. break;
  499. case -ENOMEM:
  500. case -ENONET:
  501. default:
  502. _leave(" = %d", ret);
  503. return ret;
  504. }
  505. /* initiate the update operation */
  506. ret = afs_rxvl_get_entry_by_id_async(&vlocation->upd_op, vid, voltype);
  507. if (ret < 0) {
  508. _leave(" = %d", ret);
  509. return ret;
  510. }
  511. _leave(" = %d", ret);
  512. return ret;
  513. } /* end afs_vlocation_update_begin() */
  514. /*****************************************************************************/
  515. /*
  516. * abandon updating a VL record
  517. * - does not restart the update timer
  518. */
  519. static void afs_vlocation_update_abandon(struct afs_vlocation *vlocation,
  520. afs_vlocation_upd_t state,
  521. int ret)
  522. {
  523. _enter("%s,%u", vlocation->vldb.name, state);
  524. if (ret < 0)
  525. printk("kAFS: Abandoning VL update '%s': %d\n",
  526. vlocation->vldb.name, ret);
  527. /* discard the server record */
  528. afs_put_server(vlocation->upd_op.server);
  529. vlocation->upd_op.server = NULL;
  530. spin_lock(&afs_vlocation_update_lock);
  531. afs_vlocation_update = NULL;
  532. vlocation->upd_state = state;
  533. /* TODO: start updating next VL record on pending list */
  534. spin_unlock(&afs_vlocation_update_lock);
  535. _leave("");
  536. } /* end afs_vlocation_update_abandon() */
  537. /*****************************************************************************/
  538. /*
  539. * handle periodic update timeouts and busy retry timeouts
  540. * - called from kafstimod
  541. */
  542. static void afs_vlocation_update_timer(struct afs_timer *timer)
  543. {
  544. struct afs_vlocation *vlocation =
  545. list_entry(timer, struct afs_vlocation, upd_timer);
  546. int ret;
  547. _enter("%s", vlocation->vldb.name);
  548. /* only update if not in the graveyard (defend against putting too) */
  549. spin_lock(&vlocation->cell->vl_gylock);
  550. if (!atomic_read(&vlocation->usage))
  551. goto out_unlock1;
  552. spin_lock(&afs_vlocation_update_lock);
  553. /* if we were woken up due to EBUSY sleep then restart immediately if
  554. * possible or else jump to front of pending queue */
  555. if (vlocation->upd_state == AFS_VLUPD_BUSYSLEEP) {
  556. if (afs_vlocation_update) {
  557. list_add(&vlocation->upd_op.link,
  558. &afs_vlocation_update_pendq);
  559. }
  560. else {
  561. afs_get_vlocation(vlocation);
  562. afs_vlocation_update = vlocation;
  563. vlocation->upd_state = AFS_VLUPD_INPROGRESS;
  564. }
  565. goto out_unlock2;
  566. }
  567. /* put on pending queue if there's already another update in progress */
  568. if (afs_vlocation_update) {
  569. vlocation->upd_state = AFS_VLUPD_PENDING;
  570. list_add_tail(&vlocation->upd_op.link,
  571. &afs_vlocation_update_pendq);
  572. goto out_unlock2;
  573. }
  574. /* hold a ref on it while actually updating */
  575. afs_get_vlocation(vlocation);
  576. afs_vlocation_update = vlocation;
  577. vlocation->upd_state = AFS_VLUPD_INPROGRESS;
  578. spin_unlock(&afs_vlocation_update_lock);
  579. spin_unlock(&vlocation->cell->vl_gylock);
  580. /* okay... we can start the update */
  581. _debug("BEGIN VL UPDATE [%s]", vlocation->vldb.name);
  582. vlocation->upd_first_svix = vlocation->cell->vl_curr_svix;
  583. vlocation->upd_curr_svix = vlocation->upd_first_svix;
  584. vlocation->upd_rej_cnt = 0;
  585. vlocation->upd_busy_cnt = 0;
  586. ret = afs_vlocation_update_begin(vlocation);
  587. if (ret < 0) {
  588. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, ret);
  589. afs_kafstimod_add_timer(&vlocation->upd_timer,
  590. AFS_VLDB_TIMEOUT);
  591. afs_put_vlocation(vlocation);
  592. }
  593. _leave("");
  594. return;
  595. out_unlock2:
  596. spin_unlock(&afs_vlocation_update_lock);
  597. out_unlock1:
  598. spin_unlock(&vlocation->cell->vl_gylock);
  599. _leave("");
  600. return;
  601. } /* end afs_vlocation_update_timer() */
  602. /*****************************************************************************/
  603. /*
  604. * attend to an update operation upon which an event happened
  605. * - called in kafsasyncd context
  606. */
  607. static void afs_vlocation_update_attend(struct afs_async_op *op)
  608. {
  609. struct afs_cache_vlocation vldb;
  610. struct afs_vlocation *vlocation =
  611. list_entry(op, struct afs_vlocation, upd_op);
  612. unsigned tmp;
  613. int ret;
  614. _enter("%s", vlocation->vldb.name);
  615. ret = afs_rxvl_get_entry_by_id_async2(op, &vldb);
  616. switch (ret) {
  617. case -EAGAIN:
  618. _leave(" [unfinished]");
  619. return;
  620. case 0:
  621. _debug("END VL UPDATE: %d\n", ret);
  622. vlocation->valid = 1;
  623. _debug("Done VL Lookup: %02x { %08x(%x) %08x(%x) %08x(%x) }",
  624. vldb.vidmask,
  625. ntohl(vldb.servers[0].s_addr), vldb.srvtmask[0],
  626. ntohl(vldb.servers[1].s_addr), vldb.srvtmask[1],
  627. ntohl(vldb.servers[2].s_addr), vldb.srvtmask[2]
  628. );
  629. _debug("Vids: %08x %08x %08x",
  630. vldb.vid[0], vldb.vid[1], vldb.vid[2]);
  631. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, 0);
  632. down_write(&vlocation->cell->vl_sem);
  633. /* actually update the cache */
  634. if (strncmp(vldb.name, vlocation->vldb.name,
  635. sizeof(vlocation->vldb.name)) != 0)
  636. printk("kAFS: name of volume '%s'"
  637. " changed to '%s' on server\n",
  638. vlocation->vldb.name, vldb.name);
  639. memcpy(&vlocation->vldb, &vldb, sizeof(vlocation->vldb));
  640. #if 0
  641. /* TODO update volume entry in local cache */
  642. #endif
  643. up_write(&vlocation->cell->vl_sem);
  644. if (ret < 0)
  645. printk("kAFS: failed to update local cache: %d\n", ret);
  646. afs_kafstimod_add_timer(&vlocation->upd_timer,
  647. AFS_VLDB_TIMEOUT);
  648. afs_put_vlocation(vlocation);
  649. _leave(" [found]");
  650. return;
  651. case -ENOMEDIUM:
  652. vlocation->upd_rej_cnt++;
  653. goto try_next;
  654. /* the server is locked - retry in a very short while */
  655. case -EBUSY:
  656. vlocation->upd_busy_cnt++;
  657. if (vlocation->upd_busy_cnt > 3)
  658. goto try_next; /* too many retries */
  659. afs_vlocation_update_abandon(vlocation,
  660. AFS_VLUPD_BUSYSLEEP, 0);
  661. afs_kafstimod_add_timer(&vlocation->upd_timer, HZ / 2);
  662. afs_put_vlocation(vlocation);
  663. _leave(" [busy]");
  664. return;
  665. case -ENETUNREACH:
  666. case -EHOSTUNREACH:
  667. case -ECONNREFUSED:
  668. case -EREMOTEIO:
  669. /* record bad vlserver info in the cell too
  670. * - TODO: use down_write_trylock() if available
  671. */
  672. if (vlocation->upd_curr_svix == vlocation->cell->vl_curr_svix)
  673. vlocation->cell->vl_curr_svix =
  674. vlocation->cell->vl_curr_svix %
  675. vlocation->cell->vl_naddrs;
  676. case -EBADRQC:
  677. case -EINVAL:
  678. case -EACCES:
  679. case -EBADMSG:
  680. goto try_next;
  681. default:
  682. goto abandon;
  683. }
  684. /* try contacting the next server */
  685. try_next:
  686. vlocation->upd_busy_cnt = 0;
  687. /* discard the server record */
  688. afs_put_server(vlocation->upd_op.server);
  689. vlocation->upd_op.server = NULL;
  690. tmp = vlocation->cell->vl_naddrs;
  691. if (tmp == 0)
  692. goto abandon;
  693. vlocation->upd_curr_svix++;
  694. if (vlocation->upd_curr_svix >= tmp)
  695. vlocation->upd_curr_svix = 0;
  696. if (vlocation->upd_first_svix >= tmp)
  697. vlocation->upd_first_svix = tmp - 1;
  698. /* move to the next server */
  699. if (vlocation->upd_curr_svix != vlocation->upd_first_svix) {
  700. afs_vlocation_update_begin(vlocation);
  701. _leave(" [next]");
  702. return;
  703. }
  704. /* run out of servers to try - was the volume rejected? */
  705. if (vlocation->upd_rej_cnt > 0) {
  706. printk("kAFS: Active volume no longer valid '%s'\n",
  707. vlocation->vldb.name);
  708. vlocation->valid = 0;
  709. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, 0);
  710. afs_kafstimod_add_timer(&vlocation->upd_timer,
  711. AFS_VLDB_TIMEOUT);
  712. afs_put_vlocation(vlocation);
  713. _leave(" [invalidated]");
  714. return;
  715. }
  716. /* abandon the update */
  717. abandon:
  718. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, ret);
  719. afs_kafstimod_add_timer(&vlocation->upd_timer, HZ * 10);
  720. afs_put_vlocation(vlocation);
  721. _leave(" [abandoned]");
  722. } /* end afs_vlocation_update_attend() */
  723. /*****************************************************************************/
  724. /*
  725. * deal with an update operation being discarded
  726. * - called in kafsasyncd context when it's dying due to rmmod
  727. * - the call has already been aborted and put()'d
  728. */
  729. static void afs_vlocation_update_discard(struct afs_async_op *op)
  730. {
  731. struct afs_vlocation *vlocation =
  732. list_entry(op, struct afs_vlocation, upd_op);
  733. _enter("%s", vlocation->vldb.name);
  734. afs_put_server(op->server);
  735. op->server = NULL;
  736. afs_put_vlocation(vlocation);
  737. _leave("");
  738. } /* end afs_vlocation_update_discard() */
  739. /*****************************************************************************/
  740. /*
  741. * match a VLDB record stored in the cache
  742. * - may also load target from entry
  743. */
  744. #ifdef AFS_CACHING_SUPPORT
  745. static cachefs_match_val_t afs_vlocation_cache_match(void *target,
  746. const void *entry)
  747. {
  748. const struct afs_cache_vlocation *vldb = entry;
  749. struct afs_vlocation *vlocation = target;
  750. _enter("{%s},{%s}", vlocation->vldb.name, vldb->name);
  751. if (strncmp(vlocation->vldb.name, vldb->name, sizeof(vldb->name)) == 0
  752. ) {
  753. if (!vlocation->valid ||
  754. vlocation->vldb.rtime == vldb->rtime
  755. ) {
  756. vlocation->vldb = *vldb;
  757. vlocation->valid = 1;
  758. _leave(" = SUCCESS [c->m]");
  759. return CACHEFS_MATCH_SUCCESS;
  760. }
  761. /* need to update cache if cached info differs */
  762. else if (memcmp(&vlocation->vldb, vldb, sizeof(*vldb)) != 0) {
  763. /* delete if VIDs for this name differ */
  764. if (memcmp(&vlocation->vldb.vid,
  765. &vldb->vid,
  766. sizeof(vldb->vid)) != 0) {
  767. _leave(" = DELETE");
  768. return CACHEFS_MATCH_SUCCESS_DELETE;
  769. }
  770. _leave(" = UPDATE");
  771. return CACHEFS_MATCH_SUCCESS_UPDATE;
  772. }
  773. else {
  774. _leave(" = SUCCESS");
  775. return CACHEFS_MATCH_SUCCESS;
  776. }
  777. }
  778. _leave(" = FAILED");
  779. return CACHEFS_MATCH_FAILED;
  780. } /* end afs_vlocation_cache_match() */
  781. #endif
  782. /*****************************************************************************/
  783. /*
  784. * update a VLDB record stored in the cache
  785. */
  786. #ifdef AFS_CACHING_SUPPORT
  787. static void afs_vlocation_cache_update(void *source, void *entry)
  788. {
  789. struct afs_cache_vlocation *vldb = entry;
  790. struct afs_vlocation *vlocation = source;
  791. _enter("");
  792. *vldb = vlocation->vldb;
  793. } /* end afs_vlocation_cache_update() */
  794. #endif