callback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*
  2. * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
  3. *
  4. * This software may be freely redistributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * You should have received a copy of the GNU General Public License
  8. * along with this program; if not, write to the Free Software
  9. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10. *
  11. * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
  12. * David Howells <dhowells@redhat.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/circ_buf.h>
  19. #include "internal.h"
  20. unsigned afs_vnode_update_timeout = 10;
  21. #define afs_breakring_space(server) \
  22. CIRC_SPACE((server)->cb_break_head, (server)->cb_break_tail, \
  23. ARRAY_SIZE((server)->cb_break))
  24. //static void afs_callback_updater(struct work_struct *);
  25. static struct workqueue_struct *afs_callback_update_worker;
  26. /*
  27. * allow the fileserver to request callback state (re-)initialisation
  28. */
  29. void afs_init_callback_state(struct afs_server *server)
  30. {
  31. struct afs_vnode *vnode;
  32. _enter("{%p}", server);
  33. spin_lock(&server->cb_lock);
  34. /* kill all the promises on record from this server */
  35. while (!RB_EMPTY_ROOT(&server->cb_promises)) {
  36. vnode = rb_entry(server->cb_promises.rb_node,
  37. struct afs_vnode, cb_promise);
  38. _debug("UNPROMISE { vid=%x vn=%u uq=%u}",
  39. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  40. rb_erase(&vnode->cb_promise, &server->cb_promises);
  41. vnode->cb_promised = false;
  42. }
  43. spin_unlock(&server->cb_lock);
  44. _leave("");
  45. }
  46. /*
  47. * handle the data invalidation side of a callback being broken
  48. */
  49. void afs_broken_callback_work(struct work_struct *work)
  50. {
  51. struct afs_vnode *vnode =
  52. container_of(work, struct afs_vnode, cb_broken_work);
  53. _enter("");
  54. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  55. return;
  56. /* we're only interested in dealing with a broken callback on *this*
  57. * vnode and only if no-one else has dealt with it yet */
  58. if (!mutex_trylock(&vnode->validate_lock))
  59. return; /* someone else is dealing with it */
  60. if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
  61. if (S_ISDIR(vnode->vfs_inode.i_mode))
  62. afs_clear_permits(vnode);
  63. if (afs_vnode_fetch_status(vnode, NULL, NULL) < 0)
  64. goto out;
  65. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  66. goto out;
  67. /* if the vnode's data version number changed then its contents
  68. * are different */
  69. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
  70. _debug("zap data {%x:%u}",
  71. vnode->fid.vid, vnode->fid.vnode);
  72. invalidate_remote_inode(&vnode->vfs_inode);
  73. }
  74. }
  75. out:
  76. mutex_unlock(&vnode->validate_lock);
  77. /* avoid the potential race whereby the mutex_trylock() in this
  78. * function happens again between the clear_bit() and the
  79. * mutex_unlock() */
  80. if (test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
  81. _debug("requeue");
  82. queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
  83. }
  84. _leave("");
  85. }
  86. /*
  87. * actually break a callback
  88. */
  89. static void afs_break_callback(struct afs_server *server,
  90. struct afs_vnode *vnode)
  91. {
  92. _enter("");
  93. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  94. if (vnode->cb_promised) {
  95. spin_lock(&vnode->lock);
  96. _debug("break callback");
  97. spin_lock(&server->cb_lock);
  98. if (vnode->cb_promised) {
  99. rb_erase(&vnode->cb_promise, &server->cb_promises);
  100. vnode->cb_promised = false;
  101. }
  102. spin_unlock(&server->cb_lock);
  103. queue_work(afs_callback_update_worker, &vnode->cb_broken_work);
  104. spin_unlock(&vnode->lock);
  105. }
  106. }
  107. /*
  108. * allow the fileserver to explicitly break one callback
  109. * - happens when
  110. * - the backing file is changed
  111. * - a lock is released
  112. */
  113. static void afs_break_one_callback(struct afs_server *server,
  114. struct afs_fid *fid)
  115. {
  116. struct afs_vnode *vnode;
  117. struct rb_node *p;
  118. _debug("find");
  119. spin_lock(&server->fs_lock);
  120. p = server->fs_vnodes.rb_node;
  121. while (p) {
  122. vnode = rb_entry(p, struct afs_vnode, server_rb);
  123. if (fid->vid < vnode->fid.vid)
  124. p = p->rb_left;
  125. else if (fid->vid > vnode->fid.vid)
  126. p = p->rb_right;
  127. else if (fid->vnode < vnode->fid.vnode)
  128. p = p->rb_left;
  129. else if (fid->vnode > vnode->fid.vnode)
  130. p = p->rb_right;
  131. else if (fid->unique < vnode->fid.unique)
  132. p = p->rb_left;
  133. else if (fid->unique > vnode->fid.unique)
  134. p = p->rb_right;
  135. else
  136. goto found;
  137. }
  138. /* not found so we just ignore it (it may have moved to another
  139. * server) */
  140. not_available:
  141. _debug("not avail");
  142. spin_unlock(&server->fs_lock);
  143. _leave("");
  144. return;
  145. found:
  146. _debug("found");
  147. ASSERTCMP(server, ==, vnode->server);
  148. if (!igrab(AFS_VNODE_TO_I(vnode)))
  149. goto not_available;
  150. spin_unlock(&server->fs_lock);
  151. afs_break_callback(server, vnode);
  152. iput(&vnode->vfs_inode);
  153. _leave("");
  154. }
  155. /*
  156. * allow the fileserver to break callback promises
  157. */
  158. void afs_break_callbacks(struct afs_server *server, size_t count,
  159. struct afs_callback callbacks[])
  160. {
  161. _enter("%p,%zu,", server, count);
  162. ASSERT(server != NULL);
  163. ASSERTCMP(count, <=, AFSCBMAX);
  164. for (; count > 0; callbacks++, count--) {
  165. _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
  166. callbacks->fid.vid,
  167. callbacks->fid.vnode,
  168. callbacks->fid.unique,
  169. callbacks->version,
  170. callbacks->expiry,
  171. callbacks->type
  172. );
  173. afs_break_one_callback(server, &callbacks->fid);
  174. }
  175. _leave("");
  176. return;
  177. }
  178. /*
  179. * record the callback for breaking
  180. * - the caller must hold server->cb_lock
  181. */
  182. static void afs_do_give_up_callback(struct afs_server *server,
  183. struct afs_vnode *vnode)
  184. {
  185. struct afs_callback *cb;
  186. _enter("%p,%p", server, vnode);
  187. cb = &server->cb_break[server->cb_break_head];
  188. cb->fid = vnode->fid;
  189. cb->version = vnode->cb_version;
  190. cb->expiry = vnode->cb_expiry;
  191. cb->type = vnode->cb_type;
  192. smp_wmb();
  193. server->cb_break_head =
  194. (server->cb_break_head + 1) &
  195. (ARRAY_SIZE(server->cb_break) - 1);
  196. /* defer the breaking of callbacks to try and collect as many as
  197. * possible to ship in one operation */
  198. switch (atomic_inc_return(&server->cb_break_n)) {
  199. case 1 ... AFSCBMAX - 1:
  200. queue_delayed_work(afs_callback_update_worker,
  201. &server->cb_break_work, HZ * 2);
  202. break;
  203. case AFSCBMAX:
  204. afs_flush_callback_breaks(server);
  205. break;
  206. default:
  207. break;
  208. }
  209. ASSERT(server->cb_promises.rb_node != NULL);
  210. rb_erase(&vnode->cb_promise, &server->cb_promises);
  211. vnode->cb_promised = false;
  212. _leave("");
  213. }
  214. /*
  215. * discard the callback on a deleted item
  216. */
  217. void afs_discard_callback_on_delete(struct afs_vnode *vnode)
  218. {
  219. struct afs_server *server = vnode->server;
  220. _enter("%d", vnode->cb_promised);
  221. if (!vnode->cb_promised) {
  222. _leave(" [not promised]");
  223. return;
  224. }
  225. ASSERT(server != NULL);
  226. spin_lock(&server->cb_lock);
  227. if (vnode->cb_promised) {
  228. ASSERT(server->cb_promises.rb_node != NULL);
  229. rb_erase(&vnode->cb_promise, &server->cb_promises);
  230. vnode->cb_promised = false;
  231. }
  232. spin_unlock(&server->cb_lock);
  233. _leave("");
  234. }
  235. /*
  236. * give up the callback registered for a vnode on the file server when the
  237. * inode is being cleared
  238. */
  239. void afs_give_up_callback(struct afs_vnode *vnode)
  240. {
  241. struct afs_server *server = vnode->server;
  242. DECLARE_WAITQUEUE(myself, current);
  243. _enter("%d", vnode->cb_promised);
  244. _debug("GIVE UP INODE %p", &vnode->vfs_inode);
  245. if (!vnode->cb_promised) {
  246. _leave(" [not promised]");
  247. return;
  248. }
  249. ASSERT(server != NULL);
  250. spin_lock(&server->cb_lock);
  251. if (vnode->cb_promised && afs_breakring_space(server) == 0) {
  252. add_wait_queue(&server->cb_break_waitq, &myself);
  253. for (;;) {
  254. set_current_state(TASK_UNINTERRUPTIBLE);
  255. if (!vnode->cb_promised ||
  256. afs_breakring_space(server) != 0)
  257. break;
  258. spin_unlock(&server->cb_lock);
  259. schedule();
  260. spin_lock(&server->cb_lock);
  261. }
  262. remove_wait_queue(&server->cb_break_waitq, &myself);
  263. __set_current_state(TASK_RUNNING);
  264. }
  265. /* of course, it's always possible for the server to break this vnode's
  266. * callback first... */
  267. if (vnode->cb_promised)
  268. afs_do_give_up_callback(server, vnode);
  269. spin_unlock(&server->cb_lock);
  270. _leave("");
  271. }
  272. /*
  273. * dispatch a deferred give up callbacks operation
  274. */
  275. void afs_dispatch_give_up_callbacks(struct work_struct *work)
  276. {
  277. struct afs_server *server =
  278. container_of(work, struct afs_server, cb_break_work.work);
  279. _enter("");
  280. /* tell the fileserver to discard the callback promises it has
  281. * - in the event of ENOMEM or some other error, we just forget that we
  282. * had callbacks entirely, and the server will call us later to break
  283. * them
  284. */
  285. afs_fs_give_up_callbacks(server, &afs_async_call);
  286. }
  287. /*
  288. * flush the outstanding callback breaks on a server
  289. */
  290. void afs_flush_callback_breaks(struct afs_server *server)
  291. {
  292. cancel_delayed_work(&server->cb_break_work);
  293. queue_delayed_work(afs_callback_update_worker,
  294. &server->cb_break_work, 0);
  295. }
  296. #if 0
  297. /*
  298. * update a bunch of callbacks
  299. */
  300. static void afs_callback_updater(struct work_struct *work)
  301. {
  302. struct afs_server *server;
  303. struct afs_vnode *vnode, *xvnode;
  304. time_t now;
  305. long timeout;
  306. int ret;
  307. server = container_of(work, struct afs_server, updater);
  308. _enter("");
  309. now = get_seconds();
  310. /* find the first vnode to update */
  311. spin_lock(&server->cb_lock);
  312. for (;;) {
  313. if (RB_EMPTY_ROOT(&server->cb_promises)) {
  314. spin_unlock(&server->cb_lock);
  315. _leave(" [nothing]");
  316. return;
  317. }
  318. vnode = rb_entry(rb_first(&server->cb_promises),
  319. struct afs_vnode, cb_promise);
  320. if (atomic_read(&vnode->usage) > 0)
  321. break;
  322. rb_erase(&vnode->cb_promise, &server->cb_promises);
  323. vnode->cb_promised = false;
  324. }
  325. timeout = vnode->update_at - now;
  326. if (timeout > 0) {
  327. queue_delayed_work(afs_vnode_update_worker,
  328. &afs_vnode_update, timeout * HZ);
  329. spin_unlock(&server->cb_lock);
  330. _leave(" [nothing]");
  331. return;
  332. }
  333. list_del_init(&vnode->update);
  334. atomic_inc(&vnode->usage);
  335. spin_unlock(&server->cb_lock);
  336. /* we can now perform the update */
  337. _debug("update %s", vnode->vldb.name);
  338. vnode->state = AFS_VL_UPDATING;
  339. vnode->upd_rej_cnt = 0;
  340. vnode->upd_busy_cnt = 0;
  341. ret = afs_vnode_update_record(vl, &vldb);
  342. switch (ret) {
  343. case 0:
  344. afs_vnode_apply_update(vl, &vldb);
  345. vnode->state = AFS_VL_UPDATING;
  346. break;
  347. case -ENOMEDIUM:
  348. vnode->state = AFS_VL_VOLUME_DELETED;
  349. break;
  350. default:
  351. vnode->state = AFS_VL_UNCERTAIN;
  352. break;
  353. }
  354. /* and then reschedule */
  355. _debug("reschedule");
  356. vnode->update_at = get_seconds() + afs_vnode_update_timeout;
  357. spin_lock(&server->cb_lock);
  358. if (!list_empty(&server->cb_promises)) {
  359. /* next update in 10 minutes, but wait at least 1 second more
  360. * than the newest record already queued so that we don't spam
  361. * the VL server suddenly with lots of requests
  362. */
  363. xvnode = list_entry(server->cb_promises.prev,
  364. struct afs_vnode, update);
  365. if (vnode->update_at <= xvnode->update_at)
  366. vnode->update_at = xvnode->update_at + 1;
  367. xvnode = list_entry(server->cb_promises.next,
  368. struct afs_vnode, update);
  369. timeout = xvnode->update_at - now;
  370. if (timeout < 0)
  371. timeout = 0;
  372. } else {
  373. timeout = afs_vnode_update_timeout;
  374. }
  375. list_add_tail(&vnode->update, &server->cb_promises);
  376. _debug("timeout %ld", timeout);
  377. queue_delayed_work(afs_vnode_update_worker,
  378. &afs_vnode_update, timeout * HZ);
  379. spin_unlock(&server->cb_lock);
  380. afs_put_vnode(vl);
  381. }
  382. #endif
  383. /*
  384. * initialise the callback update process
  385. */
  386. int __init afs_callback_update_init(void)
  387. {
  388. afs_callback_update_worker =
  389. create_singlethread_workqueue("kafs_callbackd");
  390. return afs_callback_update_worker ? 0 : -ENOMEM;
  391. }
  392. /*
  393. * shut down the callback update process
  394. */
  395. void afs_callback_update_kill(void)
  396. {
  397. destroy_workqueue(afs_callback_update_worker);
  398. }