callback.c 11 KB

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