callback.c 11 KB

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