vlocation.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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 = kmalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
  249. if (!vlocation)
  250. return -ENOMEM;
  251. memset(vlocation, 0, sizeof(struct afs_vlocation));
  252. atomic_set(&vlocation->usage, 1);
  253. INIT_LIST_HEAD(&vlocation->link);
  254. rwlock_init(&vlocation->lock);
  255. memcpy(vlocation->vldb.name, name, namesz);
  256. afs_timer_init(&vlocation->timeout, &afs_vlocation_timer_ops);
  257. afs_timer_init(&vlocation->upd_timer, &afs_vlocation_update_timer_ops);
  258. afs_async_op_init(&vlocation->upd_op, &afs_vlocation_update_op_ops);
  259. afs_get_cell(cell);
  260. vlocation->cell = cell;
  261. list_add_tail(&vlocation->link, &cell->vl_list);
  262. #ifdef AFS_CACHING_SUPPORT
  263. /* we want to store it in the cache, plus it might already be
  264. * encached */
  265. cachefs_acquire_cookie(cell->cache,
  266. &afs_volume_cache_index_def,
  267. vlocation,
  268. &vlocation->cache);
  269. if (vlocation->valid)
  270. goto found_in_cache;
  271. #endif
  272. /* try to look up an unknown volume in the cell VL databases by name */
  273. ret = afs_vlocation_access_vl_by_name(vlocation, name, namesz, &vldb);
  274. if (ret < 0) {
  275. printk("kAFS: failed to locate '%*.*s' in cell '%s'\n",
  276. namesz, namesz, name, cell->name);
  277. goto error;
  278. }
  279. goto found_on_vlserver;
  280. found_in_graveyard:
  281. /* found in the graveyard - resurrect */
  282. _debug("found in graveyard");
  283. atomic_inc(&vlocation->usage);
  284. list_move_tail(&vlocation->link, &cell->vl_list);
  285. spin_unlock(&cell->vl_gylock);
  286. afs_kafstimod_del_timer(&vlocation->timeout);
  287. goto active;
  288. found_in_memory:
  289. /* found in memory - check to see if it's active */
  290. _debug("found in memory");
  291. atomic_inc(&vlocation->usage);
  292. active:
  293. active = 1;
  294. #ifdef AFS_CACHING_SUPPORT
  295. found_in_cache:
  296. #endif
  297. /* try to look up a cached volume in the cell VL databases by ID */
  298. _debug("found in cache");
  299. _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
  300. vlocation->vldb.name,
  301. vlocation->vldb.vidmask,
  302. ntohl(vlocation->vldb.servers[0].s_addr),
  303. vlocation->vldb.srvtmask[0],
  304. ntohl(vlocation->vldb.servers[1].s_addr),
  305. vlocation->vldb.srvtmask[1],
  306. ntohl(vlocation->vldb.servers[2].s_addr),
  307. vlocation->vldb.srvtmask[2]
  308. );
  309. _debug("Vids: %08x %08x %08x",
  310. vlocation->vldb.vid[0],
  311. vlocation->vldb.vid[1],
  312. vlocation->vldb.vid[2]);
  313. if (vlocation->vldb.vidmask & AFS_VOL_VTM_RW) {
  314. vid = vlocation->vldb.vid[0];
  315. voltype = AFSVL_RWVOL;
  316. }
  317. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_RO) {
  318. vid = vlocation->vldb.vid[1];
  319. voltype = AFSVL_ROVOL;
  320. }
  321. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_BAK) {
  322. vid = vlocation->vldb.vid[2];
  323. voltype = AFSVL_BACKVOL;
  324. }
  325. else {
  326. BUG();
  327. vid = 0;
  328. voltype = 0;
  329. }
  330. ret = afs_vlocation_access_vl_by_id(vlocation, vid, voltype, &vldb);
  331. switch (ret) {
  332. /* net error */
  333. default:
  334. printk("kAFS: failed to volume '%*.*s' (%x) up in '%s': %d\n",
  335. namesz, namesz, name, vid, cell->name, ret);
  336. goto error;
  337. /* pulled from local cache into memory */
  338. case 0:
  339. goto found_on_vlserver;
  340. /* uh oh... looks like the volume got deleted */
  341. case -ENOMEDIUM:
  342. printk("kAFS: volume '%*.*s' (%x) does not exist '%s'\n",
  343. namesz, namesz, name, vid, cell->name);
  344. /* TODO: make existing record unavailable */
  345. goto error;
  346. }
  347. found_on_vlserver:
  348. _debug("Done VL Lookup: %*.*s %02x { %08x(%x) %08x(%x) %08x(%x) }",
  349. namesz, namesz, name,
  350. vldb.vidmask,
  351. ntohl(vldb.servers[0].s_addr), vldb.srvtmask[0],
  352. ntohl(vldb.servers[1].s_addr), vldb.srvtmask[1],
  353. ntohl(vldb.servers[2].s_addr), vldb.srvtmask[2]
  354. );
  355. _debug("Vids: %08x %08x %08x", vldb.vid[0], vldb.vid[1], vldb.vid[2]);
  356. if ((namesz < sizeof(vlocation->vldb.name) &&
  357. vlocation->vldb.name[namesz] != '\0') ||
  358. memcmp(vldb.name, name, namesz) != 0)
  359. printk("kAFS: name of volume '%*.*s' changed to '%s' on server\n",
  360. namesz, namesz, name, vldb.name);
  361. memcpy(&vlocation->vldb, &vldb, sizeof(vlocation->vldb));
  362. afs_kafstimod_add_timer(&vlocation->upd_timer, 10 * HZ);
  363. #ifdef AFS_CACHING_SUPPORT
  364. /* update volume entry in local cache */
  365. cachefs_update_cookie(vlocation->cache);
  366. #endif
  367. *_vlocation = vlocation;
  368. _leave(" = 0 (%p)",vlocation);
  369. return 0;
  370. error:
  371. if (vlocation) {
  372. if (active) {
  373. __afs_put_vlocation(vlocation);
  374. }
  375. else {
  376. list_del(&vlocation->link);
  377. #ifdef AFS_CACHING_SUPPORT
  378. cachefs_relinquish_cookie(vlocation->cache, 0);
  379. #endif
  380. afs_put_cell(vlocation->cell);
  381. kfree(vlocation);
  382. }
  383. }
  384. _leave(" = %d", ret);
  385. return ret;
  386. } /* end afs_vlocation_lookup() */
  387. /*****************************************************************************/
  388. /*
  389. * finish using a volume location record
  390. * - caller must have cell->vol_sem write-locked
  391. */
  392. static void __afs_put_vlocation(struct afs_vlocation *vlocation)
  393. {
  394. struct afs_cell *cell;
  395. if (!vlocation)
  396. return;
  397. _enter("%s", vlocation->vldb.name);
  398. cell = vlocation->cell;
  399. /* sanity check */
  400. BUG_ON(atomic_read(&vlocation->usage) <= 0);
  401. spin_lock(&cell->vl_gylock);
  402. if (likely(!atomic_dec_and_test(&vlocation->usage))) {
  403. spin_unlock(&cell->vl_gylock);
  404. _leave("");
  405. return;
  406. }
  407. /* move to graveyard queue */
  408. list_move_tail(&vlocation->link,&cell->vl_graveyard);
  409. /* remove from pending timeout queue (refcounted if actually being
  410. * updated) */
  411. list_del_init(&vlocation->upd_op.link);
  412. /* time out in 10 secs */
  413. afs_kafstimod_del_timer(&vlocation->upd_timer);
  414. afs_kafstimod_add_timer(&vlocation->timeout, 10 * HZ);
  415. spin_unlock(&cell->vl_gylock);
  416. _leave(" [killed]");
  417. } /* end __afs_put_vlocation() */
  418. /*****************************************************************************/
  419. /*
  420. * finish using a volume location record
  421. */
  422. void afs_put_vlocation(struct afs_vlocation *vlocation)
  423. {
  424. if (vlocation) {
  425. struct afs_cell *cell = vlocation->cell;
  426. down_write(&cell->vl_sem);
  427. __afs_put_vlocation(vlocation);
  428. up_write(&cell->vl_sem);
  429. }
  430. } /* end afs_put_vlocation() */
  431. /*****************************************************************************/
  432. /*
  433. * timeout vlocation record
  434. * - removes from the cell's graveyard if the usage count is zero
  435. */
  436. void afs_vlocation_do_timeout(struct afs_vlocation *vlocation)
  437. {
  438. struct afs_cell *cell;
  439. _enter("%s", vlocation->vldb.name);
  440. cell = vlocation->cell;
  441. BUG_ON(atomic_read(&vlocation->usage) < 0);
  442. /* remove from graveyard if still dead */
  443. spin_lock(&cell->vl_gylock);
  444. if (atomic_read(&vlocation->usage) == 0)
  445. list_del_init(&vlocation->link);
  446. else
  447. vlocation = NULL;
  448. spin_unlock(&cell->vl_gylock);
  449. if (!vlocation) {
  450. _leave("");
  451. return; /* resurrected */
  452. }
  453. /* we can now destroy it properly */
  454. #ifdef AFS_CACHING_SUPPORT
  455. cachefs_relinquish_cookie(vlocation->cache, 0);
  456. #endif
  457. afs_put_cell(cell);
  458. kfree(vlocation);
  459. _leave(" [destroyed]");
  460. } /* end afs_vlocation_do_timeout() */
  461. /*****************************************************************************/
  462. /*
  463. * send an update operation to the currently selected server
  464. */
  465. static int afs_vlocation_update_begin(struct afs_vlocation *vlocation)
  466. {
  467. afs_voltype_t voltype;
  468. afs_volid_t vid;
  469. int ret;
  470. _enter("%s{ufs=%u ucs=%u}",
  471. vlocation->vldb.name,
  472. vlocation->upd_first_svix,
  473. vlocation->upd_curr_svix);
  474. /* try to look up a cached volume in the cell VL databases by ID */
  475. if (vlocation->vldb.vidmask & AFS_VOL_VTM_RW) {
  476. vid = vlocation->vldb.vid[0];
  477. voltype = AFSVL_RWVOL;
  478. }
  479. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_RO) {
  480. vid = vlocation->vldb.vid[1];
  481. voltype = AFSVL_ROVOL;
  482. }
  483. else if (vlocation->vldb.vidmask & AFS_VOL_VTM_BAK) {
  484. vid = vlocation->vldb.vid[2];
  485. voltype = AFSVL_BACKVOL;
  486. }
  487. else {
  488. BUG();
  489. vid = 0;
  490. voltype = 0;
  491. }
  492. /* contact the chosen server */
  493. ret = afs_server_lookup(
  494. vlocation->cell,
  495. &vlocation->cell->vl_addrs[vlocation->upd_curr_svix],
  496. &vlocation->upd_op.server);
  497. switch (ret) {
  498. case 0:
  499. break;
  500. case -ENOMEM:
  501. case -ENONET:
  502. default:
  503. _leave(" = %d", ret);
  504. return ret;
  505. }
  506. /* initiate the update operation */
  507. ret = afs_rxvl_get_entry_by_id_async(&vlocation->upd_op, vid, voltype);
  508. if (ret < 0) {
  509. _leave(" = %d", ret);
  510. return ret;
  511. }
  512. _leave(" = %d", ret);
  513. return ret;
  514. } /* end afs_vlocation_update_begin() */
  515. /*****************************************************************************/
  516. /*
  517. * abandon updating a VL record
  518. * - does not restart the update timer
  519. */
  520. static void afs_vlocation_update_abandon(struct afs_vlocation *vlocation,
  521. afs_vlocation_upd_t state,
  522. int ret)
  523. {
  524. _enter("%s,%u", vlocation->vldb.name, state);
  525. if (ret < 0)
  526. printk("kAFS: Abandoning VL update '%s': %d\n",
  527. vlocation->vldb.name, ret);
  528. /* discard the server record */
  529. afs_put_server(vlocation->upd_op.server);
  530. vlocation->upd_op.server = NULL;
  531. spin_lock(&afs_vlocation_update_lock);
  532. afs_vlocation_update = NULL;
  533. vlocation->upd_state = state;
  534. /* TODO: start updating next VL record on pending list */
  535. spin_unlock(&afs_vlocation_update_lock);
  536. _leave("");
  537. } /* end afs_vlocation_update_abandon() */
  538. /*****************************************************************************/
  539. /*
  540. * handle periodic update timeouts and busy retry timeouts
  541. * - called from kafstimod
  542. */
  543. static void afs_vlocation_update_timer(struct afs_timer *timer)
  544. {
  545. struct afs_vlocation *vlocation =
  546. list_entry(timer, struct afs_vlocation, upd_timer);
  547. int ret;
  548. _enter("%s", vlocation->vldb.name);
  549. /* only update if not in the graveyard (defend against putting too) */
  550. spin_lock(&vlocation->cell->vl_gylock);
  551. if (!atomic_read(&vlocation->usage))
  552. goto out_unlock1;
  553. spin_lock(&afs_vlocation_update_lock);
  554. /* if we were woken up due to EBUSY sleep then restart immediately if
  555. * possible or else jump to front of pending queue */
  556. if (vlocation->upd_state == AFS_VLUPD_BUSYSLEEP) {
  557. if (afs_vlocation_update) {
  558. list_add(&vlocation->upd_op.link,
  559. &afs_vlocation_update_pendq);
  560. }
  561. else {
  562. afs_get_vlocation(vlocation);
  563. afs_vlocation_update = vlocation;
  564. vlocation->upd_state = AFS_VLUPD_INPROGRESS;
  565. }
  566. goto out_unlock2;
  567. }
  568. /* put on pending queue if there's already another update in progress */
  569. if (afs_vlocation_update) {
  570. vlocation->upd_state = AFS_VLUPD_PENDING;
  571. list_add_tail(&vlocation->upd_op.link,
  572. &afs_vlocation_update_pendq);
  573. goto out_unlock2;
  574. }
  575. /* hold a ref on it while actually updating */
  576. afs_get_vlocation(vlocation);
  577. afs_vlocation_update = vlocation;
  578. vlocation->upd_state = AFS_VLUPD_INPROGRESS;
  579. spin_unlock(&afs_vlocation_update_lock);
  580. spin_unlock(&vlocation->cell->vl_gylock);
  581. /* okay... we can start the update */
  582. _debug("BEGIN VL UPDATE [%s]", vlocation->vldb.name);
  583. vlocation->upd_first_svix = vlocation->cell->vl_curr_svix;
  584. vlocation->upd_curr_svix = vlocation->upd_first_svix;
  585. vlocation->upd_rej_cnt = 0;
  586. vlocation->upd_busy_cnt = 0;
  587. ret = afs_vlocation_update_begin(vlocation);
  588. if (ret < 0) {
  589. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, ret);
  590. afs_kafstimod_add_timer(&vlocation->upd_timer,
  591. AFS_VLDB_TIMEOUT);
  592. afs_put_vlocation(vlocation);
  593. }
  594. _leave("");
  595. return;
  596. out_unlock2:
  597. spin_unlock(&afs_vlocation_update_lock);
  598. out_unlock1:
  599. spin_unlock(&vlocation->cell->vl_gylock);
  600. _leave("");
  601. return;
  602. } /* end afs_vlocation_update_timer() */
  603. /*****************************************************************************/
  604. /*
  605. * attend to an update operation upon which an event happened
  606. * - called in kafsasyncd context
  607. */
  608. static void afs_vlocation_update_attend(struct afs_async_op *op)
  609. {
  610. struct afs_cache_vlocation vldb;
  611. struct afs_vlocation *vlocation =
  612. list_entry(op, struct afs_vlocation, upd_op);
  613. unsigned tmp;
  614. int ret;
  615. _enter("%s", vlocation->vldb.name);
  616. ret = afs_rxvl_get_entry_by_id_async2(op, &vldb);
  617. switch (ret) {
  618. case -EAGAIN:
  619. _leave(" [unfinished]");
  620. return;
  621. case 0:
  622. _debug("END VL UPDATE: %d\n", ret);
  623. vlocation->valid = 1;
  624. _debug("Done VL Lookup: %02x { %08x(%x) %08x(%x) %08x(%x) }",
  625. vldb.vidmask,
  626. ntohl(vldb.servers[0].s_addr), vldb.srvtmask[0],
  627. ntohl(vldb.servers[1].s_addr), vldb.srvtmask[1],
  628. ntohl(vldb.servers[2].s_addr), vldb.srvtmask[2]
  629. );
  630. _debug("Vids: %08x %08x %08x",
  631. vldb.vid[0], vldb.vid[1], vldb.vid[2]);
  632. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, 0);
  633. down_write(&vlocation->cell->vl_sem);
  634. /* actually update the cache */
  635. if (strncmp(vldb.name, vlocation->vldb.name,
  636. sizeof(vlocation->vldb.name)) != 0)
  637. printk("kAFS: name of volume '%s'"
  638. " changed to '%s' on server\n",
  639. vlocation->vldb.name, vldb.name);
  640. memcpy(&vlocation->vldb, &vldb, sizeof(vlocation->vldb));
  641. #if 0
  642. /* TODO update volume entry in local cache */
  643. #endif
  644. up_write(&vlocation->cell->vl_sem);
  645. if (ret < 0)
  646. printk("kAFS: failed to update local cache: %d\n", ret);
  647. afs_kafstimod_add_timer(&vlocation->upd_timer,
  648. AFS_VLDB_TIMEOUT);
  649. afs_put_vlocation(vlocation);
  650. _leave(" [found]");
  651. return;
  652. case -ENOMEDIUM:
  653. vlocation->upd_rej_cnt++;
  654. goto try_next;
  655. /* the server is locked - retry in a very short while */
  656. case -EBUSY:
  657. vlocation->upd_busy_cnt++;
  658. if (vlocation->upd_busy_cnt > 3)
  659. goto try_next; /* too many retries */
  660. afs_vlocation_update_abandon(vlocation,
  661. AFS_VLUPD_BUSYSLEEP, 0);
  662. afs_kafstimod_add_timer(&vlocation->upd_timer, HZ / 2);
  663. afs_put_vlocation(vlocation);
  664. _leave(" [busy]");
  665. return;
  666. case -ENETUNREACH:
  667. case -EHOSTUNREACH:
  668. case -ECONNREFUSED:
  669. case -EREMOTEIO:
  670. /* record bad vlserver info in the cell too
  671. * - TODO: use down_write_trylock() if available
  672. */
  673. if (vlocation->upd_curr_svix == vlocation->cell->vl_curr_svix)
  674. vlocation->cell->vl_curr_svix =
  675. vlocation->cell->vl_curr_svix %
  676. vlocation->cell->vl_naddrs;
  677. case -EBADRQC:
  678. case -EINVAL:
  679. case -EACCES:
  680. case -EBADMSG:
  681. goto try_next;
  682. default:
  683. goto abandon;
  684. }
  685. /* try contacting the next server */
  686. try_next:
  687. vlocation->upd_busy_cnt = 0;
  688. /* discard the server record */
  689. afs_put_server(vlocation->upd_op.server);
  690. vlocation->upd_op.server = NULL;
  691. tmp = vlocation->cell->vl_naddrs;
  692. if (tmp == 0)
  693. goto abandon;
  694. vlocation->upd_curr_svix++;
  695. if (vlocation->upd_curr_svix >= tmp)
  696. vlocation->upd_curr_svix = 0;
  697. if (vlocation->upd_first_svix >= tmp)
  698. vlocation->upd_first_svix = tmp - 1;
  699. /* move to the next server */
  700. if (vlocation->upd_curr_svix != vlocation->upd_first_svix) {
  701. afs_vlocation_update_begin(vlocation);
  702. _leave(" [next]");
  703. return;
  704. }
  705. /* run out of servers to try - was the volume rejected? */
  706. if (vlocation->upd_rej_cnt > 0) {
  707. printk("kAFS: Active volume no longer valid '%s'\n",
  708. vlocation->vldb.name);
  709. vlocation->valid = 0;
  710. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, 0);
  711. afs_kafstimod_add_timer(&vlocation->upd_timer,
  712. AFS_VLDB_TIMEOUT);
  713. afs_put_vlocation(vlocation);
  714. _leave(" [invalidated]");
  715. return;
  716. }
  717. /* abandon the update */
  718. abandon:
  719. afs_vlocation_update_abandon(vlocation, AFS_VLUPD_SLEEP, ret);
  720. afs_kafstimod_add_timer(&vlocation->upd_timer, HZ * 10);
  721. afs_put_vlocation(vlocation);
  722. _leave(" [abandoned]");
  723. } /* end afs_vlocation_update_attend() */
  724. /*****************************************************************************/
  725. /*
  726. * deal with an update operation being discarded
  727. * - called in kafsasyncd context when it's dying due to rmmod
  728. * - the call has already been aborted and put()'d
  729. */
  730. static void afs_vlocation_update_discard(struct afs_async_op *op)
  731. {
  732. struct afs_vlocation *vlocation =
  733. list_entry(op, struct afs_vlocation, upd_op);
  734. _enter("%s", vlocation->vldb.name);
  735. afs_put_server(op->server);
  736. op->server = NULL;
  737. afs_put_vlocation(vlocation);
  738. _leave("");
  739. } /* end afs_vlocation_update_discard() */
  740. /*****************************************************************************/
  741. /*
  742. * match a VLDB record stored in the cache
  743. * - may also load target from entry
  744. */
  745. #ifdef AFS_CACHING_SUPPORT
  746. static cachefs_match_val_t afs_vlocation_cache_match(void *target,
  747. const void *entry)
  748. {
  749. const struct afs_cache_vlocation *vldb = entry;
  750. struct afs_vlocation *vlocation = target;
  751. _enter("{%s},{%s}", vlocation->vldb.name, vldb->name);
  752. if (strncmp(vlocation->vldb.name, vldb->name, sizeof(vldb->name)) == 0
  753. ) {
  754. if (!vlocation->valid ||
  755. vlocation->vldb.rtime == vldb->rtime
  756. ) {
  757. vlocation->vldb = *vldb;
  758. vlocation->valid = 1;
  759. _leave(" = SUCCESS [c->m]");
  760. return CACHEFS_MATCH_SUCCESS;
  761. }
  762. /* need to update cache if cached info differs */
  763. else if (memcmp(&vlocation->vldb, vldb, sizeof(*vldb)) != 0) {
  764. /* delete if VIDs for this name differ */
  765. if (memcmp(&vlocation->vldb.vid,
  766. &vldb->vid,
  767. sizeof(vldb->vid)) != 0) {
  768. _leave(" = DELETE");
  769. return CACHEFS_MATCH_SUCCESS_DELETE;
  770. }
  771. _leave(" = UPDATE");
  772. return CACHEFS_MATCH_SUCCESS_UPDATE;
  773. }
  774. else {
  775. _leave(" = SUCCESS");
  776. return CACHEFS_MATCH_SUCCESS;
  777. }
  778. }
  779. _leave(" = FAILED");
  780. return CACHEFS_MATCH_FAILED;
  781. } /* end afs_vlocation_cache_match() */
  782. #endif
  783. /*****************************************************************************/
  784. /*
  785. * update a VLDB record stored in the cache
  786. */
  787. #ifdef AFS_CACHING_SUPPORT
  788. static void afs_vlocation_cache_update(void *source, void *entry)
  789. {
  790. struct afs_cache_vlocation *vldb = entry;
  791. struct afs_vlocation *vlocation = source;
  792. _enter("");
  793. *vldb = vlocation->vldb;
  794. } /* end afs_vlocation_cache_update() */
  795. #endif