callback.c 11 KB

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